Search in sources :

Example 1 with BeansInfo

use of org.apache.openejb.assembler.classic.BeansInfo in project tomee by apache.

the class OpenEJBBeanInfoService method createBeanArchiveInformation.

public DefaultBeanArchiveInformation createBeanArchiveInformation(final BeansInfo.BDAInfo bda, final BeansInfo info, final ClassLoader loader) {
    String mode = bda.discoveryMode == null ? "ALL" : bda.discoveryMode;
    if (info != null && info.version != null && !"1.0".equals(info.version) && info.discoveryMode == null) {
        throw new WebBeansConfigurationException("beans.xml with version 1.1 and higher must declare a bean-discovery-mode!");
    }
    if ("ALL".equalsIgnoreCase(mode) && bda.trim) {
        mode = "TRIM";
    }
    final DefaultBeanArchiveInformation information = new DefaultBeanArchiveInformation(bda.uri.toASCIIString());
    information.setVersion(info == null ? "1.1" : info.version);
    information.setBeanDiscoveryMode(BeanDiscoveryMode.valueOf(mode.trim().toUpperCase(Locale.ENGLISH)));
    information.setDecorators(bda.decorators);
    information.setInterceptors(bda.interceptors);
    if (info != null) {
        information.getAlternativeClasses().addAll(bda.alternatives);
        information.getAlternativeStereotypes().addAll(bda.stereotypeAlternatives);
        for (final BeansInfo.ExclusionEntryInfo exclusionInfo : info.excludes) {
            boolean skip = false;
            for (final String n : exclusionInfo.exclusion.availableClasses) {
                if (!isClassAvailable(loader, n)) {
                    skip = true;
                    break;
                }
            }
            if (!skip) {
                for (final String n : exclusionInfo.exclusion.notAvailableClasses) {
                    if (isClassAvailable(loader, n)) {
                        skip = true;
                        break;
                    }
                }
            }
            if (!skip) {
                for (final String n : exclusionInfo.exclusion.systemPropertiesPresence) {
                    // our system instance is more powerful here
                    if (SystemInstance.get().getProperty(n) == null) {
                        skip = true;
                        break;
                    }
                }
            }
            if (!skip) {
                for (final String n : exclusionInfo.exclusion.systemProperties.stringPropertyNames()) {
                    // our system instance is more powerful here
                    if (!exclusionInfo.exclusion.systemProperties.getProperty(n).equals(SystemInstance.get().getProperty(n))) {
                        skip = true;
                        break;
                    }
                }
            }
            if (skip) {
                continue;
            }
            final String name = exclusionInfo.name;
            if (name.endsWith(".*")) {
                information.addClassExclude(name.substring(0, name.length() - 2));
            } else if (name.endsWith(".**")) {
                information.addPackageExclude(name.substring(0, name.length() - 3));
            } else {
                information.addClassExclude(name);
            }
        }
    }
    return information;
}
Also used : WebBeansConfigurationException(org.apache.webbeans.exception.WebBeansConfigurationException) BeansInfo(org.apache.openejb.assembler.classic.BeansInfo) DefaultBeanArchiveInformation(org.apache.webbeans.xml.DefaultBeanArchiveInformation)

Example 2 with BeansInfo

use of org.apache.openejb.assembler.classic.BeansInfo in project tomee by apache.

the class Container method deploy.

