Search in sources :

Example 1 with Server

use of org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server 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 Server

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

the class LiferayPublishOperation method publishDir.

private void publishDir(IModule module2, List status, IProgressMonitor monitor) throws CoreException {
    final IPath path = server.getModuleDeployDirectory(module2);
    // Remove if requested or if previously published and are now serving without publishing
    if (kind == IServer.PUBLISH_CLEAN || deltaKind == ServerBehaviourDelegate.REMOVED || server.getTomcatServer().isServeModulesWithoutPublish()) {
        File f = path.toFile();
        if (f.exists()) {
            try {
                IPath baseDir = server.getRuntimeBaseDirectory();
                // $NON-NLS-1$ //$NON-NLS-2$
                IPath serverXml = baseDir.append("conf").append("server.xml");
                ServerInstance oldInstance = TomcatVersionHelper.getCatalinaServerInstance(serverXml, null, null);
                // $NON-NLS-1$
                IPath contextDir = oldInstance.getContextXmlDirectory(baseDir.append("conf"));
                // $NON-NLS-1$
                String contextFileName = path.lastSegment() + ".xml";
                File contextFile = contextDir.append(contextFileName).toFile();
                if (contextFile.exists()) {
                    contextFile.delete();
                }
                File autoDeployDir = baseDir.append(server.getLiferayTomcatServer().getAutoDeployDirectory()).toFile();
                File autoDeployFile = new File(autoDeployDir, contextFileName);
                if (autoDeployFile.exists()) {
                    autoDeployFile.delete();
                }
            } catch (Exception e) {
                // $NON-NLS-1$
                LiferayTomcatPlugin.logError("Could not delete context xml file.", e);
            }
            IStatus[] stat = PublishHelper.deleteDirectory(f, monitor);
            addArrayToList(status, stat);
        }
        if (deltaKind == ServerBehaviourDelegate.REMOVED || server.getTomcatServer().isServeModulesWithoutPublish())
            return;
    }
    IPath baseDir = server.getTomcatServer().getRuntimeBaseDirectory();
    IPath autoDeployDir = new Path(server.getLiferayTomcatServer().getAutoDeployDirectory());
    boolean serverStopped = server.getServer().getServerState() == IServer.STATE_STOPPED;
    if (kind == IServer.PUBLISH_CLEAN || kind == IServer.PUBLISH_FULL) {
        IModuleResource[] mr = server.getResources(module);
        IStatus[] stat = helper.publishFull(mr, path, monitor);
        addArrayToList(status, stat);
        clearWebXmlDescriptors(module2.getProject(), path, monitor);
        server.moveContextToAutoDeployDir(module2, path, baseDir, autoDeployDir, true, serverStopped);
        return;
    }
    IModuleResourceDelta[] delta = server.getPublishedResourceDelta(module);
    // check if we have a anti*Locking directory temp files and copy the resources out there as well
    File[] antiDirs = new File[0];
    try {
        File tempDir = // $NON-NLS-1$
        server.getLiferayTomcatServer().getTomcatRuntime().getRuntime().getLocation().append("temp").toFile();
        antiDirs = tempDir.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith(path.lastSegment());
            }
        });
    } catch (Exception e) {
    }
    int size = delta.length;
    for (int i = 0; i < size; i++) {
        IStatus[] stat = helper.publishDelta(delta[i], path, monitor);
        for (File antiDir : antiDirs) {
            if (antiDir.exists()) {
                try {
                    helper.publishDelta(delta[i], new Path(antiDir.getCanonicalPath()), monitor);
                } catch (Exception e) {
                // best effort
                }
            }
        }
        addArrayToList(status, stat);
    }
    // check to see if we need to re-invoke the liferay plugin deployer
    String[] paths = new String[] { // $NON-NLS-1$ //$NON-NLS-2$
    WEB_XML_PATH, // $NON-NLS-1$ //$NON-NLS-2$
    "WEB-INF/portlet.xml", // $NON-NLS-1$ //$NON-NLS-2$
    "WEB-INF/liferay-portlet.xml", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
    "WEB-INF/liferay-display.xml", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
    "WEB-INF/liferay-look-and-feel.xml", // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
    "WEB-INF/liferay-hook.xml", // $NON-NLS-1$//$NON-NLS-2$
    "WEB-INF/liferay-layout-templates.xml", // $NON-NLS-1$//$NON-NLS-2$
    "WEB-INF/liferay-plugin-package.properties", "WEB-INF/liferay-plugin-package.xml", // $NON-NLS-1$ //$NON-NLS-2$
    "WEB-INF/server-config.wsdd" };
    for (IModuleResourceDelta del : delta) {
        if (ComponentUtil.containsMember(del, paths) || isHookProjectDelta(del)) {
            clearWebXmlDescriptors(module2.getProject(), path, monitor);
            server.moveContextToAutoDeployDir(module2, path, baseDir, autoDeployDir, true, serverStopped);
            break;
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IModuleResource(org.eclipse.wst.server.core.model.IModuleResource) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) IModuleResourceDelta(org.eclipse.wst.server.core.model.IModuleResourceDelta) CoreException(org.eclipse.core.runtime.CoreException) FilenameFilter(java.io.FilenameFilter) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) ModuleFile(org.eclipse.wst.server.core.util.ModuleFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 3 with Server

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

the class Tomcat85Configuration method modifyServerPort.

/**
 * Modify the port with the given id.
 *
 * @param id java.lang.String
 * @param port int
 */
public void modifyServerPort(String id, int port) {
    try {
        if ("server".equals(id)) {
            server.setPort(port + "");
            isServerDirty = true;
            firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port));
            return;
        }
        int i = id.indexOf("/");
        // If a connector in the instance Service
        if (i < 0) {
            int connNum = Integer.parseInt(id);
            Connector connector = serverInstance.getConnector(connNum);
            if (connector != null) {
                connector.setPort(port + "");
                isServerDirty = true;
                firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port));
            }
        } else // Else a connector in another Service
        {
            int servNum = Integer.parseInt(id.substring(0, i));
            int connNum = Integer.parseInt(id.substring(i + 1));
            Service service = server.getService(servNum);
            Connector connector = service.getConnector(connNum);
            connector.setPort(port + "");
            isServerDirty = true;
            firePropertyChangeEvent(MODIFY_PORT_PROPERTY, id, new Integer(port));
        }
    } catch (Exception e) {
        Trace.trace(Trace.SEVERE, "Error modifying server port " + id, e);
    }
}
Also used : Connector(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector) Service(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service) CoreException(org.eclipse.core.runtime.CoreException)

