Search in sources :

Example 1 with CompositeArchive

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

the class OpenEJBArchiveProcessor method createModule.

public static AppModule createModule(final Archive<?> archive, final TestClass testClass, final Closeables closeables) {
    final Class<?> javaClass;
    if (testClass != null) {
        javaClass = testClass.getJavaClass();
    } else {
        javaClass = null;
    }
    final ClassLoader parent;
    if (javaClass == null) {
        parent = Thread.currentThread().getContextClassLoader();
    } else {
        parent = javaClass.getClassLoader();
    }
    final List<URL> additionalPaths = new ArrayList<>();
    CompositeArchive scannedArchive = null;
    final Map<URL, List<String>> earMap = new HashMap<>();
    final Map<String, Object> altDD = new HashMap<>();
    final List<Archive> earLibsArchives = new ArrayList<>();
    final CompositeBeans earBeans = new CompositeBeans();
    final boolean isEar = EnterpriseArchive.class.isInstance(archive);
    final boolean isWebApp = WebArchive.class.isInstance(archive);
    final String prefix = isWebApp ? WEB_INF : META_INF;
    if (isEar || isWebApp) {
        final Map<ArchivePath, Node> jars = archive.getContent(new IncludeRegExpPaths(isEar ? "/.*\\.jar" : "/WEB-INF/lib/.*\\.jar"));
        scannedArchive = analyzeLibs(parent, additionalPaths, earMap, earLibsArchives, earBeans, jars, altDD);
    }
    final URL[] urls = additionalPaths.toArray(new URL[additionalPaths.size()]);
    final URLClassLoaderFirst swParent = new URLClassLoaderFirst(urls, parent);
    closeables.add(swParent);
    if (!isEar) {
        earLibsArchives.add(archive);
    }
    final SWClassLoader loader = new SWClassLoader(swParent, earLibsArchives.toArray(new Archive<?>[earLibsArchives.size()]));
    closeables.add(loader);
    final URLClassLoader tempClassLoader = ClassLoaderUtil.createTempClassLoader(loader);
    closeables.add(tempClassLoader);
    final AppModule appModule = new AppModule(loader, archive.getName());
    if (WEB_INF.equals(prefix)) {
        appModule.setDelegateFirst(false);
        appModule.setStandloneWebModule();
        final WebModule webModule = new WebModule(createWebApp(archive), contextRoot(archive.getName()), loader, "", appModule.getModuleId());
        webModule.setUrls(additionalPaths);
        appModule.getWebModules().add(webModule);
    } else if (isEar) {
        // mainly for CDI TCKs
        final FinderFactory.OpenEJBAnnotationFinder earLibFinder = new FinderFactory.OpenEJBAnnotationFinder(new SimpleWebappAggregatedArchive(tempClassLoader, scannedArchive, earMap));
        appModule.setEarLibFinder(earLibFinder);
        final EjbModule earCdiModule = new EjbModule(appModule.getClassLoader(), DeploymentLoader.EAR_SCOPED_CDI_BEANS + appModule.getModuleId(), new EjbJar(), new OpenejbJar());
        earCdiModule.setBeans(earBeans);
        earCdiModule.setFinder(earLibFinder);
        final EjbJar ejbJar;
        final AssetSource ejbJarXml = AssetSource.class.isInstance(altDD.get(EJB_JAR_XML)) ? AssetSource.class.cast(altDD.get(EJB_JAR_XML)) : null;
        if (ejbJarXml != null) {
            try {
                ejbJar = ReadDescriptors.readEjbJar(ejbJarXml.get());
            } catch (final Exception e) {
                throw new OpenEJBRuntimeException(e);
            }
        } else {
            ejbJar = new EjbJar();
        }
        // EmptyEjbJar would prevent to add scanned EJBs but this is *here* an aggregator so we need to be able to do so
        earCdiModule.setEjbJar(ejbJar);
        appModule.getEjbModules().add(earCdiModule);
        for (final Map.Entry<ArchivePath, Node> node : archive.getContent(new IncludeRegExpPaths("/.*\\.war")).entrySet()) {
            final Asset asset = node.getValue().getAsset();
            if (ArchiveAsset.class.isInstance(asset)) {
                final Archive<?> webArchive = ArchiveAsset.class.cast(asset).getArchive();
                if (WebArchive.class.isInstance(webArchive)) {
                    final Map<String, Object> webAltDD = new HashMap<>();
                    final Node beansXml = findBeansXml(webArchive, WEB_INF);
                    final List<URL> webappAdditionalPaths = new LinkedList<>();
                    final CompositeBeans webAppBeansXml = new CompositeBeans();
                    final List<Archive> webAppArchives = new LinkedList<Archive>();
                    final Map<URL, List<String>> webAppClassesByUrl = new HashMap<URL, List<String>>();
                    final CompositeArchive webAppArchive = analyzeLibs(parent, webappAdditionalPaths, webAppClassesByUrl, webAppArchives, webAppBeansXml, webArchive.getContent(new IncludeRegExpPaths("/WEB-INF/lib/.*\\.jar")), webAltDD);
                    webAppArchives.add(webArchive);
                    final SWClassLoader webLoader = new SWClassLoader(parent, webAppArchives.toArray(new Archive<?>[webAppArchives.size()]));
                    closeables.add(webLoader);
                    final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(finderArchive(beansXml, webArchive, webLoader, webAppArchive, webAppClassesByUrl, webAppBeansXml));
                    final String contextRoot = contextRoot(webArchive.getName());
                    final WebModule webModule = new WebModule(createWebApp(webArchive), contextRoot, webLoader, "", appModule.getModuleId() + "_" + contextRoot);
                    webModule.setUrls(Collections.<URL>emptyList());
                    webModule.setScannableUrls(Collections.<URL>emptyList());
                    webModule.setFinder(finder);
                    final EjbJar webEjbJar = createEjbJar(prefix, webArchive);
                    final EjbModule ejbModule = new EjbModule(webLoader, webModule.getModuleId(), null, webEjbJar, new OpenejbJar());
                    ejbModule.setBeans(webAppBeansXml);
                    ejbModule.getAltDDs().putAll(webAltDD);
                    ejbModule.getAltDDs().put("beans.xml", webAppBeansXml);
                    ejbModule.setFinder(finder);
                    ejbModule.setClassLoader(webLoader);
                    ejbModule.setWebapp(true);
                    appModule.getEjbModules().add(ejbModule);
                    appModule.getWebModules().add(webModule);
                    addPersistenceXml(archive, WEB_INF, appModule);
                    addOpenEJbJarXml(archive, WEB_INF, ejbModule);
                    addValidationXml(archive, WEB_INF, new HashMap<String, Object>(), ejbModule);
                    addResourcesXml(archive, WEB_INF, ejbModule);
                    addEnvEntries(archive, WEB_INF, appModule, ejbModule);
                }
            }
        }
    }
    if (isEar) {
        // adding the test class as lib class can break test if tested against the web part of the ear
        addTestClassAsManagedBean(javaClass, tempClassLoader, appModule);
        return appModule;
    }
    // add the test as a managed bean to be able to inject into it easily
    final Map<String, Object> testDD;
    if (javaClass != null) {
        final EjbModule testEjbModule = addTestClassAsManagedBean(javaClass, tempClassLoader, appModule);
        testDD = testEjbModule.getAltDDs();
    } else {
        // ignore
        testDD = new HashMap<>();
    }
    final EjbJar ejbJar = createEjbJar(prefix, archive);
    if (ejbJar.getModuleName() == null) {
        final String name = archive.getName();
        if (name.endsWith("ar") && name.length() > 4) {
            ejbJar.setModuleName(name.substring(0, name.length() - ".jar".length()));
        } else {
            ejbJar.setModuleName(name);
        }
    }
    final EjbModule ejbModule = new EjbModule(ejbJar);
    ejbModule.setClassLoader(tempClassLoader);
    final Node beansXml = findBeansXml(archive, prefix);
    final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(finderArchive(beansXml, archive, loader, scannedArchive, earMap, earBeans));
    ejbModule.setFinder(finder);
    ejbModule.setBeans(earBeans);
    ejbModule.getAltDDs().put("beans.xml", earBeans);
    if (appModule.isWebapp()) {
        // war
        appModule.getWebModules().iterator().next().setFinder(ejbModule.getFinder());
    }
    appModule.getEjbModules().add(ejbModule);
    addPersistenceXml(archive, prefix, appModule);
    addOpenEJbJarXml(archive, prefix, ejbModule);
    addValidationXml(archive, prefix, testDD, ejbModule);
    addResourcesXml(archive, prefix, ejbModule);
    addEnvEntries(archive, prefix, appModule, ejbModule);
    if (!appModule.isWebapp()) {
        appModule.getAdditionalLibraries().addAll(additionalPaths);
    }
    if (archive.getName().endsWith(".jar")) {
        // otherwise global naming will be broken
        appModule.setStandloneWebModule();
    }
    return appModule;
}
Also used : URLClassLoaderFirst(org.apache.openejb.util.classloader.URLClassLoaderFirst) 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) AppModule(org.apache.openejb.config.AppModule) HashMap(java.util.HashMap) Node(org.jboss.shrinkwrap.api.Node) ArrayList(java.util.ArrayList) EjbModule(org.apache.openejb.config.EjbModule) URL(java.net.URL) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) URLClassLoader(java.net.URLClassLoader) 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) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IncludeRegExpPaths(org.jboss.shrinkwrap.impl.base.filter.IncludeRegExpPaths) EjbJar(org.apache.openejb.jee.EjbJar) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) ArchiveAsset(org.jboss.shrinkwrap.api.asset.ArchiveAsset) WebModule(org.apache.openejb.config.WebModule) FinderFactory(org.apache.openejb.config.FinderFactory) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) URLClassLoader(java.net.URLClassLoader) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with CompositeArchive

