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

The following examples show how to use org.apache.tomcat.util.scan.StandardJarScanner. 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: HugeGraphStudio.java    From hugegraph-studio with Apache License 2.0 6 votes vote down vote up
private static StandardContext configureWarFile(Tomcat tomcat,
                                                final String warFile,
                                                final String appBase)
        throws ServletException, IOException {

    if (warFile != null && warFile.length() > 0) {
        StandardContext context = (StandardContext) tomcat.addWebapp(appBase,
                                  new File(warFile).getAbsolutePath());
        Host host = (Host) context.getParent();
        File appBaseDirectory = host.getAppBaseFile();
        if (!appBaseDirectory.exists()) {
            appBaseDirectory.mkdirs();
        }
        context.setUnpackWAR(true);
        if (context.getJarScanner() instanceof StandardJarScanner) {
            ((StandardJarScanner) context.getJarScanner())
                    .setScanAllDirectories(true);
        }
        return context;
    }

    return null;
}
 
Example #3
Source File: BootstrapHelper.java    From watcher with Apache License 2.0 6 votes vote down vote up
private void configTomcat(final Tomcat tomcat) throws ServletException {
	//设置tomcat工作目录,maven工程里面就放到target目录下,看起来爽点,注意,这行代码不要随便移动位置,不然你可以have a try。
	tomcat.setBaseDir("target");
	tomcat.setPort(port);
	Connector connector = new Connector("HTTP/1.1");
	connector.setPort(port);
	connector.setURIEncoding("utf-8");
	tomcat.setConnector(connector);
	tomcat.getService().addConnector(connector);
	String webappPath = getWebappsPath();
	System.out.println("webapp目录:" + webappPath);
	Context ctx = tomcat.addWebapp("/", webappPath);
	StandardJarScanner scanner = (StandardJarScanner) ctx.getJarScanner();
	if (!isServlet3Enable) {
		scanner.setScanAllDirectories(false);
		scanner.setScanClassPath(false);
	}
	tomcat.setSilent(true);
	System.setProperty("org.apache.catalina.SESSION_COOKIE_NAME", "JSESSIONID" + port);
}
 
Example #4
Source File: TomEEJarScanner.java    From tomee with Apache License 2.0 6 votes vote down vote up
private void doScan(final JarScanType scanType, final JarScannerCallback callback, final Deque<URL> urls) {
    Method process = null;
    final boolean scanAllDirectories = isScanAllDirectories();
    for (final URL url : urls) {
        final File cpe = URLs.toFile(url);
        if ((cpe.getName().endsWith(".jar") ||
                scanType == JarScanType.PLUGGABILITY ||
                scanAllDirectories) &&
                getJarScanFilter().check(scanType, cpe.getName())) {
            try {
                if (process == null) {
                    process = StandardJarScanner.class.getDeclaredMethod("process",
                            JarScanType.class, JarScannerCallback.class, URL.class, String.class, boolean.class, Deque.class);
                    if (!process.isAccessible()) {
                        process.setAccessible(true);
                    }
                }
                process.invoke(this, scanType, callback, url, null, true, urls);
            } catch (final Exception ioe) {
                // no-op
            }
        }
    }
}
 
Example #5
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 #6
Source File: TestTldScanner.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBug55807() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");
    Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    ((StandardJarScanner) context.getJarScanner()).setScanAllDirectories(true);
    tomcat.start();

    ByteChunk res = new ByteChunk();
    Map<String,List<String>> headers = new HashMap<>();

    getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug55807.jsp",
            res, headers);

    // Check request completed
    String result = res.toString();
    assertEcho(result, "OK");

    // Check the dependencies count
    Assert.assertTrue(result.contains("<p>DependenciesCount: 1</p>"));

    // Check the right timestamp was used in the dependency
    File tld = new File("test/webapp/WEB-INF/classes/META-INF/bug55807.tld");
    String expected = "<p>/WEB-INF/classes/META-INF/bug55807.tld : " +
            tld.lastModified() + "</p>";
    Assert.assertTrue(result.contains(expected));


    // Check content type
    String contentType = getSingleHeader("Content-Type", headers);
    Assert.assertTrue(contentType.startsWith("text/html"));
}
 
Example #7
Source File: JeecgApplication.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
/**
* tomcat-embed-jasper引用后提示jar找不到的问题
*/
@Bean
public TomcatServletWebServerFactory tomcatFactory() {
 return new TomcatServletWebServerFactory() {
  @Override
  protected void postProcessContext(Context context) {
   ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
  }
 };
}
 
