Search in sources :

Example 1 with Factory

use of org.eclipse.jst.server.tomcat.core.internal.xml.Factory in project liferay-ide by liferay.

the class LiferayTomcatServerBehavior method moveContextToAutoDeployDir.

public IStatus moveContextToAutoDeployDir(IModule module, IPath deployDir, IPath baseDir, IPath autoDeployDir, boolean noPath, boolean serverStopped) {
    // $NON-NLS-1$
    IPath confDir = baseDir.append("conf");
    // $NON-NLS-1$
    IPath serverXml = confDir.append("server.xml");
    try (InputStream newInputStream = Files.newInputStream(serverXml.toFile().toPath())) {
        Factory factory = new Factory();
        // $NON-NLS-1$
        factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        Server publishedServer = (Server) factory.loadDocument(newInputStream);
        ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
        IPath contextPath = null;
        if (autoDeployDir.isAbsolute()) {
            contextPath = autoDeployDir;
        } else {
            contextPath = baseDir.append(autoDeployDir);
        }
        File contextDir = contextPath.toFile();
        if (!contextDir.exists()) {
            contextDir.mkdirs();
        }
        Context context = publishedInstance.createContext(-1);
        // $NON-NLS-1$
        context.setReloadable("true");
        final String moduleName = module.getName();
        final String requiredSuffix = ProjectUtil.getRequiredSuffix(module.getProject());
        String contextName = moduleName;
        if (!moduleName.endsWith(requiredSuffix)) {
            contextName = moduleName + requiredSuffix;
        }
        // $NON-NLS-1$
        context.setSource("org.eclipse.jst.jee.server:" + contextName);
        if (// $NON-NLS-1$
        Boolean.valueOf(context.getAttributeValue("antiResourceLocking")).booleanValue()) {
            // $NON-NLS-1$ //$NON-NLS-2$
            context.setAttributeValue("antiResourceLocking", "false");
        }
        // $NON-NLS-1$
        File contextFile = new File(contextDir, contextName + ".xml");
        if (!LiferayTomcatUtil.isExtProjectContext(context)) {
            // If requested, remove path attribute
            if (noPath) {
                // $NON-NLS-1$
                context.removeAttribute("path");
            }
            // need to fix the doc base to contain entire path to help autoDeployer for Liferay
            context.setDocBase(deployDir.toOSString());
            // context.setAttributeValue("antiJARLocking", "true");
            // check to see if we need to move from conf folder
            // IPath existingContextPath = confDir.append("Catalina/localhost").append(contextFile.getName());
            // if (existingContextPath.toFile().exists()) {
            // existingContextPath.toFile().delete();
            // }
            DocumentBuilder builder = XMLUtil.getDocumentBuilder();
            Document contextDoc = builder.newDocument();
            contextDoc.appendChild(contextDoc.importNode(context.getElementNode(), true));
            XMLUtil.save(contextFile.getAbsolutePath(), contextDoc);
        }
    } catch (Exception e) {
        // 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) IServer(org.eclipse.wst.server.core.IServer) DocumentBuilder(javax.xml.parsers.DocumentBuilder) 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) Document(org.w3c.dom.Document) File(java.io.File) CoreException(org.eclipse.core.runtime.CoreException)

Example 2 with Factory

use of org.eclipse.jst.server.tomcat.core.internal.xml.Factory in project liferay-ide by liferay.

the class LiferayTomcatUtil method loadContextFile.

public static Context loadContextFile(File contextFile) {
    Context context = null;
    if (contextFile != null && contextFile.exists()) {
        try (InputStream fis = Files.newInputStream(contextFile.toPath())) {
            Factory factory = new Factory();
            // $NON-NLS-1$
            factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
            context = (Context) factory.loadDocument(fis);
            if (context != null) {
                String path = context.getPath();
                // If path attribute is not set, derive from file name
                if (path == null) {
                    String fileName = contextFile.getName();
                    // $NON-NLS-1$
                    path = fileName.substring(0, fileName.length() - ".xml".length());
                    if (// $NON-NLS-1$
                    "ROOT".equals(path))
                        path = StringPool.EMPTY;
                    context.setPath(StringPool.FORWARD_SLASH + path);
                }
            }
        } catch (Exception e) {
        // may be a spurious xml file in the host dir?
        }
    }
    return context;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) InputStream(java.io.InputStream) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) NoSuchFileException(java.nio.file.NoSuchFileException) CoreException(org.eclipse.core.runtime.CoreException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 3 with Factory

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

the class TomcatPublishModuleVisitor method getProjectContextXml.

/**
 * Load a META-INF/context.xml file from project, if available
 *
 * @param component web component containing the context.xml
 * @return context element containing the context.xml
 * @throws CoreException
 */
protected Context getProjectContextXml(IVirtualComponent component) throws CoreException {
    // load or create module's context.xml document
    IVirtualFile contextFile = (IVirtualFile) component.getRootFolder().findMember("META-INF/context.xml");
    Context contextElement = null;
    if (contextFile != null && contextFile.exists()) {
        Factory factory = new Factory();
        factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
        InputStream fis = null;
        try {
            fis = contextFile.getUnderlyingFile().getContents();
            contextElement = (Context) factory.loadDocument(fis);
        } catch (Exception e) {
            Trace.trace(Trace.SEVERE, "Exception reading " + contextFile, e);
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
    return contextElement;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) IVirtualFile(org.eclipse.wst.common.componentcore.resources.IVirtualFile) InputStream(java.io.InputStream) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) IOException(java.io.IOException) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException)

Example 4 with Factory

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

the class TomcatVersionHelper method publishCatalinaContextConfig.

/**
 * Add context configuration found in META-INF/context.xml files
 * present in projects to published server.xml.  Used by
 * Tomcat 4.1, 5.0, and 5.5 which support use of META-INF/context.xml
 * in some form.
 *
 * @param baseDir absolute path to catalina instance directory
 * @param webappsDir absolute path to deployment directory
 * @param monitor a progress monitor or null
 * @return result of operation
 */
public static IStatus publishCatalinaContextConfig(IPath baseDir, IPath webappsDir, IProgressMonitor monitor) {
    if (Trace.isTraceEnabled())
        Trace.trace(Trace.FINER, "Apply context configurations");
    IPath confDir = baseDir.append("conf");
    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(confDir.append("server.xml").toFile()));
        ServerInstance publishedInstance = new ServerInstance(publishedServer, null, null);
        monitor.worked(100);
        boolean modified = false;
        MultiStatus ms = new MultiStatus(TomcatPlugin.PLUGIN_ID, 0, Messages.publishContextConfigTask, null);
        Context[] contexts = publishedInstance.getContexts();
        if (contexts != null) {
            for (int i = 0; i < contexts.length; i++) {
                Context context = contexts[i];
                monitor.subTask(NLS.bind(Messages.checkingContextTask, new String[] { context.getPath() }));
                if (addCatalinaContextConfig(webappsDir, context, ms)) {
                    modified = true;
                }
            }
        }
        monitor.worked(100);
        if (modified) {
            monitor.subTask(Messages.savingContextConfigTask);
            factory.save(confDir.append("server.xml").toOSString());
        }
        // If problem(s) occurred adding context configurations, return error status
        if (ms.getChildren().length > 0) {
            return ms;
        }
        if (Trace.isTraceEnabled())
            Trace.trace(Trace.FINER, "Server.xml updated with context.xml configurations");
        return Status.OK_STATUS;
    } catch (Exception e) {
        Trace.trace(Trace.WARNING, "Could not apply context configurations to published Tomcat configuration from " + 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();
    }
}
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) 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)

