Search in sources :

Example 1 with ModuleContext

use of org.apache.openejb.ModuleContext in project tomee by apache.

the class JndiBuilder method bindJava.

private void bindJava(final BeanContext cdi, final Class intrface, final Reference ref, final Bindings bindings, final EnterpriseBeanInfo beanInfo) throws NamingException {
    final ModuleContext module = cdi.getModuleContext();
    final AppContext application = module.getAppContext();
    final Context moduleContext = module.getModuleJndiContext();
    final Context appContext = application.getAppJndiContext();
    final Context globalContext = application.getGlobalJndiContext();
    final String appName = application.isStandaloneModule() ? "" : application.getId() + "/";
    String moduleName = moduleName(cdi);
    if (moduleName.startsWith("/")) {
        moduleName = moduleName.substring(1);
    }
    String beanName = cdi.getEjbName();
    if (intrface != null) {
        beanName = beanName + "!" + intrface.getName();
    }
    final String globalName = "global/" + appName + moduleName + beanName;
    try {
        if (embeddedEjbContainerApi && !(beanInfo instanceof ManagedBeanInfo && ((ManagedBeanInfo) beanInfo).hidden)) {
            logger.info(String.format("Jndi(name=\"java:%s\")", globalName));
        }
        globalContext.bind(globalName, ref);
        application.getBindings().put(globalName, ref);
        bind("openejb/global/" + globalName, ref, bindings, beanInfo, intrface);
    } catch (final NameAlreadyBoundException e) {
        // one interface in more than one role (e.g. both Local and Remote
        return;
    }
    appContext.bind("app/" + moduleName + beanName, ref);
    application.getBindings().put("app/" + moduleName + beanName, ref);
    final String moduleJndi = "module/" + beanName;
    moduleContext.bind(moduleJndi, ref);
    // contextual if the same ejb (api) is deployed in 2 wars of an ear
    ContextualEjbLookup contextual = ContextualEjbLookup.class.cast(application.getBindings().get(moduleJndi));
    if (contextual == null) {
        final Map<BeanContext, Object> potentials = new HashMap<>();
        contextual = new ContextualEjbLookup(potentials, ref);
        // TODO: we shouldn't do it but use web bindings
        application.getBindings().put(moduleJndi, contextual);
    }
    contextual.potentials.put(cdi, ref);
}
Also used : BeanContext(org.apache.openejb.BeanContext) ModuleContext(org.apache.openejb.ModuleContext) Context(javax.naming.Context) AppContext(org.apache.openejb.AppContext) BeanContext(org.apache.openejb.BeanContext) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) HashMap(java.util.HashMap) AppContext(org.apache.openejb.AppContext) ModuleContext(org.apache.openejb.ModuleContext)

Example 2 with ModuleContext

use of org.apache.openejb.ModuleContext in project tomee by apache.

the class JndiBuilder method computeGlobalName.

// ee6 specified ejb bindings in module, app, and global contexts
private String computeGlobalName(final BeanContext cdi, final Class<?> intrface) {
    final ModuleContext module = cdi.getModuleContext();
    final AppContext application = module.getAppContext();
    final String appName = application.isStandaloneModule() ? "" : application.getId() + "/";
    final String moduleName = moduleName(cdi);
    String beanName = cdi.getEjbName();
    if (intrface != null) {
        beanName = beanName + "!" + intrface.getName();
    }
    return "global/" + appName + moduleName + beanName;
}
Also used : AppContext(org.apache.openejb.AppContext) ModuleContext(org.apache.openejb.ModuleContext)

Example 3 with ModuleContext

use of org.apache.openejb.ModuleContext in project tomee by apache.

the class EnterpriseBeanBuilder method build.

