Permalink
Browse files

JBTM-1145 TXFramework Code Tidy Part 2

  • Loading branch information...
paulrobinson committed Dec 17, 2012
1 parent 74b1b7f commit b4747c25fd2e4beda0f8e855da7ffb8cc41c55b9
Showing with 145 additions and 953 deletions.
  1. +0 −3 txframework/src/main/java/org/jboss/narayana/txframework/api/management/BATxControl.java
  2. +2 −3 txframework/src/main/java/org/jboss/narayana/txframework/impl/Participant.java
  3. +3 −3 txframework/src/main/java/org/jboss/narayana/txframework/impl/ServiceRequestInterceptor.java
  4. +67 −19 txframework/src/main/java/org/jboss/narayana/txframework/impl/handlers/HandlerFactory.java
  5. +0 −5 ...work/src/main/java/org/jboss/narayana/txframework/impl/handlers/UnsupportedProtocolException.java
  6. +2 −2 ...in/java/org/jboss/narayana/txframework/impl/handlers/restat/client/RestTXRequiredInterceptor.java
  7. +3 −3 ...c/main/java/org/jboss/narayana/txframework/impl/handlers/restat/service/RESTAT2PCParticipant.java
  8. +11 −26 ...work/src/main/java/org/jboss/narayana/txframework/impl/handlers/restat/service/RESTATHandler.java
  9. +5 −6 ...java/org/jboss/narayana/txframework/impl/handlers/restat/service/RestParticipantEndpointImpl.java
  10. +12 −30 txframework/src/main/java/org/jboss/narayana/txframework/impl/handlers/wsat/WSATHandler.java
  11. +0 −72 ...work/src/main/java/org/jboss/narayana/txframework/impl/handlers/wsat/WSATParticipantRegistry.java
  12. +1 −8 ...k/src/main/java/org/jboss/narayana/txframework/impl/handlers/wsat/WSATVolatile2PCParticipant.java
  13. +0 −86 ...main/java/org/jboss/narayana/txframework/impl/handlers/wsba/WSBACoordinatorCompletionHandler.java
  14. +2 −2 .../java/org/jboss/narayana/txframework/impl/handlers/wsba/WSBACoordinatorCompletionParticipant.java
  15. +32 −9 txframework/src/main/java/org/jboss/narayana/txframework/impl/handlers/wsba/WSBAHandler.java
  16. +0 −87 ...main/java/org/jboss/narayana/txframework/impl/handlers/wsba/WSBAParticipantCompletionHandler.java
  17. +1 −9 .../java/org/jboss/narayana/txframework/impl/handlers/wsba/WSBAParticipantCompletionParticipant.java
  18. +0 −84 ...work/src/main/java/org/jboss/narayana/txframework/impl/handlers/wsba/WSBAParticipantRegistry.java
  19. +1 −11 txframework/src/main/java/org/jboss/narayana/txframework/impl/handlers/wsba/WSBATxControlImpl.java
  20. +0 −140 txframework/src/test/java/org/jboss/narayana/txframework/functional/ATStatefullTest.java
  21. +0 −58 txframework/src/test/java/org/jboss/narayana/txframework/functional/clients/ATStatefullClient.java
  22. +0 −70 txframework/src/test/java/org/jboss/narayana/txframework/functional/common/SimpleHandler.java
  23. +0 −47 txframework/src/test/java/org/jboss/narayana/txframework/functional/interfaces/ATStatefull.java
  24. +0 −3 txframework/src/test/java/org/jboss/narayana/txframework/functional/services/ATBridgeImpl.java
  25. +0 −164 txframework/src/test/java/org/jboss/narayana/txframework/functional/services/ATStatefullService.java
  26. +1 −1 ...c/test/java/org/jboss/narayana/txframework/impl/handlers/wsat/WSATVolatile2PCParticipantTest.java
  27. +1 −1 ...a/org/jboss/narayana/txframework/impl/handlers/wsba/WSBACoordinatorCompletionParticipantTest.java
  28. +1 −1 ...a/org/jboss/narayana/txframework/impl/handlers/wsba/WSBAParticipantCompletionParticipantTest.java
