Author: alex.guizar(a)jboss.com
Date: 2009-04-26 07:00:44 -0400 (Sun, 26 Apr 2009)
New Revision: 4624
Added:
jbpm4/trunk/modules/enterprise/src/main/java/org/jbpm/enterprise/internal/jta/
jbpm4/trunk/modules/enterprise/src/main/java/org/jbpm/enterprise/internal/jta/JtaTransactionInterceptor.java
jbpm4/trunk/modules/enterprise/src/main/java/org/jbpm/enterprise/internal/wire/binding/JtaTransactionInterceptorBinding.java
Modified:
jbpm4/trunk/modules/enterprise/src/main/resources/jbpm.enterprise.wire.bindings.xml
Log:
[JBPM-2101] provide transaction interceptor with 'required' semantics
Added:
jbpm4/trunk/modules/enterprise/src/main/java/org/jbpm/enterprise/internal/jta/JtaTransactionInterceptor.java
===================================================================
---
jbpm4/trunk/modules/enterprise/src/main/java/org/jbpm/enterprise/internal/jta/JtaTransactionInterceptor.java
(rev 0)
+++
jbpm4/trunk/modules/enterprise/src/main/java/org/jbpm/enterprise/internal/jta/JtaTransactionInterceptor.java 2009-04-26
11:00:44 UTC (rev 4624)
@@ -0,0 +1,123 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jbpm.enterprise.internal.jta;
+
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.UserTransaction;
+
+import org.jbpm.api.JbpmException;
+import org.jbpm.api.cmd.Command;
+import org.jbpm.internal.log.Log;
+import org.jbpm.pvm.internal.svc.Interceptor;
+
+/**
+ * @author Alejandro Guizar
+ */
+public class JtaTransactionInterceptor extends Interceptor {
+
+ protected String userTransactionName = "java:comp/UserTransaction";
+ protected Properties jndiProperties;
+
+ private UserTransaction userTransaction;
+ private static Log log = Log.getLog(JtaTransactionInterceptor.class.getName());
+
+ public <T> T execute(Command<T> command) {
+ UserTransaction transaction = getUserTransaction();
+ try {
+ T result;
+ if (transaction.getStatus() == Status.STATUS_NO_TRANSACTION) {
+ result = execute(command, transaction);
+ }
+ else {
+ // transaction already underway, just call next interceptor
+ result = getNext().execute(command);
+ }
+ return result;
+ }
+ catch (SystemException e) {
+ throw new JbpmException("transaction manager encountered unexpected error
condition", e);
+ }
+ }
+
+ protected UserTransaction getUserTransaction() {
+ if (userTransaction == null) {
+ try {
+ Context initialContext = new InitialContext(jndiProperties);
+ userTransaction = (UserTransaction) initialContext.lookup(userTransactionName);
+ initialContext.close();
+ }
+ catch (NamingException e) {
+ throw new JbpmException("failed to retrieve user transaction: " +
userTransactionName);
+ }
+ }
+ return userTransaction;
+ }
+
+ private <T> T execute(Command<T> command, UserTransaction transaction)
+ throws SystemException {
+ try {
+ transaction.begin();
+ T result = getNext().execute(command);
+ transaction.commit();
+ return result;
+ }
+ catch (RuntimeException e) {
+ // attempt rollback
+ try {
+ transaction.rollback();
+ }
+ catch (SystemException se) {
+ // log this exception at a quiet level, the original exception will be rethrown
+ log.debug("transaction rollback failed", se);
+ }
+ // rethrow original exception
+ throw e;
+ }
+ catch (NotSupportedException e) {
+ // thrown by begin() call
+ // no need to rollback since transaction has not begun
+ throw new JbpmException("cannot begin transaction at this point", e);
+ }
+ catch (RollbackException e) {
+ // transaction rolled back already, no need to rollback again
+ throw new JbpmException("transaction has been rolled back", e);
+ }
+ catch (HeuristicRollbackException e) {
+ // transaction rolled back already, no need to rollback again
+ throw new JbpmException("transaction rolled back by heuristic decision",
e);
+ }
+ catch (HeuristicMixedException e) {
+ throw new JbpmException("transaction had mixed outcome, possible data
inconsistency", e);
+ }
+ }
+
+}
Added:
jbpm4/trunk/modules/enterprise/src/main/java/org/jbpm/enterprise/internal/wire/binding/JtaTransactionInterceptorBinding.java
===================================================================
---
jbpm4/trunk/modules/enterprise/src/main/java/org/jbpm/enterprise/internal/wire/binding/JtaTransactionInterceptorBinding.java
(rev 0)
+++
jbpm4/trunk/modules/enterprise/src/main/java/org/jbpm/enterprise/internal/wire/binding/JtaTransactionInterceptorBinding.java 2009-04-26
11:00:44 UTC (rev 4624)
@@ -0,0 +1,65 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jbpm.enterprise.internal.wire.binding;
+
+import org.jbpm.enterprise.internal.jta.JtaTransactionInterceptor;
+import org.jbpm.pvm.internal.util.XmlUtil;
+import org.jbpm.pvm.internal.wire.Descriptor;
+import org.jbpm.pvm.internal.wire.binding.PropertiesBinding;
+import org.jbpm.pvm.internal.wire.binding.WireDescriptorBinding;
+import org.jbpm.pvm.internal.wire.descriptor.ObjectDescriptor;
+import org.jbpm.pvm.internal.wire.descriptor.StringDescriptor;
+import org.jbpm.pvm.internal.xml.Parse;
+import org.jbpm.pvm.internal.xml.Parser;
+import org.w3c.dom.Element;
+
+/**
+ * Mapping between a <code>jta-transaction-interceptor</code> element and a
+ * {@link JtaTransactionInterceptor} object.
+ *
+ * @author Alejandro Guizar
+ */
+public class JtaTransactionInterceptorBinding extends WireDescriptorBinding {
+
+ private static final PropertiesBinding propertiesBinding = new PropertiesBinding();
+
+ public JtaTransactionInterceptorBinding() {
+ super("jta-transaction-interceptor");
+ }
+
+ public Object parse(Element element, Parse parse, Parser parser) {
+ ObjectDescriptor descriptor = new ObjectDescriptor(JtaTransactionInterceptor.class);
+ // user transaction jndi name
+ String userTransactionName = XmlUtil.attribute(element,
"user-transaction-name");
+ if (userTransactionName != null) {
+ descriptor.addInjection("userTransactionName", new
StringDescriptor(userTransactionName));
+ }
+ // jndi properties
+ Element jndiPropertiesElement = XmlUtil.element(element,
"jndi-properties");
+ if (jndiPropertiesElement != null) {
+ descriptor.addInjection("jndiProperties", (Descriptor)
propertiesBinding.parse(
+ jndiPropertiesElement, parse, parser));
+ }
+ return descriptor;
+ }
+
+}
Modified:
jbpm4/trunk/modules/enterprise/src/main/resources/jbpm.enterprise.wire.bindings.xml
===================================================================
---
jbpm4/trunk/modules/enterprise/src/main/resources/jbpm.enterprise.wire.bindings.xml 2009-04-26
10:41:34 UTC (rev 4623)
+++
jbpm4/trunk/modules/enterprise/src/main/resources/jbpm.enterprise.wire.bindings.xml 2009-04-26
11:00:44 UTC (rev 4624)
@@ -2,5 +2,6 @@
<binding
class="org.jbpm.enterprise.internal.wire.binding.EjbTimerSessionBinding" />
<binding
class="org.jbpm.enterprise.internal.wire.binding.EjbLocalCommandServiceBinding"
/>
+ <binding
class="org.jbpm.enterprise.internal.wire.binding.JtaTransactionInterceptorBinding"
/>
</wire-bindings>