Search in sources :

Example 6 with CompositeBeans

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

the class ScanJarService method ensureInit.

private void ensureInit() throws Exception {
    if (beans != null) {
        return;
    }
    synchronized (this) {
        if (beans != null) {
            return;
        }
        final ClassLoader loader = ParentClassLoaderFinder.Helper.get();
        final CompositeBeans mergedModel = new CompositeBeans();
        for (final File file : findFiles()) {
            final URL url = file.toURI().toURL();
            if (file.isDirectory()) {
                final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(new FileArchive(loader, file));
                mergedModel.getManagedClasses().put(url, finder.getAnnotatedClassNames());
                final File beansXml = new File(file, "META-INF/beans.xml");
                if (beansXml.exists()) {
                    try (final FileInputStream inputStream = new FileInputStream(beansXml)) {
                        final Beans beansModel = ReadDescriptors.readBeans(inputStream);
                        mergedModel.mergeClasses(url, beansModel);
                    }
                }
            } else {
                final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(new JarArchive(loader, url));
                mergedModel.getManagedClasses().put(url, finder.getAnnotatedClassNames());
                try (final URLClassLoader cl = new URLClassLoader(new URL[] { url })) {
                    try (final InputStream is = cl.getResourceAsStream("META-INF/beans.xml")) {
                        if (is != null) {
                            final Beans beansModel = ReadDescriptors.readBeans(is);
                            mergedModel.mergeClasses(url, beansModel);
                        }
                    }
                }
            }
        }
        beans = mergedModel;
    }
}
Also used : CompositeBeans(org.apache.openejb.cdi.CompositeBeans) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FinderFactory(org.apache.openejb.config.FinderFactory) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Beans(org.apache.openejb.jee.Beans) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) JarArchive(org.apache.xbean.finder.archive.JarArchive) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) FileArchive(org.apache.xbean.finder.archive.FileArchive) File(java.io.File)

Example 7 with CompositeBeans

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

the class DeploymentLoader method mergeBeansXml.

private static Beans mergeBeansXml(final CompositeBeans current, final URL url) {
    try {
        final Beans beans;
        try {
            beans = ReadDescriptors.readBeans(url.openStream());
        } catch (final IOException e) {
            return current;
        }
        doMerge(url, current, beans);
    } catch (final OpenEJBException e) {
        LOGGER.error("Unable to read beans.xml from: " + url.toExternalForm(), e);
    }
    return current;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) Beans(org.apache.openejb.jee.Beans) IOException(java.io.IOException)

Example 8 with CompositeBeans

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

the class DeploymentLoader method addBeansXmls.

public static void addBeansXmls(final WebModule webModule) {
    final List<URL> urls = webModule.getScannableUrls();
    // parent returns nothing when calling getresources because we don't want here to be fooled by maven classloader
    final URLClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]), new EmptyResourcesClassLoader());
    final List<URL> xmls = new LinkedList<>();
    try {
        final URL e = (URL) webModule.getAltDDs().get("beans.xml");
        if (e != null) {
            // first!
            xmls.add(e);
        }
        xmls.addAll(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()) {
        complete.removeDuplicates();
    }
    webModule.getAltDDs().put("beans.xml", complete);
}
Also used : CompositeBeans(org.apache.openejb.cdi.CompositeBeans) URLClassLoader(java.net.URLClassLoader) EmptyResourcesClassLoader(org.apache.openejb.core.EmptyResourcesClassLoader) IOException(java.io.IOException) URL(java.net.URL) LinkedList(java.util.LinkedList)

Example 9 with CompositeBeans

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

the class EjbJarInfoBuilder method buildInfo.