@@ -22,7 +22,6 @@
package org.jboss.narayana.txframework.api.management;
-import org.jboss.narayana.txframework.api.configuration.trigger.BALifecycleEvent;
import org.jboss.narayana.txframework.api.exception.TXControlException;
/**
@@ -31,7 +30,5 @@
*/
public interface BATxControl extends TxControl {
- public void readOnly(BALifecycleEvent event) throws TXControlException;
-
public void completed() throws TXControlException;
}
@@ -38,7 +38,7 @@
protected Map<Class<? extends Annotation>, Method> lifecycleEventMap = new HashMap<Class<? extends Annotation>, Method>();
protected List<Method> visibleMethods;
- private final Map txDataMap;
+ private Map txDataMap;
public Participant(ServiceInvocationMeta serviceInvocationMeta, Map txDataMap) throws ParticipantRegistrationException {
@@ -82,14 +82,13 @@ protected Object invoke(Class<? extends Annotation> lifecycleEvent, Object... ar
try {
method.setAccessible(true);
- //todo: detect parameters better. Maybe have a different participant per interface.
if (lifecycleEvent == ConfirmCompleted.class) {
return method.invoke(serviceInvocationMeta.getProxyInstance(), args);
} else {
return method.invoke(serviceInvocationMeta.getProxyInstance());
}
} catch (Exception e) {
- //todo: Log stacktrace to error and throw a SystemException
+ e.printStackTrace();
throw new RuntimeException("Unable to invoke method '" + method.getName() + "' on '" + serviceInvocationMeta.getServiceClass().getName() + "'", e);
} finally {
suspend();
@@ -45,7 +45,7 @@ public Object intercept(InvocationContext ic) throws Throwable {
Object serviceImpl = ic.getTarget();
Class serviceClass = getServiceClass(serviceImpl);
- ProtocolHandler protocolHandler = HandlerFactory.createInstance(new ServiceInvocationMeta(serviceImpl, serviceClass, serviceMethod));
+ ProtocolHandler protocolHandler = HandlerFactory.getInstance(new ServiceInvocationMeta(serviceImpl, serviceClass, serviceMethod));
Object result;
try {
@@ -63,8 +63,8 @@ private Class getServiceClass(Object serviceImpl) throws Throwable {
if (serviceImpl instanceof TargetInstanceProxy) //Weld proxy
{
- final BeanInfo bi2 = Introspector.getBeanInfo(serviceImpl.getClass().getSuperclass());
- return bi2.getBeanDescriptor().getBeanClass();
+ final BeanInfo beanInfo = Introspector.getBeanInfo(serviceImpl.getClass().getSuperclass());
+ return beanInfo.getBeanDescriptor().getBeanClass();
} else //EJB Proxy
{
return serviceImpl.getClass();
@@ -22,45 +22,58 @@
package org.jboss.narayana.txframework.impl.handlers;
+import com.arjuna.mw.wst11.BusinessActivityManager;
+import com.arjuna.mw.wst11.BusinessActivityManagerFactory;
import com.arjuna.mw.wst11.UserTransaction;
import com.arjuna.mw.wst11.UserTransactionFactory;
+import com.arjuna.wst.SystemException;
import org.jboss.narayana.txframework.api.annotation.transaction.Compensatable;
import org.jboss.narayana.txframework.api.annotation.transaction.Transactional;
-import org.jboss.narayana.txframework.api.configuration.transaction.CompletionType;
import org.jboss.narayana.txframework.api.exception.TXFrameworkException;
import org.jboss.narayana.txframework.impl.ServiceInvocationMeta;
import org.jboss.narayana.txframework.impl.handlers.restat.service.RESTATHandler;
import org.jboss.narayana.txframework.impl.handlers.wsat.WSATHandler;
-import org.jboss.narayana.txframework.impl.handlers.wsba.WSBACoordinatorCompletionHandler;
-import org.jboss.narayana.txframework.impl.handlers.wsba.WSBAParticipantCompletionHandler;
+import org.jboss.narayana.txframework.impl.handlers.wsba.WSBAHandler;
+import org.jboss.resteasy.spi.ResteasyProviderFactory;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
+import java.util.Map;
public class HandlerFactory {
- //todo: improve the way transaction type is detected.
- public static ProtocolHandler createInstance(ServiceInvocationMeta serviceInvocationMeta) throws TXFrameworkException {
-
- Compensatable Compensatable = (Compensatable) serviceInvocationMeta.getServiceClass().getAnnotation(Compensatable.class);
- if (Compensatable != null) {
- CompletionType completionType = Compensatable.completionType();
- if (completionType == CompletionType.PARTICIPANT) {
- return new WSBAParticipantCompletionHandler(serviceInvocationMeta);
- } else if (completionType == CompletionType.COORDINATOR) {
- return new WSBACoordinatorCompletionHandler(serviceInvocationMeta);
- } else {
- throw new UnsupportedProtocolException("Unexpected or null completionType");
- }
+ private static final Map<String, ProtocolHandler> protocolHandlerMap = new HashMap<String, ProtocolHandler>();
+
+ public static ProtocolHandler getInstance(ServiceInvocationMeta serviceInvocationMeta) throws TXFrameworkException {
+
+ String txid = getCurrentTXID() + ":" + serviceInvocationMeta.getServiceClass();
+ ProtocolHandler protocolHandler = protocolHandlerMap.get(txid);
+
+ if (protocolHandler != null) {
+ return protocolHandler;
+ }
+
+ Compensatable compensatable = (Compensatable) serviceInvocationMeta.getServiceClass().getAnnotation(Compensatable.class);
+ if (compensatable != null) {
+ protocolHandler = new WSBAHandler(serviceInvocationMeta, compensatable.completionType());
}
Transactional Transactional = (Transactional) serviceInvocationMeta.getServiceClass().getAnnotation(Transactional.class);
if (Transactional != null) {
if (isWSATTransactionRunning()) {
- return new WSATHandler(serviceInvocationMeta);
+ protocolHandler = new WSATHandler(serviceInvocationMeta);
} else //assume it must be a REST-AT transaction running.
{
- return new RESTATHandler(serviceInvocationMeta);
+ protocolHandler = new RESTATHandler(serviceInvocationMeta);
}
}
- throw new UnsupportedProtocolException("Expected to find a transaction type annotation on '" + serviceInvocationMeta.getServiceClass().getName() + "'");
+
+ if (protocolHandler == null) {
+ throw new UnsupportedProtocolException("Expected to find a transaction type annotation on '" + serviceInvocationMeta.getServiceClass().getName() + "'");
+ }
+
+ protocolHandlerMap.put(txid, protocolHandler);
+ return protocolHandler;
}
private static boolean isWSATTransactionRunning() {
@@ -69,4 +82,39 @@ private static boolean isWSATTransactionRunning() {
return !ut.transactionIdentifier().equals("Unknown");
}
+ private static String getCurrentTXID() throws TXFrameworkException {
+
+ String txid;
+
+ //Try WS-AT
+ txid = UserTransactionFactory.userTransaction().transactionIdentifier();
+ if (!txid.equals("Unknown")) {
+ return txid;
+ }
+
+ //Try WS-BA
+ try {
+ BusinessActivityManager businessActivityManager = BusinessActivityManagerFactory.businessActivityManager();
+
+ if (businessActivityManager.currentTransaction() != null) {
+ txid = businessActivityManager.currentTransaction().toString();
+ if (!txid.equals("Unknown")) {
+ return txid;
+ }
+ }
+ } catch (SystemException e) {
+ throw new TXFrameworkException("Error when looking up Business Activity", e);
+ }
+
+ //Try REST-AT
+ HttpServletRequest req = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
+ String enlistUrl = req.getHeader("enlistURL");
+ if (enlistUrl != null) {
+ String[] parts = enlistUrl.split("/");
+ return parts[parts.length - 1];
+ }
+
+ throw new TXFrameworkException("No Transaction detected");
+ }
+
}
@@ -30,9 +30,4 @@ public UnsupportedProtocolException(String message) {
super(message);
}
-
- public UnsupportedProtocolException(String message, Throwable cause) {
-
- super(message, cause);
- }
}
@@ -28,8 +28,8 @@
/**
* @author paul.robinson@redhat.com 09/04/2012
- * <p/>
- * Quick and dirty implementation of a TX annotation for REST-AT, similar to the EJB tx annotations. Needs doing properly and supporting all variations
+ * <p/>
+ * Quick and dirty implementation of a TX annotation for REST-AT, similar to the EJB tx annotations. Needs doing properly and supporting all variations
*/
@Required
@Interceptor
@@ -28,13 +28,13 @@
import org.jboss.narayana.txframework.impl.ServiceInvocationMeta;
import org.jboss.narayana.txframework.impl.handlers.ParticipantRegistrationException;
-import java.util.Map;
+import java.util.HashMap;
public class RESTAT2PCParticipant extends org.jboss.narayana.txframework.impl.Participant {
- public RESTAT2PCParticipant(ServiceInvocationMeta serviceInvocationMeta, Map txDataMap) throws ParticipantRegistrationException {
+ public RESTAT2PCParticipant(ServiceInvocationMeta serviceInvocationMeta) throws ParticipantRegistrationException {
- super(serviceInvocationMeta, txDataMap);
+ super(serviceInvocationMeta, new HashMap());
registerEventsOfInterest(Rollback.class, Commit.class, Prepare.class);
}
@@ -23,10 +23,9 @@
package org.jboss.narayana.txframework.impl.handlers.restat.service;
import org.jboss.narayana.txframework.api.exception.TXFrameworkException;
+import org.jboss.narayana.txframework.impl.Participant;
import org.jboss.narayana.txframework.impl.ServiceInvocationMeta;
-import org.jboss.narayana.txframework.impl.handlers.ParticipantRegistrationException;
import org.jboss.narayana.txframework.impl.handlers.ProtocolHandler;
-import org.jboss.narayana.txframework.impl.handlers.wsat.WSATParticipantRegistry;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import javax.interceptor.InvocationContext;
@@ -38,49 +37,35 @@
*/
public class RESTATHandler implements ProtocolHandler {
- private ServiceInvocationMeta serviceInvocationMeta;
-
- private static final WSATParticipantRegistry participantRegistry = new WSATParticipantRegistry();
+ private Participant participant;
public RESTATHandler(ServiceInvocationMeta serviceInvocationMeta) throws TXFrameworkException {
- this.serviceInvocationMeta = serviceInvocationMeta;
+ HttpServletRequest req = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
+ String enlistUrl = req.getHeader("enlistURL");
+ UriInfo info = ResteasyProviderFactory.getContextData(UriInfo.class);
+ String txid = getTransactionId(enlistUrl);
- registerParticipant(serviceInvocationMeta);
+ participant = RestParticipantEndpointImpl.enlistParticipant(txid, info, enlistUrl, serviceInvocationMeta);
}
@Override
public Object proceed(InvocationContext ic) throws Exception {
+ participant.resume();
return ic.proceed();
}
@Override
public void notifySuccess() {
- //do nothing
+
+ Participant.suspend();
}
@Override
public void notifyFailure() {
//Todo: ensure transaction rolled back
- }
-
- private void registerParticipant(ServiceInvocationMeta serviceInvocationMeta) throws ParticipantRegistrationException {
-
- HttpServletRequest req = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
- String enlistUrl = req.getHeader("enlistURL");
- UriInfo info = ResteasyProviderFactory.getContextData(UriInfo.class);
- String txid = getTransactionId(enlistUrl);
-
- synchronized (participantRegistry) {
-
- //Only create participant if there is not already a participant for this ServiceImpl and this transaction
- if (!participantRegistry.isRegistered(txid, serviceInvocationMeta.getServiceClass())) {
- RestParticipantEndpointImpl.enlistParticipant(txid, info, enlistUrl, serviceInvocationMeta);
- //todo: need to remove this when done.
- participantRegistry.register(txid, serviceInvocationMeta.getServiceClass());
- }
- }
+ Participant.suspend();
}
private static String getTransactionId(String enlistUrl) {
@@ -24,6 +24,7 @@
import org.jboss.jbossts.star.util.TxStatus;
import org.jboss.jbossts.star.util.TxSupport;
+import org.jboss.narayana.txframework.impl.Participant;
import org.jboss.narayana.txframework.impl.ServiceInvocationMeta;
import org.jboss.narayana.txframework.impl.handlers.ParticipantRegistrationException;
@@ -32,7 +33,6 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.net.HttpURLConnection;
-import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
@@ -52,7 +52,7 @@
private static Map<Integer, RESTAT2PCParticipant> participants = new ConcurrentHashMap<Integer, RESTAT2PCParticipant>();
- public static void enlistParticipant(String txid, UriInfo info, String enlistUrl, ServiceInvocationMeta serviceInvocationMeta) throws ParticipantRegistrationException {
+ public static Participant enlistParticipant(String txid, UriInfo info, String enlistUrl, ServiceInvocationMeta serviceInvocationMeta) throws ParticipantRegistrationException {
//todo: use a @Notnull annotation.
checkNotNull(info, "txid");
@@ -62,7 +62,7 @@ public static void enlistParticipant(String txid, UriInfo info, String enlistUrl
final int pid = currentParticipantId.getAndIncrement();
- RESTAT2PCParticipant participant = new RESTAT2PCParticipant(serviceInvocationMeta, new HashMap());
+ RESTAT2PCParticipant participant = new RESTAT2PCParticipant(serviceInvocationMeta);
participants.put(pid, participant);
participant.resume();
@@ -75,11 +75,10 @@ public static void enlistParticipant(String txid, UriInfo info, String enlistUrl
*/
String linkHeader = txSupport.makeTwoPhaseAwareParticipantLinkHeader(
info.getAbsolutePath().toString(), txid, String.valueOf(pid));
- System.out.println("Service: Enlisting " + linkHeader);
String recoveryUri = txSupport.enlistParticipant(enlistUrl, linkHeader);
- System.out.println("Service: recoveryURI: " + recoveryUri);
- // TODO the recovery URI should be use by the framework to provide recovery support
+ // TODO the recovery URI should be used by the framework to provide recovery support
+ return participant;
}
private static void checkNotNull(Object object, String name) throws ParticipantRegistrationException {
Oops, something went wrong.

0 comments on commit b4747c2

Please sign in to comment.