Example 4 with Server

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

the class Tomcat85Configuration method getServerPorts.

/**
 * Returns a list of ServerPorts that this configuration uses.
 *
 * @return java.util.List
 */
public List getServerPorts() {
    List<ServerPort> ports = new ArrayList<ServerPort>();
    // first add server port
    try {
        int port = Integer.parseInt(server.getPort());
        ports.add(new ServerPort("server", Messages.portServer, port, "TCPIP"));
    } catch (Exception e) {
    // ignore
    }
    // add connectors
    try {
        String instanceServiceName = serverInstance.getService().getName();
        int size = server.getServiceCount();
        for (int i = 0; i < size; i++) {
            Service service = server.getService(i);
            int size2 = service.getConnectorCount();
            for (int j = 0; j < size2; j++) {
                Connector connector = service.getConnector(j);
                String name = "HTTP/1.1";
                String protocol2 = "HTTP";
                boolean advanced = true;
                String[] contentTypes = null;
                int port = -1;
                try {
                    port = Integer.parseInt(connector.getPort());
                } catch (Exception e) {
                // ignore
                }
                String protocol = connector.getProtocol();
                if (protocol != null && protocol.length() > 0) {
                    if (protocol.startsWith("HTTP")) {
                        name = protocol;
                    } else if (protocol.startsWith("AJP")) {
                        name = protocol;
                        protocol2 = "AJP";
                    } else {
                        // Get Tomcat equivalent name if protocol handler class specified
                        name = protocolHandlerMap.get(protocol);
                        if (name != null) {
                            // Prepare simple protocol string for ServerPort protocol
                            int index = name.indexOf('/');
                            if (index > 0)
                                protocol2 = name.substring(0, index);
                            else
                                protocol2 = name;
                        } else // Specified protocol is unknown, just use as is
                        {
                            name = protocol;
                            protocol2 = protocol;
                        }
                    }
                }
                if (protocol2.toLowerCase().equals("http"))
                    contentTypes = new String[] { "web", "webservices" };
                String secure = connector.getSecure();
                if (secure != null && secure.length() > 0) {
                    name = "SSL";
                    protocol2 = "SSL";
                } else
                    advanced = false;
                String portId;
                if (instanceServiceName != null && instanceServiceName.equals(service.getName()))
                    portId = Integer.toString(j);
                else
                    portId = i + "/" + j;
                ports.add(new ServerPort(portId, name, port, protocol2, contentTypes, advanced));
            }
        }
    } catch (Exception e) {
        Trace.trace(Trace.SEVERE, "Error getting server ports", e);
    }
    return ports;
}
Also used : Connector(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector) ArrayList(java.util.ArrayList) Service(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service) ServerPort(org.eclipse.wst.server.core.ServerPort) CoreException(org.eclipse.core.runtime.CoreException)

