Search in sources :

Example 1 with JtaTransactionPolicyFactory

use of org.apache.openejb.core.transaction.JtaTransactionPolicyFactory in project tomee by apache.

the class Assembler method createTransactionPolicyFactory.

private TransactionPolicyFactory createTransactionPolicyFactory(final EjbJarInfo ejbJar, final ClassLoader classLoader) {
    TransactionPolicyFactory factory = null;
    final Object value = ejbJar.properties.get(TransactionPolicyFactory.class.getName());
    if (value instanceof TransactionPolicyFactory) {
        factory = (TransactionPolicyFactory) value;
    } else if (value instanceof String) {
        try {
            final String[] parts = ((String) value).split(":", 2);
            final ResourceFinder finder = new ResourceFinder("META-INF", classLoader);
            final Map<String, Class<? extends TransactionPolicyFactory>> plugins = finder.mapAvailableImplementations(TransactionPolicyFactory.class);
            final Class<? extends TransactionPolicyFactory> clazz = plugins.get(parts[0]);
            if (clazz != null) {
                if (parts.length == 1) {
                    factory = clazz.getConstructor(String.class).newInstance(parts[1]);
                } else {
                    factory = clazz.newInstance();
                }
            }
        } catch (final Exception ignored) {
        // couldn't determine the plugins, which isn't fatal
        }
    }
    if (factory == null) {
        factory = new JtaTransactionPolicyFactory(transactionManager);
    }
    return factory;
}
Also used : ResourceFinder(org.apache.xbean.finder.ResourceFinder) JtaTransactionPolicyFactory(org.apache.openejb.core.transaction.JtaTransactionPolicyFactory) JtaTransactionPolicyFactory(org.apache.openejb.core.transaction.JtaTransactionPolicyFactory) TransactionPolicyFactory(org.apache.openejb.core.transaction.TransactionPolicyFactory) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException)

Example 2 with JtaTransactionPolicyFactory

use of org.apache.openejb.core.transaction.JtaTransactionPolicyFactory in project tomee by apache.

the class TransactionRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    final Method mtd = getMethod(description.getTestClass(), description.getMethodName());
    final Transaction tx = mtd.getAnnotation(Transaction.class);
    if (tx != null) {
        return new Statement() {

            @Override
            public void evaluate() throws Throwable {
                final TransactionManager transactionManager = SystemInstance.get().getComponent(TransactionManager.class);
                final JtaTransactionPolicyFactory factory = new JtaTransactionPolicyFactory(transactionManager);
                final TransactionPolicy policy = factory.createTransactionPolicy(TransactionType.RequiresNew);
                try {
                    base.evaluate();
                } finally {
                    if (tx.rollback()) {
                        policy.setRollbackOnly();
                    }
                    policy.commit();
                }
            }
        };
    } else {
        return new Statement() {

            @Override
            public void evaluate() throws Throwable {
                base.evaluate();
            }
        };
    }
}
Also used : Statement(org.junit.runners.model.Statement) TransactionManager(javax.transaction.TransactionManager) JtaTransactionPolicyFactory(org.apache.openejb.core.transaction.JtaTransactionPolicyFactory) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Method(java.lang.reflect.Method)

Aggregations

JtaTransactionPolicyFactory (org.apache.openejb.core.transaction.JtaTransactionPolicyFactory)2 IOException (java.io.IOException)1 InvalidObjectException (java.io.InvalidObjectException)1 ObjectStreamException (java.io.ObjectStreamException)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 DefinitionException (javax.enterprise.inject.spi.DefinitionException)1 DeploymentException (javax.enterprise.inject.spi.DeploymentException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)1 NamingException (javax.naming.NamingException)1 ResourceAdapterInternalException (javax.resource.spi.ResourceAdapterInternalException)1