public EjbJarInfo buildInfo(final EjbModule jar) throws OpenEJBException {
    deploymentIds.clear();
    securityRoles.clear();
    final Map<String, EjbDeployment> ejbds = jar.getOpenejbJar().getDeploymentsByEjbName();
    final int beansDeployed = jar.getOpenejbJar().getEjbDeploymentCount();
    final int beansInEjbJar = jar.getEjbJar().getEnterpriseBeans().length;
    if (beansInEjbJar != beansDeployed) {
        for (final EnterpriseBean bean : jar.getEjbJar().getEnterpriseBeans()) {
            if (!ejbds.containsKey(bean.getEjbName())) {
                ConfigUtils.logger.warning("conf.0018", bean.getEjbName(), jar.getJarLocation());
            }
        }
        final String message = messages.format("conf.0008", jar.getJarLocation(), String.valueOf(beansInEjbJar), String.valueOf(beansDeployed));
        logger.warning(message);
        throw new OpenEJBException(message);
    }
    final Map<String, EnterpriseBeanInfo> infos = new HashMap<>();
    final Map<String, EnterpriseBean> items = new HashMap<>();
    final EjbJarInfo ejbJar = new EjbJarInfo();
    ejbJar.path = jar.getJarLocation();
    ejbJar.moduleUri = jar.getModuleUri();
    ejbJar.moduleId = jar.getModuleId();
    if (jar.getEjbJar() != null && jar.getEjbJar().getModuleName() != null) {
        ejbJar.moduleName = jar.getEjbJar().getModuleName();
    } else {
        ejbJar.moduleName = jar.getModuleId();
    }
    ejbJar.watchedResources.addAll(jar.getWatchedResources());
    ejbJar.properties.putAll(jar.getProperties());
    ejbJar.properties.putAll(jar.getOpenejbJar().getProperties());
    for (final EnterpriseBean bean : jar.getEjbJar().getEnterpriseBeans()) {
        final EnterpriseBeanInfo beanInfo;
        if (bean instanceof SessionBean) {
            beanInfo = initSessionBean((SessionBean) bean, ejbJar, ejbds);
        } else if (bean instanceof EntityBean) {
            beanInfo = initEntityBean((EntityBean) bean, ejbds);
        } else if (bean instanceof MessageDrivenBean) {
            beanInfo = initMessageBean((MessageDrivenBean) bean, ejbds);
        } else {
            throw new OpenEJBException("Unknown bean type: " + bean.getClass().getName());
        }
        ejbJar.enterpriseBeans.add(beanInfo);
        if (deploymentIds.contains(beanInfo.ejbDeploymentId)) {
            final String message = messages.format("conf.0100", beanInfo.ejbDeploymentId, jar.getJarLocation(), beanInfo.ejbName);
            logger.warning(message);
            throw new OpenEJBException(message);
        }
        deploymentIds.add(beanInfo.ejbDeploymentId);
        beanInfo.codebase = jar.getJarLocation();
        infos.put(beanInfo.ejbName, beanInfo);
        items.put(beanInfo.ejbName, bean);
        if (bean.getSecurityIdentity() != null) {
            beanInfo.runAs = bean.getSecurityIdentity().getRunAs();
            final EjbDeployment deployment = ejbds.get(beanInfo.ejbName);
            if (deployment != null) {
                for (final RoleMapping mapping : deployment.getRoleMapping()) {
                    if (mapping.getRoleName().equals(beanInfo.runAs)) {
                        beanInfo.runAsUser = mapping.getPrincipalName();
                        break;
                    }
                }
            }
        }
        initJndiNames(ejbds, beanInfo);
    }
    if (jar.getEjbJar().getAssemblyDescriptor() != null) {
        initInterceptors(jar, ejbJar);
        initSecurityRoles(jar, ejbJar);
        initMethodPermissions(jar, ejbds, ejbJar);
        initExcludesList(jar, ejbds, ejbJar);
        initMethodTransactions(jar, ejbds, ejbJar);
        initMethodConcurrency(jar, ejbds, ejbJar);
        initApplicationExceptions(jar, ejbJar);
        for (final EnterpriseBeanInfo bean : ejbJar.enterpriseBeans) {
            resolveRoleLinks(bean, items.get(bean.ejbName));
        }
    }
    if (jar.getEjbJar().getRelationships() != null) {
        initRelationships(jar, infos);
    }
    final Beans beans = jar.getBeans();
    if (beans != null) {
        ejbJar.beans = new BeansInfo();
        ejbJar.beans.version = beans.getVersion();
        ejbJar.beans.discoveryMode = beans.getBeanDiscoveryMode();
        if (beans.getScan() != null) {
            for (final Beans.Scan.Exclude exclude : beans.getScan().getExclude()) {
                final ExclusionInfo exclusionInfo = new ExclusionInfo();
                for (final Object config : exclude.getIfClassAvailableOrIfClassNotAvailableOrIfSystemProperty()) {
                    if (Beans.Scan.Exclude.IfAvailableClassCondition.class.isInstance(config)) {
                        exclusionInfo.availableClasses.add(Beans.Scan.Exclude.ClassCondition.class.cast(config).getName());
                    } else if (Beans.Scan.Exclude.IfNotAvailableClassCondition.class.isInstance(config)) {
                        exclusionInfo.notAvailableClasses.add(Beans.Scan.Exclude.ClassCondition.class.cast(config).getName());
                    } else if (Beans.Scan.Exclude.IfSystemProperty.class.isInstance(config)) {
                        final Beans.Scan.Exclude.IfSystemProperty systemProperty = Beans.Scan.Exclude.IfSystemProperty.class.cast(config);
                        if (systemProperty.getValue() == null) {
                            exclusionInfo.systemPropertiesPresence.add(systemProperty.getName());
                        } else {
                            exclusionInfo.systemProperties.put(systemProperty.getName(), systemProperty.getValue());
                        }
                    } else {
                        throw new IllegalArgumentException("Not supported: " + config);
                    }
                }
                final BeansInfo.ExclusionEntryInfo exclusionEntryInfo = new BeansInfo.ExclusionEntryInfo();
                exclusionEntryInfo.name = exclude.getName();
                exclusionEntryInfo.exclusion = exclusionInfo;
                ejbJar.beans.excludes.add(exclusionEntryInfo);
            }
        }
        ejbJar.beans.duplicatedAlternativeClasses.addAll(beans.getDuplicatedAlternatives().getClasses());
        ejbJar.beans.duplicatedAlternativeStereotypes.addAll(beans.getDuplicatedAlternatives().getStereotypes());
        ejbJar.beans.duplicatedInterceptors.addAll(beans.getDuplicatedInterceptors());
        ejbJar.beans.duplicatedDecorators.addAll(beans.getDuplicatedDecorators());
        ejbJar.beans.startupClasses.addAll(beans.getStartupBeans());
        final Map<URL, String> discoveryModeByUrl = new HashMap<>();
        final CompositeBeans composite;
        final boolean isComposite = CompositeBeans.class.isInstance(beans);
        if (isComposite) {
            composite = CompositeBeans.class.cast(beans);
            discoveryModeByUrl.putAll(composite.getDiscoveryByUrl());
        } else {
            composite = null;
            URL key = DEFAULT_BEANS_XML_KEY;
            if (beans.getUri() != null) {
                try {
                    key = new URL(beans.getUri());
                } catch (final MalformedURLException e) {
                // no-op
                }
            }
            discoveryModeByUrl.put(key, beans.getBeanDiscoveryMode());
        }
        for (final Map.Entry<URL, List<String>> next : beans.getManagedClasses().entrySet()) {
            final URL key = next.getKey();
            final BeansInfo.BDAInfo bdaInfo = new BeansInfo.BDAInfo();
            bdaInfo.discoveryMode = discoveryModeByUrl.get(key);
            merge(composite, key == null ? DEFAULT_BEANS_XML_KEY : key, bdaInfo, next.getValue());
            ejbJar.beans.bdas.add(bdaInfo);
        }
        for (final Map.Entry<URL, List<String>> next : beans.getNotManagedClasses().entrySet()) {
            final URL key = next.getKey();
            final BeansInfo.BDAInfo bdaInfo = new BeansInfo.BDAInfo();
            bdaInfo.discoveryMode = BeanArchiveService.BeanDiscoveryMode.ANNOTATED.name();
            merge(composite, key == null ? DEFAULT_BEANS_XML_KEY : key, bdaInfo, next.getValue());
            ejbJar.beans.noDescriptorBdas.add(bdaInfo);
        }
        // app composer case mainly,we should really not use it anywhere else
        if (composite == null && ejbJar.beans.bdas.size() == 1) {
            final BeansInfo.BDAInfo bda = ejbJar.beans.bdas.iterator().next();
            bda.alternatives.addAll(beans.getAlternativeClasses());
            bda.interceptors.addAll(beans.getInterceptors());
            bda.decorators.addAll(beans.getDecorators());
            bda.stereotypeAlternatives.addAll(beans.getAlternativeStereotypes());
        }
    }
    return ejbJar;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) HashMap(java.util.HashMap) ExclusionInfo(org.apache.openejb.assembler.classic.ExclusionInfo) SessionBean(org.apache.openejb.jee.SessionBean) URL(java.net.URL) EnterpriseBeanInfo(org.apache.openejb.assembler.classic.EnterpriseBeanInfo) BeansInfo(org.apache.openejb.assembler.classic.BeansInfo) MessageDrivenBean(org.apache.openejb.jee.MessageDrivenBean) List(java.util.List) ArrayList(java.util.ArrayList) ExcludeList(org.apache.openejb.jee.ExcludeList) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) RoleMapping(org.apache.openejb.jee.oejb3.RoleMapping) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) Beans(org.apache.openejb.jee.Beans) EntityBean(org.apache.openejb.jee.EntityBean) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo) Map(java.util.Map) HashMap(java.util.HashMap)

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