Search in sources :

Example 1 with PatternFilter

use of org.apache.xbean.finder.filter.PatternFilter in project tomee by apache.

the class NewLoaderLogic method _loadFromClasspath.

@SuppressWarnings("UseOfSystemOutOrSystemErr")
public static void _loadFromClasspath(final FileUtils base, final List<URL> jarList, final ClassLoader classLoader) {
    final PerformanceTimer timer = new PerformanceTimer();
    timer.event("create filters");
    final Options options = SystemInstance.get().getOptions();
    final String include = "";
    final String exclude = "";
    final PatternFilter classpathInclude = new PatternFilter(options.get(DeploymentFilterable.CLASSPATH_INCLUDE, ".*"));
    final PatternFilter classpathExclude = new PatternFilter(options.get(DeploymentFilterable.CLASSPATH_EXCLUDE, ""));
    final Filter classpathFilter = new ExcludeIncludeFilter(classpathInclude, classpathExclude);
    final PatternFilter packageInclude = new PatternFilter(options.get(DeploymentFilterable.PACKAGE_INCLUDE, ".*"));
    final PatternFilter packageExclude = new PatternFilter(options.get(DeploymentFilterable.PACKAGE_EXCLUDE, ""));
    final IncludeExcludeFilter packageFilter;
    if (classpathInclude.getPattern().pattern().equals(".*") && packageInclude.getPattern().pattern().equals(".*")) {
        timer.event("callers");
        final Set<String> callers = callers();
        timer.event("parse packages");
        callers.size();
        final Set<String> packages = new HashSet<String>();
        for (final String caller : callers) {
            String[] parts = caller.split("\\.");
            if (parts.length > 2) {
                parts = new String[] { parts[0], parts[1] };
            }
            packages.add(Join.join(".", parts));
        }
        final Filter includes = Filters.packages(packages.toArray(new String[packages.size()]));
        packageFilter = new IncludeExcludeFilter(includes, packageExclude);
    } else {
        packageFilter = new IncludeExcludeFilter(packageInclude, packageExclude);
    }
    timer.event("urlset");
    final Set<RequireDescriptors> requireDescriptors = options.getAll(DeploymentFilterable.CLASSPATH_REQUIRE_DESCRIPTOR, RequireDescriptors.CLIENT);
    try {
        UrlSet urlSet = new UrlSet(classLoader);
        timer.event("exclude system urls");
        urlSet = URLs.cullSystemJars(urlSet);
        timer.event("classpath filter");
        final UrlSet beforeFiltering = urlSet;
        urlSet = urlSet.filter(classpathFilter);
        // If the user filtered out too much, that's a problem
        if (urlSet.size() == 0) {
            final String message = String.format("Classpath Include/Exclude resulted in zero URLs.  There were %s possible URLs before filtering and 0 after: include=\"%s\", exclude=\"%s\"", beforeFiltering.size(), include, exclude);
            logger.error(message);
            logger.info("Eligible Classpath before filtering:");
            for (final URL url : beforeFiltering) {
                logger.info(String.format("   %s", url.toExternalForm()));
            }
        }
        // If they are the same size, than nothing was filtered
        // and we know the user did not take action to change the default
        final boolean userSuppliedClasspathFilter = beforeFiltering.size() != urlSet.size();
        if (!userSuppliedClasspathFilter) {
            logger.info("Applying buildin classpath excludes");
            timer.event("buildin excludes");
            urlSet = applyBuiltinExcludes(urlSet);
        }
        DeploymentsResolver.processUrls("NewLoaderLogic1", urlSet.getUrls(), classLoader, EnumSet.allOf(RequireDescriptors.class), base, jarList);
        timer.event("package filter");
        urlSet = filterArchives(packageFilter, classLoader, urlSet);
        timer.event("process urls");
        final List<URL> urls = urlSet.getUrls();
        final long begin = System.currentTimeMillis();
        DeploymentsResolver.processUrls("NewLoaderLogic2", urls, classLoader, requireDescriptors, base, jarList);
        final long end = System.currentTimeMillis();
        final long time = end - begin;
        timer.stop(System.out);
        final UrlSet unchecked = new UrlSet();
        DeploymentsResolver.processUrls("NewLoaderLogic3", unchecked.getUrls(), classLoader, EnumSet.allOf(RequireDescriptors.class), base, jarList);
        if (logger.isDebugEnabled()) {
            final int urlCount = urlSet.getUrls().size() + unchecked.getUrls().size();
            logger.debug("URLs after filtering: " + urlCount);
            for (final URL url : urlSet.getUrls()) {
                logger.debug("Annotations path: " + url);
            }
            for (final URL url : unchecked.getUrls()) {
                logger.debug("Descriptors path: " + url);
            }
        }
        if (urls.size() == 0) {
            return;
        }
        if (time < 1000) {
            logger.debug("Searched " + urls.size() + " classpath urls in " + time + " milliseconds.  Average " + time / urls.size() + " milliseconds per url.");
        } else if (time < 4000 || urls.size() < 3) {
            logger.info("Searched " + urls.size() + " classpath urls in " + time + " milliseconds.  Average " + time / urls.size() + " milliseconds per url.");
        } else if (time < 10000) {
            logger.warning("Searched " + urls.size() + " classpath urls in " + time + " milliseconds.  Average " + time / urls.size() + " milliseconds per url.");
            logger.warning("Consider adjusting your " + DeploymentFilterable.CLASSPATH_EXCLUDE + " and " + DeploymentFilterable.CLASSPATH_INCLUDE + " settings.  Current settings: exclude='" + exclude + "', include='" + include + "'");
        } else {
            logger.fatal("Searched " + urls.size() + " classpath urls in " + time + " milliseconds.  Average " + time / urls.size() + " milliseconds per url.  TOO LONG!");
            logger.fatal("ADJUST THE EXCLUDE/INCLUDE!!!.  Current settings: " + DeploymentFilterable.CLASSPATH_EXCLUDE + "='" + exclude + "', " + DeploymentFilterable.CLASSPATH_INCLUDE + "='" + include + "'");
            final List<String> list = new ArrayList<String>();
            for (final URL url : urls) {
                list.add(url.toExternalForm());
            }
            Collections.sort(list);
            for (final String url : list) {
                logger.info("Matched: " + url);
            }
        }
    } catch (final IOException e1) {
        e1.printStackTrace();
        logger.warning("Unable to search classpath for modules: Received Exception: " + e1.getClass().getName() + " " + e1.getMessage(), e1);
    }
}
Also used : Options(org.apache.openejb.loader.Options) PatternFilter(org.apache.xbean.finder.filter.PatternFilter) ArrayList(java.util.ArrayList) ExcludeIncludeFilter(org.apache.xbean.finder.filter.ExcludeIncludeFilter) IOException(java.io.IOException) URL(java.net.URL) PerformanceTimer(org.apache.openejb.util.PerformanceTimer) IncludeExcludeFilter(org.apache.xbean.finder.filter.IncludeExcludeFilter) PatternFilter(org.apache.xbean.finder.filter.PatternFilter) Filter(org.apache.xbean.finder.filter.Filter) ExcludeIncludeFilter(org.apache.xbean.finder.filter.ExcludeIncludeFilter) UrlSet(org.apache.xbean.finder.UrlSet) IncludeExcludeFilter(org.apache.xbean.finder.filter.IncludeExcludeFilter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Options (org.apache.openejb.loader.Options)1 PerformanceTimer (org.apache.openejb.util.PerformanceTimer)1 UrlSet (org.apache.xbean.finder.UrlSet)1 ExcludeIncludeFilter (org.apache.xbean.finder.filter.ExcludeIncludeFilter)1 Filter (org.apache.xbean.finder.filter.Filter)1 IncludeExcludeFilter (org.apache.xbean.finder.filter.IncludeExcludeFilter)1 PatternFilter (org.apache.xbean.finder.filter.PatternFilter)1