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());
}
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());
}
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());
}
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);
}
}
}
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);
}
Aggregations