Search in sources :

Example 36 with AppContext

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

the class ServiceProviderInheritanceTest method test.

public void test() throws Exception {
    SystemInstance.get().setComponent(ProviderManager.class, new ProviderManager(new ProviderLoader() {

        final ProviderLoader loader = new ServiceJarXmlLoader();

        @Override
        public ServiceProvider load(final ID id) {
            {
                // try the regular loader
                final ServiceProvider provider = loader.load(id);
                if (provider != null)
                    return provider;
            }
            if ("color".equalsIgnoreCase(id.getName())) {
                final ServiceProvider color = new ServiceProvider(Color.class, "Color", "Resource");
                color.getProperties().setProperty("red", "0");
                color.getProperties().setProperty("green", "0");
                color.getProperties().setProperty("blue", "0");
                color.getTypes().add(Color.class.getName());
                return color;
            }
            if ("red".equalsIgnoreCase(id.getName())) {
                final ServiceProvider red = new ServiceProvider();
                red.setId("Red");
                red.setParent("Color");
                red.getProperties().setProperty("red", "255");
                return red;
            }
            if ("orange".equalsIgnoreCase(id.getName())) {
                final ServiceProvider orange = new ServiceProvider();
                orange.setId("Orange");
                orange.setParent("Red");
                orange.getProperties().setProperty("green", "200");
                return orange;
            }
            throw new IllegalStateException(id.toString());
        }

        @Override
        public List<ServiceProvider> load(final String namespace) {
            final List<ServiceProvider> list = loader.load(namespace);
            list.add(load(new ID(namespace, "color")));
            list.add(load(new ID(namespace, "red")));
            list.add(load(new ID(namespace, "orange")));
            return list;
        }
    }));
    final ConfigurationFactory factory = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createSecurityService(factory.configureService(SecurityServiceInfo.class));
    assembler.createTransactionManager(factory.configureService(TransactionServiceInfo.class));
    {
        final Resource orange = new Resource("Orange", Color.class.getName(), "Orange");
        final ResourceInfo resourceInfo = factory.configureService(orange, ResourceInfo.class);
        assertEquals(Color.class.getName(), resourceInfo.className);
        assertEquals("Orange", resourceInfo.id);
        assertEquals(3, resourceInfo.properties.size());
        assertEquals("255", resourceInfo.properties.get("red"));
        assertEquals("200", resourceInfo.properties.get("green"));
        assertEquals("0", resourceInfo.properties.get("blue"));
        assembler.createResource(resourceInfo);
    }
    {
        final EjbJar ejbJar = new EjbJar();
        ejbJar.addEnterpriseBean(new SingletonBean(MyBean.class));
        final AppContext application = assembler.createApplication(factory.configureApplication(new EjbModule(ejbJar)));
        final MyBean myBean = (MyBean) application.getBeanContexts().get(0).getBusinessLocalBeanHome().create();
        final Color color = myBean.getColor();
        assertNotNull(color);
        assertEquals(255, color.getRed());
        assertEquals(200, color.getGreen());
        assertEquals(0, color.getBlue());
    }
}
Also used : ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) AppContext(org.apache.openejb.AppContext) EjbModule(org.apache.openejb.config.EjbModule) ProviderLoader(org.apache.openejb.config.provider.ProviderLoader) SingletonBean(org.apache.openejb.jee.SingletonBean) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) ProviderManager(org.apache.openejb.config.provider.ProviderManager) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) ID(org.apache.openejb.config.provider.ID) Assembler(org.apache.openejb.assembler.classic.Assembler) ServiceJarXmlLoader(org.apache.openejb.config.provider.ServiceJarXmlLoader) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) EjbJar(org.apache.openejb.jee.EjbJar)

Example 37 with AppContext

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

the class InheritedAppExceptionTest method testRollback.

@Test
public void testRollback() throws Exception {
    SystemInstance.init(new Properties());
    final BeanContext cdi = new BeanContext("foo", null, new ModuleContext("foo", null, "bar", new AppContext("foo", SystemInstance.get(), null, null, null, false), null, null), Object.class, null, new HashMap<String, String>());
    cdi.addApplicationException(AE1.class, true, true);
    cdi.addApplicationException(AE3.class, true, false);
    cdi.addApplicationException(AE6.class, false, true);
    assertEquals(ExceptionType.APPLICATION_ROLLBACK, cdi.getExceptionType(new AE1()));
    assertEquals(ExceptionType.APPLICATION_ROLLBACK, cdi.getExceptionType(new AE2()));
    assertEquals(ExceptionType.APPLICATION_ROLLBACK, cdi.getExceptionType(new AE3()));
    assertEquals(ExceptionType.SYSTEM, cdi.getExceptionType(new AE4()));
    assertEquals(ExceptionType.SYSTEM, cdi.getExceptionType(new AE5()));
    assertEquals(ExceptionType.APPLICATION, cdi.getExceptionType(new AE6()));
    assertEquals(ExceptionType.APPLICATION, cdi.getExceptionType(new AE7()));
}
Also used : AppContext(org.apache.openejb.AppContext) Properties(java.util.Properties) BeanContext(org.apache.openejb.BeanContext) ModuleContext(org.apache.openejb.ModuleContext) Test(org.junit.Test)

