Search in sources :

Example 1 with FilteredArchive

use of org.apache.xbean.finder.archive.FilteredArchive in project tomee by apache.

the class ApplicationComposerDeployer method configureClasses.

private void configureClasses(final WebModule web, final EjbModule ejbModule, final ApplicationComposer applicationComposer, final Classes classes) {
    ejbModule.getEjbJar().setMetadataComplete(applicationComposer.metadataComplete());
    final Collection<Archive> archives = new LinkedList<>();
    if (classes.value().length > 0) {
        archives.add(new ClassesArchive(classes.value()));
    }
    if (classes.cdi()) {
        final Beans beans = new Beans();
        for (final Class<?> c : classes.cdiAlternatives()) {
            beans.addAlternativeClass(c);
        }
        for (final Class<?> c : classes.cdiDecorators()) {
            beans.addDecorator(c);
        }
        for (final Class<?> c : classes.cdiInterceptors()) {
            beans.addInterceptor(c);
        }
        ejbModule.setBeans(beans);
        if (applicationComposer.metadataComplete()) {
            for (final Class<?> c : classes.value()) {
                beans.addManagedClass(null, c.getName());
            }
            final String name = BeanContext.Comp.openejbCompName(web.getModuleId());
            final org.apache.openejb.jee.ManagedBean managedBean = new CompManagedBean(name, BeanContext.Comp.class);
            managedBean.setTransactionType(TransactionType.BEAN);
            ejbModule.getEjbJar().addEnterpriseBean(managedBean);
        }
    }
    final CompositeArchive archive = new CompositeArchive(archives);
    final Archive finalArchive = classes.excludes().length > 0 ? new FilteredArchive(archive, Filters.invert(Filters.prefixes(classes.excludes()))) : archive;
    ejbModule.setFinder(new FinderFactory.OpenEJBAnnotationFinder(finalArchive).link());
    web.setFinder(ejbModule.getFinder());
    web.getWebApp().setMetadataComplete(ejbModule.getEjbJar().isMetadataComplete());
}
Also used : FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) Archive(org.apache.xbean.finder.archive.Archive) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) LinkedList(java.util.LinkedList) BeanContext(org.apache.openejb.BeanContext) Beans(org.apache.openejb.jee.Beans) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive)

Example 2 with FilteredArchive

use of org.apache.xbean.finder.archive.FilteredArchive in project stdlib by petergeneric.

the class ClassScannerFactory method getArchivesForPackage.

private static CompositeArchive getArchivesForPackage(final ClassLoader classloader, final String... packages) {
    try {
        final List<Archive> archives = new ArrayList<>();
        for (String pkg : packages) {
            if (!pkg.endsWith("."))
                // Add a trailing dot for easier package matching
                pkg += ".";
            final String baseFolder = pkg.replace('.', '/');
            final Enumeration<URL> urls = classloader.getResources(baseFolder);
            while (urls.hasMoreElements()) {
                final URL url = urls.nextElement();
                if (log.isTraceEnabled())
                    log.trace("Found source: " + url);
                if (url.getProtocol() != null && (url.getProtocol().equals("zip") || url.getProtocol().equals("jar")))
                    archives.add(new FilteredArchive(new JarArchive(classloader, url), new PrefixFilter(pkg)));
                else
                    archives.add(new FileArchive(classloader, url, pkg));
            }
        }
        return new CompositeArchive(archives);
    } catch (IOException e) {
        throw new IllegalArgumentException("Error loading archives for packages: " + Arrays.asList(packages), e);
    }
}
Also used : JarArchive(org.apache.xbean.finder.archive.JarArchive) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) FileArchive(org.apache.xbean.finder.archive.FileArchive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) Archive(org.apache.xbean.finder.archive.Archive) PrefixFilter(org.apache.xbean.finder.filter.PrefixFilter) ArrayList(java.util.ArrayList) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) IOException(java.io.IOException) URL(java.net.URL) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) JarArchive(org.apache.xbean.finder.archive.JarArchive) FileArchive(org.apache.xbean.finder.archive.FileArchive)

Example 3 with FilteredArchive

use of org.apache.xbean.finder.archive.FilteredArchive in project tomee by apache.

the class ApplicationComposers method finderFromClasses.