public BeanContext build() throws OpenEJBException {
    Class ejbClass = loadClass(bean.ejbClass, "classNotFound.ejbClass");
    if (DynamicSubclass.isDynamic(ejbClass)) {
        ejbClass = DynamicSubclass.createSubclass(ejbClass, moduleContext.getClassLoader());
    }
    Class home = null;
    Class remote = null;
    if (bean.home != null) {
        home = loadClass(bean.home, "classNotFound.home");
        remote = loadClass(bean.remote, "classNotFound.remote");
    }
    Class<?> proxy = null;
    if (bean.proxy != null) {
        proxy = loadClass(bean.proxy, "classNotFound.proxy");
    }
    Class<?> localhome = null;
    Class<?> local = null;
    if (bean.localHome != null) {
        localhome = loadClass(bean.localHome, "classNotFound.localHome");
        local = loadClass(bean.local, "classNotFound.local");
    }
    final List<Class> businessLocals = new ArrayList<>();
    for (final String businessLocal : bean.businessLocal) {
        businessLocals.add(loadClass(businessLocal, "classNotFound.businessLocal"));
    }
    final List<Class> businessRemotes = new ArrayList<>();
    for (final String businessRemote : bean.businessRemote) {
        businessRemotes.add(loadClass(businessRemote, "classNotFound.businessRemote"));
    }
    Class serviceEndpoint = null;
    if (BeanType.STATELESS == ejbType || BeanType.SINGLETON == ejbType) {
        if (bean.serviceEndpoint != null) {
            serviceEndpoint = loadClass(bean.serviceEndpoint, "classNotFound.sei");
        }
    }
    Class primaryKey = null;
    if (ejbType.isEntity() && ((EntityBeanInfo) bean).primKeyClass != null) {
        final String className = ((EntityBeanInfo) bean).primKeyClass;
        primaryKey = loadClass(className, "classNotFound.primaryKey");
    }
    final String transactionType = bean.transactionType;
    // determine the injections
    final InjectionBuilder injectionBuilder = new InjectionBuilder(moduleContext.getClassLoader());
    final List<Injection> injections = injectionBuilder.buildInjections(bean.jndiEnc);
    final Set<Class<?>> relevantClasses = new HashSet<>();
    Class c = ejbClass;
    do {
        relevantClasses.add(c);
        c = c.getSuperclass();
    } while (c != null && c != Object.class);
    for (final Injection injection : moduleInjections) {
        if (relevantClasses.contains(injection.getTarget())) {
            injections.add(injection);
        }
    }
    // build the enc
    final JndiEncBuilder jndiEncBuilder = new JndiEncBuilder(bean.jndiEnc, injections, transactionType, moduleContext.getId(), null, moduleContext.getUniqueId(), moduleContext.getClassLoader(), moduleContext.getAppContext() == null ? moduleContext.getProperties() : moduleContext.getAppContext().getProperties());
    final Context compJndiContext = jndiEncBuilder.build(JndiEncBuilder.JndiScope.comp);
    bind(compJndiContext, "module", moduleContext.getModuleJndiContext());
    bind(compJndiContext, "app", moduleContext.getAppContext().getAppJndiContext());
    bind(compJndiContext, "global", moduleContext.getAppContext().getGlobalJndiContext());
    final BeanContext deployment;
    if (BeanType.MESSAGE_DRIVEN != ejbType) {
        deployment = new BeanContext(bean.ejbDeploymentId, compJndiContext, moduleContext, ejbClass, home, remote, localhome, local, proxy, serviceEndpoint, businessLocals, businessRemotes, primaryKey, ejbType, bean.localbean && ejbType.isSession(), bean.passivable);
        if (bean instanceof ManagedBeanInfo) {
            deployment.setHidden(((ManagedBeanInfo) bean).hidden);
        }
    } else {
        final MessageDrivenBeanInfo messageDrivenBeanInfo = (MessageDrivenBeanInfo) bean;
        final Class mdbInterface = loadClass(messageDrivenBeanInfo.mdbInterface, "classNotFound.mdbInterface");
        deployment = new BeanContext(bean.ejbDeploymentId, compJndiContext, moduleContext, ejbClass, mdbInterface, messageDrivenBeanInfo.activationProperties);
        deployment.setDestinationId(messageDrivenBeanInfo.destinationId);
    }
    deployment.getProperties().putAll(bean.properties);
    deployment.setEjbName(bean.ejbName);
    deployment.setRunAs(bean.runAs);
    deployment.setRunAsUser(bean.runAsUser);
    deployment.getInjections().addAll(injections);
    // ejbTimeout
    deployment.setEjbTimeout(getTimeout(ejbClass, bean.timeoutMethod));
    if (bean.statefulTimeout != null) {
        deployment.setStatefulTimeout(new Duration(bean.statefulTimeout.time, TimeUnit.valueOf(bean.statefulTimeout.unit)));
    }
    if (bean instanceof StatefulBeanInfo) {
        final StatefulBeanInfo statefulBeanInfo = (StatefulBeanInfo) bean;
        for (final InitMethodInfo init : statefulBeanInfo.initMethods) {
            final Method beanMethod = MethodInfoUtil.toMethod(ejbClass, init.beanMethod);
            final List<Method> methods = new ArrayList<>();
            if (home != null) {
                methods.addAll(Arrays.asList(home.getMethods()));
            }
            if (localhome != null) {
                methods.addAll(Arrays.asList(localhome.getMethods()));
            }
            for (final Method homeMethod : methods) {
                if (init.createMethod != null && !init.createMethod.methodName.equals(homeMethod.getName())) {
                    continue;
                }
                if (!homeMethod.getName().startsWith("create")) {
                    continue;
                }
                if (paramsMatch(beanMethod, homeMethod)) {
                    deployment.mapMethods(homeMethod, beanMethod);
                }
            }
        }
        for (final RemoveMethodInfo removeMethod : statefulBeanInfo.removeMethods) {
            if (removeMethod.beanMethod.methodParams == null) {
                final MethodInfo methodInfo = new MethodInfo();
                methodInfo.methodName = removeMethod.beanMethod.methodName;
                methodInfo.methodParams = removeMethod.beanMethod.methodParams;
                methodInfo.className = removeMethod.beanMethod.className;
                final List<Method> methods = MethodInfoUtil.matchingMethods(methodInfo, ejbClass);
                for (final Method method : methods) {
                    deployment.getRemoveMethods().add(method);
                    deployment.setRetainIfExeption(method, removeMethod.retainIfException);
                }
            } else {
                final Method method = MethodInfoUtil.toMethod(ejbClass, removeMethod.beanMethod);
                deployment.getRemoveMethods().add(method);
                deployment.setRetainIfExeption(method, removeMethod.retainIfException);
            }
        }
        final Map<EntityManagerFactory, BeanContext.EntityManagerConfiguration> extendedEntityManagerFactories = new HashMap<>();
        for (final PersistenceContextReferenceInfo info : statefulBeanInfo.jndiEnc.persistenceContextRefs) {
            if (info.extended) {
                try {
                    final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
                    final Object o = containerSystem.getJNDIContext().lookup(PersistenceBuilder.getOpenEJBJndiName(info.unitId));
                    final EntityManagerFactory emf = EntityManagerFactory.class.cast(o);
                    extendedEntityManagerFactories.put(emf, new BeanContext.EntityManagerConfiguration(info.properties, JtaEntityManager.isJPA21(emf) && info.synchronizationType != null ? SynchronizationType.valueOf(info.synchronizationType) : null));
                } catch (final NamingException e) {
                    throw new OpenEJBException("PersistenceUnit '" + info.unitId + "' not found for EXTENDED ref '" + info.referenceName + "'");
                }
            }
        }
        deployment.setExtendedEntityManagerFactories(new Index<>(extendedEntityManagerFactories));
    }
    if (ejbType.isSession() || ejbType.isMessageDriven()) {
        deployment.setBeanManagedTransaction("Bean".equalsIgnoreCase(bean.transactionType));
    }
    if (ejbType.isSession()) {
        // Allow dependsOn to work for all session beans
        deployment.getDependsOn().addAll(bean.dependsOn);
    }
    if (ejbType == BeanType.SINGLETON) {
        deployment.setBeanManagedConcurrency("Bean".equalsIgnoreCase(bean.concurrencyType));
        deployment.setLoadOnStartup(bean.loadOnStartup);
    }
    if (ejbType.isEntity()) {
        final EntityBeanInfo entity = (EntityBeanInfo) bean;
        deployment.setCmp2(entity.cmpVersion == 2);
        deployment.setIsReentrant(entity.reentrant.equalsIgnoreCase("true"));
        if (ejbType == BeanType.CMP_ENTITY) {
            Class cmpImplClass = null;
            final String cmpImplClassName = CmpUtil.getCmpImplClassName(entity.abstractSchemaName, entity.ejbClass);
            cmpImplClass = loadClass(cmpImplClassName, "classNotFound.cmpImplClass");
            deployment.setCmpImplClass(cmpImplClass);
            deployment.setAbstractSchemaName(entity.abstractSchemaName);
            for (final QueryInfo query : entity.queries) {
                if (query.remoteResultType) {
                    final StringBuilder methodSignature = new StringBuilder();
                    methodSignature.append(query.method.methodName);
                    if (query.method.methodParams != null && !query.method.methodParams.isEmpty()) {
                        methodSignature.append('(');
                        boolean first = true;
                        for (final String methodParam : query.method.methodParams) {
                            if (!first) {
                                methodSignature.append(",");
                            }
                            methodSignature.append(methodParam);
                            first = false;
                        }
                        methodSignature.append(')');
                    }
                    deployment.setRemoteQueryResults(methodSignature.toString());
                }
            }
            if (entity.primKeyField != null) {
                deployment.setPrimaryKeyField(entity.primKeyField);
            }
        }
    }
    deployment.createMethodMap();
    // we could directly check the matching bean method.
    if (ejbType == BeanType.STATELESS || ejbType == BeanType.SINGLETON || ejbType == BeanType.STATEFUL) {
        for (final NamedMethodInfo methodInfo : bean.asynchronous) {
            final Method method = MethodInfoUtil.toMethod(ejbClass, methodInfo);
            deployment.getMethodContext(deployment.getMatchingBeanMethod(method)).setAsynchronous(true);
        }
        for (final String className : bean.asynchronousClasses) {
            deployment.getAsynchronousClasses().add(loadClass(className, "classNotFound.ejbClass"));
        }
        deployment.createAsynchronousMethodSet();
    }
    for (final SecurityRoleReferenceInfo securityRoleReference : bean.securityRoleReferences) {
        deployment.addSecurityRoleReference(securityRoleReference.roleName, securityRoleReference.roleLink);
    }
    return deployment;
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) OpenEJBException(org.apache.openejb.OpenEJBException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NamingException(javax.naming.NamingException) HashSet(java.util.HashSet) BeanContext(org.apache.openejb.BeanContext) ModuleContext(org.apache.openejb.ModuleContext) Context(javax.naming.Context) Duration(org.apache.openejb.util.Duration) Injection(org.apache.openejb.Injection) Method(java.lang.reflect.Method) BeanContext(org.apache.openejb.BeanContext) EntityManagerFactory(javax.persistence.EntityManagerFactory) TimedObject(javax.ejb.TimedObject)

Example 4 with ModuleContext

use of org.apache.openejb.ModuleContext in project tomee by apache.

the class ModulePropertiesTest method assertContexts.

private void assertContexts(final ContainerSystem containerSystem) {
    final BeanContext beanContext = containerSystem.getBeanContext("WidgetBean");
    final ModuleContext moduleContext = beanContext.getModuleContext();
    final AppContext appContext = moduleContext.getAppContext();
    {
        // Assert as Properties
        // ModuleContext should have color property
        assertProperty(moduleContext.getProperties(), "color", "orange");
        // BeanContext and AppContext should not
        assertNoProperty(beanContext.getProperties(), "color");
        assertNoProperty(appContext.getProperties(), "color");
        // Try all the above again with mixed case
        assertProperty(moduleContext.getProperties(), "coLOr", "orange");
        assertNoProperty(beanContext.getProperties(), "coLOr");
        assertNoProperty(appContext.getProperties(), "coLOr");
    }
    {
        // Assert as Options
        // ModuleContext should have color option
        assertOption(moduleContext.getOptions(), "color", "orange");
        // BeanContext should inherit ModuleContext color
        assertOption(beanContext.getOptions(), "color", "orange");
        // AppContext should remain unpolluted
        assertNoOption(appContext.getOptions(), "color");
        // Try all the above again using mixed case
        assertOption(moduleContext.getOptions(), "coLoR", "orange");
        assertOption(beanContext.getOptions(), "coLoR", "orange");
        assertNoOption(appContext.getOptions(), "coLoR");
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) AppContext(org.apache.openejb.AppContext) ModuleContext(org.apache.openejb.ModuleContext)

Example 5 with ModuleContext

use of org.apache.openejb.ModuleContext in project tomee by apache.

the class BeanPropertiesTest method assertContexts.

private void assertContexts(final ContainerSystem containerSystem) {
    final BeanContext beanContext = containerSystem.getBeanContext("WidgetBean");
    final ModuleContext moduleContext = beanContext.getModuleContext();
    final AppContext appContext = moduleContext.getAppContext();
    {
        // Assert as Properties
        // BeanContext should have color property
        assertProperty(beanContext.getProperties(), "color", "orange");
        // ModuleContext and AppContext should not
        assertNoProperty(moduleContext.getProperties(), "color");
        assertNoProperty(appContext.getProperties(), "color");
        // Try all the above again with mixed case
        assertProperty(beanContext.getProperties(), "coLOr", "orange");
        assertNoProperty(moduleContext.getProperties(), "coLOr");
        assertNoProperty(appContext.getProperties(), "coLOr");
    }
    {
        // Assert as Options
        // ModuleContext should have color option
        assertOption(beanContext.getOptions(), "color", "orange");
        // AppContext and ModuleContext should remain unpolluted
        assertNoOption(moduleContext.getOptions(), "color");
        assertNoOption(appContext.getOptions(), "color");
        // Try all the above again using mixed case
        assertOption(beanContext.getOptions(), "coLoR", "orange");
        assertNoOption(moduleContext.getOptions(), "coLoR");
        assertNoOption(appContext.getOptions(), "coLoR");
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) AppContext(org.apache.openejb.AppContext) ModuleContext(org.apache.openejb.ModuleContext)

Aggregations

ModuleContext (org.apache.openejb.ModuleContext)15 BeanContext (org.apache.openejb.BeanContext)14 AppContext (org.apache.openejb.AppContext)13 Context (javax.naming.Context)4 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 TimerService (javax.ejb.TimerService)2 Injection (org.apache.openejb.Injection)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 IvmContext (org.apache.openejb.core.ivm.naming.IvmContext)2 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 Properties (java.util.Properties)1 EJBContext (javax.ejb.EJBContext)1 TimedObject (javax.ejb.TimedObject)1 Timer (javax.ejb.Timer)1 InitialContext (javax.naming.InitialContext)1 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)1 NamingException (javax.naming.NamingException)1