public AppContext deploy(final String name, final File file, final boolean overrideName) throws OpenEJBException, IOException, NamingException {
    final AppContext context;
    final AppInfo appInfo;
    if (WebAppDeployer.Helper.isWebApp(file)) {
        String contextRoot = file.getName();
        if (overrideName) {
            contextRoot = name;
        }
        appInfo = SystemInstance.get().getComponent(WebAppDeployer.class).deploy(null, contextRoot, file);
        if (appInfo != null) {
            context = SystemInstance.get().getComponent(ContainerSystem.class).getAppContext(appInfo.appId);
        } else {
            context = null;
        }
    } else {
        appInfo = configurationFactory.configureApplication(file);
        // ensure to activate CDI for classpath deployment, we can desire to move it but it breaks less apps this way
        for (final EjbJarInfo jar : appInfo.ejbJars) {
            if (jar.enterpriseBeans.size() == 1) {
                final EnterpriseBeanInfo next = jar.enterpriseBeans.iterator().next();
                if (ManagedBeanInfo.class.isInstance(next) && ManagedBeanInfo.class.cast(next).hidden) {
                    continue;
                }
            }
            if (jar.beans == null) {
                if (!jar.enterpriseBeans.isEmpty()) {
                    jar.beans = new BeansInfo();
                    jar.beans.version = "1.1";
                    jar.beans.discoveryMode = "annotated";
                    final BeansInfo.BDAInfo info = new BeansInfo.BDAInfo();
                    info.discoveryMode = "annotated";
                    info.uri = jar.moduleUri;
                    jar.beans.noDescriptorBdas.add(info);
                    for (final EnterpriseBeanInfo bean : jar.enterpriseBeans) {
                        if (bean.ejbClass == null) {
                            continue;
                        }
                        info.managedClasses.add(bean.ejbClass);
                    }
                }
            }
        }
        if (overrideName) {
            appInfo.appId = name;
            for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
                if (file.getName().equals(ejbJar.moduleName)) {
                    ejbJar.moduleName = name;
                    ejbJar.moduleId = name;
                }
                for (final EnterpriseBeanInfo ejb : ejbJar.enterpriseBeans) {
                    if (BeanContext.Comp.openejbCompName(file.getName()).equals(ejb.ejbName)) {
                        ejb.ejbName = BeanContext.Comp.openejbCompName(name);
                    }
                }
            }
            for (final WebAppInfo webApp : appInfo.webApps) {
                if (sameApplication(file, webApp)) {
                    webApp.moduleId = name;
                    webApp.contextRoot = lastPart(name, webApp.contextRoot);
                    if ("ROOT".equals(webApp.contextRoot)) {
                        webApp.contextRoot = "";
                    }
                }
            }
        }
        context = assembler.createApplication(appInfo);
    }
    moduleIds.put(name, null != appInfo ? appInfo.path : null);
    infos.put(name, appInfo);
    appContexts.put(name, context);
    return context;
}
Also used : EnterpriseBeanInfo(org.apache.openejb.assembler.classic.EnterpriseBeanInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) BeansInfo(org.apache.openejb.assembler.classic.BeansInfo) AppContext(org.apache.openejb.AppContext) ManagedBeanInfo(org.apache.openejb.assembler.classic.ManagedBeanInfo) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo) AppInfo(org.apache.openejb.assembler.classic.AppInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo)

Example 3 with BeansInfo

use of org.apache.openejb.assembler.classic.BeansInfo in project tomee by apache.

the class CdiScanner method init.

