org.apache.tomcat.util.scan.StandardJarScanFilter Java Examples

The following examples show how to use org.apache.tomcat.util.scan.StandardJarScanFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TomcatBaseTest.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Make the Tomcat instance preconfigured with test/webapp available to
 * sub-classes.
 * @param addJstl Should JSTL support be added to the test webapp
 * @param start   Should the Tomcat instance be started
 *
 * @return A Tomcat instance pre-configured with the web application located
 *         at test/webapp
 *
 * @throws LifecycleException If a problem occurs while starting the
 *                            instance
 */
public Tomcat getTomcatInstanceTestWebapp(boolean addJstl, boolean start)
        throws LifecycleException {
    File appDir = new File("test/webapp");
    Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    StandardJarScanner scanner = (StandardJarScanner) ctx.getJarScanner();
    StandardJarScanFilter filter = (StandardJarScanFilter) scanner.getJarScanFilter();
    filter.setTldSkip(filter.getTldSkip() + ",testclasses");
    filter.setPluggabilitySkip(filter.getPluggabilitySkip() + ",testclasses");

    if (addJstl) {
        File lib = new File("webapps/examples/WEB-INF/lib");
        ctx.setResources(new StandardRoot(ctx));
        ctx.getResources().createWebResourceSet(
                WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib",
                lib.getAbsolutePath(), null, "/");
    }

    if (start) {
        tomcat.start();
    }
    return tomcat;
}
 
Example #2
Source File: JspCServletContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private Map<String, WebXml> scanForFragments(WebXmlParser webXmlParser) throws JasperException {
    StandardJarScanner scanner = new StandardJarScanner();
    // TODO - enabling this means initializing the classloader first in JspC
    scanner.setScanClassPath(false);
    // TODO - configure filter rules from Ant rather then system properties
    scanner.setJarScanFilter(new StandardJarScanFilter());

    FragmentJarScannerCallback callback =
            new FragmentJarScannerCallback(webXmlParser, false, true);
    scanner.scan(JarScanType.PLUGGABILITY, this, callback);
    if (!callback.isOk()) {
        throw new JasperException(Localizer.getMessage("jspc.error.invalidFragment"));
    }
    return callback.getFragments();
}
 
Example #3
Source File: TomcatApplication.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
public static void configure(TomcatServletWebServerFactory tomcatFactory) {

        tomcatFactory.addContextCustomizers(context -> {

            StandardJarScanFilter standardJarScanFilter = new StandardJarScanFilter();
            standardJarScanFilter.setTldScan("jstl-*.jar,spring-security-taglibs-*.jar,spring-web-*.jar,spring-webmvc-*.jar,string-*.jar,taglibs-standard-impl-*.jar,tomcat-annotations-api-*.jar,tomcat-embed-jasper-*.jar");
            standardJarScanFilter.setTldSkip("*");
            context.getJarScanner().setJarScanFilter(standardJarScanFilter);

            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));
            }
        });
    }
 
Example #4
Source File: TomcatApplication.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
public static void configure(TomcatServletWebServerFactory tomcatFactory) {

        tomcatFactory.addContextCustomizers(context -> {

            StandardJarScanFilter standardJarScanFilter = new StandardJarScanFilter();
            standardJarScanFilter.setTldScan("dwr-*.jar,jstl-*.jar,spring-security-taglibs-*.jar,spring-web-*.jar,spring-webmvc-*.jar,string-*.jar,taglibs-standard-impl-*.jar,tomcat-annotations-api-*.jar,tomcat-embed-jasper-*.jar");
            standardJarScanFilter.setTldSkip("*");
            context.getJarScanner().setJarScanFilter(standardJarScanFilter);

            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));
            }
        });
    }
 
