Search in sources :

Example 1 with CompositeBeans

use of org.apache.openejb.cdi.CompositeBeans 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 CompositeBeans

use of org.apache.openejb.cdi.CompositeBeans in project tomee by apache.

the class DeploymentLoader method addBeansXmls.

private void addBeansXmls(final AppModule appModule) {
    final List<URL> urls = appModule.getAdditionalLibraries();
    final URLClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
    final ArrayList<URL> xmls;
    try {
        xmls = Collections.list(loader.getResources("META-INF/beans.xml"));
    } catch (final IOException e) {
        return;
    }
    final CompositeBeans complete = new CompositeBeans();
    for (final URL url : xmls) {
        if (url == null) {
            continue;
        }
        mergeBeansXml(complete, url);
    }
    if (complete.getDiscoveryByUrl().isEmpty()) {
        return;
    }
    complete.removeDuplicates();
    ensureContainerUrls();
    final List<URL> scannableUrls = new ArrayList<>(this.containerUrls);
    SystemInstance.get().fireEvent(new EnhanceScannableUrlsEvent(scannableUrls));
    appModule.getScannableContainerUrls().addAll(scannableUrls);
    IAnnotationFinder finder;
    try {
        finder = FinderFactory.createFinder(appModule);
    } catch (final Exception e) {
        finder = new FinderFactory.ModuleLimitedFinder(new FinderFactory.OpenEJBAnnotationFinder(new WebappAggregatedArchive(appModule.getClassLoader(), appModule.getAltDDs(), xmls)));
    }
    appModule.setEarLibFinder(finder);
    final EjbModule ejbModule = new EjbModule(appModule.getClassLoader(), EAR_SCOPED_CDI_BEANS + appModule.getModuleId(), new EjbJar(), new OpenejbJar());
    ejbModule.setBeans(complete);
    ejbModule.setFinder(finder);
    ejbModule.setEjbJar(new EmptyEjbJar());
    appModule.getEjbModules().add(ejbModule);
}
Also used : CompositeBeans(org.apache.openejb.cdi.CompositeBeans) EnhanceScannableUrlsEvent(org.apache.openejb.config.event.EnhanceScannableUrlsEvent) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) OpenEJBException(org.apache.openejb.OpenEJBException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) URLClassLoader(java.net.URLClassLoader) EjbJar(org.apache.openejb.jee.EjbJar)

Example 3 with CompositeBeans

use of org.apache.openejb.cdi.CompositeBeans in project tomee by apache.

the class ScanJarService method doMerge.

private void doMerge(final EjbModule ejbModule) throws Exception {
    final Beans beans = ejbModule.getBeans();
    if (CompositeBeans.class.isInstance(beans)) {
        final CompositeBeans cb = CompositeBeans.class.cast(beans);
        ensureInit();
        merge(cb);
    } else if (beans != null) {
        ensureInit();
        for (final URL key : this.beans.getManagedClasses().keySet()) {
            beans.getManagedClasses().putAll(this.beans.getManagedClasses());
            addIfNotNull(beans.getInterceptors(), this.beans.getInterceptorsByUrl().get(key));
            addIfNotNull(beans.getAlternativeClasses(), this.beans.getAlternativesByUrl().get(key));
            addIfNotNull(beans.getAlternativeStereotypes(), this.beans.getAlternativeStereotypesByUrl().get(key));
            addIfNotNull(beans.getDecorators(), this.beans.getDecoratorsByUrl().get(key));
        }
    }
}
Also used : CompositeBeans(org.apache.openejb.cdi.CompositeBeans) Beans(org.apache.openejb.jee.Beans) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) URL(java.net.URL)

Example 4 with CompositeBeans

use of org.apache.openejb.cdi.CompositeBeans in project tomee by apache.

the class AddContainerCdiBeansExtension method addCdiExtLib.

