Search in sources :

Example 11 with EJBContext

use of javax.ejb.EJBContext in project tomee by apache.

the class JavaLookupScopesTest method test.

public void test() throws Exception {
    final AppContext app;
    {
        final ConfigurationFactory config = new ConfigurationFactory();
        final Assembler assembler = new Assembler();
        assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
        assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
        // Setup the descriptor information
        final EjbJar ejbJar = new EjbJar("testmodule");
        ejbJar.addEnterpriseBean(new SingletonBean(Bean.class));
        // Deploy the bean a second time to simulate situations
        // where the same java:module java:app java:global names
        // are re-declared in a compatible way
        ejbJar.addEnterpriseBean(new SingletonBean("Other", Bean.class));
        final EjbModule ejbModule = new EjbModule(ejbJar);
        final AppModule module = new AppModule(ejbModule);
        app = assembler.createApplication(config.configureApplication(module));
    }
    final BeanContext bean = app.getBeanContexts().get(0);
    final ModuleContext module = bean.getModuleContext();
    {
        // app context lookups
        final Context context = app.getAppJndiContext();
        assertTrue(context.lookup("app") instanceof Context);
        assertTrue(context.lookup("app/AppName") instanceof String);
        assertTrue(context.lookup("app/green") instanceof DataSource);
        assertTrue(context.lookup("app/testmodule") instanceof Context);
        assertTrue(context.lookup("app/testmodule/Bean") instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Other") instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Other!" + Bean.class.getName()) instanceof Bean);
        assertEquals("testmodule", context.lookup("app/AppName"));
    }
    {
        // module context lookups
        final Context context = module.getModuleJndiContext();
        assertTrue(context.lookup("module") instanceof Context);
        assertTrue(context.lookup("module/ModuleName") instanceof String);
        assertTrue(context.lookup("module/blue") instanceof DataSource);
        assertTrue(context.lookup("module/Bean") instanceof Bean);
        assertTrue(context.lookup("module/Bean!" + Bean.class.getName()) instanceof Bean);
        assertEquals("testmodule", context.lookup("module/ModuleName"));
    // TODO the Module JNDI context *should* be able to see the App context
    }
    {
        final Context context = bean.getJndiContext();
        assertTrue(context.lookup("comp") instanceof Context);
        assertTrue(context.lookup("comp/EJBContext") instanceof EJBContext);
        assertTrue(context.lookup("comp/TimerService") instanceof TimerService);
        assertTrue(context.lookup("comp/TransactionManager") instanceof TransactionManager);
        assertTrue(context.lookup("comp/TransactionSynchronizationRegistry") instanceof TransactionSynchronizationRegistry);
        assertTrue(context.lookup("comp/WebServiceContext") instanceof WebServiceContext);
        assertTrue(context.lookup("comp/env") instanceof Context);
        assertTrue(context.lookup("comp/env") instanceof Context);
        assertTrue(context.lookup("comp/env/orange") instanceof DataSource);
        assertTrue(context.lookup("comp/env/" + Bean.class.getName()) instanceof Context);
        assertTrue(context.lookup("comp/env/" + Bean.class.getName() + "/red") instanceof DataSource);
        assertTrue(context.lookup("module") instanceof Context);
        assertTrue(context.lookup("module/ModuleName") instanceof String);
        assertTrue(context.lookup("module/blue") instanceof DataSource);
        assertTrue(context.lookup("module/Bean") instanceof Bean);
        assertTrue(context.lookup("module/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("module/Other") instanceof Bean);
        assertTrue(context.lookup("module/Other!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("app") instanceof Context);
        assertTrue(context.lookup("app/AppName") instanceof String);
        assertTrue(context.lookup("app/green") instanceof DataSource);
        assertTrue(context.lookup("app/testmodule") instanceof Context);
        assertTrue(context.lookup("app/testmodule/Bean") instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Other") instanceof Bean);
        assertTrue(context.lookup("app/testmodule/Other!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("global") instanceof Context);
        assertTrue(context.lookup("global/yellow") instanceof DataSource);
        assertTrue(context.lookup("global/testmodule") instanceof Context);
        assertTrue(context.lookup("global/testmodule/Bean") instanceof Bean);
        assertTrue(context.lookup("global/testmodule/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("global/testmodule/Other") instanceof Bean);
        assertTrue(context.lookup("global/testmodule/Other!" + Bean.class.getName()) instanceof Bean);
        assertEquals("testmodule", context.lookup("app/AppName"));
        assertEquals("testmodule", context.lookup("module/ModuleName"));
    }
}
Also used : InitialContext(javax.naming.InitialContext) BeanContext(org.apache.openejb.BeanContext) ModuleContext(org.apache.openejb.ModuleContext) WebServiceContext(javax.xml.ws.WebServiceContext) AppContext(org.apache.openejb.AppContext) Context(javax.naming.Context) EJBContext(javax.ejb.EJBContext) EJBContext(javax.ejb.EJBContext) AppModule(org.apache.openejb.config.AppModule) AppContext(org.apache.openejb.AppContext) EjbModule(org.apache.openejb.config.EjbModule) WebServiceContext(javax.xml.ws.WebServiceContext) TimerService(javax.ejb.TimerService) DataSource(javax.sql.DataSource) SingletonBean(org.apache.openejb.jee.SingletonBean) BeanContext(org.apache.openejb.BeanContext) SingletonBean(org.apache.openejb.jee.SingletonBean) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) ModuleContext(org.apache.openejb.ModuleContext) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJar(org.apache.openejb.jee.EjbJar)

Example 12 with EJBContext

use of javax.ejb.EJBContext in project tomee by apache.

the class StatefulContainer method deploy.

@Override
public synchronized void deploy(final BeanContext beanContext) throws OpenEJBException {
    final Map<Method, MethodType> methods = getLifecycleMethodsOfInterface(beanContext);
    deploymentsById.put(beanContext.getDeploymentID(), beanContext);
    beanContext.setContainer(this);
    final Data data = new Data(new Index<Method, MethodType>(methods));
    beanContext.setContainerData(data);
    // Create stats interceptor
    if (StatsInterceptor.isStatsActivated()) {
        final StatsInterceptor stats = new StatsInterceptor(beanContext.getBeanClass());
        beanContext.addFirstSystemInterceptor(stats);
        final MBeanServer server = LocalMBeanServer.get();
        final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
        jmxName.set("J2EEServer", "openejb");
        jmxName.set("J2EEApplication", null);
        jmxName.set("EJBModule", beanContext.getModuleID());
        jmxName.set("StatefulSessionBean", beanContext.getEjbName());
        jmxName.set("j2eeType", "");
        jmxName.set("name", beanContext.getEjbName());
        // register the invocation stats interceptor
        try {
            final ObjectName objectName = jmxName.set("j2eeType", "Invocations").build();
            if (server.isRegistered(objectName)) {
                server.unregisterMBean(objectName);
            }
            server.registerMBean(new ManagedMBean(stats), objectName);
            data.jmxNames.add(objectName);
        } catch (final Exception e) {
            logger.error("Unable to register MBean ", e);
        }
    }
    try {
        final Context context = beanContext.getJndiEnc();
        context.bind("comp/EJBContext", sessionContext);
    } catch (final NamingException e) {
        throw new OpenEJBException("Failed to bind EJBContext", e);
    }
    beanContext.set(EJBContext.class, this.sessionContext);
}
Also used : SessionContext(javax.ejb.SessionContext) CurrentCreationalContext(org.apache.openejb.cdi.CurrentCreationalContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) EJBContext(javax.ejb.EJBContext) InstanceContext(org.apache.openejb.core.InstanceContext) ThreadContext(org.apache.openejb.core.ThreadContext) OpenEJBException(org.apache.openejb.OpenEJBException) StatsInterceptor(org.apache.openejb.monitoring.StatsInterceptor) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) Method(java.lang.reflect.Method) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) OpenEJBException(org.apache.openejb.OpenEJBException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) SystemException(org.apache.openejb.SystemException) NoSuchObjectException(java.rmi.NoSuchObjectException) EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ObjectName(javax.management.ObjectName) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) NamingException(javax.naming.NamingException) ManagedMBean(org.apache.openejb.monitoring.ManagedMBean) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer)

Example 13 with EJBContext

use of javax.ejb.EJBContext in project tomee by apache.

the class StatelessInstanceManager method deploy.

@SuppressWarnings("unchecked")
public void deploy(final BeanContext beanContext) throws OpenEJBException {
    final Options options = new Options(beanContext.getProperties());
    final Duration accessTimeout = getDuration(options, "AccessTimeout", // default timeout
    getDuration(options, "Timeout", this.accessTimeout, TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
    final Duration closeTimeout = getDuration(options, "CloseTimeout", this.closeTimeout, TimeUnit.MINUTES);
    final ObjectRecipe recipe = PassthroughFactory.recipe(new Pool.Builder(poolBuilder));
    recipe.allow(Option.CASE_INSENSITIVE_FACTORY);
    recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
    recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
    recipe.setAllProperties(beanContext.getProperties());
    final Pool.Builder builder = (Pool.Builder) recipe.create();
    setDefault(builder.getMaxAge(), TimeUnit.HOURS);
    setDefault(builder.getIdleTimeout(), TimeUnit.MINUTES);
    setDefault(builder.getInterval(), TimeUnit.MINUTES);
    final StatelessSupplier supplier = new StatelessSupplier(beanContext);
    builder.setSupplier(supplier);
    builder.setExecutor(executor);
    builder.setScheduledExecutor(scheduledExecutor);
    final Data data = new Data(builder.build(), accessTimeout, closeTimeout);
    beanContext.setContainerData(data);
    beanContext.set(EJBContext.class, data.sessionContext);
    try {
        final Context context = beanContext.getJndiEnc();
        context.bind("comp/EJBContext", data.sessionContext);
        context.bind("comp/WebServiceContext", new EjbWsContext(data.sessionContext));
        context.bind("comp/TimerService", new TimerServiceWrapper());
    } catch (final NamingException e) {
        throw new OpenEJBException("Failed to bind EJBContext/WebServiceContext/TimerService", e);
    }
    final int min = builder.getMin();
    final long maxAge = builder.getMaxAge().getTime(TimeUnit.MILLISECONDS);
    final double maxAgeOffset = builder.getMaxAgeOffset();
    final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
    jmxName.set("J2EEServer", "openejb");
    jmxName.set("J2EEApplication", null);
    jmxName.set("EJBModule", beanContext.getModuleID());
    jmxName.set("StatelessSessionBean", beanContext.getEjbName());
    jmxName.set("name", beanContext.getEjbName());
    final MBeanServer server = LocalMBeanServer.get();
    // Create stats interceptor
    if (StatsInterceptor.isStatsActivated()) {
        StatsInterceptor stats = null;
        for (final InterceptorInstance interceptor : beanContext.getUserAndSystemInterceptors()) {
            if (interceptor.getInterceptor() instanceof StatsInterceptor) {
                stats = (StatsInterceptor) interceptor.getInterceptor();
            }
        }
        if (stats == null) {
            // normally useless
            stats = new StatsInterceptor(beanContext.getBeanClass());
            beanContext.addFirstSystemInterceptor(stats);
        }
        // register the invocation stats interceptor
        try {
            final ObjectName objectName = jmxName.set("j2eeType", "Invocations").build();
            if (server.isRegistered(objectName)) {
                server.unregisterMBean(objectName);
            }
            server.registerMBean(new ManagedMBean(stats), objectName);
            data.add(objectName);
        } catch (final Exception e) {
            logger.error("Unable to register MBean ", e);
        }
    }
    // register the pool
    try {
        final ObjectName objectName = jmxName.set("j2eeType", "Pool").build();
        if (server.isRegistered(objectName)) {
            server.unregisterMBean(objectName);
        }
        server.registerMBean(new ManagedMBean(data.pool), objectName);
        data.add(objectName);
    } catch (final Exception e) {
        logger.error("Unable to register MBean ", e);
    }
    // Finally, fill the pool and start it
    if (!options.get("BackgroundStartup", false) && min > 0) {
        final ExecutorService es = Executors.newFixedThreadPool(min);
        for (int i = 0; i < min; i++) {
            es.submit(new InstanceCreatorRunnable(maxAge, i, min, maxAgeOffset, data, supplier));
        }
        es.shutdown();
        try {
            es.awaitTermination(5, TimeUnit.MINUTES);
        } catch (final InterruptedException e) {
            logger.error("can't fill the stateless pool", e);
        }
    }
    data.getPool().start();
}
Also used : Options(org.apache.openejb.loader.Options) OpenEJBException(org.apache.openejb.OpenEJBException) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) TimerServiceWrapper(org.apache.openejb.core.timer.TimerServiceWrapper) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Pool(org.apache.openejb.util.Pool) NamingException(javax.naming.NamingException) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) SessionContext(javax.ejb.SessionContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) EJBContext(javax.ejb.EJBContext) InstanceContext(org.apache.openejb.core.InstanceContext) ThreadContext(org.apache.openejb.core.ThreadContext) StatsInterceptor(org.apache.openejb.monitoring.StatsInterceptor) Duration(org.apache.openejb.util.Duration) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorInstance(org.apache.openejb.core.interceptor.InterceptorInstance) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) SystemException(org.apache.openejb.SystemException) ApplicationException(org.apache.openejb.ApplicationException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ManagedMBean(org.apache.openejb.monitoring.ManagedMBean)

Example 14 with EJBContext

use of javax.ejb.EJBContext in project tomee by apache.

the class ContextLookupStatelessBean method lookupSessionContext.

public void lookupSessionContext() throws TestFailureException {
    try {
        try {
            final InitialContext ctx = new InitialContext();
            Assert.assertNotNull("The InitialContext is null", ctx);
            // lookup in enc
            final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
            Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
            // lookup using global name
            final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
            Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
            // verify context was set via legacy set method
            Assert.assertNotNull("The SessionContext is null from setter method", ejbContext);
        } catch (final Exception e) {
            Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
        }
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
Also used : EJBContext(javax.ejb.EJBContext) SessionContext(javax.ejb.SessionContext) TestFailureException(org.apache.openejb.test.TestFailureException) AssertionFailedError(junit.framework.AssertionFailedError) InitialContext(javax.naming.InitialContext) JMSException(javax.jms.JMSException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) TestFailureException(org.apache.openejb.test.TestFailureException)

Example 15 with EJBContext

use of javax.ejb.EJBContext in project tomee by apache.

the class EncStatelessBean method lookupSessionContext.

public void lookupSessionContext() throws TestFailureException {
    try {
        try {
            final InitialContext ctx = new InitialContext();
            Assert.assertNotNull("The InitialContext is null", ctx);
            // lookup in enc
            final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
            Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
            // lookup using global name
            final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
            Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
            // verify context was set via legacy set method
            Assert.assertNotNull("The SessionContext is null from setter method", ejbContext);
        } catch (final Exception e) {
            Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
        }
    } catch (final AssertionFailedError afe) {
        throw new TestFailureException(afe);
    }
}
Also used : EJBContext(javax.ejb.EJBContext) SessionContext(javax.ejb.SessionContext) TestFailureException(org.apache.openejb.test.TestFailureException) AssertionFailedError(junit.framework.AssertionFailedError) InitialContext(javax.naming.InitialContext) JMSException(javax.jms.JMSException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) TestFailureException(org.apache.openejb.test.TestFailureException)

Aggregations

EJBContext (javax.ejb.EJBContext)21 InitialContext (javax.naming.InitialContext)15 EJBException (javax.ejb.EJBException)13 SessionContext (javax.ejb.SessionContext)12 AssertionFailedError (junit.framework.AssertionFailedError)12 TestFailureException (org.apache.openejb.test.TestFailureException)12 RemoteException (java.rmi.RemoteException)11 JMSException (javax.jms.JMSException)10 NamingException (javax.naming.NamingException)10 Context (javax.naming.Context)6 BeanContext (org.apache.openejb.BeanContext)5 OpenEJBException (org.apache.openejb.OpenEJBException)5 MBeanServer (javax.management.MBeanServer)4 ObjectName (javax.management.ObjectName)4 ApplicationException (org.apache.openejb.ApplicationException)4 SystemException (org.apache.openejb.SystemException)4 InstanceContext (org.apache.openejb.core.InstanceContext)4 ThreadContext (org.apache.openejb.core.ThreadContext)4 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)4 LocalMBeanServer (org.apache.openejb.monitoring.LocalMBeanServer)4