public static void main(String[] args) throws Exception { String userDir = System.getProperty("user.dir") + File.separator + "server.tomcat"; String webappDirLocation = userDir + File.separator +"src/main/webapp/"; Tomcat tomcat = new Tomcat(); String webPort = System.getenv("PORT"); if (webPort == null || webPort.isEmpty()) { webPort = "8080"; } tomcat.setPort(Integer.valueOf(webPort)); System.out.println("configuring app with basedir: " + new File(webappDirLocation).getAbsolutePath()); StandardContext standardContext = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); File additionWebInfClasses = new File(userDir + File.separator + "build/classes"); WebResourceRoot resourceRoot = new StandardRoot(standardContext); resourceRoot.addPreResources(new DirResourceSet(resourceRoot, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/")); standardContext.setResources(resourceRoot); tomcat.start(); tomcat.getServer().await(); }
@Test public void customize() throws LifecycleException { Context standardContext = mock(Context.class); StandardRoot webResourceRoot = new StandardRoot(standardContext); Mockito.when(standardContext.getResources()).thenReturn(webResourceRoot); Mockito.when(standardContext.getAddWebinfClassesResources()).thenReturn(Boolean.FALSE); JsfTomcatContextCustomizer jsfTomcatContextCustomizer = new JsfTomcatContextCustomizer(); jsfTomcatContextCustomizer.customize(standardContext); JsfTomcatApplicationListener jsfTomcatApplicationListener = JsfTomcatApplicationListener .builder().context(jsfTomcatContextCustomizer.getContext()).build(); jsfTomcatApplicationListener.onApplicationEvent(mock(ApplicationReadyEvent.class)); assertThat(webResourceRoot.getPostResources().length) .isEqualTo(9); }
/** * Sets up the test environment, generates data to upload, starts a * Tomcat instance which will receive the client requests. * @throws Exception If an error occurred with the servlets */ @BeforeClass public static void setUpClass() throws Exception { server = new Tomcat(); Path base = Paths.get("build/tomcat"); Files.createDirectories(base); server.setPort(8100); server.setBaseDir("build/tomcat"); server.getHost().setAppBase("build/tomcat"); server.getHost().setAutoDeploy(true); server.getHost().setDeployOnStartup(true); StandardContext context = (StandardContext) server.addWebapp("/", base.toAbsolutePath().toString()); Path additionWebInfClasses = Paths.get("build/classes"); WebResourceRoot resources = new StandardRoot(context); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.toAbsolutePath().toString(), "/")); context.setResources(resources); context.getJarScanner().setJarScanFilter((jarScanType, jarName) -> false); server.start(); }
@Bean public EmbeddedServletContainerFactory servletContainer() { final TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); if(useReverseProxy) { tomcat.addAdditionalTomcatConnectors(createAjpConnector()); } // Set the web resources cache size (this defaults to 10MB but that is too small for Find) tomcat.addContextCustomizers(context -> { final WebResourceRoot resources = new StandardRoot(context); resources.setCacheMaxSize(webResourcesCacheSize); context.setResources(resources); }); tomcat.addConnectorCustomizers(connector -> { connector.setMaxPostSize(connectorMaxPostSize); }); return tomcat; }
public static void main(String[] args) { Connector connector = new Connector(); connector.setPort(80); Tomcat tomcat = new Tomcat(); tomcat.setPort(80); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); try { Context ctx = tomcat.addWebapp("", Conf.getAbsolutePath("../src/main/webapp")); WebResourceRoot resources = new StandardRoot(ctx); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", Conf.getAbsolutePath("classes"), "/")); ctx.setResources(resources); Server server = tomcat.getServer(); server.start(); server.setPort(8005); server.await(); server.stop(); } catch (ServletException | LifecycleException e) { Log.e(e); } }
@Override public void setResources(final WebResourceRoot resources) { this.resources = resources; if (StandardRoot.class.isInstance(resources)) { final List<WebResourceSet> jars = (List<WebResourceSet>) Reflections.get(resources, "jarResources"); if (jars != null && !jars.isEmpty()) { final Iterator<WebResourceSet> jarIt = jars.iterator(); while (jarIt.hasNext()) { final WebResourceSet set = jarIt.next(); if (set.getBaseUrl() == null) { continue; } final File file = URLs.toFile(set.getBaseUrl()); try { if (file.exists() && (!TomEEClassLoaderEnricher.validateJarFile(file) || !jarIsAccepted(file))) { // need to remove this resource LOGGER.warning("Removing " + file.getAbsolutePath() + " since it is offending"); jarIt.remove(); } } catch (final IOException e) { // ignore } } } } }
public static void configure(TomcatEmbeddedServletContainerFactory tomcatFactory) { tomcatFactory.addContextCustomizers((TomcatContextCustomizer) context -> { boolean development = (System.getProperty("airsonic.development") != null); // Increase the size and time before eviction of the Tomcat // cache so that resources aren't uncompressed too often. // See https://github.com/jhipster/generator-jhipster/issues/3995 StandardRoot resources = new StandardRoot(); if (development) { resources.setCachingAllowed(false); } else { resources.setCacheMaxSize(100000); resources.setCacheObjectMaxSize(4000); resources.setCacheTtl(24 * 3600 * 1000); // 1 day, in milliseconds } context.setResources(resources); // Put Jasper in production mode so that JSP aren't recompiled // on each request. // See http://stackoverflow.com/questions/29653326/spring-boot-application-slow-because-of-jsp-compilation Container jsp = context.findChild("jsp"); if (jsp instanceof Wrapper) { ((Wrapper) jsp).addInitParameter("development", Boolean.toString(development)); } }); }
/** * Servlet container embedded servlet container factory. * * @return the embedded servlet container factory * @source http://stackoverflow.com/questions/39146476/how-does-spring-boot-control-tomcat-cache */ @Bean public EmbeddedServletContainerFactory servletContainer() { return new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { final int cacheSize = 256 * 1024; final StandardRoot standardRoot = new StandardRoot(context); standardRoot.setCacheMaxSize(cacheSize); context.setResources(standardRoot); logger.info(String.format("New cache size (KB): %d", context.getResources().getCacheMaxSize())); } }; }
public static void start() throws Exception { Tomcat tomcat = new Tomcat(); tomcat.setHostname("localhost"); tomcat.setPort(80); // tomcat.setBaseDir("D:\\work\\zhongcao\\zhongcao\\zhongcao-web"); // tomcat.addWebapp("WebRoot", "D:\\work\\zhongcao\\zhongcao\\zhongcao-web\\target\\zhongcao-web"); StandardContext ctx = (StandardContext) tomcat.addWebapp("/", "D:\\work\\zhongcao\\zhongcao\\zhongcao-web\\target\\zhongcao-web"); StandardRoot resources = new StandardRoot(ctx); resources.setCachingAllowed(false); ctx.setResources(resources); tomcat.start(); }
@Test public void customizeTargetTestClasses() throws LifecycleException { Context standardContext = mock(Context.class); StandardRoot webResourceRoot = new StandardRoot(standardContext); Mockito.when(standardContext.getResources()).thenReturn(webResourceRoot); Mockito.when(standardContext.getAddWebinfClassesResources()).thenReturn(Boolean.FALSE); String absolutePath = new File("").getAbsolutePath(); String internalPath = METAINF_RESOURCES; String targetTestClassesBase = absolutePath + "/" + "target/test-classes"; File testClassesResources = new File(targetTestClassesBase + internalPath); if (!testClassesResources.mkdirs()) { throw new RuntimeException("Could not create dir: " + testClassesResources.toString()); } JsfTomcatContextCustomizer jsfTomcatContextCustomizer = new JsfTomcatContextCustomizer(); jsfTomcatContextCustomizer.customize(standardContext); JsfTomcatApplicationListener jsfTomcatApplicationListener = JsfTomcatApplicationListener .builder().context(jsfTomcatContextCustomizer.getContext()).build(); jsfTomcatApplicationListener.onApplicationEvent(mock(ApplicationReadyEvent.class)); if (!testClassesResources.delete()) { throw new RuntimeException("Could not delete dir: " + testClassesResources.toString()); } assertThat(webResourceRoot.getPostResources().length) .isEqualTo(10); }
/** * Customize Tomcat configuration. */ private void customizeTomcat(ConfigurableEmbeddedServletContainer container) { if (container instanceof TomcatEmbeddedServletContainerFactory) { TomcatEmbeddedServletContainerFactory tomcatFactory = (TomcatEmbeddedServletContainerFactory) container; tomcatFactory.addContextCustomizers((TomcatContextCustomizer) context -> { // See https://github.com/jhipster/generator-jhipster/issues/3995 StandardRoot resources = new StandardRoot(); resources.setCacheMaxSize(40960); resources.setCacheObjectMaxSize(2048); context.setResources(resources); }); } }
@Override public void configure(TomcatEmbeddedServletContainerFactory tomcat, int cacheSize, int tomcatCacheSize) { tomcat.addContextCustomizers((context) -> { StandardRoot standardRoot = new StandardRoot(context); standardRoot.setCachingAllowed(true); standardRoot.setCacheMaxSize(cacheSize); standardRoot.setCacheTtl(tomcatCacheSize); context.setResources(standardRoot); }); }
public static void main(String[] args) throws Exception { String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); //The port that we should run on can be set into an environment variable //Look for that variable and default to 8080 if it isn't there. String webPort = System.getenv("PORT"); if(webPort == null || webPort.isEmpty()) { webPort = "8080"; } tomcat.setPort(Integer.valueOf(webPort)); StandardContext ctx = (StandardContext) tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath()); // Declare an alternative location for your "WEB-INF/classes" dir // Servlet 3.0 annotation will work File additionWebInfClasses = new File("target/classes"); WebResourceRoot resources = new StandardRoot(ctx); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/")); ctx.setResources(resources); tomcat.start(); tomcat.getServer().await(); }
public static void main(String[] args) { Connector connector = new Connector(); connector.setPort(80); Tomcat tomcat = new Tomcat(); tomcat.setPort(80); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); try { Context ctx = tomcat.addWebapp("", Conf.getAbsolutePath("../src/main/webapp")); // Ensure to Load All Classes in the Same Class Loader ctx.setLoader(new WebappLoader(Startup.class.getClassLoader()) { @Override public ClassLoader getClassLoader() { return Startup.class.getClassLoader(); } }); WebResourceRoot resources = new StandardRoot(ctx); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", Conf.getAbsolutePath("classes"), "/")); ctx.setResources(resources); Server server = tomcat.getServer(); server.start(); server.setPort(8005); server.await(); server.stop(); } catch (ServletException | LifecycleException e) { Log.e(e); } }
@Bean public EmbeddedServletContainerFactory servletContainer() { return new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context ctx) { final int sizeInKB = 32 * 1024; // default is 10MiB, increase to 32 WebResourceRoot resourceRoot = new StandardRoot(ctx); resourceRoot.setCacheMaxSize(sizeInKB); ctx.setResources(resourceRoot); } }; }
public List<String> getTrackedResources() { // IDE? return StandardRoot.class.cast(delegate).getTrackedResources(); }
@Before public void initContext() throws LifecycleException { final OpenEjbConfiguration configuration = new OpenEjbConfiguration(); configuration.facilities = new FacilitiesInfo(); final CoreContainerSystem containerSystem = new CoreContainerSystem(new IvmJndiFactory()); SystemInstance.get().setComponent(OpenEjbConfiguration.class, configuration); SystemInstance.get().setComponent(ContainerSystem.class, containerSystem); SystemInstance.get().setComponent(WebAppEnricher.class, new WebAppEnricher() { @Override public URL[] enrichment(final ClassLoader webappClassLaoder) { return new URL[0]; } }); parentInstance = new AtomicReference<>(ParentClassLoaderFinder.Helper.get()); loader = new TomEEWebappClassLoader(parentInstance.get()) { @Override public ClassLoader getInternalParent() { return parentInstance.get(); } @Override protected void clearReferences() { // no-op: this test should be reworked to support it but in real life a loader is not stopped/started } }; loader.init(); final StandardRoot resources = new StandardRoot(); loader.setResources(resources); resources.setContext(new StandardContext() { @Override public String getDocBase() { final File file = new File("target/foo"); file.mkdirs(); return file.getAbsolutePath(); } @Override public String getMBeanKeyProperties() { return "foo"; } {}}); resources.start(); loader.start(); info = new AppInfo(); info.appId = "test"; context = new AppContext(info.appId, SystemInstance.get(), loader, new IvmContext(), new IvmContext(), true); containerSystem.addAppContext(context); final WebContext webDeployment = new WebContext(context); webDeployment.setId(context.getId()); webDeployment.setClassLoader(loader); containerSystem.addWebContext(webDeployment); }