public void addCdiExtLib(@Observes final BeforeAppInfoBuilderEvent event) {
    for (final EjbModule ejbModule : event.getAppModule().getEjbModules()) {
        if (ejbModule.getModuleId().startsWith("ear-scoped-cdi-beans")) {
            final Beans beans = ejbModule.getBeans();
            if (CompositeBeans.class.isInstance(beans)) {
                final CompositeBeans cb = CompositeBeans.class.cast(beans);
                cb.getManagedClasses().put(EXT_LIB, new ArrayList<>(BEANS));
            }
            return;
        }
    }
    // else a war
    for (final WebModule webModule : event.getAppModule().getWebModules()) {
        for (final EjbModule ejbModule : event.getAppModule().getEjbModules()) {
            if (ejbModule.getModuleId().equals(webModule.getModuleId())) {
                final Beans beans = ejbModule.getBeans();
                if (CompositeBeans.class.isInstance(beans)) {
                    final CompositeBeans cb = CompositeBeans.class.cast(beans);
                    cb.getManagedClasses().put(EXT_LIB, new ArrayList<>(BEANS));
                }
                return;
            }
        }
    }
}
Also used : CompositeBeans(org.apache.openejb.cdi.CompositeBeans) Beans(org.apache.openejb.jee.Beans) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) EjbModule(org.apache.openejb.config.EjbModule) WebModule(org.apache.openejb.config.WebModule)

Example 5 with CompositeBeans

use of org.apache.openejb.cdi.CompositeBeans in project tomee by apache.

the class ReadDescriptors method readBeans.

private void readBeans(final EjbModule ejbModule) throws OpenEJBException {
    if (ejbModule.getBeans() != null) {
        return;
    }
    final Object raw = ejbModule.getAltDDs().get("beans.xml");
    final Source data = getSource(raw);
    if (data != null) {
        try {
            final Beans beans = readBeans(data.get());
            checkDuplicatedByBeansXml(beans, beans);
            if (UrlSource.class.isInstance(data)) {
                beans.setUri(UrlSource.class.cast(data).getUrl().toExternalForm());
            } else {
                beans.setUri("jar:file://" + ejbModule.getModuleId() + "!/META-INF/beans.xml");
            }
            ejbModule.setBeans(beans);
        } catch (final IOException e) {
            throw new OpenEJBException(e);
        }
    } else if (raw instanceof Beans) {
        ejbModule.setBeans((Beans) raw);
    } else if (List.class.isInstance(raw)) {
        final CompositeBeans compositeBeans = new CompositeBeans();
        final List list = List.class.cast(raw);
        if (!list.isEmpty()) {
            for (final Object o : list) {
                try {
                    final UrlSource urlSource = UrlSource.class.cast(o);
                    mergeBeansXml(compositeBeans, readBeans(urlSource.get()), urlSource.getUrl());
                } catch (final IOException e) {
                    throw new OpenEJBException(e);
                }
            }
            ejbModule.setBeans(compositeBeans);
        }
    }
}
Also used : CompositeBeans(org.apache.openejb.cdi.CompositeBeans) OpenEJBException(org.apache.openejb.OpenEJBException) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) Beans(org.apache.openejb.jee.Beans) List(java.util.List) IOException(java.io.IOException) InputSource(org.xml.sax.InputSource)

Aggregations

CompositeBeans (org.apache.openejb.cdi.CompositeBeans)9 URL (java.net.URL)6 Beans (org.apache.openejb.jee.Beans)6 IOException (java.io.IOException)5 OpenEJBException (org.apache.openejb.OpenEJBException)5 URLClassLoader (java.net.URLClassLoader)4 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 EjbModule (org.apache.openejb.config.EjbModule)2 FinderFactory (org.apache.openejb.config.FinderFactory)2 WebModule (org.apache.openejb.config.WebModule)2 EjbJar (org.apache.openejb.jee.EjbJar)2 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)2 JarArchive (org.apache.xbean.finder.archive.JarArchive)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1