Example 5 with Server

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

the class TomcatVersionHelper method cleanupCatalinaServer.

/**
 * Cleanup server instance location in preparation for next server publish.
 * This currently involves deleting work directories for currently
 * existing Contexts which will not be included in the next publish.
 * In addition, Context XML files which may have been created for these
 * Contexts are also deleted. If requested, Context XML files for
 * kept Contexts will be deleted since they will be kept in server.xml.
 *
 * @param baseDir path to server instance directory, i.e. catalina.base
 * @param installDir path to server installation directory (not currently used)
 * @param removeKeptContextFiles true if kept contexts should have a separate
 *  context XML file removed
 * @param modules list of currently added modules
 * @param monitor a progress monitor or null
 * @return MultiStatus containing results of the cleanup operation
 */
public static IStatus cleanupCatalinaServer(IPath baseDir, IPath installDir, boolean removeKeptContextFiles, List modules, IProgressMonitor monitor) {
    MultiStatus ms = new MultiStatus(TomcatPlugin.PLUGIN_ID, 0, Messages.cleanupServerTask, null);
    try {
        monitor = ProgressUtil.getMonitorFor(monitor);
        monitor.beginTask(Messages.cleanupServerTask, 200);
        monitor.subTask(Messages.detectingRemovedProjects);
        IPath serverXml = baseDir.append("conf").append("server.xml");
        ServerInstance oldInstance = TomcatVersionHelper.getCatalinaServerInstance(serverXml, null, null);
        if (oldInstance != null) {
            Map<String, Context> removedContextsMap = new HashMap<String, Context>();
            Map<String, Context> keptContextsMap = new HashMap<String, Context>();
            TomcatVersionHelper.getRemovedKeptCatalinaContexts(oldInstance, modules, removedContextsMap, keptContextsMap);
            monitor.worked(100);
            if (removedContextsMap.size() > 0) {
                // Delete context files and work directories for managed web modules that have gone away
                IProgressMonitor subMonitor = ProgressUtil.getSubMonitorFor(monitor, 100);
                subMonitor.beginTask(Messages.deletingContextFilesTask, removedContextsMap.size() * 200);
                Iterator iter = removedContextsMap.keySet().iterator();
                while (iter.hasNext()) {
                    String oldPath = (String) iter.next();
                    Context ctx = removedContextsMap.get(oldPath);
                    // Delete the corresponding context file, if it exists
                    IPath ctxFilePath = oldInstance.getContextFilePath(baseDir, ctx);
                    if (ctxFilePath != null) {
                        File ctxFile = ctxFilePath.toFile();
                        if (ctxFile.exists()) {
                            subMonitor.subTask(NLS.bind(Messages.deletingContextFile, ctxFile.getName()));
                            if (ctxFile.delete()) {
                                if (Trace.isTraceEnabled())
                                    Trace.trace(Trace.FINER, "Leftover context file " + ctxFile.getName() + " deleted.");
                                ms.add(new Status(IStatus.OK, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.deletedContextFile, ctxFile.getName()), null));
                            } else {
                                Trace.trace(Trace.SEVERE, "Could not delete obsolete context file " + ctxFilePath.toOSString());
                                ms.add(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotDeleteContextFile, ctxFilePath.toOSString()), null));
                            }
                        }
                    }
                    subMonitor.worked(100);
                    // Delete work directory associated with the removed context if it is within confDir.
                    // If it is outside of confDir, assume user is going to manage it.
                    IPath ctxWorkPath = oldInstance.getContextWorkDirectory(baseDir, ctx);
                    if (baseDir.isPrefixOf(ctxWorkPath)) {
                        File ctxWorkDir = ctxWorkPath.toFile();
                        if (ctxWorkDir.exists() && ctxWorkDir.isDirectory()) {
                            IStatus[] results = PublishHelper.deleteDirectory(ctxWorkDir, ProgressUtil.getSubMonitorFor(monitor, 100));
                            if (results.length > 0) {
                                Trace.trace(Trace.SEVERE, "Could not delete work directory " + ctxWorkDir.getPath() + " for removed context " + oldPath);
                                for (int i = 0; i < results.length; i++) {
                                    ms.add(results[i]);
                                }
                            }
                        } else
                            subMonitor.worked(100);
                    } else
                        subMonitor.worked(100);
                }
                subMonitor.done();
            }
            monitor.worked(100);
            // If requested, remove any separate context XML files for contexts being kept
            if (removeKeptContextFiles && keptContextsMap.size() > 0) {
                // Delete context files and work directories for managed web modules that have gone away
                IProgressMonitor subMonitor = ProgressUtil.getSubMonitorFor(monitor, 100);
                // TODO Improve task name
                subMonitor.beginTask(Messages.deletingContextFilesTask, keptContextsMap.size() * 100);
                Iterator iter = keptContextsMap.keySet().iterator();
                while (iter.hasNext()) {
                    String keptPath = (String) iter.next();
                    Context ctx = keptContextsMap.get(keptPath);
                    // Delete the corresponding context file, if it exists
                    IPath ctxFilePath = oldInstance.getContextFilePath(baseDir, ctx);
                    if (ctxFilePath != null) {
                        File ctxFile = ctxFilePath.toFile();
                        if (ctxFile.exists()) {
                            subMonitor.subTask(NLS.bind(Messages.deletingContextFile, ctxFile.getName()));
                            if (ctxFile.delete()) {
                                if (Trace.isTraceEnabled())
                                    Trace.trace(Trace.FINER, "Leftover context file " + ctxFile.getName() + " deleted.");
                                ms.add(new Status(IStatus.OK, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.deletedContextFile, ctxFile.getName()), null));
                            } else {
                                Trace.trace(Trace.SEVERE, "Could not delete obsolete context file " + ctxFilePath.toOSString());
                                ms.add(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCouldNotDeleteContextFile, ctxFilePath.toOSString()), null));
                            }
                        }
                    }
                    subMonitor.worked(100);
                }
                subMonitor.done();
            }
        } else // Else no server.xml.  Assume first publish to new temp directory
        {
            monitor.worked(200);
        }
        if (Trace.isTraceEnabled())
            Trace.trace(Trace.FINER, "Server cleaned");
    } catch (Exception e) {
        Trace.trace(Trace.SEVERE, "Could not cleanup server at " + baseDir.toOSString() + ": " + e.getMessage());
        ms.add(new Status(IStatus.ERROR, TomcatPlugin.PLUGIN_ID, 0, NLS.bind(Messages.errorCleanupServer, new String[] { e.getLocalizedMessage() }), e));
    } finally {
        monitor.done();
    }
    return ms;
}
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) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MultiStatus(org.eclipse.core.runtime.MultiStatus) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Iterator(java.util.Iterator) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) JarFile(java.util.jar.JarFile) File(java.io.File)

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