org.apache.tomcat.JarScannerCallback Java Examples

The following examples show how to use org.apache.tomcat.JarScannerCallback. 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: FatJarScanner.java    From oxygen with Apache License 2.0 6 votes vote down vote up
/**
 * Scan the provided ServletContext and class loade r for JAR files. Each JAR file found will be
 * passed to the callback handle to be processed.
 *
 * @param scanType The type of JAR scan to perform. This is passed to the filter which uses it to
 * determine how to filter the results
 * @param context The ServletContext - used to locate and access WEB-INF/lib
 * @param callback The handle to process any JARs found
 */
@Override
public void scan(JarScanType scanType, ServletContext context, JarScannerCallback callback) {

  if (log.isTraceEnabled()) {
    log.trace(SM.getString("jarScan.webinflibStart"));
  }

  Set<URL> processedURLs = new HashSet<>();
  // Scan WEB-INF/lib
  doScanWebInfLib(scanType, context, callback, processedURLs);
  // Scan WEB-INF/classes
  doScanWebInf(context, callback, processedURLs);
  // Scan the classpath
  doScanClassPath(scanType, context, callback, processedURLs);
}
 
Example #2
Source File: FatJarScanner.java    From oxygen with Apache License 2.0 6 votes vote down vote up
private void doScanWebInf(ServletContext context, JarScannerCallback callback,
    Set<URL> processedURLs) {
  try {
    URL webInfURL = context.getResource(TomcatConf.WEB_INF_CLASSES);
    if (webInfURL != null) {
      // WEB-INF/classes will also be included in the URLs returned
      // by the web application class loader so ensure the class path
      // scanning below does not re-scan this location.
      processedURLs.add(webInfURL);

      URL url = context.getResource(TomcatConf.WEB_INF_CLASSES + TomcatConf.META_INF_PATH);
      if (url != null) {
        callback.scanWebInfClasses();
      }
    }
  } catch (IOException e) {
    log.warn(SM.getString("jarScan.webinfclassesFail"), e);
  }
}
 
Example #3
Source File: FatJarScanner.java    From oxygen with Apache License 2.0 6 votes vote down vote up
private void processURLs(JarScanType scanType, JarScannerCallback callback,
    Set<URL> processedURLs, boolean isWebapp, Deque<URL> classPathUrlsToProcess) {
  while (!classPathUrlsToProcess.isEmpty()) {
    URL url = classPathUrlsToProcess.pop();

    if (processedURLs.contains(url)) {
      // Skip this URL it has already been processed
      continue;
    }
    if (log.isDebugEnabled()) {
      log.debug(SM.getString("jarScan.classloaderJarScan", url));
    }
    try {
      processedURLs.add(url);
      process(scanType, callback, url, null, isWebapp, classPathUrlsToProcess);
    } catch (IOException ioe) {
      log.warn(SM.getString("jarScan.classloaderFail", url), ioe);
    }
  }
}
 
Example #4
Source File: FatJarScanner.java    From oxygen with Apache License 2.0 6 votes vote down vote up
/**
 * Scan a URL for JARs with the optional extensions to look at all files and all directories.
 */
private void process(JarScanType scanType, JarScannerCallback callback, URL url,
    String webappPath, boolean isWebapp, Deque<URL> classPathUrlsToProcess) throws IOException {

  if (log.isTraceEnabled()) {
    log.trace(SM.getString("jarScan.jarUrlStart", url));
  }

  if (Urls.URL_PROTOCOL_JAR.equals(url.getProtocol()) || url.getPath()
      .endsWith(TomcatConf.JAR_EXT)) {
    try (Jar jar = JarFactory.newInstance(url)) {
      processManifest(jar, isWebapp, classPathUrlsToProcess);
      callback.scan(jar, webappPath, isWebapp);
    }
  } else if (Urls.URL_PROTOCOL_FILE.equals(url.getProtocol())) {
    processFile(scanType, callback, url, webappPath, isWebapp, classPathUrlsToProcess);
  }
}
 
Example #5
Source File: FatJarScanner.java    From oxygen with Apache License 2.0 6 votes vote down vote up
private void processFile(JarScanType scanType, JarScannerCallback callback, URL url,
    String webappPath, boolean isWebapp, Deque<URL> classPathUrlsToProcess) throws IOException {
  try {
    File f = new File(url.toURI());
    if (f.isFile()) {
      // Treat this file as a JAR
      try (Jar jar = JarFactory.newInstance(UriUtil.buildJarUrl(f))) {
        processManifest(jar, isWebapp, classPathUrlsToProcess);
        callback.scan(jar, webappPath, isWebapp);
      }
    } else if (f.isDirectory()) {
      if (scanType == JarScanType.PLUGGABILITY) {
        callback.scan(f, webappPath, isWebapp);
      } else {
        if (new File(f.getAbsoluteFile(), TomcatConf.META_INF).isDirectory()) {
          callback.scan(f, webappPath, isWebapp);
        }
      }
    }
  } catch (URISyntaxException e) {
    throw new IOException(e);
  }
}
 
