Search in sources :

Example 31 with TransactionSynchronizationRegistry

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

the class TransactionSynchronizationRegistryWrapper method getRegistry.

public TransactionSynchronizationRegistry getRegistry() {
    final SystemInstance system = SystemInstance.get();
    if (system != this.system) {
        this.registry = system.getComponent(TransactionSynchronizationRegistry.class);
        this.system = system;
    }
    return registry;
}
Also used : SystemInstance(org.apache.openejb.loader.SystemInstance) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry)

Example 32 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)

Example 33 with TransactionSynchronizationRegistry

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

the class JtaExtension method init.

void init(@Observes final AfterDeploymentValidation afterDeploymentValidation, final BeanManager bm) {
    if (!hasRegistry && hasManager) {
        afterDeploymentValidation.addDeploymentProblem(new IllegalStateException("You should produce a TransactionManager and TransactionSynchronizationRegistry"));
        return;
    }
    final TransactionManager manager = TransactionManager.class.cast(bm.getReference(bm.resolve(bm.getBeans(TransactionManager.class)), TransactionManager.class, bm.createCreationalContext(null)));
    final TransactionSynchronizationRegistry registry = TransactionSynchronizationRegistry.class.isInstance(manager) ? TransactionSynchronizationRegistry.class.cast(manager) : TransactionSynchronizationRegistry.class.cast(bm.getReference(bm.resolve(bm.getBeans(TransactionSynchronizationRegistry.class)), TransactionSynchronizationRegistry.class, bm.createCreationalContext(null)));
    context.init(manager, registry);
    try {
        final Class<?> builder = Thread.currentThread().getContextClassLoader().loadClass("org.apache.meecrowave.Meecrowave$Builder");
        final JtaConfig ext = JtaConfig.class.cast(builder.getMethod("getExtension", Class.class).invoke(bm.getReference(bm.resolve(bm.getBeans(builder)), builder, bm.createCreationalContext(null)), JtaConfig.class));
        config.handleExceptionOnlyForClient = ext.handleExceptionOnlyForClient;
    } catch (final Exception e) {
        config.handleExceptionOnlyForClient = Boolean.getBoolean("meecrowave.jta.handleExceptionOnlyForClient");
    }
}
Also used : GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) XAException(javax.transaction.xa.XAException)

Example 34 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry in project Payara by payara.

the class TransactionScopedContextImpl method get.

@Override
public <T> T get(Contextual<T> contextual) {
    TransactionSynchronizationRegistry transactionSynchronizationRegistry = getTransactionSynchronizationRegistry();
    Object beanKey = getContextualId(contextual);
    return getContextualInstance(beanKey, transactionSynchronizationRegistry);
}
Also used : TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry)

Example 35 with TransactionSynchronizationRegistry

use of javax.transaction.TransactionSynchronizationRegistry in project Payara by payara.

the class TransactionScopedContextImpl method getTransactionSynchronizationRegistry.

private TransactionSynchronizationRegistry getTransactionSynchronizationRegistry() {
    TransactionSynchronizationRegistry transactionSynchronizationRegistry;
    try {
        InitialContext initialContext = new InitialContext();
        transactionSynchronizationRegistry = (TransactionSynchronizationRegistry) initialContext.lookup(TRANSACTION_SYNCHRONIZATION_REGISTRY_JNDI_NAME);
    } catch (NamingException ne) {
        throw new ContextNotActiveException("Could not get TransactionSynchronizationRegistry", ne);
    }
    int status = transactionSynchronizationRegistry.getTransactionStatus();
    if (status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK || status == Status.STATUS_PREPARED || status == Status.STATUS_UNKNOWN || status == Status.STATUS_PREPARING || status == Status.STATUS_COMMITTING || status == Status.STATUS_ROLLING_BACK) {
        return transactionSynchronizationRegistry;
    }
    throw new ContextNotActiveException("TransactionSynchronizationRegistry status is not active.");
}
Also used : TransactionSynchronizationRegistry(javax.transaction.TransactionSynchronizationRegistry) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Aggregations

TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)45 TransactionManager (javax.transaction.TransactionManager)20 Test (org.junit.Test)13 InitialContext (javax.naming.InitialContext)8 ContextTransactionManager (org.wildfly.transaction.client.ContextTransactionManager)8 Synchronization (javax.transaction.Synchronization)7 ServiceName (org.jboss.msc.service.ServiceName)7 NamingException (javax.naming.NamingException)6 TransactionSynchronizationRegistryImple (com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple)5 HashMap (java.util.HashMap)5 CapabilityServiceSupport (org.jboss.as.controller.capability.CapabilityServiceSupport)5 UserTransaction (javax.transaction.UserTransaction)4 ValidatorFactory (javax.validation.ValidatorFactory)4 BinderService (org.jboss.as.naming.service.BinderService)4 BeanManager (javax.enterprise.inject.spi.BeanManager)3 DataSource (javax.sql.DataSource)3 BeanManagerAfterDeploymentValidation (org.jboss.as.jpa.beanmanager.BeanManagerAfterDeploymentValidation)3 PersistenceUnitServiceImpl (org.jboss.as.jpa.service.PersistenceUnitServiceImpl)3 PhaseOnePersistenceUnitServiceImpl (org.jboss.as.jpa.service.PhaseOnePersistenceUnitServiceImpl)3 PersistenceUnitService (org.jboss.as.jpa.spi.PersistenceUnitService)3