Example 5 with Factory

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

the class TomcatVersionHelper method loadContextFile.

private static Context loadContextFile(File contextFile) {
    FileInputStream fis = null;
    Context context = null;
    if (contextFile != null && contextFile.exists()) {
        try {
            Factory factory = new Factory();
            factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
            fis = new FileInputStream(contextFile);
            context = (Context) factory.loadDocument(fis);
            if (context != null) {
                String path = context.getPath();
                // If path attribute is not set, derive from file name
                if (path == null) {
                    String fileName = contextFile.getName();
                    path = fileName.substring(0, fileName.length() - ".xml".length());
                    if ("ROOT".equals(path))
                        path = "";
                    // Assuming this use for "#" since Tomcat has "reserved" this use of "#" since 5.5.
                    path = path.replace('#', '/');
                    context.setPath("/" + path);
                }
            }
        } catch (Exception e) {
            // may be a spurious xml file in the host dir?
            Trace.trace(Trace.FINER, "Unable to read context " + contextFile.getAbsolutePath());
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
    return context;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException)

Aggregations

Factory (org.eclipse.jst.server.tomcat.core.internal.xml.Factory)34 FileInputStream (java.io.FileInputStream)25 ServerInstance (org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)24 IStatus (org.eclipse.core.runtime.IStatus)23 Status (org.eclipse.core.runtime.Status)23 InputStream (java.io.InputStream)22 CoreException (org.eclipse.core.runtime.CoreException)21 InputSource (org.xml.sax.InputSource)20 ByteArrayInputStream (java.io.ByteArrayInputStream)18 IFile (org.eclipse.core.resources.IFile)18 File (java.io.File)11 Context (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context)11 Server (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server)8 FileNotFoundException (java.io.FileNotFoundException)7 IOException (java.io.IOException)7 IPath (org.eclipse.core.runtime.IPath)6 MultiStatus (org.eclipse.core.runtime.MultiStatus)5 SAXException (org.xml.sax.SAXException)5 JarFile (java.util.jar.JarFile)3 HashMap (java.util.HashMap)2