Example #6
Source File: TomEEJarScanner.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public void scan(final JarScanType scanType, final ServletContext context, final JarScannerCallback callback) {
    super.scan(scanType, context, callback);
    if (!embeddedSurefireScanning(scanType, context, callback) && isScanClassPath() && !URLClassLoader.class.isInstance(getSystemClassLoader())
            && !Boolean.getBoolean("tomee.classpath.scanning.disabled")) {
        // TODO: check on tomcat upgrade if it is fixed
        final String cp = System.getProperty("java.class.path");
        final Collection<URL> urls = new HashSet<>();
        for (final String jar : cp.split(File.pathSeparator)) {
            if(!jar.isEmpty()){
                try {
                    urls.add(new File(jar).toURI().toURL());
                } catch (MalformedURLException e) {
                    // no-op
                }
            }
        }
        doScan(scanType, callback, new LinkedList<>(urls));
    }
}
 
Example #7
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 #8
Source File: StandardJarScanner.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
protected void processURLs(JarScanType scanType, JarScannerCallback callback,
        Set<URL> processedURLs, boolean isWebapp, Deque<URL> classPathUrlsToProcess) {
    while (!classPathUrlsToProcess.isEmpty()) {
        URL url = classPathUrlsToProcess.pop();

        if (processedURLs.contains(url)) {
            // Skip this URL it has already been processed
            continue;
        }

        ClassPathEntry cpe = new ClassPathEntry(url);

        // JARs are scanned unless the filter says not to.
        // Directories are scanned for pluggability scans or
        // if scanAllDirectories is enabled unless the
        // filter says not to.
        if ((cpe.isJar() ||
                scanType == JarScanType.PLUGGABILITY ||
                isScanAllDirectories()) &&
                        getJarScanFilter().check(scanType,
                                cpe.getName())) {
            if (log.isDebugEnabled()) {
                log.debug(sm.getString("jarScan.classloaderJarScan", url));
            }
            try {
                processedURLs.add(url);
                process(scanType, callback, url, null, isWebapp, classPathUrlsToProcess);
            } catch (IOException ioe) {
                log.warn(sm.getString("jarScan.classloaderFail", url), ioe);
            }
        } else {
            // JAR / directory has been skipped
            if (log.isTraceEnabled()) {
                log.trace(sm.getString("jarScan.classloaderJarNoScan", url));
            }
        }
    }
}
 
Example #9
Source File: FatJarScanner.java    From oxygen with Apache License 2.0 5 votes vote down vote up
private void doScanWebInfLib(JarScanType scanType, ServletContext context,
    JarScannerCallback callback, Set<URL> processedURLs) {
  Set<String> dirList = context.getResourcePaths(TomcatConf.WEB_INF_LIB);
  if (dirList == null) {
    return;
  }
  for (String path : dirList) {
    if (path.endsWith(TomcatConf.JAR_EXT) && getJarScanFilter()
        .check(scanType, path.substring(path.lastIndexOf(Strings.SLASH) + 1))) {
      // Need to scan this JAR
      if (log.isDebugEnabled()) {
        log.debug(SM.getString("jarScan.webinflibJarScan", path));
      }
      URL url = null;
      try {
        url = context.getResource(path);
        processedURLs.add(url);
        process(scanType, callback, url, path, true, null);
      } catch (IOException e) {
        log.warn(SM.getString("jarScan.webinflibFail", url), e);
      }
    } else {
      if (log.isTraceEnabled()) {
        log.trace(SM.getString("jarScan.webinflibJarNoScan", path));
      }
    }
  }
}
 
Example #10
Source File: OWBJarScanner.java    From openwebbeans-meecrowave with Apache License 2.0 5 votes vote down vote up
@Override
public void scan(final JarScanType jarScanType, final ServletContext servletContext, final JarScannerCallback callback) {
    switch (jarScanType) {
        case PLUGGABILITY:
            CdiArchive.class.cast(WebScannerService.class.cast(WebBeansContext.getInstance().getScannerService()).getFinder().getArchive())
                    .classesByUrl().keySet().stream()
                    .filter(u -> !"jar:file://!/".equals(u)) // not a fake in memory url
                    .forEach(u -> {
                        try {
                            final URL url = new URL(u);
                            final File asFile = Files.toFile(url);
                            if (!asFile.exists()) {
                                return;
                            }
                            if (filter != null && !filter.check(jarScanType, asFile.getName())) {
                                return;
                            }

                            if (asFile.getName().endsWith(Constants.JAR_EXT)) {
                                try (final Jar jar = JarFactory.newInstance(asFile.toURI().toURL())) {
                                    callback.scan(jar, u, true);
                                }
                            } else if (asFile.isDirectory()) {
                                callback.scan(asFile, asFile.getAbsolutePath(), true);
                            }
                        } catch (final MalformedURLException e) {
                            // skip
                        } catch (final IOException ioe) {
                            throw new IllegalArgumentException(ioe);
                        }
                    });
            return;

        case TLD:
        default:
    }
}
 