private static IAnnotationFinder finderFromClasses(final DeploymentModule module, final Class<?>[] value, final Collection<File> others, final String[] excludes) {
    final Collection<Archive> archives = new ArrayList<>(1 + (others == null ? 0 : others.size()));
    final Filter filter = excludes == null || excludes.length == 0 ? null : Filters.invert(Filters.prefixes(excludes));
    final Collection<Class<?>> classes = new ArrayList<>(asList(FinderFactory.ensureMinimalClasses(module)));
    if (value != null) {
        classes.addAll(asList(value));
    }
    final ClassesArchive classesArchive = new ClassesArchive(classes);
    archives.add(filter == null ? classesArchive : new FilteredArchive(classesArchive, filter));
    if (others != null) {
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        for (final File f : others) {
            try {
                final Archive archive = f.isDirectory() ? new FileArchive(classLoader, f) : new JarArchive(classLoader, f.toURI().toURL());
                archives.add(filter == null ? archive : new FilteredArchive(archive, filter));
            } catch (final MalformedURLException e) {
                throw new IllegalArgumentException(e);
            }
        }
    }
    return new FinderFactory.OpenEJBAnnotationFinder(new CompositeArchive(archives)).link();
}
Also used : MalformedURLException(java.net.MalformedURLException) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) FileArchive(org.apache.xbean.finder.archive.FileArchive) JarArchive(org.apache.xbean.finder.archive.JarArchive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) Archive(org.apache.xbean.finder.archive.Archive) ArrayList(java.util.ArrayList) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) FinderFactory(org.apache.openejb.config.FinderFactory) Filter(org.apache.xbean.finder.filter.Filter) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) JarArchive(org.apache.xbean.finder.archive.JarArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) FileArchive(org.apache.xbean.finder.archive.FileArchive) File(java.io.File)

Example 4 with FilteredArchive

use of org.apache.xbean.finder.archive.FilteredArchive in project tomee by apache.

the class WebappAggregatedArchive method doScan.

private List<Archive> doScan(final ClassLoader loader, final Iterable<URL> urls, final Filter filter) {
    final List<Archive> archives = new ArrayList<>();
    for (final URL url : urls) {
        final List<String> classes = new ArrayList<>();
        final Archive archive = new FilteredArchive(new ConfigurableClasspathArchive(loader, singletonList(url)), new ScanXmlSaverFilter(scanXmlExists, handler, classes, filter));
        map.put(url, classes);
        archives.add(archive);
    }
    return archives;
}
Also used : ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) Archive(org.apache.xbean.finder.archive.Archive) ArrayList(java.util.ArrayList) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) URL(java.net.URL)

Example 5 with FilteredArchive

use of org.apache.xbean.finder.archive.FilteredArchive in project tomee by apache.

the class OpenEJBArchiveProcessor method analyzeLibs.

