user671731
user671731

Reputation: 509

Embedded Tomcat 7.0.32 startup jar scanner

I am embedding Tomcat 7.0.32 in a Flume Source. The problem i am having is Flume places a ton of stuff on the Classpath and upon startup, Tomcat scans the classpath looking for TLDs, which is causing problems because of the wildcards used by flume.

INFO: Starting Servlet Engine: Apache Tomcat/7.0.32 Nov 05, 2012 8:21:08 AM org.apache.catalina.startup.TldConfig tldScanJar WARNING: Failed to process JAR [jar:file:/opt/mapr/hadoop/hadoop-0.20.2/hadoop*core*.jar!/] for TLD files java.io.FileNotFoundException: /opt/mapr/hadoop/hadoop-0.20.2/hadoop*core*.jar (No such file or directory) at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.(ZipFile.java:214) at java.util.zip.ZipFile.(ZipFile.java:144) at java.util.jar.JarFile.(JarFile.java:152) at java.util.jar.JarFile.(JarFile.java:89) at sun.net.www.protocol.jar.URLJarFile.(URLJarFile.java:93) at sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69) at sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:88) at sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122) at sun.net.www.protocol.jar.JarURLConnection.getJarFile(JarURLConnection.java:89) at org.apache.tomcat.util.scan.FileUrlJar.(FileUrlJar.java:41) at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:34) at org.apache.catalina.startup.TldConfig.tldScanJar(TldConfig.java:487) at org.apache.catalina.startup.TldConfig.access$100(TldConfig.java:58) at org.apache.catalina.startup.TldConfig$TldJarScannerCallback.scan(TldConfig.java:303) at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:241) at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:204) at org.apache.catalina.startup.TldConfig.execute(TldConfig.java:277)

In my code i have tried to disable this scanning and even tried setting the JarScanner to null on my context. And this does not help. Below is my code:

File docBase = new File(System.getProperty("java.io.tmpdir"));
    System.out.println("------- " + docBase.getAbsolutePath());

    String servletName = "1 source";

    Tomcat tomcat = new Tomcat();
    tomcat.setPort(8080);
    tomcat.setBaseDir(docBase.getAbsolutePath());

    Context context = tomcat.addContext("", docBase.getAbsolutePath());
    context.setIgnoreAnnotations(true);
    context.setJarScanner(null);

    Tomcat.addServlet(context, servletName, new HttpServlet() {

Upvotes: 1

Views: 2718

Answers (3)

Wojtek Sierka
Wojtek Sierka

Reputation: 31

The other two answers don't work with Tomcat 8.5+. addWebApp takes contextconfig as string. But you can use code below to do selective scanning. Note that if JarScanner is defined in context.xml it will take precedence over the code. JarScanFilter will be called back once you call tomcat.start()

            Context context = tomcat.addWebapp("/"+sApp, location);
            //Keep in mind that this JarScanFilter will be overwritten by JarScanner that defined in META-INF\context.xml
            JarScanFilter jarScanFilter = new JarScanFilter() {

                @Override
                public boolean check(JarScanType jarScanType, String jarName) {

                    if(jarName.startsWith("spring")) {
                        System.out.println("JarScanType: "+ jarScanType +" JarName: "+ jarName + " Will Scan: true");
                        return true;
                    }
                    System.out.println("JarScanType: "+ jarScanType +" JarName: "+ jarName + " Will Scan: false");
                    return false;
                }
            };
            context.getJarScanner().setJarScanFilter(jarScanFilter);

Upvotes: 0

Andriy
Andriy

Reputation: 21

Yo can disable scan classpath and manifest before tomcat.addWebapp():

ContextConfig contextConfig = new ContextConfig() {
            private boolean invoked = false;

            @Override
            public void lifecycleEvent(LifecycleEvent event) {
                if (!invoked) {
                    StandardJarScanner scanner = new StandardJarScanner();
                    scanner.setScanClassPath(false);
                    scanner.setScanManifest(false);
                    ((Context) event.getLifecycle()).setJarScanner(scanner);
                    invoked = true;
                }
                super.lifecycleEvent(event);
            }
        };
        Context ctx = tomcat.addWebapp(tomcat.getHost(), "/", "path/to/war", contextConfig);

Upvotes: 1

CCQ
CCQ

Reputation: 11

try this! Context context = tomcat.addWebapp("/d", "d:/"); JarScanner jarScanner = new JarScanner() {

        @Override
        public void scan(ServletContext arg0, ClassLoader arg1,
                JarScannerCallback arg2, Set<String> arg3)
        {
            //do nothing
        }
    };
    context.setJarScanner(jarScanner);

Upvotes: 1

Related Questions