Example #11
Source File: TomEEJarScanner.java    From tomee with Apache License 2.0 5 votes vote down vote up
private boolean embeddedSurefireScanning(final JarScanType scanType, final ServletContext context, final JarScannerCallback callback) {
    if (isScanClassPath() && System.getProperty("surefire.real.class.path") != null) {
        try {
            final Set<URL> urls = ClassLoaders.findUrls(context.getClassLoader().getParent());
            doScan(scanType, callback, new LinkedList<>(urls));
            return true;
        } catch (final IOException e) {
            // no-op
        }
    }
    return false;
}
 
Example #12
Source File: StandardJarScanner.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
protected void doScanClassPath(JarScanType scanType, ServletContext context,
        JarScannerCallback callback, Set<URL> processedURLs) {
    if (log.isTraceEnabled()) {
        log.trace(sm.getString("jarScan.classloaderStart"));
    }

    ClassLoader stopLoader = null;
    if (!isScanBootstrapClassPath()) {
        // Stop when we reach the bootstrap class loader
        stopLoader = ClassLoader.getSystemClassLoader().getParent();
    }

    ClassLoader classLoader = context.getClassLoader();

    // JARs are treated as application provided until the common class
    // loader is reached.
    boolean isWebapp = true;

    // Use a Deque so URLs can be removed as they are processed
    // and new URLs can be added as they are discovered during
    // processing.
    Deque<URL> classPathUrlsToProcess = new LinkedList<>();

    while (classLoader != null && classLoader != stopLoader) {
        if (classLoader instanceof URLClassLoader) {
            if (isWebapp) {
                isWebapp = isWebappClassLoader(classLoader);
            }

            classPathUrlsToProcess.addAll(
                    Arrays.asList(((URLClassLoader) classLoader).getURLs()));

            processURLs(scanType, callback, processedURLs, isWebapp, classPathUrlsToProcess);
        }
        classLoader = classLoader.getParent();
    }

    if (JreCompat.isJre9Available()) {
        // The application and platform class loaders are not
        // instances of URLClassLoader. Use the class path in this
        // case.
        addClassPath(classPathUrlsToProcess);
        // Also add any modules
        JreCompat.getInstance().addBootModulePath(classPathUrlsToProcess);
        processURLs(scanType, callback, processedURLs, false, classPathUrlsToProcess);
    }
}
 
Example #13
Source File: FatJarScanner.java    From oxygen with Apache License 2.0 4 votes vote down vote up
private void doScanClassPath(JarScanType scanType, ServletContext context,
    JarScannerCallback callback, Set<URL> processedURLs) {
  if (log.isTraceEnabled()) {
    log.trace(SM.getString("jarScan.classloaderStart"));
  }
  ClassLoader classLoader = context.getClassLoader();

  ClassLoader stopLoader = null;
  if (classLoader.getParent() != null) {
    // Stop when we reach the bootstrap class loader
    stopLoader = classLoader.getParent().getParent();
  }

  // JARs are treated as application provided until the common class
  // loader is reached.
  boolean isWebapp = true;

  // Use a Deque so URLs can be removed as they are processed
  // and new URLs can be added as they are discovered during
  // processing.
  Deque<URL> classPathUrlsToProcess = new LinkedList<>();

  while (classLoader != null && classLoader != stopLoader) {
    if (classLoader instanceof URLClassLoader) {
      if (isWebapp) {
        isWebapp = isWebappClassLoader(classLoader);
      }

      classPathUrlsToProcess.addAll(Arrays.asList(((URLClassLoader) classLoader).getURLs()));

      processURLs(scanType, callback, processedURLs, isWebapp, classPathUrlsToProcess);
    }
    classLoader = classLoader.getParent();
  }

  if (JreCompat.isJre9Available()) {
    // The application and platform class loaders are not
    // instances of URLClassLoader. Use the class path in this
    // case.
    addClassPath(classPathUrlsToProcess);
    // Also add any modules
    JreCompat.getInstance().addBootModulePath(classPathUrlsToProcess);
    processURLs(scanType, callback, processedURLs, false, classPathUrlsToProcess);
  }
}
 
Example #14
Source File: ServerTest.java    From raml-tester with Apache License 2.0 4 votes vote down vote up
@Override
public void scan(ServletContext context, ClassLoader classloader, JarScannerCallback callback, Set<String> jarsToSkip) {
}
 
Example #15
Source File: EmptyScanner.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public void scan(final JarScanType scanType, final ServletContext context,
                 final JarScannerCallback callback) {
    // no-op
}