use of org.apache.xbean.finder.archive.CompositeArchive 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 3 with CompositeArchive

use of org.apache.xbean.finder.archive.CompositeArchive 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 4 with CompositeArchive

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

the class ClassScannerFactory method forPackages.

private static ClassScanner forPackages(ClassLoader classloader, String... packages) {
    final long started = System.currentTimeMillis();
    CompositeArchive archive = getArchivesForPackage(classloader, packages);
    AnnotationFinder finder = new AnnotationFinder(archive, true);
    final long finished = System.currentTimeMillis();
    return new ClassScanner(finder, finished - started);
}
Also used : CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder)

Example 5 with CompositeArchive

use of org.apache.xbean.finder.archive.CompositeArchive 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)

Aggregations

CompositeArchive (org.apache.xbean.finder.archive.CompositeArchive)9 FilteredArchive (org.apache.xbean.finder.archive.FilteredArchive)8 ClassesArchive (org.apache.xbean.finder.archive.ClassesArchive)7 URL (java.net.URL)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Archive (org.apache.xbean.finder.archive.Archive)5 JarArchive (org.apache.xbean.finder.archive.JarArchive)5 MalformedURLException (java.net.MalformedURLException)4 URLClassLoader (java.net.URLClassLoader)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 WebappAggregatedArchive (org.apache.openejb.config.WebappAggregatedArchive)3 Archive (org.jboss.shrinkwrap.api.Archive)3 ArchivePath (org.jboss.shrinkwrap.api.ArchivePath)3 Node (org.jboss.shrinkwrap.api.Node)3 EnterpriseArchive (org.jboss.shrinkwrap.api.spec.EnterpriseArchive)3 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)3 IncludeRegExpPaths (org.jboss.shrinkwrap.impl.base.filter.IncludeRegExpPaths)3