You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/07/27 18:42:06 UTC
svn commit: r1613812 - in /tomee/tomee/trunk/container/openejb-core/src:
main/java/org/apache/openejb/cdi/
main/java/org/apache/openejb/cdi/transactional/
main/java/org/apache/openejb/core/
test/java/org/apache/openejb/cdi/transactional/
Author: rmannibucau
Date: Sun Jul 27 16:42:05 2014
New Revision: 1613812
URL: http://svn.apache.org/r1613812
Log:
TOMEE-1282 @Transactional and @TransactionScoped
Added:
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/InterceptorBase.java
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/MandatoryInterceptor.java
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NeverInterceptor.java
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NotSupportedInterceptor.java
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredInterceptor.java
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredNewInterceptor.java
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/SupportsInterceptor.java
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/TransactionContext.java
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionContextTest.java
tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionalTest.java
Modified:
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiScanner.java
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/CoreUserTransaction.java
Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiScanner.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiScanner.java?rev=1613812&r1=1613811&r2=1613812&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiScanner.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/CdiScanner.java Sun Jul 27 16:42:05 2014
@@ -22,6 +22,12 @@ import org.apache.openejb.BeanContext;
import org.apache.openejb.assembler.classic.AppInfo;
import org.apache.openejb.assembler.classic.BeansInfo;
import org.apache.openejb.assembler.classic.EjbJarInfo;
+import org.apache.openejb.cdi.transactional.MandatoryInterceptor;
+import org.apache.openejb.cdi.transactional.NeverInterceptor;
+import org.apache.openejb.cdi.transactional.NotSupportedInterceptor;
+import org.apache.openejb.cdi.transactional.RequiredInterceptor;
+import org.apache.openejb.cdi.transactional.RequiredNewInterceptor;
+import org.apache.openejb.cdi.transactional.SupportsInterceptor;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.util.PropertyPlaceHolderHelper;
import org.apache.openejb.util.classloader.ClassLoaderComparator;
@@ -35,18 +41,26 @@ import org.apache.webbeans.spi.BDABeansX
import org.apache.webbeans.spi.ScannerService;
import java.net.URL;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
+import static java.util.Arrays.asList;
+
/**
* @version $Rev:$ $Date:$
*/
public class CdiScanner implements ScannerService {
public static final String OPENEJB_CDI_FILTER_CLASSLOADER = "openejb.cdi.filter.classloader";
+ private static final Class<?>[] TRANSACTIONAL_INTERCEPTORS = new Class<?>[]{
+ MandatoryInterceptor.class, NeverInterceptor.class, NotSupportedInterceptor.class,
+ RequiredInterceptor.class, RequiredInterceptor.class, SupportsInterceptor.class
+ };
+
// TODO add all annotated class
private final Set<Class<?>> classes = new HashSet<Class<?>>();
@@ -71,6 +85,9 @@ public class CdiScanner implements Scann
final DecoratorsManager decoratorsManager = webBeansContext.getDecoratorsManager();
final InterceptorsManager interceptorsManager = webBeansContext.getInterceptorsManager();
+ classes.addAll(asList(TRANSACTIONAL_INTERCEPTORS));
+
+ // app beans
for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
final BeansInfo beans = ejbJar.beans;
@@ -114,6 +131,9 @@ public class CdiScanner implements Scann
throw new WebBeansConfigurationException("Could not load interceptor class: " + className);
}
}
+ for (final Class<?> interceptor : TRANSACTIONAL_INTERCEPTORS) {
+ interceptorsManager.addEnabledInterceptorClass(interceptor);
+ }
for (final String className : beans.decorators) {
final Class<?> clazz = load(PropertyPlaceHolderHelper.simpleValue(className), classLoader);
Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java?rev=1613812&r1=1613811&r2=1613812&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/ThreadSingletonServiceImpl.java Sun Jul 27 16:42:05 2014
@@ -21,6 +21,7 @@ import org.apache.openejb.AppContext;
import org.apache.openejb.OpenEJBRuntimeException;
import org.apache.openejb.assembler.classic.AppInfo;
import org.apache.openejb.assembler.classic.EjbJarInfo;
+import org.apache.openejb.cdi.transactional.TransactionContext;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.spi.ContainerSystem;
import org.apache.openejb.util.AppFinder;
@@ -29,6 +30,7 @@ import org.apache.openejb.util.Logger;
import org.apache.openejb.util.classloader.MultipleClassLoader;
import org.apache.webbeans.config.OpenWebBeansConfiguration;
import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.container.BeanManagerImpl;
import org.apache.webbeans.intercept.ApplicationScopedBeanInterceptorHandler;
import org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler;
import org.apache.webbeans.spi.ContainerLifecycle;
@@ -43,6 +45,7 @@ import org.apache.webbeans.spi.Transacti
import org.apache.webbeans.spi.adaptor.ELAdaptor;
import org.apache.webbeans.web.intercept.RequestScopedBeanInterceptorHandler;
+import javax.transaction.Transactional;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@@ -147,6 +150,10 @@ public class ThreadSingletonServiceImpl
webBeansContext = new WebappWebBeansContext(services, properties, appContext.getWebBeansContext());
startupObject.getWebContext().setWebbeansContext(webBeansContext);
}
+ final BeanManagerImpl beanManagerImpl = webBeansContext.getBeanManagerImpl();
+ beanManagerImpl.addContext(new TransactionContext());
+ beanManagerImpl.addAdditionalInterceptorBindings(Transactional.class);
+
SystemInstance.get().fireEvent(new WebBeansContextCreated(webBeansContext));
OpenEJBTransactionService.class.cast(services.get(TransactionService.class)).setWebBeansContext(webBeansContext);
Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/InterceptorBase.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/InterceptorBase.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/InterceptorBase.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/InterceptorBase.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.openejb.ApplicationException;
+import org.apache.openejb.OpenEJB;
+import org.apache.openejb.SystemException;
+import org.apache.openejb.core.CoreUserTransaction;
+import org.apache.openejb.core.transaction.TransactionPolicy;
+
+import javax.interceptor.InvocationContext;
+import javax.transaction.TransactionManager;
+import javax.transaction.Transactional;
+import javax.transaction.TransactionalException;
+import java.io.Serializable;
+import java.lang.reflect.Method;
+
+public abstract class InterceptorBase implements Serializable {
+ protected Object intercept(final InvocationContext ic) throws Exception {
+ Exception error = null;
+ TransactionPolicy policy = null;
+
+ final boolean forbidsUt = doesForbidUtUsage();
+ final RuntimeException oldEx;
+ final IllegalStateException illegalStateException;
+ if (forbidsUt) {
+ illegalStateException = new IllegalStateException("Can't use UserTransaction from @Transaction call");
+ oldEx = CoreUserTransaction.error(illegalStateException);
+ } else {
+ illegalStateException = null;
+ oldEx = null;
+ }
+
+ try {
+ policy = getPolicy();
+ return ic.proceed();
+ } catch (final Exception e) {
+ error = e;
+ if (illegalStateException == e) {
+ throw e;
+ }
+ throw new TransactionalException(e.getMessage(), e);
+ } finally {
+ if (forbidsUt) {
+ CoreUserTransaction.resetError(oldEx);
+ }
+
+ if (policy != null) {
+ if (error != null) {
+ // computed lazily but we could cache it later for sure if that's really a normal case
+ final Method method = ic.getMethod();
+ Transactional tx = method.getAnnotation(Transactional.class);
+ if (tx == null) {
+ tx = method.getDeclaringClass().getAnnotation(Transactional.class);
+ }
+ if (tx != null) { // TODO: find Bean instead of reflection
+ if (new ExceptionPriotiryRules(tx.rollbackOn(), tx.dontRollbackOn()).accept(error)) {
+ policy.setRollbackOnly();
+ }
+ }
+ }
+ policy.commit();
+ }
+ }
+ }
+
+ protected boolean doesForbidUtUsage() {
+ return true;
+ }
+
+ protected abstract TransactionPolicy getPolicy() throws SystemException, ApplicationException;
+
+ protected static TransactionManager getTransactionManager() {
+ return OpenEJB.getTransactionManager();
+ }
+
+ private static class ExceptionPriotiryRules {
+ private final Class<?>[] includes;
+ private final Class<?>[] excludes;
+
+ private ExceptionPriotiryRules(final Class<?>[] includes, final Class<?>[] excludes) {
+ this.includes = includes;
+ this.excludes = excludes;
+ }
+
+ public boolean accept(final Exception e) {
+ if (e == null) {
+ return false;
+ }
+
+ final Class<? extends Exception> key = e.getClass();
+ final int includeScore = contains(includes, e);
+ final int excludeScore = contains(excludes, e);
+
+ if (excludeScore < 0) {
+ return includeScore >= 0;
+ }
+
+ return includeScore >= 0 && includeScore - excludeScore <= 0;
+ }
+
+ private static int contains(final Class<?>[] list, final Exception e) {
+ int score = -1;
+ for (final Class<?> clazz : list) {
+ if (clazz.isInstance(e)) {
+ final int thisScore = score(clazz, e.getClass());
+ if (score < 0) {
+ score = thisScore;
+ } else {
+ score = Math.min(thisScore, score);
+ }
+ }
+ }
+ return score;
+ }
+
+ private static int score(final Class<?> config, final Class<?> ex) {
+ int score = 0;
+ Class<?> current = ex;
+ while (current != null && !current.equals(config)) {
+ score++;
+ current = current.getSuperclass();
+ }
+ return score;
+ }
+ }
+
+}
Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/MandatoryInterceptor.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/MandatoryInterceptor.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/MandatoryInterceptor.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/MandatoryInterceptor.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.openejb.ApplicationException;
+import org.apache.openejb.SystemException;
+import org.apache.openejb.core.transaction.TransactionPolicy;
+import org.apache.openejb.core.transaction.TxMandatory;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+import javax.transaction.Transactional;
+
+@Interceptor
+@Transactional(Transactional.TxType.MANDATORY)
+public class MandatoryInterceptor extends InterceptorBase {
+ @AroundInvoke
+ public Object intercept(final InvocationContext ic) throws Exception {
+ return super.intercept(ic);
+ }
+
+ @Override
+ protected TransactionPolicy getPolicy() throws SystemException, ApplicationException {
+ return new TxMandatory(getTransactionManager());
+ }
+}
Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NeverInterceptor.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NeverInterceptor.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NeverInterceptor.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NeverInterceptor.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.openejb.ApplicationException;
+import org.apache.openejb.SystemException;
+import org.apache.openejb.core.transaction.TransactionPolicy;
+import org.apache.openejb.core.transaction.TxNever;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+import javax.transaction.Transactional;
+
+@Interceptor
+@Transactional(Transactional.TxType.NEVER)
+public class NeverInterceptor extends InterceptorBase {
+ @AroundInvoke
+ public Object intercept(final InvocationContext ic) throws Exception {
+ return super.intercept(ic);
+ }
+
+ @Override
+ protected TransactionPolicy getPolicy() throws SystemException, ApplicationException {
+ return new TxNever(getTransactionManager());
+ }
+
+ @Override
+ protected boolean doesForbidUtUsage() {
+ return false;
+ }
+}
Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NotSupportedInterceptor.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NotSupportedInterceptor.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NotSupportedInterceptor.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/NotSupportedInterceptor.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.openejb.ApplicationException;
+import org.apache.openejb.SystemException;
+import org.apache.openejb.core.transaction.TransactionPolicy;
+import org.apache.openejb.core.transaction.TxNotSupported;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+import javax.transaction.Transactional;
+
+@Interceptor
+@Transactional(Transactional.TxType.NOT_SUPPORTED)
+public class NotSupportedInterceptor extends InterceptorBase {
+ @AroundInvoke
+ public Object intercept(final InvocationContext ic) throws Exception {
+ return super.intercept(ic);
+ }
+
+ @Override
+ protected TransactionPolicy getPolicy() throws SystemException, ApplicationException {
+ return new TxNotSupported(getTransactionManager());
+ }
+
+ @Override
+ protected boolean doesForbidUtUsage() {
+ return false;
+ }
+}
Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredInterceptor.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredInterceptor.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredInterceptor.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredInterceptor.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.openejb.SystemException;
+import org.apache.openejb.core.transaction.TransactionPolicy;
+import org.apache.openejb.core.transaction.TxRequired;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+import javax.transaction.Transactional;
+
+@Interceptor
+@Transactional(Transactional.TxType.REQUIRED)
+public class RequiredInterceptor extends InterceptorBase {
+ @AroundInvoke
+ public Object intercept(final InvocationContext ic) throws Exception {
+ return super.intercept(ic);
+ }
+
+ @Override
+ protected TransactionPolicy getPolicy() throws SystemException {
+ return new TxRequired(getTransactionManager());
+ }
+}
Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredNewInterceptor.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredNewInterceptor.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredNewInterceptor.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/RequiredNewInterceptor.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.openejb.ApplicationException;
+import org.apache.openejb.SystemException;
+import org.apache.openejb.core.transaction.TransactionPolicy;
+import org.apache.openejb.core.transaction.TxRequiresNew;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+import javax.transaction.Transactional;
+
+@Interceptor
+@Transactional(Transactional.TxType.REQUIRES_NEW)
+public class RequiredNewInterceptor extends InterceptorBase {
+ @AroundInvoke
+ public Object intercept(final InvocationContext ic) throws Exception {
+ return super.intercept(ic);
+ }
+
+ @Override
+ protected TransactionPolicy getPolicy() throws SystemException, ApplicationException {
+ return new TxRequiresNew(getTransactionManager());
+ }
+}
Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/SupportsInterceptor.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/SupportsInterceptor.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/SupportsInterceptor.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/SupportsInterceptor.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.openejb.ApplicationException;
+import org.apache.openejb.SystemException;
+import org.apache.openejb.core.transaction.TransactionPolicy;
+import org.apache.openejb.core.transaction.TxSupports;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.Interceptor;
+import javax.interceptor.InvocationContext;
+import javax.transaction.Transactional;
+
+@Interceptor
+@Transactional(Transactional.TxType.SUPPORTS)
+public class SupportsInterceptor extends InterceptorBase {
+ @AroundInvoke
+ public Object intercept(final InvocationContext ic) throws Exception {
+ return super.intercept(ic);
+ }
+
+ @Override
+ protected TransactionPolicy getPolicy() throws SystemException, ApplicationException {
+ return new TxSupports(getTransactionManager());
+ }
+}
Added: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/TransactionContext.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/TransactionContext.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/TransactionContext.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/cdi/transactional/TransactionContext.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.geronimo.transaction.manager.GeronimoTransactionManager;
+import org.apache.geronimo.transaction.manager.TransactionImpl;
+import org.apache.openejb.OpenEJB;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.webbeans.context.AbstractContext;
+import org.apache.webbeans.context.creational.BeanInstanceBag;
+
+import javax.enterprise.context.ContextNotActiveException;
+import javax.enterprise.context.spi.Contextual;
+import javax.transaction.Status;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+import javax.transaction.TransactionManager;
+import javax.transaction.TransactionScoped;
+import javax.transaction.TransactionSynchronizationRegistry;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.HashMap;
+import java.util.Map;
+
+public class TransactionContext extends AbstractContext implements Synchronization {
+ private static final Class<?>[] MAP = new Class<?>[]{Map.class};
+ private final TransactionManager transactionManager = OpenEJB.getTransactionManager();
+ private final boolean geronimoTxMgr = GeronimoTransactionManager.class.isInstance(OpenEJB.getTransactionManager());
+
+ public TransactionContext() {
+ super(TransactionScoped.class);
+ }
+
+ @Override
+ public boolean isActive() {
+ try {
+ final int status = transactionManager.getTransaction().getStatus();
+ return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK
+ || status == Status.STATUS_PREPARED || status == Status.STATUS_PREPARING
+ || status == Status.STATUS_COMMITTING || status == Status.STATUS_ROLLING_BACK
+ || status == Status.STATUS_UNKNOWN;
+ } catch (final Throwable e) {
+ return false;
+ }
+ }
+
+ @Override
+ protected void checkActive() {
+ if (!isActive()) {
+ throw new ContextNotActiveException("Context with scope annotation @" + getScope().getName() + " is not active");
+ }
+ }
+
+ @Override
+ protected void setComponentInstanceMap() {
+ // TODO: think if not using a proxy could be more clever ;)
+ componentInstanceMap = Map.class.cast(Proxy.newProxyInstance(
+ Thread.currentThread().getContextClassLoader(), MAP,
+ new TransactionalMapHandler(
+ this, SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class))));
+ }
+
+ @Override
+ public void beforeCompletion() {
+ if (!geronimoTxMgr) {
+ // too early but we don't know how to do otherwise ATM
+ destroy();
+ }
+ }
+
+ @Override
+ public void afterCompletion(final int status) {
+ if (geronimoTxMgr) {
+ destroy();
+ }
+ }
+
+ private static class TransactionalMapHandler implements InvocationHandler {
+ private static final String KEY = "@Transactional#OpenEJB.map";
+ private final TransactionSynchronizationRegistry registry;
+ private final TransactionContext context;
+
+ public TransactionalMapHandler(final TransactionContext transactionContext, final TransactionSynchronizationRegistry registry) {
+ this.context = transactionContext;
+ this.registry = registry;
+ }
+
+ @Override
+ public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+ try {
+ return method.invoke(findMap(), args);
+ } catch (final InvocationTargetException ite) {
+ throw ite.getCause();
+ }
+ }
+
+ private Map<Contextual<?>, BeanInstanceBag<?>> findMap() {
+ final Object resource;
+ try { // we can't call registry.getResource(KEY) in afterCompletion
+ resource = context.geronimoTxMgr ?
+ TransactionImpl.class.cast(context.transactionManager.getTransaction()).getResource(KEY):
+ registry.getResource(KEY);
+ } catch (final SystemException e) {
+ throw new IllegalStateException(e);
+ }
+
+ if (resource == null) {
+ final Map<Contextual<?>, BeanInstanceBag<?>> map = new HashMap<>();
+ registry.putResource(KEY, map);
+ registry.registerInterposedSynchronization(context);
+ return map;
+ }
+ return Map.class.cast(resource);
+ }
+ }
+}
Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/CoreUserTransaction.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/CoreUserTransaction.java?rev=1613812&r1=1613811&r2=1613812&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/CoreUserTransaction.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/CoreUserTransaction.java Sun Jul 27 16:42:05 2014
@@ -37,12 +37,23 @@ import java.io.Serializable;
public class CoreUserTransaction implements UserTransaction, Serializable {
private static final long serialVersionUID = 9203248912222645965L;
private static final transient Logger transactionLogger = Logger.getInstance(LogCategory.TRANSACTION, "org.apache.openejb.util.resources");
+ private static final ThreadLocal<RuntimeException> ERROR = new ThreadLocal<>();
private transient TransactionManager transactionManager;
public CoreUserTransaction(final TransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
+ public static RuntimeException error(final RuntimeException e) {
+ final RuntimeException p = ERROR.get();
+ ERROR.set(e);
+ return p;
+ }
+
+ public static void resetError(final RuntimeException old) {
+ ERROR.set(old);
+ }
+
private TransactionManager transactionManager() {
if (transactionManager == null) {
transactionManager = OpenEJB.getTransactionManager();
@@ -52,15 +63,24 @@ public class CoreUserTransaction impleme
@Override
public void begin() throws NotSupportedException, SystemException {
+ check();
transactionManager().begin();
if (transactionLogger.isDebugEnabled()) {
transactionLogger.debug("Started user transaction " + transactionManager().getTransaction());
}
}
+ private void check() {
+ final RuntimeException e = ERROR.get();
+ if (e != null) {
+ throw e;
+ }
+ }
+
@Override
public void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
SecurityException, IllegalStateException, SystemException {
+ check();
if (transactionLogger.isDebugEnabled()) {
transactionLogger.debug("Committing user transaction " + transactionManager().getTransaction());
}
@@ -69,6 +89,7 @@ public class CoreUserTransaction impleme
@Override
public int getStatus() throws SystemException {
+ check();
final int status = transactionManager().getStatus();
if (transactionLogger.isDebugEnabled()) {
transactionLogger.debug("User transaction " + transactionManager().getTransaction() + " has status " + getStatus(status));
@@ -78,6 +99,7 @@ public class CoreUserTransaction impleme
@Override
public void rollback() throws IllegalStateException, SecurityException, SystemException {
+ check();
if (transactionLogger.isDebugEnabled()) {
transactionLogger.debug("Rolling back user transaction " + transactionManager().getTransaction());
}
@@ -86,6 +108,7 @@ public class CoreUserTransaction impleme
@Override
public void setRollbackOnly() throws SystemException {
+ check();
if (transactionLogger.isDebugEnabled()) {
transactionLogger.debug("Marking user transaction for rollback: " + transactionManager().getTransaction());
}
@@ -94,6 +117,7 @@ public class CoreUserTransaction impleme
@Override
public void setTransactionTimeout(final int seconds) throws SystemException {
+ check();
transactionManager().setTransactionTimeout(seconds);
}
Added: tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionContextTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionContextTest.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionContextTest.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionContextTest.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.openejb.OpenEJB;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.Module;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.PreDestroy;
+import javax.ejb.Singleton;
+import javax.enterprise.context.ContextNotActiveException;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.inject.Inject;
+import javax.transaction.SystemException;
+import javax.transaction.TransactionScoped;
+import java.io.Serializable;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(ApplicationComposer.class)
+public class TransactionContextTest {
+ @Module
+ @Classes(cdi = true, value = { TxBean.class, Wrapper.class })
+ public EjbJar jar() {
+ return new EjbJar();
+ }
+
+ @Inject
+ private BeanManager bm;
+
+ @Inject
+ private Wrapper wrapper;
+
+ @Test(expected = ContextNotActiveException.class)
+ public void notActive() {
+ bm.getReference(bm.resolve(bm.getBeans(TxBean.class)), TxBean.class, bm.createCreationalContext(null)).toString();
+ }
+
+ @Test
+ public void normalCase() {
+ TxBean.destroyed = false;
+ assertNotNull(wrapper.id());
+ assertTrue(TxBean.destroyed);
+ }
+
+ @Singleton
+ public static class Wrapper {
+ @Inject
+ private TxBean bean;
+
+ public String id() {
+ return bean.id();
+ }
+ }
+
+ @TransactionScoped
+ public static class TxBean implements Serializable {
+ private static boolean destroyed;
+
+ public String id() {
+ try {
+ return OpenEJB.getTransactionManager().getTransaction().toString();
+ } catch (final SystemException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @PreDestroy
+ public void killed() {
+ destroyed = true;
+ }
+ }
+}
Added: tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionalTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionalTest.java?rev=1613812&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionalTest.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/cdi/transactional/TransactionalTest.java Sun Jul 27 16:42:05 2014
@@ -0,0 +1,202 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.cdi.transactional;
+
+import org.apache.openejb.OpenEJB;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.Module;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.inject.Inject;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+import javax.transaction.TransactionManager;
+import javax.transaction.Transactional;
+import javax.transaction.TransactionalException;
+import javax.transaction.UserTransaction;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static javax.transaction.Transactional.TxType.MANDATORY;
+import static javax.transaction.Transactional.TxType.NOT_SUPPORTED;
+import static javax.transaction.Transactional.TxType.REQUIRED;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+@RunWith(ApplicationComposer.class)
+public class TransactionalTest {
+ @Module
+ @Classes(cdi = true, value = { TxBean.class })
+ public EjbJar jar() {
+ return new EjbJar();
+ }
+
+ @Inject
+ private TxBean bean;
+
+ @Test(expected = TransactionalException.class)
+ public void mandatoryKO() {
+ bean.withoutATxIllThrowAnException();
+ }
+
+ @Test
+ public void mandatoryOk() throws Exception {
+ OpenEJB.getTransactionManager().begin();
+ bean.withoutATxIllThrowAnException();
+ OpenEJB.getTransactionManager().rollback();
+ }
+
+ @Test
+ public void requiredStartsTx() throws Exception {
+ bean.required(); // asserts in the method
+ }
+
+ @Test
+ public void utAllowedWhenThereIsNoTx() throws Exception {
+ bean.notSupportedUtOk();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void utForbiddenWhenThereIsATx() throws Exception {
+ bean.requiredUtForbidden();
+ }
+
+ @Test
+ public void rollbackException() throws Exception {
+ final AtomicInteger status = new AtomicInteger();
+ final TransactionManager transactionManager = OpenEJB.getTransactionManager();
+ transactionManager.begin();
+ transactionManager.getTransaction().registerSynchronization(new Synchronization() {
+ @Override
+ public void beforeCompletion() {
+ // no-op
+ }
+
+ @Override
+ public void afterCompletion(int state) {
+ status.set(state);
+ }
+ });
+ try {
+ bean.anException();
+ fail();
+ } catch (final TransactionalException e) {
+ // no-op
+ }
+ OpenEJB.getTransactionManager().rollback();
+ assertEquals(Status.STATUS_ROLLEDBACK, status.get());
+ }
+
+ @Test
+ public void dontRollbackException() throws Exception {
+ final AtomicInteger status = new AtomicInteger();
+ final TransactionManager transactionManager = OpenEJB.getTransactionManager();
+ try {
+ bean.anotherException(status);
+ fail();
+ } catch (final TransactionalException e) {
+ // no-op
+ }
+ assertEquals(Status.STATUS_COMMITTED, status.get());
+ }
+
+ public static class TxBean {
+ @Resource
+ private UserTransaction ut;
+
+ @Transactional(value = REQUIRED)
+ public void required() {
+ assertHasTx();
+ }
+
+ @Transactional(value = REQUIRED)
+ public void requiredUtForbidden() {
+ assertHasTx();
+ try {
+ ut.begin();
+ } catch (final NotSupportedException e) {
+ fail();
+ } catch (final SystemException e) {
+ fail();
+ }
+ }
+
+ @Transactional(value = MANDATORY)
+ public void withoutATxIllThrowAnException() {
+ assertHasTx();
+ }
+
+ @Transactional(value = NOT_SUPPORTED)
+ public void notSupportedUtOk() {
+ try {
+ assertEquals(Status.STATUS_NO_TRANSACTION, OpenEJB.getTransactionManager().getStatus());
+ ut.begin();
+ ut.commit();
+ } catch (final Exception e) {
+ fail(e.getMessage());
+ }
+ }
+
+ private void assertHasTx() {
+ try {
+ assertEquals(Status.STATUS_ACTIVE, OpenEJB.getTransactionManager().getStatus());
+ } catch (final SystemException e) {
+ fail("no active tx");
+ }
+ }
+
+ @Transactional(value = MANDATORY, rollbackOn = AnException.class)
+ public void anException() {
+ throw new AnException();
+ }
+
+ @Transactional(value = REQUIRED, dontRollbackOn = AnotherException.class)
+ public void anotherException(final AtomicInteger status) {
+ try {
+ OpenEJB.getTransactionManager().getTransaction().registerSynchronization(new Synchronization() {
+ @Override
+ public void beforeCompletion() {
+ // no-op
+ }
+
+ @Override
+ public void afterCompletion(final int state) {
+ status.set(state);
+ }
+ });
+ } catch (final RollbackException e) {
+ fail();
+ } catch (final SystemException e) {
+ fail();
+ }
+ throw new AnotherException();
+ }
+ }
+
+ public static class AnException extends RuntimeException {
+
+ }
+ public static class AnotherException extends RuntimeException {
+
+ }
+}