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(); }
/** * Inform tomcat runtime setup. UNPACKAGED_WAR not covered yet. * @param resources of the tomcat * @return tomcat runtime */ TomcatRuntime getTomcatRuntime(WebResourceRoot resources) { TomcatRuntime result = null; if (isUberJar(resources)) { result = TomcatRuntime.UBER_JAR; } else if (isUberWar(resources)) { result = TomcatRuntime.UBER_WAR; } else if (isTesting(resources)) { result = TomcatRuntime.TEST; } else if (isUnpackagedJar(resources)) { result = TomcatRuntime.UNPACKAGED_JAR; } return result; }
@Test public void jarResourcesNull() throws LifecycleException { Context standardContext = mock(Context.class); WebResourceRoot webResourceRoot = mock(WebResourceRoot.class); Mockito.when(standardContext.getResources()).thenReturn(webResourceRoot); Mockito.when(standardContext.getAddWebinfClassesResources()).thenReturn(Boolean.FALSE); Mockito.when(webResourceRoot.getJarResources()).thenReturn(null); JsfTomcatContextCustomizer jsfTomcatContextCustomizer = new JsfTomcatContextCustomizer(); jsfTomcatContextCustomizer.customize(standardContext); JsfTomcatApplicationListener jsfTomcatApplicationListener = JsfTomcatApplicationListener .builder().context(jsfTomcatContextCustomizer.getContext()).build(); jsfTomcatApplicationListener.onApplicationEvent(mock(ApplicationReadyEvent.class)); assertThat(jsfTomcatApplicationListener) .isNotNull(); }
public RAFSResource(WebResourceRoot root, Path path, RAFS rafs) { super(root, path); m_RAFS = rafs; m_Cache = null; try { if(Files.exists(path) && !Files.isDirectory(path) && isInRAF()) { Version v = rafs.getVersionDataForFile(path, null); if(v != null) { m_Cache = v.dataSource.read(); } } } catch(IOException e) { m_Cache = null; } }
/** * 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 } } } } }
/** * {@inheritDoc} */ @Override public void destroy(final StandardContext standardContext) { final Loader standardContextLoader = standardContext.getLoader(); if (LazyStopLoader.class.isInstance(standardContextLoader)) { final Loader delegate = LazyStopLoader.class.cast(standardContextLoader).getDelegateLoader(); if (TomEEWebappLoader.class.isInstance(delegate)) { final TomEEWebappLoader webappLoader = TomEEWebappLoader.class.cast(delegate); final ClassLoader loader = webappLoader.internalLoader(); webappLoader.clearLoader(); if (TomEEWebappClassLoader.class.isInstance(loader)) { TomEEWebappClassLoader.class.cast(loader).internalDestroy(); } } } final WebResourceRoot root = standardContext.getResources(); if (LazyStopStandardRoot.class.isInstance(root)) { try { LazyStopStandardRoot.class.cast(root).internalDestroy(); } catch (final LifecycleException e) { throw new IllegalStateException(e); } } }
private JarWarResourceSet getFirstJarWarResourceSetAtJarResources(WebResourceRoot resources) { JarWarResourceSet result = null; for (WebResourceSet resourceSet :resources.getJarResources()) { if (resourceSet instanceof JarWarResourceSet) { result = (JarWarResourceSet) resourceSet; break; } } return result; }
private URL mainFile(WebResourceRoot resources) { URL result = null; for (WebResourceSet resourceSet :resources.getJarResources()) { if (resourceSet instanceof JarWarResourceSet) { result = resourceSet.getBaseUrl(); break; } } return result; }
private void addMainJarResourceSet(WebResourceRoot resources) throws URISyntaxException { String webAppMount = "/"; String archivePath = null; String internalPath = "/META-INF/resources"; resources.createWebResourceSet(WebResourceRoot.ResourceSetType.POST, webAppMount, base(mainFile(resources)), archivePath, internalPath); }
private void addClasspathResourceSets(WebResourceRoot resources) throws URISyntaxException { String webAppMount = "/"; String archivePath = null; String internalPath = "/META-INF/resources"; for (URL url : ClasspathHelper.forResource("META-INF/resources/", this.getClass().getClassLoader())) { resources.createWebResourceSet(WebResourceRoot.ResourceSetType.POST, webAppMount, base(url), archivePath, internalPath); } }
JarSubsetResourceSet(WebResourceRoot root, String webAppMount, String base, String internalPath, String jarRoot) { super(root, webAppMount, base, internalPath); // Should be normalized by TomcatServiceBuilder. assert !"/".equals(jarRoot) : "JarResourceSet should be used instead."; assert jarRoot.startsWith("/") : "jarRoot must be absolute."; assert !jarRoot.endsWith("/") : "jarRoot must not end with '/'."; prefix = jarRoot.substring(1) + '/'; }
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); } }; }
/** * Scan JARs that contain web-fragment.xml files that will be used to * configure this application to see if they also contain static resources. * If static resources are found, add them to the context. Resources are * added in web-fragment.xml priority order. */ @Override protected void processResourceJARs(Set<WebXml> fragments) { for (WebXml fragment : fragments) { URL url = fragment.getURL(); try { String urlString = url.toString(); if (isInsideNestedJar(urlString)) { // It's a nested jar but we now don't want the suffix // because // Tomcat // is going to try and locate it as a root URL (not the // resource // inside it) urlString = urlString.substring(0, urlString.length() - 2); } url = new URL(urlString); if ("jar".equals(url.getProtocol())) { try (Jar jar = JarFactory.newInstance(url)) { jar.nextEntry(); String entryName = jar.getEntryName(); while (entryName != null) { if (entryName.startsWith("META-INF/resources/")) { context.getResources().createWebResourceSet( WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", url, "/META-INF/resources"); break; } jar.nextEntry(); entryName = jar.getEntryName(); } } } else if ("file".equals(url.getProtocol())) { File file = new File(url.toURI()); File resources = new File(file, "META-INF/resources/"); if (resources.isDirectory()) { context.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", resources.getAbsolutePath(), null, "/"); } } } catch (IOException ioe) { log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName())); } catch (URISyntaxException e) { log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName())); } } }
private boolean isUberJar(WebResourceRoot resources) { JarWarResourceSet jarWarResourceSet = getFirstJarWarResourceSetAtJarResources(resources); return jarWarResourceSet != null && jarWarResourceSet.getBaseUrl().getFile().endsWith(".jar"); }
private boolean isUberWar(WebResourceRoot resources) { JarWarResourceSet jarWarResourceSet = getFirstJarWarResourceSetAtJarResources(resources); return jarWarResourceSet != null && jarWarResourceSet.getBaseUrl().getFile().endsWith(".war"); }
private boolean isTesting(WebResourceRoot resources) { return !isUberJar(resources) && !isUberWar(resources) && getFirstDirResourceSetAtJarResources(resources) == null; }
private boolean isUnpackagedJar(WebResourceRoot resources) { return !isUberJar(resources) && getFirstDirResourceSetAtJarResources(resources) != null; }
public NIOResource(WebResourceRoot root, Path path) { super(root, getWebappRoot(path)); this.path = path; }
public PremptiveDirResourceSet(final WebResourceRoot resources, final String s, final String absolutePath, final String s1) { super(resources, s, absolutePath, s1); }
public LazyStopStandardRoot(final WebResourceRoot delegate) { this.delegate = delegate; this.isJmxEnabled = JmxEnabled.class.isInstance(delegate); this.isLifecycleMBeanBase = LifecycleMBeanBase.class.isInstance(delegate); }
public WebResourceRoot getDelegate() { return delegate; }
private static DeploymentLoader.ExternalConfiguration configuredClasspath(final StandardContext standardContext) { Loader loader = standardContext.getLoader(); if (loader != null && LazyStopLoader.class.isInstance(loader)) { loader = LazyStopLoader.class.cast(loader).getDelegateLoader(); } if (loader != null) { final ClassLoader cl = standardContext.getLoader().getClassLoader(); if (cl == null) { return null; } final Collection<String> cp = new LinkedList<>(); final WebResourceRoot webResources = standardContext.getResources(); if (webResources != null) { // to enhance for (final WebResourceSet[] sets : asList(webResources.getPreResources(), webResources.getPostResources(), webResources.getJarResources())) { for (final WebResourceSet wr : sets) { final URL base = wr.getBaseUrl(); if (base != null) { final File baseFile = URLs.toFile(base); if (baseFile.isDirectory()) { final String[] libs = wr.list("/WEB-INF/lib/"); if (libs != null) { for (final String resource : libs) { cp.add(new File(baseFile, resource).getAbsolutePath()); } } final WebResource classes = wr.getResource("/WEB-INF/classes/"); if (classes != null) { final String path = classes.getCanonicalPath(); if (path != null) { cp.add(path); } } } else if (baseFile.exists() && baseFile.getName().endsWith(".jar") && wr.getResource("/WEB-INF/classes/").exists()) { try { cp.add(baseFile.getCanonicalPath()); } catch (final IOException e) { throw new IllegalStateException(e); } } } } } } if (!cp.isEmpty()) { return new DeploymentLoader.ExternalConfiguration(cp.toArray(new String[cp.size()]), null /*for now doesnt make sense, todo: configure*/); } } return null; }
@Override public TomcatApplicationBuilder setResources(WebResourceRoot resources) { context.setResources(resources); return this; }
/** * @return this */ TomcatApplicationBuilder setResources(WebResourceRoot resources);