Search in sources :

Example 31 with EjbDeployment

use of org.apache.openejb.jee.oejb3.EjbDeployment 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)

Example 32 with EjbDeployment

use of org.apache.openejb.jee.oejb3.EjbDeployment in project tomee by apache.

the class EjbJarInfoBuilder method initJndiNames.

private void initJndiNames(final Map<String, EjbDeployment> ejbds, final EnterpriseBeanInfo info) {
    final EjbDeployment deployment = ejbds.get(info.ejbName);
    if (deployment != null) {
        for (final Jndi jndi : deployment.getJndi()) {
            final JndiNameInfo jndiNameInfo = new JndiNameInfo();
            jndiNameInfo.intrface = jndi.getInterface();
            jndiNameInfo.name = jndi.getName();
            info.jndiNamess.add(jndiNameInfo);
        }
    }
}
Also used : JndiNameInfo(org.apache.openejb.assembler.classic.JndiNameInfo) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) Jndi(org.apache.openejb.jee.oejb3.Jndi)

Example 33 with EjbDeployment

use of org.apache.openejb.jee.oejb3.EjbDeployment in project tomee by apache.

the class EjbJarInfoBuilder method initEntityBean.

private EnterpriseBeanInfo initEntityBean(final EntityBean e, final Map m) throws OpenEJBException {
    final EntityBeanInfo bean = new EntityBeanInfo();
    final EjbDeployment d = (EjbDeployment) m.get(e.getEjbName());
    if (d == null) {
        throw new OpenEJBException("No deployment information in openejb-jar.xml for bean " + e.getEjbName() + ". Please redeploy the jar");
    }
    bean.ejbDeploymentId = d.getDeploymentId();
    bean.containerId = d.getContainerId();
    final Icon icon = e.getIcon();
    bean.largeIcon = icon == null ? null : icon.getLargeIcon();
    bean.smallIcon = icon == null ? null : icon.getSmallIcon();
    bean.description = e.getDescription();
    bean.displayName = e.getDisplayName();
    bean.ejbClass = e.getEjbClass();
    bean.abstractSchemaName = e.getAbstractSchemaName();
    bean.ejbName = e.getEjbName();
    bean.home = e.getHome();
    bean.remote = e.getRemote();
    bean.localHome = e.getLocalHome();
    bean.local = e.getLocal();
    bean.transactionType = "Container";
    bean.primKeyClass = e.getPrimKeyClass();
    bean.primKeyField = e.getPrimkeyField();
    bean.persistenceType = e.getPersistenceType().toString();
    bean.reentrant = String.valueOf(e.getReentrant());
    bean.properties.putAll(d.getProperties());
    final CmpVersion cmpVersion = e.getCmpVersion();
    if (e.getPersistenceType() == PersistenceType.CONTAINER) {
        if (cmpVersion != null && cmpVersion == CmpVersion.CMP1) {
            bean.cmpVersion = 1;
        } else {
            bean.cmpVersion = 2;
        }
    }
    final List<CmpField> cmpFields = e.getCmpField();
    for (final CmpField cmpField : cmpFields) {
        bean.cmpFieldNames.add(cmpField.getFieldName());
    }
    if (bean.persistenceType.equalsIgnoreCase("Container")) {
        for (final Query q : e.getQuery()) {
            final QueryInfo query = new QueryInfo();
            query.queryStatement = q.getEjbQl().trim();
            final MethodInfo method = new MethodInfo();
            method.ejbName = bean.ejbName;
            method.className = "*";
            final QueryMethod qm = q.getQueryMethod();
            method.methodName = qm.getMethodName();
            if (qm.getMethodParams() != null) {
                method.methodParams = qm.getMethodParams().getMethodParam();
            }
            query.method = method;
            final ResultTypeMapping resultType = q.getResultTypeMapping();
            if (ResultTypeMapping.REMOTE.equals(resultType)) {
                query.remoteResultType = true;
            }
            bean.queries.add(query);
        }
        for (final org.apache.openejb.jee.oejb3.Query q : d.getQuery()) {
            final QueryInfo query = new QueryInfo();
            query.description = q.getDescription();
            query.queryStatement = q.getObjectQl().trim();
            final MethodInfo method = new MethodInfo();
            method.ejbName = bean.ejbName;
            method.className = "*";
            final org.apache.openejb.jee.oejb3.QueryMethod qm = q.getQueryMethod();
            method.methodName = qm.getMethodName();
            if (qm.getMethodParams() != null) {
                method.methodParams = qm.getMethodParams().getMethodParam();
            }
            query.method = method;
            bean.queries.add(query);
        }
    }
    return bean;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ResultTypeMapping(org.apache.openejb.jee.ResultTypeMapping) Query(org.apache.openejb.jee.Query) QueryMethod(org.apache.openejb.jee.QueryMethod) QueryInfo(org.apache.openejb.assembler.classic.QueryInfo) CmpField(org.apache.openejb.jee.CmpField) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) MethodInfo(org.apache.openejb.assembler.classic.MethodInfo) InitMethodInfo(org.apache.openejb.assembler.classic.InitMethodInfo) NamedMethodInfo(org.apache.openejb.assembler.classic.NamedMethodInfo) RemoveMethodInfo(org.apache.openejb.assembler.classic.RemoveMethodInfo) EntityBeanInfo(org.apache.openejb.assembler.classic.EntityBeanInfo) Icon(org.apache.openejb.jee.Icon) CmpVersion(org.apache.openejb.jee.CmpVersion)

