嵌入式Tomcat 7.0.32启动jar扫描程序

时间:2012-11-05 13:34:04

标签: tomcat flume

我在Tomme 7.0.32中嵌入了Flume Source。我遇到的问题是Flume在Classpath上放置了大量的东西,在启动时,Tomcat扫描类路径寻找TLD,由于水槽使用的通配符导致TLD问题。

信息:启动Servlet引擎:Apache Tomcat / 7.0.32 2012年11月5日上午8:21:08 org.apache.catalina.startup.TldConfig tldScanJar 警告:无法处理TLD文件的JAR [jar:file:/opt/mapr/hadoop/hadoop-0.20.2/hadoop*core*.jar!/] java.io.FileNotFoundException:/opt/mapr/hadoop/hadoop-0.20.2/hadoop*core*.jar(没有这样的文件或目录)     at java.util.zip.ZipFile.open(Native Method)     在java.util.zip.ZipFile。(ZipFile.java:214)     在java.util.zip.ZipFile。(ZipFile.java:144)     在java.util.jar.JarFile。(JarFile.java:152)     在java.util.jar.JarFile。(JarFile.java:89)     在sun.net.www.protocol.jar.URLJarFile。(URLJarFile.java:93)     在sun.net.www.protocol.jar.URLJarFile.getJarFile(URLJarFile.java:69)     在sun.net.www.protocol.jar.JarFileFactory.get(JarFileFactory.java:88)     在sun.net.www.protocol.jar.JarURLConnection.connect(JarURLConnection.java:122)     在sun.net.www.protocol.jar.JarURLConnection.getJarFile(JarURLConnection.java:89)     在org.apache.tomcat.util.scan.FileUrlJar。(FileUrlJar.java:41)     在org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:34)     在org.apache.catalina.startup.TldConfig.tldScanJar(TldConfig.java:487)     在org.apache.catalina.startup.TldConfig.access $ 100(TldConfig.java:58)     在org.apache.catalina.startup.TldConfig $ TldJarScannerCallback.scan(TldConfig.java:303)     at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:241)     在org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:204)     在org.apache.catalina.startup.TldConfig.execute(TldConfig.java:277)

在我的代码中,我试图禁用此扫描,甚至尝试在我的上下文中将JarScanner设置为null。这没有用。以下是我的代码:

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() {

3 个答案:

答案 0 :(得分:1)

试试这个!         Context context = tomcat.addWebapp(“/ d”,“d:/”);         JarScanner jarScanner =新的JarScanner()         {

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

答案 1 :(得分:1)

Yo可以在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);

答案 2 :(得分:0)

其他两个答案不适用于Tomcat 8.5+。 addWebApp将contextconfig作为字符串。但是您可以使用下面的代码进行选择性扫描。请注意,如果在context.xml中定义了JarScanner,它将优先于代码。一旦调用tomcat.start()

,JarScanFilter将被回调。
            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);