@Override
public void init(final Object object) {
    if (!StartupObject.class.isInstance(object)) {
        return;
    }
    containerLoader = ParentClassLoaderFinder.Helper.get();
    final StartupObject startupObject = StartupObject.class.cast(object);
    final AppInfo appInfo = startupObject.getAppInfo();
    final ClassLoader classLoader = startupObject.getClassLoader();
    final ClassLoaderComparator comparator;
    if (classLoader instanceof ClassLoaderComparator) {
        comparator = (ClassLoaderComparator) classLoader;
    } else {
        comparator = new DefaultClassLoaderComparator(classLoader);
    }
    final WebBeansContext webBeansContext = startupObject.getWebBeansContext();
    final InterceptorsManager interceptorsManager = webBeansContext.getInterceptorsManager();
    // app beans
    for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
        Set<Class<?>> classes = new HashSet<>();
        final BeansInfo beans = ejbJar.beans;
        if (beans == null || "false".equalsIgnoreCase(ejbJar.properties.getProperty("openejb.cdi.activated"))) {
            continue;
        }
        if (startupObject.isFromWebApp()) {
            // deploy only the related ejbmodule
            if (!ejbJar.moduleId.equals(startupObject.getWebContext().getId())) {
                continue;
            }
        } else if (ejbJar.webapp && !appInfo.webAppAlone) {
            continue;
        }
        if (appInfo.webAppAlone || !ejbJar.webapp) {
            // "manual" extension to avoid to add it through SPI mecanism
            classes.addAll(asList(TRANSACTIONAL_INTERCEPTORS));
            for (final Class<?> interceptor : TRANSACTIONAL_INTERCEPTORS) {
                interceptorsManager.addEnabledInterceptorClass(interceptor);
            }
        }
        // here for ears we need to skip classes in the parent classloader
        final ClassLoader scl = ClassLoader.getSystemClassLoader();
        final boolean filterByClassLoader = "true".equals(ejbJar.properties.getProperty(OPENEJB_CDI_FILTER_CLASSLOADER, SystemInstance.get().getProperty(OPENEJB_CDI_FILTER_CLASSLOADER, "true")));
        final BeanArchiveService beanArchiveService = webBeansContext.getBeanArchiveService();
        final boolean openejb = OpenEJBBeanInfoService.class.isInstance(beanArchiveService);
        final Map<BeansInfo.BDAInfo, BeanArchiveService.BeanArchiveInformation> infoByBda = new HashMap<>();
        for (final BeansInfo.BDAInfo bda : beans.bdas) {
            if (bda.uri != null) {
                try {
                    beansXml.add(bda.uri.toURL());
                } catch (final MalformedURLException e) {
                // no-op
                }
            }
            infoByBda.put(bda, handleBda(startupObject, classLoader, comparator, ejbJar, scl, filterByClassLoader, beanArchiveService, openejb, bda));
        }
        for (final BeansInfo.BDAInfo bda : beans.noDescriptorBdas) {
            // infoByBda.put() not needed since we know it means annotated
            handleBda(startupObject, classLoader, comparator, ejbJar, scl, filterByClassLoader, beanArchiveService, openejb, bda);
        }
        if (startupObject.getBeanContexts() != null) {
            for (final BeanContext bc : startupObject.getBeanContexts()) {
                final String name = bc.getBeanClass().getName();
                if (BeanContext.Comp.class.getName().equals(name)) {
                    continue;
                }
                boolean cdi = false;
                for (final BeansInfo.BDAInfo bda : beans.bdas) {
                    final BeanArchiveService.BeanArchiveInformation info = infoByBda.get(bda);
                    if (info.getBeanDiscoveryMode() == BeanArchiveService.BeanDiscoveryMode.NONE) {
                        continue;
                    }
                    if (bda.managedClasses.contains(name)) {
                        classes.add(load(name, classLoader));
                        cdi = true;
                        break;
                    }
                }
                if (!cdi) {
                    for (final BeansInfo.BDAInfo bda : beans.noDescriptorBdas) {
                        if (bda.managedClasses.contains(name)) {
                            classes.add(load(name, classLoader));
                            break;
                        }
                    }
                }
            }
        }
        if ("true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cdi.debug", "false"))) {
            final Logger logger = Logger.getInstance(LogCategory.OPENEJB, CdiScanner.class.getName());
            logger.info("CDI beans for " + startupObject.getAppInfo().appId + (startupObject.getWebContext() != null ? " webcontext = " + startupObject.getWebContext().getContextRoot() : ""));
            final List<String> names = new ArrayList<>(classes.size());
            for (final Class<?> c : classes) {
                names.add(c.getName());
            }
            Collections.sort(names);
            for (final String c : names) {
                logger.info("    " + c);
            }
        }
        if (!classes.isEmpty()) {
            addClasses(tomeeBeanArchiveInformation, classes);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ClassLoaderComparator(org.apache.openejb.util.classloader.ClassLoaderComparator) DefaultClassLoaderComparator(org.apache.openejb.util.classloader.DefaultClassLoaderComparator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Logger(org.apache.openejb.util.Logger) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeansInfo(org.apache.openejb.assembler.classic.BeansInfo) HashSet(java.util.HashSet) DefaultClassLoaderComparator(org.apache.openejb.util.classloader.DefaultClassLoaderComparator) BeanArchiveService(org.apache.webbeans.spi.BeanArchiveService) AppInfo(org.apache.openejb.assembler.classic.AppInfo) BeanContext(org.apache.openejb.BeanContext) DefaultBeanArchiveInformation(org.apache.webbeans.xml.DefaultBeanArchiveInformation) InterceptorsManager(org.apache.webbeans.intercept.InterceptorsManager) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo)

Example 4 with BeansInfo

use of org.apache.openejb.assembler.classic.BeansInfo 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

BeansInfo (org.apache.openejb.assembler.classic.BeansInfo)4 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)3 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 AppInfo (org.apache.openejb.assembler.classic.AppInfo)2 EnterpriseBeanInfo (org.apache.openejb.assembler.classic.EnterpriseBeanInfo)2 DefaultBeanArchiveInformation (org.apache.webbeans.xml.DefaultBeanArchiveInformation)2 URL (java.net.URL)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 AppContext (org.apache.openejb.AppContext)1 BeanContext (org.apache.openejb.BeanContext)1 OpenEJBException (org.apache.openejb.OpenEJBException)1 ExclusionInfo (org.apache.openejb.assembler.classic.ExclusionInfo)1 ManagedBeanInfo (org.apache.openejb.assembler.classic.ManagedBeanInfo)1 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)1 CompositeBeans (org.apache.openejb.cdi.CompositeBeans)1 Beans (org.apache.openejb.jee.Beans)1