Example 34 with EjbDeployment

use of org.apache.openejb.jee.oejb3.EjbDeployment in project tomee by apache.

the class InitEjbDeployments method deploy.

private EjbModule deploy(final EjbModule ejbModule, final Map<String, String> contextData, final Set<String> abstractSchemaNames) throws OpenEJBException {
    contextData.put("moduleId", ejbModule.getModuleId());
    contextData.put("moduleUri", ejbModule.getModuleUri().toString());
    final OpenejbJar openejbJar;
    if (ejbModule.getOpenejbJar() != null) {
        openejbJar = ejbModule.getOpenejbJar();
    } else {
        openejbJar = new OpenejbJar();
        ejbModule.setOpenejbJar(openejbJar);
    }
    StringTemplate deploymentIdTemplate = this.deploymentIdTemplate;
    if (openejbJar.getProperties().containsKey(DEPLOYMENT_ID_FORMAT)) {
        final String format = openejbJar.getProperties().getProperty(DEPLOYMENT_ID_FORMAT);
        logger.info("Using " + DEPLOYMENT_ID_FORMAT + " '" + format + "'");
        deploymentIdTemplate = new StringTemplate(format);
    }
    for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
        StringTemplate template = deploymentIdTemplate;
        final org.apache.openejb.api.EjbDeployment annotation = getEjbDeploymentAnnotation(ejbModule, bean);
        EjbDeployment ejbDeployment = openejbJar.getDeploymentsByEjbName().get(bean.getEjbName());
        if (ejbDeployment == null) {
            ejbDeployment = new EjbDeployment();
            ejbDeployment.setEjbName(bean.getEjbName());
            if (annotation != null && isDefined(annotation.id())) {
                template = new StringTemplate(annotation.id());
                ejbDeployment.setDeploymentId(formatDeploymentId(bean, contextData, template));
            } else {
                ejbDeployment.setDeploymentId(formatDeploymentId(bean, contextData, template));
                if (!(bean instanceof ManagedBean) || !((ManagedBean) bean).isHidden()) {
                    logger.info("Auto-deploying ejb " + bean.getEjbName() + ": EjbDeployment(deployment-id=" + ejbDeployment.getDeploymentId() + ")");
                }
            }
            openejbJar.getEjbDeployment().add(ejbDeployment);
        } else if (ejbDeployment.getDeploymentId() == null) {
            if (annotation != null && isDefined(annotation.id())) {
                template = new StringTemplate(annotation.id());
                ejbDeployment.setDeploymentId(formatDeploymentId(bean, contextData, template));
            } else {
                ejbDeployment.setDeploymentId(formatDeploymentId(bean, contextData, template));
                logger.info("Auto-assigning deployment-id for ejb " + bean.getEjbName() + ": EjbDeployment(deployment-id=" + ejbDeployment.getDeploymentId() + ")");
            }
        }
        if (ejbDeployment.getContainerId() == null && annotation != null && isDefined(annotation.container())) {
            ejbDeployment.setContainerId(annotation.container());
        }
        if (isCmpEntity(bean)) {
            final EntityBean entity = (EntityBean) bean;
            if (entity.getAbstractSchemaName() == null) {
                String abstractSchemaName = bean.getEjbName().trim().replaceAll("[ \\t\\n\\r-]+", "_");
                // The AbstractSchemaName must be unique, we should check that it is
                if (abstractSchemaNames.contains(abstractSchemaName)) {
                    int i = 2;
                    while (abstractSchemaNames.contains(abstractSchemaName + i)) {
                        i++;
                    }
                    abstractSchemaName = abstractSchemaName + i;
                }
                abstractSchemaNames.add(abstractSchemaName);
                entity.setAbstractSchemaName(abstractSchemaName);
            }
        }
    }
    return ejbModule;
}
Also used : OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) StringTemplate(org.apache.openejb.util.StringTemplate) EntityBean(org.apache.openejb.jee.EntityBean) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) ManagedBean(org.apache.openejb.jee.ManagedBean)

