Search in sources :

Example 1 with Host

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

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

Example 3 with Host

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

the class TomcatVersionHelper method getCatalinaServerInstance.

/**
 * Gets a ServerInstance for the specified server.xml, Service name,
 * and Host name.  Returns null if server.xml does not exist
 * or an error occurs.
 *
 * @param serverXml path to previously published server.xml
 * @param serviceName name of Service to be used by this instance or null
 * @param hostName name of Host to be used by this instance or null
 * @return ServerInstance for specified server.xml using specified
 * Service and Host names.  null if server.xml does not exist.
 * @throws FileNotFoundException should not occur since existence is tested
 * @throws IOException if there is an error reading server.xml
 * @throws SAXException if there is a syntax error in server.xml
 */
public static ServerInstance getCatalinaServerInstance(IPath serverXml, String serviceName, String hostName) throws FileNotFoundException, IOException, SAXException {
    ServerInstance serverInstance = null;
    Factory factory = new Factory();
    factory.setPackageName("org.eclipse.jst.server.tomcat.core.internal.xml.server40");
    File serverFile = serverXml.toFile();
    if (serverFile.exists()) {
        Server server = (Server) factory.loadDocument(new FileInputStream(serverFile));
        serverInstance = new ServerInstance(server, serviceName, hostName);
        IPath contextPath = serverInstance.getContextXmlDirectory(serverXml.removeLastSegments(1));
        File contextDir = contextPath.toFile();
        if (contextDir.exists()) {
            Map<File, Context> projectContexts = new HashMap<File, Context>();
            loadSeparateContextFiles(contextPath.toFile(), factory, projectContexts);
            // add any separately saved contexts
            Host host = serverInstance.getHost();
            Collection contexts = projectContexts.values();
            Iterator iter = contexts.iterator();
            while (iter.hasNext()) {
                Context context = (Context) iter.next();
                host.importNode(context.getElementNode(), true);
            }
        // TODO Add handling for non-project contexts when there removal can be addressed
        }
    }
    return serverInstance;
}
Also used : Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) Server(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Iterator(java.util.Iterator) Factory(org.eclipse.jst.server.tomcat.core.internal.xml.Factory) Collection(java.util.Collection) Host(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Host) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance) JarFile(java.util.jar.JarFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with Host

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

the class XmlTestCase method testServerInstance2.

/**
 * Test behavior of ServerInstance
 */
public void testServerInstance2() {
    Server server = getXml40Server("serverxml.test2");
    assertNotNull(server);
    ServerInstance si = new ServerInstance(server, "Service", "nonexistent_host");
    assertNotNull(si.getService());
    assertNotNull(si.getEngine());
    assertEquals("Engine", si.getEngine().getName());
    assertNull(si.getHost());
    assertEquals("Host \"nonexistent_host\" was not found under Engine \"Engine\" and Service \"Service\".", si.getStatus().getMessage());
    si = new ServerInstance(server, "Service", null);
    assertNotNull(si.getService());
    assertNotNull(si.getEngine());
    assertEquals("Engine", si.getEngine().getName());
    assertNotNull(si.getHost());
    assertEquals("localhost", si.getHost().getName());
    assertEquals((new Path("/Base")).append("Engine").append("localhost"), si.getContextXmlDirectory(new Path("/Base")));
    Context context = si.getContext(0);
    assertNotNull(context);
    assertEquals("/WebApp1", context.getPath());
    context = si.getContext(1);
    assertNotNull(context);
    assertEquals("/WebApp2", context.getPath());
    context = si.getContext(2);
    assertNotNull(context);
    assertEquals("/WebApp3", context.getPath());
    // create new context
    context = si.getContext(3);
    context.setPath("/WebApp4");
    Context[] contexts = si.getContexts();
    assertEquals(4, contexts.length);
    assertEquals("/WebApp1", contexts[0].getPath());
    assertEquals("/WebApp2", contexts[1].getPath());
    assertEquals("/WebApp3", contexts[2].getPath());
    assertEquals("/WebApp4", contexts[3].getPath());
    context = si.createContext(2);
    context.setPath("/WebApp2b");
    contexts = si.getContexts();
    assertEquals(5, contexts.length);
    assertEquals("/WebApp1", contexts[0].getPath());
    assertEquals("/WebApp2", contexts[1].getPath());
    assertEquals("/WebApp2b", contexts[2].getPath());
    assertEquals("/WebApp3", contexts[3].getPath());
    assertEquals("/WebApp4", contexts[4].getPath());
    assertTrue(si.removeContext("WebApp2b"));
    contexts = si.getContexts();
    assertEquals(4, contexts.length);
    assertEquals("/WebApp1", contexts[0].getPath());
    assertEquals("/WebApp2", contexts[1].getPath());
    assertEquals("/WebApp3", contexts[2].getPath());
    assertEquals("/WebApp4", contexts[3].getPath());
    assertTrue(si.removeContext(3));
    contexts = si.getContexts();
    assertEquals(3, contexts.length);
    assertEquals("/WebApp1", contexts[0].getPath());
    assertEquals("/WebApp2", contexts[1].getPath());
    assertEquals("/WebApp3", contexts[2].getPath());
    context = si.getContext("/WebApp1");
    assertNotNull(context);
    assertEquals("/WebApp1", context.getPath());
    assertEquals(new Path("/Base/work/Engine/localhost/WebApp1"), si.getContextWorkDirectory(new Path("/Base"), context));
    context = si.getContext("WebApp2");
    assertNotNull(context);
    assertEquals("/WebApp2", context.getPath());
    assertEquals(new Path("/Base/relative/workdir"), si.getContextWorkDirectory(new Path("/Base"), context));
    context = si.getContext("WebApp3");
    assertNotNull(context);
    assertEquals("/WebApp3", context.getPath());
    assertEquals(new Path("/absolute/workdir"), si.getContextWorkDirectory(new Path("/Base"), context));
    context = si.createContext(3);
    context.setPath("");
    context = si.getContext("");
    assertNotNull(context);
    assertEquals("", context.getPath());
    assertEquals(new Path("/Base/work/Engine/localhost/_"), si.getContextWorkDirectory(new Path("/Base"), context));
    assertEquals(new Path("/Base/work/Engine/localhost"), si.getHostWorkDirectory(new Path("/Base")));
    assertNull(si.getContext("nonexistent"));
    assertEquals("Context with path \"/nonexistent\" was not found under Service \"Service\", Engine \"Engine\", and Host \"localhost\".", si.getStatus().getMessage());
}
Also used : Path(org.eclipse.core.runtime.Path) Context(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context) Server(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server) ServerInstance(org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)

Example 5 with Host

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

the class XmlTestCase method testDefaultServerXml50.

/**
 * Test reading of the default server.xml provided by the
 * current Tomcat 5.0 release (28).
 */
public void testDefaultServerXml50() {
    Server server = getXml40Server("default.serverxml.50");
    assertNotNull(server);
    // Check contents of XML
    String port = server.getPort();
    assertEquals("8005", port);
    assertEquals(server.getListenerCount(), 2);
    Listener listener = server.getListener(0);
    assertNotNull(listener);
    assertEquals("org.apache.catalina.mbeans.ServerLifecycleListener", listener.getClassName());
    listener = server.getListener(1);
    assertNotNull(listener);
    assertEquals("org.apache.catalina.mbeans.GlobalResourcesLifecycleListener", listener.getClassName());
    assertEquals(1, server.getServiceCount());
    Service service = server.getService(0);
    assertNotNull(service);
    assertEquals("Catalina", service.getName());
    assertEquals(2, service.getConnectorCount());
    Connector connector = service.getConnector(0);
    assertNotNull(connector);
    assertEquals("8080", connector.getPort());
    assertNull(connector.getProtocol());
    connector = service.getConnector(1);
    assertNotNull(connector);
    assertEquals("8009", connector.getPort());
    assertEquals("AJP/1.3", connector.getProtocol());
    Engine engine = service.getEngine();
    assertNotNull(engine);
    assertEquals("Catalina", engine.getName());
    assertEquals("localhost", engine.getDefaultHost());
    assertEquals(engine.getHostCount(), 1);
    Host host = engine.getHost(0);
    assertNotNull(host);
    assertEquals("localhost", host.getName());
    assertEquals("webapps", host.getAppBase());
    assertEquals("true", host.getAttributeValue("unpackWARs"));
    assertEquals("true", host.getAttributeValue("autoDeploy"));
    assertEquals(0, host.getContextCount());
}
Also used : Connector(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector) Listener(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Listener) Server(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server) Service(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service) Host(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Host) Engine(org.eclipse.jst.server.tomcat.core.internal.xml.server40.Engine)

Aggregations

Server (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Server)8 Context (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Context)7 Host (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Host)6 ServerInstance (org.eclipse.jst.server.tomcat.core.internal.xml.server40.ServerInstance)5 Factory (org.eclipse.jst.server.tomcat.core.internal.xml.Factory)4 Connector (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Connector)4 Engine (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Engine)4 Listener (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Listener)4 Service (org.eclipse.jst.server.tomcat.core.internal.xml.server40.Service)4 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 File (java.io.File)2 JarFile (java.util.jar.JarFile)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 SAXException (org.xml.sax.SAXException)2 InputStream (java.io.InputStream)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Collection (java.util.Collection)1