Search in sources :

Example 21 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry 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 22 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry in project tomee by apache.

the class JavaLookupTest method test.

public void test() throws Exception {
    final Assembler assembler = new Assembler();
    final ConfigurationFactory config = new ConfigurationFactory();
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    final InitialContext context = new InitialContext();
    assertTrue(context.lookup("java:openejb/TransactionManager") instanceof TransactionManager);
    assertTrue(context.lookup("java:comp/TransactionManager") instanceof TransactionManager);
    assertTrue(context.lookup("java:comp/UserTransaction") instanceof UserTransaction);
    assertTrue(context.lookup("java:comp/TransactionSynchronizationRegistry") instanceof TransactionSynchronizationRegistry);
}
Also used : UserTransaction(javax.transaction.UserTransaction) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) InitialContext(javax.naming.InitialContext)

Example 23 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry in project tomee by apache.

the class TomcatJndiBuilder method mergeJava.

public static void mergeJava(final StandardContext standardContext) {
    final ContainerSystem cs = SystemInstance.get().getComponent(ContainerSystem.class);
    final String name = standardContext.getNamingContextListener().getName();
    final Object namingToken = standardContext.getNamingToken();
    ContextAccessController.setWritable(name, namingToken);
    Context root = null;
    try {
        root = ContextBindings.getClassLoader();
    } catch (final NamingException ignored) {
    // shouldn't occur
    // no-op
    }
    // classical deployment - needed because can be overriden through META-INF/context.xml
    final String hostname = org.apache.tomee.catalina.Contexts.getHostname(standardContext);
    String path = standardContext.findParameter(TomcatWebAppBuilder.OPENEJB_WEBAPP_MODULE_ID);
    if (path == null) {
        // standardContext not created by OpenEJB
        path = hostname;
        if (standardContext.getPath().startsWith("/")) {
            path += standardContext.getPath();
        } else {
            path += "/" + standardContext.getPath();
        }
    }
    WebContext webContext = cs.getWebContextByHost(path, hostname);
    if (webContext == null) {
        // tomee-embedded deployment
        webContext = cs.getWebContextByHost(standardContext.getPath().replaceFirst("/", ""), hostname);
        if (webContext == null) {
            webContext = cs.getWebContextByHost(standardContext.getPath(), hostname);
        }
    }
    final TomcatWebAppBuilder builder = (TomcatWebAppBuilder) SystemInstance.get().getComponent(WebAppBuilder.class);
    TomcatWebAppBuilder.ContextInfo contextInfo = null;
    if (builder != null) {
        contextInfo = builder.getContextInfo(standardContext);
        if (webContext == null && contextInfo != null && contextInfo.appInfo != null) {
            // can happen if deployed from apps/
            for (final WebAppInfo webAppInfo : contextInfo.appInfo.webApps) {
                if (webAppInfo.path != null && webAppInfo.path.replace(File.separatorChar, '/').equals(standardContext.getDocBase())) {
                    webContext = cs.getWebContextByHost(webAppInfo.moduleId, hostname);
                    if (webContext != null) {
                        break;
                    }
                }
            }
        }
    }
    Collection<String> ignoreNames = null;
    if (contextInfo != null) {
        ignoreNames = contextInfo.resourceNames;
    }
    if (webContext != null && webContext.getBindings() != null && root != null) {
        for (final Map.Entry<String, Object> entry : webContext.getBindings().entrySet()) {
            try {
                final String key = entry.getKey();
                if (key.startsWith("global/")) {
                    // will be done later
                    continue;
                }
                final Object value = normalize(entry.getValue());
                if (ignoreNames.contains(removeCompEnv(key))) {
                    // keep tomcat resources
                    try {
                        // tomcat can get the reference but the bound value
                        // can come from OpenEJB (ejb-jar.xml for instance)
                        // so check the lookup can be resolved before skipping it
                        root.lookup(key);
                        continue;
                    } catch (final NameNotFoundException nnfe) {
                    // no-op: let it be rebound or bound
                    }
                }
                Contexts.createSubcontexts(root, key);
                root.rebind(key, value);
            } catch (final NamingException e) {
                e.printStackTrace();
            }
        }
    }
    // merge global: we bind our global to be able to get a real global context and not a local one (bindigns)
    if (root != null) {
        try {
            root.bind("global", SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext().lookup("global"));
        } catch (final NamingException e) {
            // bind only global bindings
            if (webContext != null && webContext.getBindings() != null) {
                for (final Map.Entry<String, Object> entry : webContext.getBindings().entrySet()) {
                    try {
                        final String key = entry.getKey();
                        if (!key.startsWith("global/")) {
                            continue;
                        }
                        final Object value = normalize(entry.getValue());
                        Contexts.createSubcontexts(root, key);
                        root.rebind(key, value);
                    } catch (final NamingException ignored) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    // try to force some binding which probably failed earlier (see in TomcatWebappBuilder)
    try {
        final Context comp = (Context) ContextBindings.getClassLoader().lookup("comp");
        final TransactionManager transactionManager = SystemInstance.get().getComponent(TransactionManager.class);
        comp.rebind("TransactionManager", transactionManager);
        // bind TransactionSynchronizationRegistry
        final TransactionSynchronizationRegistry synchronizationRegistry = SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class);
        comp.rebind("TransactionSynchronizationRegistry", synchronizationRegistry);
        try {
            comp.rebind("ORB", new SystemComponentReference(TomcatJndiBuilder.class.getClassLoader().loadClass("org.omg.CORBA.ORB")));
        } catch (final NoClassDefFoundError | ClassNotFoundException ncdfe) {
        // no-op
        }
        comp.rebind("HandleDelegate", new SystemComponentReference(HandleDelegate.class));
        if (webContext != null && webContext.getWebbeansContext() != null) {
            comp.rebind("BeanManager", new InjectableBeanManager(webContext.getWebbeansContext().getBeanManagerImpl()));
        } else if (contextInfo != null) {
            final WebBeansContext webBeansContext = cs.getAppContext(contextInfo.appInfo.appId).getWebBeansContext();
            if (webBeansContext != null) {
                // can be null if cdi is inhibited
                comp.rebind("BeanManager", new InjectableBeanManager(webBeansContext.getBeanManagerImpl()));
            }
        }
    } catch (final Exception ignored) {
        ignored.printStackTrace();
    // no-op
    }
    // merge comp/env in app if available (some users are doing it, JBoss habit?)
    try {
        final Context app = (Context) ContextBindings.getClassLoader().lookup("app");
        final Context ctx = (Context) ContextBindings.getClassLoader().lookup("comp/env");
        final List<Binding> bindings = Collections.list(ctx.listBindings("app"));
        for (final Binding binding : bindings) {
            try {
                app.bind(binding.getName(), binding.getObject());
            } catch (final NamingException ne) {
            // we don't want to rebind
            // no-op
            }
        }
    } catch (final Exception ne) {
    // no-op
    }
    ContextAccessController.setReadOnly(name);
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) WebContext(org.apache.openejb.core.WebContext) InjectableBeanManager(org.apache.webbeans.container.InjectableBeanManager) WebAppBuilder(org.apache.openejb.assembler.classic.WebAppBuilder) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) WebBeansContext(org.apache.webbeans.config.WebBeansContext) NamingException(javax.naming.NamingException) WebContext(org.apache.openejb.core.WebContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) StandardContext(org.apache.catalina.core.StandardContext) Context(javax.naming.Context) Binding(javax.naming.Binding) NameNotFoundException(javax.naming.NameNotFoundException) SystemComponentReference(org.apache.openejb.core.ivm.naming.SystemComponentReference) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) NameNotFoundException(javax.naming.NameNotFoundException) MalformedURLException(java.net.MalformedURLException) HandleDelegate(javax.ejb.spi.HandleDelegate) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) Map(java.util.Map)

Aggregations

TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)23 TransactionManager (javax.transaction.TransactionManager)14 InitialContext (javax.naming.InitialContext)6 Synchronization (javax.transaction.Synchronization)4 UserTransaction (javax.transaction.UserTransaction)4 ServiceName (org.jboss.msc.service.ServiceName)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 BeanManager (javax.enterprise.inject.spi.BeanManager)3 Context (javax.naming.Context)3 ValidatorFactory (javax.validation.ValidatorFactory)3 XATerminator (com.arjuna.ats.internal.jbossatx.jta.jca.XATerminator)2 TransactionManagerImple (com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple)2 TransactionSynchronizationRegistryImple (com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple)2 JTAEnvironmentBean (com.arjuna.ats.jta.common.JTAEnvironmentBean)2 LinkedList (java.util.LinkedList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 NamingException (javax.naming.NamingException)2 BeanContext (org.apache.openejb.BeanContext)2 OpenEJBException (org.apache.openejb.OpenEJBException)2