Example #5
Source File: TomcatCustomServer.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
protected void init(final String contextRoot, final String resourceBase) throws ServletException, IOException {
    final Path basePath = Files.createTempDirectory(DEFAULT_TOMCAT_BASE_TEMP_DIR);
    setBaseDir(basePath.toString());

    if (StringUtils.isNotEmpty(resourceBase)) {
        this.resourceBase = resourceBase;
    }

    initExecutor();
    initConnector();

    final ContextConfig conf = new ContextConfig();
    final StandardContext ctx = (StandardContext) this.addWebapp(getHost(), contextRoot, new File(this.resourceBase).getAbsolutePath(), conf);
    createGlobalXml();
    conf.setDefaultWebXml(globalWebXml.getAbsolutePath());

    for (LifecycleListener listen : ctx.findLifecycleListeners()) {
        if (listen instanceof DefaultWebXmlListener) {
            ctx.removeLifecycleListener(listen);
        }
    }

    ctx.setParentClassLoader(TomcatCustomServer.class.getClassLoader());

    //Disable TLD scanning by default
    if (System.getProperty(Constants.SKIP_JARS_PROPERTY) == null && System.getProperty(Constants.SKIP_JARS_PROPERTY) == null) {
        LOGGER.debug("disabling TLD scanning");
        StandardJarScanFilter jarScanFilter = (StandardJarScanFilter) ctx.getJarScanner().getJarScanFilter();
        jarScanFilter.setTldSkip("*");
    }
}
 
Example #6
Source File: TomcatBaseTest.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
public static void skipTldsForResourceJars(Context context) {
    StandardJarScanner scanner = (StandardJarScanner) context.getJarScanner();
    StandardJarScanFilter filter = (StandardJarScanFilter) scanner.getJarScanFilter();
    filter.setTldSkip(filter.getTldSkip() + ",resources*.jar");
}
 
Example #7
Source File: TomcatOnlyApplication.java    From spring-graalvm-native with Apache License 2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
	Registry.disableRegistry();

	tomcatBase.mkdir();
	docBase.mkdir();
	serverBase.mkdir();

	Tomcat tomcat = new Tomcat();
	tomcat.setBaseDir(serverBase.getAbsolutePath());
	Connector connector = new Connector(Http11NioProtocol.class.getName());
	connector.setThrowOnFailure(true);
	connector.setPort(8080);
	tomcat.getService().addConnector(connector);
	tomcat.setConnector(connector);
	tomcat.getHost().setAutoDeploy(false);

	TomcatEmbeddedContext context = new TomcatEmbeddedContext();
	context.setResources(new LoaderHidingResourceRoot(context));
	context.setName("ROOT");
	context.setDisplayName("sample-tomcat-context");
	context.setPath("");
	context.setDocBase(docBase.getAbsolutePath());
	context.addLifecycleListener(new Tomcat.FixContextListener());
	context.setParentClassLoader(Thread.currentThread().getContextClassLoader());
	context.addLocaleEncodingMappingParameter(Locale.ENGLISH.toString(), DEFAULT_CHARSET.displayName());
	context.addLocaleEncodingMappingParameter(Locale.FRENCH.toString(), DEFAULT_CHARSET.displayName());
	context.setUseRelativeRedirects(false);
	try {
		context.setCreateUploadTargets(true);
	}
	catch (NoSuchMethodError ex) {
		// Tomcat is < 8.5.39. Continue.
	}
	StandardJarScanFilter filter = new StandardJarScanFilter();
	filter.setTldSkip(collectionToDelimitedString(TldSkipPatterns.DEFAULT, ",", "", ""));
	context.getJarScanner().setJarScanFilter(filter);
	WebappLoader loader = new WebappLoader(context.getParentClassLoader());
	loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());
	loader.setDelegate(true);
	context.setLoader(loader);

	Wrapper helloServlet = context.createWrapper();
	String servletName = HelloFromTomcatServlet.class.getSimpleName();
	helloServlet.setName(servletName);
	helloServlet.setServletClass(HelloFromTomcatServlet.class.getName());
	helloServlet.setLoadOnStartup(1);
	helloServlet.setOverridable(true);
	context.addChild(helloServlet);
	context.addServletMappingDecoded("/", servletName);

	tomcat.getHost().addChild(context);
	tomcat.getHost().setAutoDeploy(false);
	TomcatWebServer server = new TomcatWebServer(tomcat);
	server.start();
}
 
Example #8
Source File: ArkTomcatServletWebServerFactory.java    From sofa-ark with Apache License 2.0 4 votes vote down vote up
private void configureTldSkipPatterns(StandardContext context) {
    StandardJarScanFilter filter = new StandardJarScanFilter();
    filter.setTldSkip(StringUtils.collectionToCommaDelimitedString(getTldSkipPatterns()));
    context.getJarScanner().setJarScanFilter(filter);
}