Search in sources :

Example 36 with Server

use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.

the class Tomcat85Configuration method getWebModules.

/**
 * Return a list of the web modules in this server.
 * @return java.util.List
 */
public List getWebModules() {
    List<WebModule> list = new ArrayList<WebModule>();
    try {
        Context[] contexts = serverInstance.getContexts();
        if (contexts != null) {
            for (int i = 0; i < contexts.length; i++) {
                Context context = contexts[i];
                String reload = context.getReloadable();
                if (reload == null)
                    reload = "false";
                WebModule module = new WebModule(context.getPath(), context.getDocBase(), context.getSource(), reload.equalsIgnoreCase("true") ? true : false);
                list.add(module);
            }
        }
    } catch (Exception e) {
        Trace.trace(Trace.SEVERE, "Error getting project refs", e);
    }
    return list;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) ArrayList(java.util.ArrayList) CoreException(org.eclipse.core.runtime.CoreException)

Example 37 with Server

use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.

the class TomcatPublishModuleVisitor method includeProjectContextXml.

boolean includeProjectContextXml(IVirtualComponent component, Context context) throws CoreException {
    boolean dirty = false;
    // handle project context.xml
    Context projectContext = getProjectContextXml(component);
    if (projectContext != null) {
        // copy configuration to server context
        projectContext.copyChildrenTo(context);
        Map attrs = projectContext.getAttributes();
        Iterator iter = attrs.keySet().iterator();
        while (iter.hasNext()) {
            String name = (String) iter.next();
            if (!name.equalsIgnoreCase("path") && !name.equalsIgnoreCase("docBase") && !name.equalsIgnoreCase("source")) {
                String value = (String) attrs.get(name);
                String actualValue = context.getAttributeValue(name);
                if (!value.equals(actualValue)) {
                    context.setAttributeValue(name, value);
                    dirty = true;
                }
            }
        }
    }
    return dirty;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 38 with Server

use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.

the class TomcatVersionHelper method updateContextsToServeDirectly.

/**
 * Update Contexts to serve web projects directly.
 *
 * @param baseDir directory where the Catalina instance is found
 * @param loader name of the catalina.properties loader to use for global
 * classpath entries
 * @param monitor a progress monitor
 * @return result of update operation
 */
public static IStatus updateContextsToServeDirectly(IPath baseDir, String tomcatVersion, String loader, boolean enableMetaInfResources, IProgressMonitor monitor) {
    IPath confDir = baseDir.append("conf");
    IPath serverXml = confDir.append("server.xml");
    try {
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.publishConfigurationTask, 300);
        monitor.subTask(Messages.publishContextConfigTask);
        Factory factory = new Factory();
        factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        Server publishedServer = (Server) factory.loadDocument(new FileInputStream(serverXml.toFile()));
        ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
        monitor.worked(100);
        boolean modified = false;
        boolean isTomcat80 = tomcatVersion.startsWith("8.0");
        boolean isTomcat85 = tomcatVersion.startsWith("8.5");
        boolean isTomcat9 = tomcatVersion.startsWith("9.");
        // care about top-level modules only
        TomcatPublishModuleVisitor visitor;
        if (isTomcat80) {
            visitor = new Tomcat80PublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
        } else if (isTomcat85) {
            visitor = new Tomcat85PublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
        } else if (isTomcat9) {
            visitor = new Tomcat90PublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
        } else {
            visitor = new TomcatPublishModuleVisitor(baseDir, tomcatVersion, publishedInstance, loader, enableMetaInfResources);
        }
        Context[] contexts = publishedInstance.getContexts();
        for (int i = 0; i < contexts.length; i++) {
            String moduleId = contexts[i].getSource();
            if (moduleId != null && moduleId.length() > 0) {
                IModule module = ServerUtil.getModule(moduleId);
                ModuleTraverser.traverse(module, visitor, monitor);
                modified = true;
            }
        }
        if (modified) {
            monitor.subTask(Messages.savingContextConfigTask);
            factory.save(serverXml.toOSString());
        }
        monitor.worked(100);
        if (Trace.isTraceEnabled())
            Trace.trace(Trace.FINER, "Context docBase settings updated in server.xml.");
    } catch (Exception e) {
        Trace.trace(Trace.SEVERE, "Could not modify context configurations to serve directly for Tomcat configuration " + confDir.toOSString() + ": " + e.getMessage());
        return new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishConfiguration, new String[] { e.getLocalizedMessage() }), e);
    } finally {
        monitor.done();
    }
    return Status.OK_STATUS;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IModule(org.eclipse.wst.server.core.IModule) IPath(org.eclipse.core.runtime.IPath) Server(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)

Example 39 with Server

use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.

the class TomcatVersionHelper method getRemovedKeptCatalinaContexts.

/**
 * Gets the paths for Contexts that are being removed in the
 * next server publish. Reads the old server.xml to determine
 * what Contexts were previously servered and returns those
 * that are not included in the specified list of modules.
 *
 * @param oldServerInstance for server.xml from previous server publish
 * @param modules list of currently added modules
 * @param removedContextsMap Map to receive removed contexts mapped by path
 * @param keptContextsMap Map to receive kept contexts mapped by path
 */
public static void getRemovedKeptCatalinaContexts(ServerInstance oldServerInstance, List modules, Map<String, Context> removedContextsMap, Map<String, Context> keptContextsMap) {
    // Collect paths of old web modules managed by WTP
    Context[] contexts = oldServerInstance.getContexts();
    if (contexts != null) {
        for (int i = 0; i < contexts.length; i++) {
            String source = contexts[i].getSource();
            if (source != null && source.length() > 0) {
                removedContextsMap.put(contexts[i].getPath(), contexts[i]);
            }
        }
    }
    // Remove paths for web modules that are staying around
    int size = modules.size();
    for (int i = 0; i < size; i++) {
        WebModule module = (WebModule) modules.get(i);
        String modulePath = module.getPath();
        // normalize "/" to ""
        if (modulePath.equals("/"))
            modulePath = "";
        Context context = removedContextsMap.remove(modulePath);
        if (context != null)
            keptContextsMap.put(context.getPath(), context);
    }
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context)