private static CompositeArchive analyzeLibs(final ClassLoader parent, final List<URL> additionalPaths, final Map<URL, List<String>> earMap, final List<Archive> earLibsArchives, final CompositeBeans earBeans, final Map<ArchivePath, Node> jars, final Map<String, Object> altDD) {
    final List<org.apache.xbean.finder.archive.Archive> archives = new ArrayList<>(jars.size());
    for (final Map.Entry<ArchivePath, Node> node : jars.entrySet()) {
        final Asset asset = node.getValue().getAsset();
        if (ArchiveAsset.class.isInstance(asset)) {
            final Archive<?> libArchive = ArchiveAsset.class.cast(asset).getArchive();
            if (!isExcluded(libArchive.getName())) {
                earLibsArchives.add(libArchive);
                final List<Class<?>> earClasses = new ArrayList<>();
                final List<String> earClassNames = new ArrayList<>();
                final Map<ArchivePath, Node> content = libArchive.getContent(new IncludeRegExpPaths(".*.class"));
                for (final Map.Entry<ArchivePath, Node> classNode : content.entrySet()) {
                    final String classname = name(classNode.getKey().get());
                    try {
                        earClasses.add(parent.loadClass(classname));
                        earClassNames.add(classname);
                    } catch (final ClassNotFoundException e) {
                        LOGGER.fine("Can't load class " + classname);
                    } catch (final NoClassDefFoundError ncdfe) {
                    // no-op
                    }
                }
                try {
                    // ends with !/META-INF/beans.xml to force it to be used as a cdi module *with bda*
                    final Node beansNode = libArchive.get(META_INF + BEANS_XML);
                    final URL arUrl = new URL("jar:file://!/lib/" + libArchive.getName() + (beansNode != null ? "!/META-INF/beans.xml" : ""));
                    if (beansNode != null) {
                        try {
                            DeploymentLoader.doMerge(arUrl, earBeans, ReadDescriptors.readBeans(beansNode.getAsset().openStream()));
                        } catch (final OpenEJBException e) {
                            throw new IllegalArgumentException(e);
                        }
                    }
                    earMap.put(arUrl, earClassNames);
                } catch (final MalformedURLException e) {
                // no-op
                }
                archives.add(new ClassesArchive(earClasses));
            }
            final Node ejbJarXml = libArchive.get(META_INF + EJB_JAR_XML);
            if (ejbJarXml != null) {
                // not super, we should merge them surely but ok for use cases we met until today
                altDD.put("ejb-jar.xml", new AssetSource(ejbJarXml.getAsset(), null));
            }
        }
        if (UrlAsset.class.isInstance(asset) || FileAsset.class.isInstance(asset)) {
            try {
                final URL url = UrlAsset.class.isInstance(asset) ? get(URL.class, "url", asset) : get(File.class, "file", asset).toURI().toURL();
                final List<String> classes = new ArrayList<String>();
                archives.add(new FilteredArchive(new JarArchive(parent, url), new WebappAggregatedArchive.ScanXmlSaverFilter(false, null, classes, null)));
                additionalPaths.add(url);
                final URLClassLoader loader = new URLClassLoader(new URL[] { url }, EMPTY_LOADER);
                for (final String beans : asList("META-INF/beans.xml", "/META-INF/beans.xml")) {
                    final URL u = loader.getResource(beans);
                    if (u != null) {
                        try {
                            DeploymentLoader.doMerge(u, earBeans, ReadDescriptors.readBeans(u.openStream()));
                        } catch (final OpenEJBException e) {
                            throw new IllegalArgumentException(e);
                        } catch (final IOException e) {
                        // no-op
                        }
                        earMap.put(u, classes);
                    }
                }
                try {
                    loader.close();
                } catch (final IOException e) {
                // no-op
                }
            } catch (final MalformedURLException e) {
                throw new IllegalArgumentException(e);
            }
        }
    }
    return new CompositeArchive(archives);
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) EnterpriseArchive(org.jboss.shrinkwrap.api.spec.EnterpriseArchive) WebappAggregatedArchive(org.apache.openejb.config.WebappAggregatedArchive) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) JarArchive(org.apache.xbean.finder.archive.JarArchive) Archive(org.jboss.shrinkwrap.api.Archive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) Node(org.jboss.shrinkwrap.api.Node) ArrayList(java.util.ArrayList) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) URL(java.net.URL) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) JarArchive(org.apache.xbean.finder.archive.JarArchive) Asset(org.jboss.shrinkwrap.api.asset.Asset) FileAsset(org.jboss.shrinkwrap.api.asset.FileAsset) ClassLoaderAsset(org.jboss.shrinkwrap.api.asset.ClassLoaderAsset) ArchiveAsset(org.jboss.shrinkwrap.api.asset.ArchiveAsset) UrlAsset(org.jboss.shrinkwrap.api.asset.UrlAsset) IncludeRegExpPaths(org.jboss.shrinkwrap.impl.base.filter.IncludeRegExpPaths) FileAsset(org.jboss.shrinkwrap.api.asset.FileAsset) ArchiveAsset(org.jboss.shrinkwrap.api.asset.ArchiveAsset) IOException(java.io.IOException) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) URLClassLoader(java.net.URLClassLoader) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) TestClass(org.jboss.arquillian.test.spi.TestClass) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) UrlAsset(org.jboss.shrinkwrap.api.asset.UrlAsset)

Aggregations

CompositeArchive (org.apache.xbean.finder.archive.CompositeArchive)5 FilteredArchive (org.apache.xbean.finder.archive.FilteredArchive)5 ArrayList (java.util.ArrayList)4 Archive (org.apache.xbean.finder.archive.Archive)4 ClassesArchive (org.apache.xbean.finder.archive.ClassesArchive)4 URL (java.net.URL)3 JarArchive (org.apache.xbean.finder.archive.JarArchive)3 File (java.io.File)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 FileArchive (org.apache.xbean.finder.archive.FileArchive)2 URLClassLoader (java.net.URLClassLoader)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 BeanContext (org.apache.openejb.BeanContext)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 FinderFactory (org.apache.openejb.config.FinderFactory)1 WebappAggregatedArchive (org.apache.openejb.config.WebappAggregatedArchive)1 Beans (org.apache.openejb.jee.Beans)1