Search in sources :

Example 6 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class Benchmarks method doTestManagerBaseCreateSession.

private void doTestManagerBaseCreateSession(int threadCount, int iterCount) throws LifecycleException {
    // Create a default session manager
    StandardManager mgr = new StandardManager();
    mgr.setPathname(null);
    Host host = new StandardHost();
    host.setName("unittest");
    Context context = new StandardContext();
    context.setPath("");
    context.setParent(host);
    mgr.setContext(context);
    mgr.start();
    mgr.generateSessionId();
    while (mgr.sessionCreationTiming.size() < ManagerBase.TIMING_STATS_CACHE_SIZE) {
        mgr.sessionCreationTiming.add(null);
    }
    while (mgr.sessionExpirationTiming.size() < ManagerBase.TIMING_STATS_CACHE_SIZE) {
        mgr.sessionExpirationTiming.add(null);
    }
    Thread[] threads = new Thread[threadCount];
    for (int i = 0; i < threadCount; i++) {
        threads[i] = new Thread(new TestThreadCreateSession(mgr, iterCount));
    }
    long start = System.currentTimeMillis();
    for (int i = 0; i < threadCount; i++) {
        threads[i].start();
    }
    for (int i = 0; i < threadCount; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    }
    long end = System.currentTimeMillis();
    StringBuilder result = new StringBuilder();
    result.append("Threads: ");
    result.append(threadCount);
    result.append(", Time(ms): ");
    result.append(end - start);
    System.out.println(result.toString());
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host)

Example 7 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class TestPersistentManagerIntegration method testCreateSessionAndPassivate.

@Test
public void testCreateSessionAndPassivate() throws IOException, LifecycleException, ClassNotFoundException {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    StandardContext ctx = (StandardContext) tomcat.addContext("", null);
    ctx.setDistributable(true);
    Tomcat.addServlet(ctx, "DummyServlet", new DummyServlet());
    ctx.addServletMappingDecoded("/dummy", "DummyServlet");
    PersistentManager manager = new PersistentManager();
    TesterStore store = new TesterStore();
    manager.setStore(store);
    manager.setMaxIdleBackup(0);
    ctx.setManager(manager);
    ctx.addValve(new PersistentValve());
    tomcat.start();
    Assert.assertEquals("No active sessions", manager.getActiveSessions(), 0);
    Assert.assertTrue("No sessions managed", manager.getSessionIdsFull().isEmpty());
    String sessionId = getUrl("http://localhost:" + getPort() + "/dummy?no_create_session=false").toString();
    Assert.assertNotNull("Session is stored", store.load(sessionId));
    Assert.assertEquals("All sessions are passivated", manager.getActiveSessions(), 0);
    Assert.assertTrue("One session was created", !manager.getSessionIdsFull().isEmpty());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) StandardContext(org.apache.catalina.core.StandardContext) PersistentValve(org.apache.catalina.valves.PersistentValve) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 8 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class TestTomcat method testBug51526.

@Test
public void testBug51526() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appFile = new File("test/deployment/context.war");
    StandardContext context = (StandardContext) tomcat.addWebapp(null, "/test", appFile.getAbsolutePath());
    tomcat.start();
    assertEquals("WAR_CONTEXT", context.getSessionCookieName());
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) File(java.io.File) Test(org.junit.Test)

Example 9 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class ContainerMBean method addChild.

/**
     * Add a new child Container to those associated with this Container,
     * if supported. Won't start the child yet. Has to be started with a call to
     * Start method after necessary configurations are done.
     *
     * @param type ClassName of the child to be added
     * @param name Name of the child to be added
     *
     * @exception MBeanException if the child cannot be added
     */
public void addChild(String type, String name) throws MBeanException {
    Container contained = (Container) newInstance(type);
    contained.setName(name);
    if (contained instanceof StandardHost) {
        HostConfig config = new HostConfig();
        contained.addLifecycleListener(config);
    } else if (contained instanceof StandardContext) {
        ContextConfig config = new ContextConfig();
        contained.addLifecycleListener(config);
    }
    boolean oldValue = true;
    ContainerBase container = doGetManagedResource();
    try {
        oldValue = container.getStartChildren();
        container.setStartChildren(false);
        container.addChild(contained);
        contained.init();
    } catch (LifecycleException e) {
        throw new MBeanException(e);
    } finally {
        if (container != null) {
            container.setStartChildren(oldValue);
        }
    }
}
Also used : ContextConfig(org.apache.catalina.startup.ContextConfig) ContainerBase(org.apache.catalina.core.ContainerBase) Container(org.apache.catalina.Container) LifecycleException(org.apache.catalina.LifecycleException) StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) HostConfig(org.apache.catalina.startup.HostConfig) MBeanException(javax.management.MBeanException)