Example 35 with EjbDeployment

use of org.apache.openejb.jee.oejb3.EjbDeployment in project tomee by apache.

the class AutoConfig method processResourceEnvRef.

private void processResourceEnvRef(final JndiReference ref, final EjbDeployment ejbDeployment, final AppResources appResources, final ClassLoader classLoader) throws OpenEJBException {
    // skip destinations with lookup name
    if (ref.getLookupName() != null) {
        return;
    }
    // skip destinations with a global jndi name
    final String mappedName = ref.getMappedName() == null ? "" : ref.getMappedName();
    if (mappedName.startsWith("jndi:")) {
        return;
    }
    final String refName = ref.getName();
    final String refType = getType(ref, classLoader);
    // skip references such as SessionContext which are automatically handled by the server
    if (isIgnoredReferenceType(refType, classLoader)) {
        return;
    }
    ResourceLink link = ejbDeployment.getResourceLink(refName);
    if (link == null) {
        String id = mappedName.length() == 0 ? refName : mappedName;
        id = getResourceEnvId(ejbDeployment.getDeploymentId(), id, refType, appResources);
        if (id == null) {
            // could be a session context ref
            return;
        }
        logger.info("Auto-linking resource-env-ref '" + refName + "' in bean " + ejbDeployment.getDeploymentId() + " to Resource(id=" + id + ")");
        link = new ResourceLink();
        link.setResId(id);
        link.setResRefName(refName);
        ejbDeployment.addResourceLink(link);
    } else {
        final String id = getResourceEnvId(ejbDeployment.getDeploymentId(), link.getResId(), refType, appResources);
        link.setResId(id);
        link.setResRefName(refName);
    }
}
Also used : ResourceLink(org.apache.openejb.jee.oejb3.ResourceLink)

Aggregations

EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)46 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)35 EjbJar (org.apache.openejb.jee.EjbJar)27 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)16 EjbModule (org.apache.openejb.config.EjbModule)15 Properties (java.util.Properties)14 OpenEJBException (org.apache.openejb.OpenEJBException)10 ResourceLink (org.apache.openejb.jee.oejb3.ResourceLink)9 Assembler (org.apache.openejb.assembler.classic.Assembler)8 StatelessBean (org.apache.openejb.jee.StatelessBean)8 HashMap (java.util.HashMap)7 EjbJarInfo (org.apache.openejb.assembler.classic.EjbJarInfo)7 InitialContext (javax.naming.InitialContext)6 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)6 ManagedBean (org.apache.openejb.jee.ManagedBean)6 MessageDrivenBean (org.apache.openejb.jee.MessageDrivenBean)6 SessionBean (org.apache.openejb.jee.SessionBean)6 Map (java.util.Map)5 SingletonBean (org.apache.openejb.jee.SingletonBean)5 ContainerSystem (org.apache.openejb.spi.ContainerSystem)5