@Test void mbeansAvailableAfterBinder() throws LifecycleException, InterruptedException { TomcatMetrics.monitor(registry, null); CountDownLatch latch = new CountDownLatch(1); registry.config().onMeterAdded(m -> { if(m.getId().getName().equals("tomcat.global.received")) latch.countDown(); }); Tomcat server = new Tomcat(); try { StandardHost host = new StandardHost(); host.setName("localhost"); server.setHost(host); server.setPort(61000); server.start(); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(registry.find("tomcat.global.received").functionCounter()).isNotNull(); } finally { server.stop(); server.destroy(); } }
@Test void mbeansAvailableBeforeBinder() throws LifecycleException { Tomcat server = new Tomcat(); try { StandardHost host = new StandardHost(); host.setName("localhost"); server.setHost(host); server.setPort(61000); server.start(); TomcatMetrics.monitor(registry, null); assertThat(registry.find("tomcat.global.received").functionCounter()).isNotNull(); } finally { server.stop(); server.destroy(); } }
@Test public void testBug57425() throws LifecycleException, IOException { Tomcat tomcat = getTomcatInstance(); Host host = tomcat.getHost(); if (host instanceof StandardHost) { ((StandardHost) host).setContextClass(ReplicatedContext.class.getName()); } File root = new File("test/webapp-3.0"); Context context = tomcat.addWebapp(host, "", root.getAbsolutePath()); Tomcat.addServlet(context, "test", new AccessContextServlet()); context.addServletMapping("/access", "test"); tomcat.start(); ByteChunk result = getUrl("http://localhost:" + getPort() + "/access"); Assert.assertEquals("OK", result.toString()); }
@Test public void testGetBrokenContextPerAddWepapp() { Tomcat tomcat = getTomcatInstance(); Host host = tomcat.getHost(); if (host instanceof StandardHost) { ((StandardHost) host).setContextClass("InvalidContextClassName"); } try { File appFile = new File("test/deployment/context.war"); tomcat.addWebapp(null, "/test", appFile.getAbsolutePath()); fail(); } catch (IllegalArgumentException e) { // OK } }
@Test public void testGetCustomContextPerAddWebappWithNullHost() { Tomcat tomcat = getTomcatInstance(); Host host = tomcat.getHost(); if (host instanceof StandardHost) { ((StandardHost) host).setContextClass(ReplicatedContext.class .getName()); } File appFile = new File("test/deployment/context.war"); Context context = tomcat.addWebapp(null, "/test", appFile.getAbsolutePath()); assertEquals(ReplicatedContext.class.getName(), context.getClass() .getName()); }
@Test public void testGetCustomContextPerAddWebappWithHost() { Tomcat tomcat = getTomcatInstance(); Host host = tomcat.getHost(); if (host instanceof StandardHost) { ((StandardHost) host).setContextClass(ReplicatedContext.class .getName()); } File appFile = new File("test/deployment/context.war"); Context context = tomcat.addWebapp(host, "/test", appFile.getAbsolutePath()); assertEquals(ReplicatedContext.class.getName(), context.getClass() .getName()); }
@Test public void testGetBrokenContextPerAddContext() { Tomcat tomcat = getTomcatInstance(); Host host = tomcat.getHost(); if (host instanceof StandardHost) { ((StandardHost) host).setContextClass("InvalidContextClassName"); } // No file system docBase required try { tomcat.addContext(null, "", null); fail(); } catch (IllegalArgumentException e) { // OK } }
private void doTestUnpackWAR(boolean unpackWARs, boolean unpackWAR, boolean external, boolean resultDir) throws Exception { Tomcat tomcat = getTomcatInstance(); StandardHost host = (StandardHost) tomcat.getHost(); host.setUnpackWARs(unpackWARs); tomcat.start(); File war; if (unpackWAR) { war = createWar(WAR_XML_UNPACKWAR_TRUE_SOURCE, !external); } else { war = createWar(WAR_XML_UNPACKWAR_FALSE_SOURCE, !external); } if (external) { createXmlInConfigBaseForExternal(war); } host.backgroundProcess(); File dir = new File(host.getAppBase(), APP_NAME.getBaseName()); Assert.assertEquals( Boolean.valueOf(resultDir), Boolean.valueOf(dir.isDirectory())); }
private void testBrokenAppWithAntiLocking(boolean unpackWARs) throws Exception { Tomcat tomcat = getTomcatInstance(); StandardHost host = (StandardHost) tomcat.getHost(); host.setUnpackWARs(unpackWARs); File war = createWar(WAR_BROKEN_SOURCE, false); createXmlInConfigBaseForExternal(war, true); File dir = new File(getAppBaseFile(host), APP_NAME.getBaseName()); tomcat.start(); // Simulate deploy on start-up tomcat.getHost().backgroundProcess(); Assert.assertTrue(war.isFile()); if (unpackWARs) { Assert.assertTrue(dir.isDirectory()); } }
@Test public void testSetContextClassName() throws Exception { Tomcat tomcat = getTomcatInstance(); Host host = tomcat.getHost(); if (host instanceof StandardHost) { StandardHost standardHost = (StandardHost) host; standardHost.setContextClass(TesterContext.class.getName()); } // Copy the WAR file File war = new File(getAppBaseFile(host), APP_NAME.getBaseName() + ".war"); copy(WAR_XML_SOURCE, war); // Deploy the copied war tomcat.start(); host.backgroundProcess(); // Check the Context class Context ctxt = (Context) host.findChild(APP_NAME.getName()); Assert.assertTrue(ctxt instanceof TesterContext); }
public StandardContext addWebapp(StandardHost host, String url, String path) throws ServletException { silence(url); StandardContext ctx = new StandardContext(); ctx.setPath( url ); ctx.setDocBase(path); if (defaultRealm == null) { initSimpleAuth(); } ctx.setRealm(defaultRealm); initWebappDefaults(ctx); ContextConfig ctxCfg = new ContextConfig(); ctx.addLifecycleListener( ctxCfg ); // prevent it from looking ( if it finds one - it'll have dup error ) ctxCfg.setDefaultWebXml("org/apache/catalin/startup/NO_DEFAULT_XML"); if (host == null) { host = getHost(); } host.addChild(ctx); return ctx; }
/** * Return the MBean Names of the Valves assoicated with this Host * * @exception Exception if an MBean cannot be created or registered */ public String [] getValves() throws Exception { Registry registry = MBeanUtils.createRegistry(); StandardHost host = (StandardHost) this.resource; String mname = MBeanUtils.createManagedName(host); ManagedBean managed = registry.findManagedBean(mname); String domain = null; if (managed != null) { domain = managed.getDomain(); } if (domain == null) domain = mserver.getDefaultDomain(); Valve [] valves = host.getValves(); String [] mbeanNames = new String[valves.length]; for (int i = 0; i < valves.length; i++) { mbeanNames[i] = MBeanUtils.createObjectName(domain, valves[i]).toString(); } return mbeanNames; }
/** * Remove an existing Host. * * @param name MBean Name of the comonent to remove * * @exception Exception if a component cannot be removed */ public void removeHost(String name) throws Exception { // Acquire a reference to the component to be removed ObjectName oname = new ObjectName(name); String hostName = oname.getKeyProperty("host"); Service service = getService(oname); Engine engine = (Engine) service.getContainer(); Host host = (Host) engine.findChild(hostName); // Remove this component from its parent component if(host!=null) { if(host instanceof StandardHost) ((StandardHost)host).destroy(); else engine.removeChild(host); } }
/** * Find potential memory leaks caused by web application reload. */ protected void findleaks(boolean statusLine, PrintWriter writer, StringManager smClient) { if (!(host instanceof StandardHost)) { writer.println(smClient.getString("managerServlet.findleaksFail")); return; } String[] results = ((StandardHost) host).findReloadedContextMemoryLeaks(); if (results.length > 0) { if (statusLine) { writer.println(smClient.getString("managerServlet.findleaksList")); } for (String result : results) { if ("".equals(result)) { result = "/"; } writer.println(result); } } else if (statusLine) { writer.println(smClient.getString("managerServlet.findleaksNone")); } }
public static int getPort(Host h) { int port = -1; StandardHost host = (StandardHost) h; CatalinaUtil.host = (StandardHost) h; StandardEngine se = (StandardEngine) host.getParent(); StandardService ss = (StandardService) se.getService(); Connector[] cs = ss.findConnectors(); for (Connector c : cs) { if (c.getProtocolHandlerClassName().contains("Http11Protocol")) port = c.getPort(); } return port; }
@Test(description = "Tests the construction of Application Server URL for a sample request") public void testConstructionOfApplicationServerURL() { Request request = new Request(); Connector connector = new Connector(); connector.setProtocol(TestConstants.SSL_PROTOCOL); connector.setPort(TestConstants.SSL_PORT); connector.setScheme(TestConstants.SSL_PROTOCOL); Engine engine = new StandardEngine(); Service service = new StandardService(); engine.setService(service); engine.getService().addConnector(connector); Host host = new StandardHost(); host.setName(TestConstants.DEFAULT_TOMCAT_HOST); request.getMappingData().host = host; host.setParent(engine); Optional<String> actual = SSOUtils.constructApplicationServerURL(request); if (actual.isPresent()) { Assert.assertEquals(actual.get(), TestConstants.DEFAULT_APPLICATION_SERVER_URL); } else { Assert.fail(); } }
@Test(description = "Tests the construction of Application Server URL for no SSL/TLS Connector") public void testConstructionOfApplicationServerURLWithNoConnector() { Request request = new Request(); Engine engine = new StandardEngine(); Service service = new StandardService(); engine.setService(service); Host host = new StandardHost(); host.setName(TestConstants.DEFAULT_TOMCAT_HOST); request.getMappingData().host = host; host.setParent(engine); Optional<String> actual = SSOUtils.constructApplicationServerURL(request); Assert.assertTrue(!actual.isPresent()); }
private void prepareCatalinaComponents() { engine = new StandardEngine(); host = new StandardHost(); fooContext = new StandardContext(); barContext = new StandardContext(); Connector connector = new Connector(); connector.setProtocol(TestConstants.SSL_PROTOCOL); connector.setPort(TestConstants.SSL_PORT); connector.setScheme(TestConstants.SSL_PROTOCOL); Service service = new StandardService(); engine.setService(service); engine.getService().addConnector(connector); host.setAppBase(TestConstants.WEB_APP_BASE); host.setName(TestConstants.DEFAULT_TOMCAT_HOST); host.setParent(engine); fooContext.setParent(host); fooContext.setDocBase(TestConstants.FOO_CONTEXT); barContext.setParent(host); barContext.setDocBase(TestConstants.BAR_CONTEXT); }
protected Host createHost(Engine engine, String hostName) throws ContainerException { Debug.logInfo("createHost(" + engine + ", " + hostName + ")", module); if (tomcat == null) { throw new ContainerException("Cannot create Host without Tomcat instance!"); } Host host = new StandardHost(); host.setAppBase(CATALINA_HOSTS_HOME); host.setName(hostName); host.setDeployOnStartup(false); host.setBackgroundProcessorDelay(5); host.setAutoDeploy(false); host.setRealm(engine.getRealm()); hosts.put(engine.getName() + hostName, host); return host; }
/** * add host to engine. * * @param hostName * name of the host * @return will return the added host of Engine */ public static Host addHostToEngine(String hostName) { String hostBaseDir = CarbonUtils.getCarbonRepository() + "/" + UrlMapperConstants.HostProperties.WEB_APPS + "/"; CarbonTomcatService carbonTomcatService = DataHolder.getInstance().getCarbonTomcatService(); // adding virtual host to tomcat engine Engine engine = carbonTomcatService.getTomcat().getEngine(); StandardHost host = new StandardHost(); host.setAppBase(hostBaseDir); host.setName(hostName); host.setUnpackWARs(false); host.addValve(new CarbonContextCreatorValve()); host.addValve(new CompositeValve()); engine.addChild(host); log.info("host added to the tomcat: " + host); return host; }