private Context findContext() { for (Container child : this.tomcat.getHost().findChildren()) { if (child instanceof Context) { return (Context) child; } } throw new IllegalStateException("The host does not contain a Context"); }
private String getContextPath() { return Arrays.stream(this.tomcat.getHost().findChildren()) .filter(TomcatEmbeddedContext.class::isInstance) .map(TomcatEmbeddedContext.class::cast) .map(TomcatEmbeddedContext::getPath).collect(Collectors.joining(" ")); }
@Override public void undeployConfig() { if (this.context != null) { this.context.removeServletMapping("/"); this.tomcatServer.getHost().removeChild(this.context); } }
private void performDeferredLoadOnStartup() { try { for (Container child : this.tomcat.getHost().findChildren()) { if (child instanceof TomcatEmbeddedContext) { ((TomcatEmbeddedContext) child).deferredLoadOnStartup(); } } } catch (Exception ex) { if (ex instanceof WebServerException) { throw (WebServerException) ex; } throw new WebServerException("Unable to start embedded Tomcat connectors", ex); } }
void start() { // '%2F' (slash /) and '%5C' (backslash \) are permitted as path delimiters in URLs System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true"); System.setProperty("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", "true"); tomcat = new Tomcat(); // Initialize directories String basedir = tomcatBasedir().getAbsolutePath(); tomcat.setBaseDir(basedir); tomcat.getHost().setAppBase(basedir); tomcat.getHost().setAutoDeploy(false); tomcat.getHost().setCreateDirs(false); tomcat.getHost().setDeployOnStartup(true); new TomcatAccessLog().configure(tomcat, props); TomcatConnectors.configure(tomcat, props); webappContext = new TomcatContexts().configure(tomcat, props); try { tomcat.start(); new TomcatStartupLogs(Loggers.get(getClass())).log(tomcat); } catch (LifecycleException e) { Loggers.get(EmbeddedTomcat.class).error("Fail to start web server", e); Throwables.propagate(e); } }
public synchronized void start() throws IOException { if (id != null) { log.info("Starting Serving Layer {}", id); } Preconditions.checkState(tomcat == null); // Has to happen very early before Tomcat init: System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true"); noSuchBaseDir = Files.createTempDirectory("Oryx"); noSuchBaseDir.toFile().deleteOnExit(); Tomcat tomcat = new Tomcat(); Connector connector = makeConnector(); configureTomcat(tomcat, connector); configureEngine(tomcat.getEngine()); configureServer(tomcat.getServer()); configureHost(tomcat.getHost()); makeContext(tomcat, noSuchBaseDir); try { tomcat.start(); } catch (LifecycleException le) { throw new IOException(le); } this.tomcat = tomcat; }
private void rethrowDeferredStartupExceptions() throws Exception { Container[] children = this.tomcat.getHost().findChildren(); for (Container container : children) { if (container instanceof TomcatEmbeddedContext) { TomcatStarter tomcatStarter = ((TomcatEmbeddedContext) container) .getStarter(); if (tomcatStarter != null) { Exception exception = tomcatStarter.getStartUpException(); if (exception != null) { throw exception; } } } if (!LifecycleState.STARTED.equals(container.getState())) { throw new IllegalStateException(container + " failed to start"); } } }
@Test public void enable_access_logs_by_Default() throws Exception { Tomcat tomcat = mock(Tomcat.class, Mockito.RETURNS_DEEP_STUBS); Props props = new Props(new Properties()); props.set(PATH_LOGS.getKey(), temp.newFolder().getAbsolutePath()); underTest.configure(tomcat, props); verify(tomcat.getHost().getPipeline()).addValve(any(ProgrammaticLogbackValve.class)); }
@Override public WebServer getWebServer(HttpHandler httpHandler) { Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat"); tomcat.setBaseDir(baseDir.getAbsolutePath()); Connector connector = new Connector(this.protocol); tomcat.getService().addConnector(connector); customizeConnector(connector); tomcat.setConnector(connector); tomcat.getHost().setAutoDeploy(false); configureEngine(tomcat.getEngine()); TomcatHttpHandlerAdapter servlet = new TomcatHttpHandlerAdapter(httpHandler); prepareContext(tomcat.getHost(), servlet); return new TomcatWebServer(tomcat, getPort() >= 0); }
@Override public WebServer getWebServer(ServletContextInitializer... initializers) { Tomcat tomcat = new Tomcat(); File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat"); tomcat.setBaseDir(baseDir.getAbsolutePath()); Connector connector = new Connector(this.protocol); tomcat.getService().addConnector(connector); customizeConnector(connector); tomcat.setConnector(connector); tomcat.getHost().setAutoDeploy(false); configureEngine(tomcat.getEngine()); for (Connector additionalConnector : this.additionalTomcatConnectors) { tomcat.getService().addConnector(additionalConnector); } prepareContext(tomcat.getHost(), initializers); return getTomcatWebServer(tomcat); }
private static void configureLogbackAccess(Tomcat tomcat, Props props) { if (props.valueAsBoolean(PROPERTY_ENABLE, true)) { ProgrammaticLogbackValve valve = new ProgrammaticLogbackValve(); LogbackHelper helper = new LogbackHelper(); LogbackHelper.RollingPolicy policy = helper.createRollingPolicy(valve, props, "access"); FileAppender appender = policy.createAppender("ACCESS_LOG"); PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder(); fileEncoder.setContext(valve); fileEncoder.setPattern(props.value(PROPERTY_PATTERN, DEFAULT_SQ_ACCESS_LOG_PATTERN)); fileEncoder.start(); appender.setEncoder(fileEncoder); appender.start(); valve.addAppender(appender); tomcat.getHost().getPipeline().addValve(valve); } }
private Optional<Context> findContext() { return Stream.of(this.webServer.getTomcat().getHost().findChildren()) .filter(Context.class::isInstance).map(Context.class::cast) .findFirst(); }
private Context findContext(TomcatWebServer tomcatWebServer) { for (Container container : tomcatWebServer.getTomcat().getHost().findChildren()) { if (container instanceof Context) { return (Context) container; } } return null; }
/** * Add a webapp using normal WEB-INF/web.xml if found. * * @param contextPath * @param baseDir * @return new Context * @throws ServletException */ public Context addWebapp(String contextPath, String baseDir) throws ServletException { return addWebapp(getHost(), contextPath, baseDir); }
private void silence(String ctx) { String base = "org.apache.catalina.core.ContainerBase.[default].["; base += getHost().getName(); base += "].["; base += ctx; base += "]"; Logger.getLogger(base).setLevel(Level.WARNING); }
/** * Add a webapp using normal WEB-INF/web.xml if found. * * @param contextPath * @param baseDir * @return new Context * @throws ServletException */ public Context addWebapp(String contextPath, String baseDir) throws ServletException { return addWebapp(getHost(), contextPath, baseDir); }
private void silence(String ctx) { String base = "org.apache.catalina.core.ContainerBase.[default].["; base += getHost().getName(); base += "].["; base += ctx; base += "]"; Logger.getLogger(base).setLevel(Level.WARNING); }
private URL getWebappConfigFileFromDirectory(File docBase, String contextName) { URL result = null; File webAppContextXml = new File(docBase, Constants.ApplicationContextXml); if (webAppContextXml.exists()) { try { result = webAppContextXml.toURI().toURL(); } catch (MalformedURLException e) { Logger.getLogger(getLoggerName(getHost(), contextName)).log(Level.WARNING, sm.getString("tomcat.noContextXml", docBase), e); } } return result; }
protected void disableUnpackWARsOption() { final Host host = server.getHost(); if (host instanceof StandardHost) { info("...Disabling unpackWARs"); ((StandardHost) host).setUnpackWARs(false); } }