Search in sources :

Example 26 with ServerInstance

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

the class TomcatVersionHelper method moveContextsToSeparateFiles.

/**
 * Moves contexts out of current published server.xml and into individual
 * context XML files.
 *
 * @param baseDir directory where the Catalina instance is found
 * @param noPath true if path attribute should be removed from the context
 * @param serverStopped true if the server is stopped
 * @param monitor a progress monitor
 * @return result of operation
 */
public static IStatus moveContextsToSeparateFiles(IPath baseDir, boolean noPath, boolean serverStopped, 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;
        Host host = publishedInstance.getHost();
        Context[] wtpContexts = publishedInstance.getContexts();
        if (wtpContexts != null && wtpContexts.length > 0) {
            IPath contextPath = publishedInstance.getContextXmlDirectory(serverXml.removeLastSegments(1));
            File contextDir = contextPath.toFile();
            if (!contextDir.exists()) {
                contextDir.mkdirs();
            }
            // Process in reverse order, since contexts may be removed
            for (int i = wtpContexts.length - 1; i >= 0; i--) {
                Context context = wtpContexts[i];
                // TODO Handle non-project contexts when their removal can be addressed
                if (context.getSource() == null)
                    continue;
                String name = context.getPath();
                if (name.startsWith("/")) {
                    name = name.substring(1);
                }
                // If the default context, adjust the file name
                if (name.length() == 0) {
                    name = "ROOT";
                }
                // Update name if multi-level path.  For 5.5 and later the "#" has been
                // "reserved" as a legal file name placeholder for "/".  For Tomcat 5.0,
                // we just need a legal unique file name since "/" will fail.  Prior to
                // 5.0, this feature is not supported.
                name = name.replace('/', '#');
                // TODO Determine circumstances, if any, where setting antiResourceLocking true can cause the original docBase content to be deleted.
                if (Boolean.valueOf(context.getAttributeValue("antiResourceLocking")).booleanValue())
                    context.setAttributeValue("antiResourceLocking", "false");
                File contextFile = new File(contextDir, name + ".xml");
                Context existingContext = loadContextFile(contextFile);
                // If server is stopped or if contexts are not the equivalent, write the context file
                if (serverStopped || !context.isEquivalent(existingContext)) {
                    // If requested, remove path attribute
                    if (noPath)
                        context.removeAttribute("path");
                    DocumentBuilder builder = XMLUtil.getDocumentBuilder();
                    Document contextDoc = builder.newDocument();
                    contextDoc.appendChild(contextDoc.importNode(context.getElementNode(), true));
                    XMLUtil.save(contextFile.getAbsolutePath(), contextDoc);
                }
                host.removeElement("Context", i);
                modified = true;
            }
        }
        monitor.worked(100);
        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) 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) Host(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Host) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 27 with ServerInstance

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

the class Tomcat40Configuration method load.

/**
 * @see TomcatConfiguration#load(IPath, IProgressMonitor)
 */
public void load(IPath path, IProgressMonitor monitor) throws CoreException {
    try {
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.loadingTask, 5);
        // check for catalina.policy to verify that this is a v4.0 config
        InputStream in = new FileInputStream(path.append("catalina.policy").toFile());
        in.read();
        in.close();
        monitor.worked(1);
        serverFactory = new Factory();
        serverFactory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        server = (Server) serverFactory.loadDocument(new FileInputStream(path.append("server.xml").toFile()));
        serverInstance = new ServerInstance(server, null, null);
        monitor.worked(1);
        webAppDocument = new WebAppDocument(path.append("web.xml"));
        monitor.worked(1);
        tomcatUsersDocument = XMLUtil.getDocumentBuilder().parse(new InputSource(new FileInputStream(path.append("tomcat-users.xml").toFile())));
        monitor.worked(1);
        // load policy file
        policyFile = TomcatVersionHelper.getFileContents(new FileInputStream(path.append("catalina.policy").toFile()));
        monitor.worked(1);
        if (monitor.isCanceled())
            return;
        monitor.done();
    } catch (Exception e) {
        Trace.trace(Trace.WARNING, "Could not load Tomcat v4.0 configuration from " + path.toOSString() + ": " + e.getMessage());
        throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, path.toOSString()), e));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) InputSource(org.xml.sax.InputSource) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) FileInputStream(java.io.FileInputStream) CoreException(org.eclipse.core.runtime.CoreException)

Example 28 with ServerInstance

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

the class Tomcat41Configuration method load.

/**
 * @see TomcatConfiguration#load(IFolder, IProgressMonitor)
 */
public void load(IFolder folder, IProgressMonitor monitor) throws CoreException {
    try {
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.loadingTask, 800);
        // check for catalina.policy to verify that this is a v4.0 config
        IFile file = folder.getFile("catalina.policy");
        if (!file.exists())
            throw new CoreException(new Status(IStatus.WARNING, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, folder.getFullPath().toOSString()), null));
        // load server.xml
        file = folder.getFile("server.xml");
        InputStream in = file.getContents();
        serverFactory = new Factory();
        serverFactory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        server = (Server) serverFactory.loadDocument(in);
        serverInstance = new ServerInstance(server, null, null);
        monitor.worked(200);
        // load web.xml
        file = folder.getFile("web.xml");
        webAppDocument = new WebAppDocument(file);
        monitor.worked(200);
        // load tomcat-users.xml
        file = folder.getFile("tomcat-users.xml");
        in = file.getContents();
        tomcatUsersDocument = XMLUtil.getDocumentBuilder().parse(new InputSource(in));
        monitor.worked(200);
        // load catalina.policy
        file = folder.getFile("catalina.policy");
        in = file.getContents();
        policyFile = TomcatVersionHelper.getFileContents(in);
        monitor.worked(200);
        if (monitor.isCanceled())
            throw new Exception("Cancelled");
        monitor.done();
    } catch (Exception e) {
        Trace.trace(Trace.WARNING, "Could not reload Tomcat v4.1 configuration from: " + folder.getFullPath() + ": " + e.getMessage());
        throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, folder.getFullPath().toOSString()), e));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) InputSource(org.xml.sax.InputSource) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) CoreException(org.eclipse.core.runtime.CoreException)

