[seam-commits] Seam SVN: r11130 - branches/community/Seam_2_2/src/main/org/jboss/seam/jms.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Jun 11 19:08:53 EDT 2009


Author: dan.j.allen
Date: 2009-06-11 19:08:53 -0400 (Thu, 11 Jun 2009)
New Revision: 11130

Added:
   branches/community/Seam_2_2/src/main/org/jboss/seam/jms/ContextualMessageHandlerRequest.java
   branches/community/Seam_2_2/src/main/org/jboss/seam/jms/ContextualMessageHandlerRequestInterceptor.java
Log:
prototype for JBSEAM-4235


Added: branches/community/Seam_2_2/src/main/org/jboss/seam/jms/ContextualMessageHandlerRequest.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/jms/ContextualMessageHandlerRequest.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/jms/ContextualMessageHandlerRequest.java	2009-06-11 23:08:53 UTC (rev 11130)
@@ -0,0 +1,118 @@
+package org.jboss.seam.jms;
+
+import java.lang.reflect.ParameterizedType;
+
+import javax.jms.Message;
+import javax.jms.TextMessage;
+
+import org.jboss.seam.Component;
+import org.jboss.seam.async.AbstractDispatcher;
+import org.jboss.seam.contexts.Contexts;
+import org.jboss.seam.contexts.Lifecycle;
+
+/**
+ * <p>
+ * A class which provides a contextual-thread context for message-driven beans
+ * in the scope of which Seam components can be instantiated and invoked.
+ * </p>
+ * 
+ * <p>
+ * NOTE: Users are still likely to get the error reported in <a
+ * href="https://jira.jboss.org/jira/browse/EJBTHREE-898">EJBTHREE-898</a> if a
+ * transaction operation is performed in the message handler.
+ * </p>
+ * 
+ * @author Dan Allen
+ */
+public abstract class ContextualMessageHandlerRequest<T>
+{
+   private Message message;
+   private boolean setupRequired = false;
+   private Object result = null;
+   private T delegate;
+   
+   public ContextualMessageHandlerRequest(Message message)
+   {
+      this.message = message;
+      this.setupRequired = !Contexts.isApplicationContextActive() && !Contexts.isEventContextActive();
+   }
+   
+   public void run() throws Exception
+   {
+      setup();
+      try
+      {
+         process();
+      }
+      finally
+      {
+         cleanup();
+      }
+   }
+   
+   public abstract void process() throws Exception;
+   
+   protected void setResult(Object result)
+   {
+      this.result = result;
+   }
+   
+   public T getDelegate()
+   {
+      return delegate;
+   }
+   
+   public Object getResult()
+   {
+      return result;
+   }
+   
+   public Message getMessage()
+   {
+      return message;
+   }
+   
+   /**
+    * A convenience method to return the message as a TextMessage. If the
+    * message is not a TextMessage, a null value is returned.
+    */
+   public TextMessage getTextMessage()
+   {
+      return message instanceof TextMessage ? (TextMessage) message : null;
+   }
+   
+   @SuppressWarnings("unchecked")
+   private void setup()
+   {
+      if (setupRequired)
+      {
+         Lifecycle.beginCall();
+      }
+      
+      Contexts.getEventContext().set(AbstractDispatcher.EXECUTING_ASYNCHRONOUS_CALL, true);
+      Class<T> delegateClass = getDelegateClass();
+      if (delegateClass != null)
+      {
+         delegate = (T) Component.getInstance(delegateClass);
+      }
+   }
+   
+   private void cleanup()
+   {
+      Contexts.getEventContext().remove(AbstractDispatcher.EXECUTING_ASYNCHRONOUS_CALL);
+      if (setupRequired)
+      {
+         Lifecycle.endCall();
+      }
+   }
+   
+   /**
+    * Get the type specified in the type parameter of the class. TODO This could
+    * be made safer
+    */
+   @SuppressWarnings("unchecked")
+   public Class<T> getDelegateClass()
+   {
+      return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
+   }
+}

Added: branches/community/Seam_2_2/src/main/org/jboss/seam/jms/ContextualMessageHandlerRequestInterceptor.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/jms/ContextualMessageHandlerRequestInterceptor.java	                        (rev 0)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/jms/ContextualMessageHandlerRequestInterceptor.java	2009-06-11 23:08:53 UTC (rev 11130)
@@ -0,0 +1,44 @@
+package org.jboss.seam.jms;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+import javax.jms.Message;
+
+/**
+ * <p>
+ * An interceptor which is intented to be applied to message-driven beans to
+ * setup a Seam request life cycle within the boundaries of the bean's method
+ * calls so Seam components can be instantiated and invoked.
+ * </p>
+ * 
+ * <p>
+ * TODO It would be nice to bake in an exception callback like the async
+ * integration supports
+ * </P>
+ * 
+ * @author Dan Allen
+ */
+public class ContextualMessageHandlerRequestInterceptor
+{
+   @AroundInvoke
+   public Object aroundInvoke(final InvocationContext invocation) throws Exception
+   {
+      String methodName = invocation.getMethod().getName();
+      Object[] args = invocation.getParameters();
+      if (!"onMessage".equals(methodName) || args.length != 1 || !(args[0] instanceof Message))
+      {
+         return invocation.proceed();
+      }
+      
+      ContextualMessageHandlerRequest contextualRequest = new ContextualMessageHandlerRequest((Message) invocation.getParameters()[0])
+      {
+         @Override
+         public void process() throws Exception
+         {
+            setResult(invocation.proceed());
+         }
+      };
+      contextualRequest.run();
+      return contextualRequest.getResult();
+   }
+}




More information about the seam-commits mailing list