Example 10 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class ContextConfig method fixDocBase.

/**
     * Adjust docBase.
     * @throws IOException cannot access the context base path
     */
protected void fixDocBase() throws IOException {
    Host host = (Host) context.getParent();
    File appBase = host.getAppBaseFile();
    String docBase = context.getDocBase();
    if (docBase == null) {
        // Trying to guess the docBase according to the path
        String path = context.getPath();
        if (path == null) {
            return;
        }
        ContextName cn = new ContextName(path, context.getWebappVersion());
        docBase = cn.getBaseName();
    }
    File file = new File(docBase);
    if (!file.isAbsolute()) {
        docBase = (new File(appBase, docBase)).getPath();
    } else {
        docBase = file.getCanonicalPath();
    }
    file = new File(docBase);
    String origDocBase = docBase;
    ContextName cn = new ContextName(context.getPath(), context.getWebappVersion());
    String pathName = cn.getBaseName();
    boolean unpackWARs = true;
    if (host instanceof StandardHost) {
        unpackWARs = ((StandardHost) host).isUnpackWARs();
        if (unpackWARs && context instanceof StandardContext) {
            unpackWARs = ((StandardContext) context).getUnpackWAR();
        }
    }
    boolean docBaseInAppBase = docBase.startsWith(appBase.getPath() + File.separatorChar);
    if (docBase.toLowerCase(Locale.ENGLISH).endsWith(".war") && !file.isDirectory()) {
        URL war = UriUtil.buildJarUrl(new File(docBase));
        if (unpackWARs) {
            docBase = ExpandWar.expand(host, war, pathName);
            file = new File(docBase);
            docBase = file.getCanonicalPath();
            if (context instanceof StandardContext) {
                ((StandardContext) context).setOriginalDocBase(origDocBase);
            }
        } else {
            ExpandWar.validate(host, war, pathName);
        }
    } else {
        File docDir = new File(docBase);
        File warFile = new File(docBase + ".war");
        URL war = null;
        if (warFile.exists() && docBaseInAppBase) {
            war = UriUtil.buildJarUrl(warFile);
        }
        if (docDir.exists()) {
            if (war != null && unpackWARs) {
                // Check if WAR needs to be re-expanded (e.g. if it has
                // changed). Note: HostConfig.deployWar() takes care of
                // ensuring that the correct XML file is used.
                // This will be a NO-OP if the WAR is unchanged.
                ExpandWar.expand(host, war, pathName);
            }
        } else {
            if (war != null) {
                if (unpackWARs) {
                    docBase = ExpandWar.expand(host, war, pathName);
                    file = new File(docBase);
                    docBase = file.getCanonicalPath();
                } else {
                    docBase = warFile.getCanonicalPath();
                    ExpandWar.validate(host, war, pathName);
                }
            }
            if (context instanceof StandardContext) {
                ((StandardContext) context).setOriginalDocBase(origDocBase);
            }
        }
    }
    // Re-calculate now docBase is a canonical path
    docBaseInAppBase = docBase.startsWith(appBase.getPath() + File.separatorChar);
    if (docBaseInAppBase) {
        docBase = docBase.substring(appBase.getPath().length());
        docBase = docBase.replace(File.separatorChar, '/');
        if (docBase.startsWith("/")) {
            docBase = docBase.substring(1);
        }
    } else {
        docBase = docBase.replace(File.separatorChar, '/');
    }
    context.setDocBase(docBase);
}
Also used : StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) Host(org.apache.catalina.Host) StandardHost(org.apache.catalina.core.StandardHost) File(java.io.File) URL(java.net.URL) ContextName(org.apache.catalina.util.ContextName)

Aggregations

StandardContext (org.apache.catalina.core.StandardContext)91 File (java.io.File)36 Tomcat (org.apache.catalina.startup.Tomcat)24 Context (org.apache.catalina.Context)19 Test (org.junit.Test)19 StandardHost (org.apache.catalina.core.StandardHost)16 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)12 Container (org.apache.catalina.Container)11 Host (org.apache.catalina.Host)11 JarFile (java.util.jar.JarFile)10 IOException (java.io.IOException)9 URL (java.net.URL)9 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)9 HashMap (java.util.HashMap)8 StandardRoot (org.apache.catalina.webresources.StandardRoot)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)6 MalformedURLException (java.net.MalformedURLException)5 ServletContext (javax.servlet.ServletContext)5