Example #8
Source File: TestCompiler.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug55807() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    ((StandardJarScanner) context.getJarScanner()).setScanAllDirectories(true);
    tomcat.start();

    ByteChunk res = new ByteChunk();
    Map<String,List<String>> headers = new HashMap<String,List<String>>();

    getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug55807.jsp",
            res, headers);

    // Check request completed
    String result = res.toString();
    assertEcho(result, "OK");

    // Check the dependencies count
    Assert.assertTrue(result.contains("<p>DependenciesCount: 1</p>"));

    // Check the right timestamp was used in the dependency
    File tld = new File("test/webapp-3.0/WEB-INF/classes/META-INF/bug55807.tld");
    String expected = "/WEB-INF/classes/META-INF/bug55807.tld : " +
            tld.lastModified() + "</p>";
    Assert.assertTrue(result.contains(expected));


    // Check content type
    Assert.assertTrue(headers.get("Content-Type").get(0).startsWith("text/html"));
}
 
Example #9
Source File: TestCompiler.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testBug55807() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    ((StandardJarScanner) context.getJarScanner()).setScanAllDirectories(true);
    tomcat.start();

    ByteChunk res = new ByteChunk();
    Map<String,List<String>> headers = new HashMap<String,List<String>>();

    getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug55807.jsp",
            res, headers);

    // Check request completed
    String result = res.toString();
    assertEcho(result, "OK");

    // Check the dependencies count
    Assert.assertTrue(result.contains("<p>DependenciesCount: 1</p>"));

    // Check the right timestamp was used in the dependency
    File tld = new File("test/webapp-3.0/WEB-INF/classes/META-INF/bug55807.tld");
    String expected = "/WEB-INF/classes/META-INF/bug55807.tld : " +
            tld.lastModified() + "</p>";
    Assert.assertTrue(result.contains(expected));


    // Check content type
    Assert.assertTrue(headers.get("Content-Type").get(0).startsWith("text/html"));
}
 
Example #10
Source File: JFishTomcat.java    From onetwo with Apache License 2.0 5 votes vote down vote up
/****
 * 设置扫描目录,让实现了ServletContainerInitializer和WebApplicationInitializer接口而不在jar里面的类被扫描到
 */
public Context addWebapp(Host host, String url, String name, String path) {
	Context ctx = super.addWebapp(host, url, name, path);
	StandardJarScanner jarScanner = new StandardJarScanner();
	jarScanner.setScanAllDirectories(true);
	ctx.setJarScanner(jarScanner);
	return ctx;
}
 
Example #11
Source File: WebXmlSpringBoot.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
public TomcatServletWebServerFactory tomcatFactory() {
    return new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
        }
    };
}
 
Example #12
Source File: WebContextConfiguration.java    From codenjoy with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public ServletContextInitializer servletContextInitializer() {
    return context -> context.setAttribute(
            JarScanner.class.getName(),
            new StandardJarScanner() {{
                setScanManifest(false);
            }}
    );
}
 
Example #13
Source File: WebXmlSpringBoot.java    From sitemonitoring-production with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
public TomcatServletWebServerFactory tomcatFactory() {
    return new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
        }
    };
}
 
Example #14
Source File: TomcatHelper.java    From tomee with Apache License 2.0 5 votes vote down vote up
public static void configureJarScanner(final Context standardContext) {
    try { // override only if default
        final JarScanner originalJarScanner = standardContext.getJarScanner();
        if ("true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.tomcat.override.jar-scanner", "true"))
                && !TomEEJarScanner.class.isInstance(originalJarScanner)
                && StandardJarScanner.class.isInstance(originalJarScanner)) {
            final TomEEJarScanner jarScanner = new TomEEJarScanner();

            final Properties properties = SystemInstance.get().getProperties();
            final String scanClasspath = properties.getProperty(TomEEJarScanner.class.getName() + ".scanClassPath");
            if (scanClasspath != null) {
                jarScanner.setScanClassPath(Boolean.parseBoolean(scanClasspath));
            }
            final String scanBootstrap = properties.getProperty(TomEEJarScanner.class.getName() + ".scanBootstrapClassPath");
            if (scanBootstrap != null) {
                jarScanner.setScanBootstrapClassPath(Boolean.parseBoolean(scanBootstrap));
            }
            final JarScanFilter jarScanFilter = originalJarScanner.getJarScanFilter();
            if (jarScanFilter != null && Boolean.parseBoolean(properties.getProperty(TomEEJarScanner.class.getName() + ".useOriginalJarScannerFilter", "true"))) {
                jarScanner.setJarScanFilter(jarScanFilter);
            }
            standardContext.setJarScanner(jarScanner);
        }
    } catch (final Exception e) {
        // ignore
    }
}
 
Example #15
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 #16
Source File: JettyLauncher.java    From JobX with Apache License 2.0 4 votes vote down vote up
public JettyJspParser(ServletContextHandler context) {
    this.jasperInitializer = new JettyJasperInitializer();
    this.context = context;
    this.context.setAttribute("org.apache.tomcat.JarScanner", new StandardJarScanner());
}