Example 29 with ServerInstance

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

the class Tomcat41Configuration method load.

/**
 * @see TomcatConfiguration#load(IPath, IProgressMonitor)
 */
public void load(IPath path, IProgressMonitor monitor) throws CoreException {
    try {
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.loadingTask, 5);
        // check for catalina.policy to verify that this is a v4.0 config
        InputStream in = new FileInputStream(path.append("catalina.policy").toFile());
        in.read();
        in.close();
        monitor.worked(1);
        serverFactory = new Factory();
        serverFactory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        server = (Server) serverFactory.loadDocument(new FileInputStream(path.append("server.xml").toFile()));
        serverInstance = new ServerInstance(server, null, null);
        monitor.worked(1);
        webAppDocument = new WebAppDocument(path.append("web.xml"));
        monitor.worked(1);
        tomcatUsersDocument = XMLUtil.getDocumentBuilder().parse(new InputSource(new FileInputStream(path.append("tomcat-users.xml").toFile())));
        monitor.worked(1);
        // load policy file
        policyFile = TomcatVersionHelper.getFileContents(new FileInputStream(path.append("catalina.policy").toFile()));
        monitor.worked(1);
        if (monitor.isCanceled())
            return;
        monitor.done();
    } catch (Exception e) {
        Trace.trace(Trace.WARNING, "Could not load Tomcat v4.0 configuration from " + path.toOSString() + ": " + e.getMessage());
        throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, path.toOSString()), e));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) InputSource(org.xml.sax.InputSource) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) FileInputStream(java.io.FileInputStream) CoreException(org.eclipse.core.runtime.CoreException)

Example 30 with ServerInstance

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

the class Tomcat50Configuration method load.

/**
 * @see TomcatConfiguration#load(IFolder, IProgressMonitor)
 */
public void load(IFolder folder, IProgressMonitor monitor) throws CoreException {
    try {
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.loadingTask, 1000);
        // check for catalina.policy to verify that this is a v4.0 config
        IFile file = folder.getFile("catalina.policy");
        if (!file.exists())
            throw new CoreException(new Status(IStatus.WARNING, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, folder.getFullPath().toOSString()), null));
        // load server.xml
        file = folder.getFile("server.xml");
        InputStream in = file.getContents();
        serverFactory = new Factory();
        serverFactory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        server = (Server) serverFactory.loadDocument(in);
        serverInstance = new ServerInstance(server, null, null);
        monitor.worked(200);
        // load web.xml
        file = folder.getFile("web.xml");
        webAppDocument = new WebAppDocument(file);
        monitor.worked(200);
        // load tomcat-users.xml
        file = folder.getFile("tomcat-users.xml");
        in = file.getContents();
        tomcatUsersDocument = XMLUtil.getDocumentBuilder().parse(new InputSource(in));
        monitor.worked(200);
        // load catalina.policy
        file = folder.getFile("catalina.policy");
        in = file.getContents();
        policyFile = TomcatVersionHelper.getFileContents(in);
        monitor.worked(200);
        // load catalina.properties
        file = folder.getFile("catalina.properties");
        if (file.exists()) {
            in = file.getContents();
            propertiesFile = TomcatVersionHelper.getFileContents(in);
        } else
            propertiesFile = null;
        monitor.worked(200);
        if (monitor.isCanceled())
            throw new Exception("Cancelled");
        monitor.done();
    } catch (Exception e) {
        Trace.trace(Trace.WARNING, "Could not reload Tomcat v5.0 configuration from: " + folder.getFullPath() + ": " + e.getMessage());
        throw new CoreException(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotLoadConfiguration, folder.getFullPath().toOSString()), e));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) InputSource(org.xml.sax.InputSource) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) CoreException(org.eclipse.core.runtime.CoreException)

Aggregations

ServerInstance (org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)38 IStatus (org.eclipse.core.runtime.IStatus)25 Status (org.eclipse.core.runtime.Status)24 Factory (org.eclipse.jst.server.tomcat.core.internal.xml.Factory)24 FileInputStream (java.io.FileInputStream)23 CoreException (org.eclipse.core.runtime.CoreException)21 InputStream (java.io.InputStream)19 ByteArrayInputStream (java.io.ByteArrayInputStream)18 Context (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context)18 InputSource (org.xml.sax.InputSource)18 IFile (org.eclipse.core.resources.IFile)17 File (java.io.File)13 Server (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server)10 IPath (org.eclipse.core.runtime.IPath)9 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 MultiStatus (org.eclipse.core.runtime.MultiStatus)6 Path (org.eclipse.core.runtime.Path)5 SAXException (org.xml.sax.SAXException)5 JarFile (java.util.jar.JarFile)4