Example 40 with Server

use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server in project webtools.servertools by eclipse.

the class TomcatVersionHelper method localizeConfiguration.

/**
 * If modules are not being deployed to the "webapps" directory, the
 * context for the published modules is updated to contain the
 * corrected docBase.
 *
 * @param baseDir runtime base directory for the server
 * @param deployDir deployment directory for the server
 * @param server server being localized
 * @param monitor a progress monitor
 * @return result of operation
 */
public static IStatus localizeConfiguration(IPath baseDir, IPath deployDir, TomcatServer server, IProgressMonitor monitor) {
    try {
        if (Trace.isTraceEnabled())
            Trace.trace(Trace.FINER, "Localizing configuration at " + baseDir);
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.publishConfigurationTask, 300);
        IPath serverXml = baseDir.append("conf/server.xml");
        Factory factory = new Factory();
        factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        Server publishedServer = (Server) factory.loadDocument(new FileInputStream(serverXml.toFile()));
        ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
        monitor.worked(100);
        if (monitor.isCanceled())
            return Status.CANCEL_STATUS;
        boolean modified = false;
        // Only add root module if running in a test env (i.e. not on the installation)
        boolean addRootWebapp = server.isTestEnvironment();
        // If not deploying to "webapps", context docBase attributes need updating
        // TODO Improve to compare with appBase value instead of hardcoded "webapps"
        boolean deployingToAppBase = "webapps".equals(server.getDeployDirectory());
        Map<String, String> pathMap = new HashMap<String, String>();
        MultiStatus ms = new MultiStatus(TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishServer, server.getServer().getName()), null);
        Context[] contexts = publishedInstance.getContexts();
        if (contexts != null) {
            for (int i = 0; i < contexts.length; i++) {
                Context context = contexts[i];
                // Normalize path and check for duplicates
                String path = context.getPath();
                if (path != null) {
                    // Save a copy of original in case it's "/"
                    String origPath = path;
                    // Normalize "/" to ""
                    if ("/".equals(path)) {
                        if (Trace.isTraceEnabled())
                            Trace.trace(Trace.FINER, "Context path is being changed from \"/\" to \"\".");
                        path = "";
                        context.setPath(path);
                        modified = true;
                    }
                    // Context paths that are the same or differ only in case are not allowed
                    String lcPath = path.toLowerCase();
                    if (!pathMap.containsKey(lcPath)) {
                        pathMap.put(lcPath, origPath);
                    } else {
                        String otherPath = pathMap.get(lcPath);
                        IStatus s = new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, origPath.equals(otherPath) ? NLS.bind(Messages.errorPublishPathDup, origPath) : NLS.bind(Messages.errorPublishPathConflict, origPath, otherPath));
                        ms.add(s);
                    }
                } else {
                    IStatus s = new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, Messages.errorPublishPathMissing);
                    ms.add(s);
                }
                // TODO Need to add a root context if deploying to webapps but with auto-deploy off
                if (addRootWebapp && "".equals(context.getPath())) {
                    // A default webapp is being deployed, don't add one
                    addRootWebapp = false;
                }
                // If not deploying to appBase, convert to absolute path under deploy dir
                if (!deployingToAppBase) {
                    String source = context.getSource();
                    if (source != null && source.length() > 0) {
                        context.setDocBase(deployDir.append(context.getDocBase()).toOSString());
                        modified = true;
                    }
                }
            }
        }
        // If errors are present, return status
        if (!ms.isOK())
            return ms;
        if (addRootWebapp) {
            // Add a context for the default webapp
            Context rootContext = publishedInstance.createContext(0);
            rootContext.setPath("");
            rootContext.setDocBase(deployDir.append("ROOT").toOSString());
            rootContext.setReloadable("false");
            modified = true;
        }
        monitor.worked(100);
        if (monitor.isCanceled())
            return Status.CANCEL_STATUS;
        if (modified) {
            monitor.subTask(Messages.savingContextConfigTask);
            factory.save(serverXml.toOSString());
        }
        monitor.worked(100);
        if (Trace.isTraceEnabled())
            Trace.trace(Trace.FINER, "Context docBase settings updated in server.xml.");
    } catch (Exception e) {
        Trace.trace(Trace.WARNING, "Could not localize server configuration published to " + baseDir.toOSString() + ": " + e.getMessage());
        return new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorPublishConfiguration, new String[] { e.getLocalizedMessage() }), e);
    } finally {
        monitor.done();
    }
    return Status.OK_STATUS;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) Server(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) MultiStatus(org.eclipse.core.runtime.MultiStatus) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)48 ServerInstance (org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)32 Factory (org.eclipse.jst.server.tomcat.core.internal.xml.Factory)26 IStatus (org.eclipse.core.runtime.IStatus)25 Status (org.eclipse.core.runtime.Status)24 FileInputStream (java.io.FileInputStream)23 Connector (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector)23 Context (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context)22 Service (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service)22 InputStream (java.io.InputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)18 ArrayList (java.util.ArrayList)18 InputSource (org.xml.sax.InputSource)18 IFile (org.eclipse.core.resources.IFile)17 Server (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server)15 File (java.io.File)13 IPath (org.eclipse.core.runtime.IPath)9 ServerPort (org.eclipse.wst.server.core.ServerPort)9 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6