Example 38 with AppContext

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

the class GlobalLookupScopesTest method _test.

// TODO  We need this for https://issues.apache.org/jira/browse/OPENEJB-1140
public void _test() throws Exception {
    SystemInstance.get().setProperty("openejb.deploymentId.format", "{appId}/{moduleId}/{ejbName}");
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final AppContext circleApp = createApp("circle", config, assembler);
    final AppContext squareApp = createApp("square", config, assembler);
    {
        final BeanContext bean = squareApp.getBeanContexts().get(0);
        final Context context = bean.getJndiContext();
        assertTrue(context.lookup("global/square") instanceof Context);
        assertTrue(context.lookup("global/square/Bean") instanceof Bean);
        assertTrue(context.lookup("global/square/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("global/square/Other") instanceof Bean);
        assertTrue(context.lookup("global/square/Other!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("global/circle") instanceof Context);
        assertTrue(context.lookup("global/circle/Bean") instanceof Bean);
        assertTrue(context.lookup("global/circle/Bean!" + Bean.class.getName()) instanceof Bean);
        assertTrue(context.lookup("global/circle/Other") instanceof Bean);
        assertTrue(context.lookup("global/circle/Other!" + Bean.class.getName()) instanceof Bean);
    }
}
Also used : InitialContext(javax.naming.InitialContext) BeanContext(org.apache.openejb.BeanContext) AppContext(org.apache.openejb.AppContext) Context(javax.naming.Context) BeanContext(org.apache.openejb.BeanContext) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) AppContext(org.apache.openejb.AppContext) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) SingletonBean(org.apache.openejb.jee.SingletonBean)

Example 39 with AppContext

use of org.apache.openejb.AppContext 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 40 with AppContext

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

the class LightweightWebAppBuilderListenerExtractor method findByTypeForContext.

public static <T> Collection<T> findByTypeForContext(final String context, final Class<T> type) {
    final WebAppBuilder builder = SystemInstance.get().getComponent(WebAppBuilder.class);
    if (!LightweightWebAppBuilder.class.isInstance(builder)) {
        return Collections.emptyList();
    }
    for (final AppContext app : SystemInstance.get().getComponent(ContainerSystem.class).getAppContexts()) {
        for (final WebContext web : app.getWebContexts()) {
            if (web.getContextRoot().replace("/", "").equals(context.replace("/", ""))) {
                final Collection<Object> potentials = LightweightWebAppBuilder.class.cast(builder).listenersFor(web.getContextRoot());
                if (potentials == null) {
                    return Collections.emptyList();
                }
                final Collection<T> filtered = new ArrayList<>(potentials.size());
                for (final Object o : potentials) {
                    if (type.isInstance(o)) {
                        filtered.add(type.cast(o));
                    }
                }
                return filtered;
            }
        }
    }
    return Collections.emptyList();
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) WebContext(org.apache.openejb.core.WebContext) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) AppContext(org.apache.openejb.AppContext) ArrayList(java.util.ArrayList) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder)

Aggregations

AppContext (org.apache.openejb.AppContext)44 BeanContext (org.apache.openejb.BeanContext)23 WebContext (org.apache.openejb.core.WebContext)17 ContainerSystem (org.apache.openejb.spi.ContainerSystem)16 ModuleContext (org.apache.openejb.ModuleContext)11 Context (javax.naming.Context)9 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)9 Assembler (org.apache.openejb.assembler.classic.Assembler)9 ArrayList (java.util.ArrayList)8 WebBeansContext (org.apache.webbeans.config.WebBeansContext)8 InitialContext (javax.naming.InitialContext)7 NamingException (javax.naming.NamingException)7 ServletContext (javax.servlet.ServletContext)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 Properties (java.util.Properties)6 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)6 OpenEJBException (org.apache.openejb.OpenEJBException)5 AppInfo (org.apache.openejb.assembler.classic.AppInfo)5 ThreadContext (org.apache.openejb.core.ThreadContext)5