Author: dallen6
Date: 2008-12-19 13:32:52 -0500 (Fri, 19 Dec 2008)
New Revision: 594
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
Log:
Refactored transactional observer code for better encapsulation and runtime performance.
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-12-19
18:29:25 UTC (rev 593)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-12-19
18:32:52 UTC (rev 594)
@@ -109,7 +109,7 @@
this.decorators = new HashSet<Decorator>();
this.interceptors = new HashSet<Interceptor>();
this.contextMap = new ContextMap();
- this.eventManager = new EventManager(this);
+ this.eventManager = new EventManager();
this.ejbDescriptorCache = new EjbDescriptorCache();
List<Class<? extends Annotation>> defaultEnabledDeploymentTypes = new
ArrayList<Class<? extends Annotation>>();
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java 2008-12-19
18:29:25 UTC (rev 593)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/DeferredEventNotification.java 2008-12-19
18:32:52 UTC (rev 594)
@@ -21,10 +21,10 @@
import javax.transaction.Synchronization;
import javax.webbeans.Observer;
-import static
org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_COMPLETION;
-import static
org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_SUCCESS;
-import static
org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_FAILURE;
-import static
org.jboss.webbeans.event.EventManager.TransactionObservationPhase.BEFORE_COMPLETION;
+import static
org.jboss.webbeans.event.ObserverImpl.TransactionObservationPhase.AFTER_COMPLETION;
+import static
org.jboss.webbeans.event.ObserverImpl.TransactionObservationPhase.AFTER_SUCCESS;
+import static
org.jboss.webbeans.event.ObserverImpl.TransactionObservationPhase.AFTER_FAILURE;
+import static
org.jboss.webbeans.event.ObserverImpl.TransactionObservationPhase.BEFORE_COMPLETION;
/**
* A synchronization object which will deliver the event to the observer after
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java 2008-12-19
18:29:25 UTC (rev 593)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/EventManager.java 2008-12-19
18:32:52 UTC (rev 594)
@@ -25,13 +25,9 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
-import javax.transaction.Status;
-import javax.transaction.SystemException;
import javax.webbeans.Observer;
-import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.contexts.DependentContext;
-import org.jboss.webbeans.transaction.UserTransaction;
import org.jboss.webbeans.util.Reflections;
import org.jboss.webbeans.util.Strings;
@@ -45,18 +41,7 @@
*/
public class EventManager
{
- private ManagerImpl manager;
-
- /**
- * The known transactional phases a transactional event observer can be
- * interested in
- */
- protected enum TransactionObservationPhase
- {
- NONE, BEFORE_COMPLETION, AFTER_COMPLETION, AFTER_FAILURE, AFTER_SUCCESS
- }
-
- /**
+ /**
* An event type -> observer list map
*/
private class RegisteredObserversMap extends ForwardingMap<Class<?>,
List<EventObserver<?>>>
@@ -140,12 +125,9 @@
/**
* Initializes a new instance of the EventManager.
- *
- * @param manager The Web Beans manager
*/
- public EventManager(ManagerImpl manager)
+ public EventManager()
{
- this.manager = manager;
registeredObservers = new RegisteredObserversMap();
}
@@ -189,24 +171,6 @@
}
/**
- * Checks if there is currently a transaction active
- *
- * @return True if there is one, false otherwise
- */
- private boolean isTransactionActive()
- {
- UserTransaction userTransaction =
manager.getInstanceByType(UserTransaction.class);
- try
- {
- return userTransaction!=null && userTransaction.getStatus() ==
Status.STATUS_ACTIVE;
- }
- catch (SystemException e)
- {
- return false;
- }
- }
-
- /**
* Iterates over the interested observers. If an observer is transactional
* and there is a transaction currently in progress, the event is deferred.
* In other cases, the observer is notified immediately.
@@ -221,14 +185,7 @@
DependentContext.INSTANCE.setActive(true);
for (Observer<T> observer : observers)
{
- if ((observer instanceof ObserverImpl) && isTransactionActive()
&& ((ObserverImpl<?>) observer).isTransactional())
- {
- deferEvent(event, observer);
- }
- else
- {
- observer.notify(event);
- }
+ observer.notify(event);
}
}
finally
@@ -238,22 +195,6 @@
}
/**
- * Defers an event for processing in a later phase of the current transaction.
- *
- * Gets the transaction listener, creates a deferred event representation and
- * registers the deferred event.
- *
- * @param event The event type
- * @param observer The interested observer
- */
- private <T> void deferEvent(T event, Observer<T> observer)
- {
- UserTransaction userTransaction =
manager.getInstanceByType(UserTransaction.class);
- DeferredEventNotification<T> deferredEvent = new
DeferredEventNotification<T>(event, observer);
- userTransaction.registerSynchronization(deferredEvent);
- }
-
- /**
* Removes an observer from the event bus.
*
* @param observer The observer to remove
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java 2008-12-19
18:29:25 UTC (rev 593)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java 2008-12-19
18:32:52 UTC (rev 594)
@@ -17,16 +17,12 @@
package org.jboss.webbeans.event;
-import static
org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_COMPLETION;
-import static
org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_FAILURE;
-import static
org.jboss.webbeans.event.EventManager.TransactionObservationPhase.AFTER_SUCCESS;
-import static
org.jboss.webbeans.event.EventManager.TransactionObservationPhase.BEFORE_COMPLETION;
-import static org.jboss.webbeans.event.EventManager.TransactionObservationPhase.NONE;
-
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
import javax.webbeans.AfterTransactionCompletion;
import javax.webbeans.AfterTransactionFailure;
import javax.webbeans.AfterTransactionSuccess;
@@ -44,9 +40,9 @@
import javax.webbeans.manager.Bean;
import org.jboss.webbeans.ManagerImpl;
-import org.jboss.webbeans.event.EventManager.TransactionObservationPhase;
import org.jboss.webbeans.introspector.AnnotatedMethod;
import org.jboss.webbeans.introspector.AnnotatedParameter;
+import org.jboss.webbeans.transaction.UserTransaction;
import org.jboss.webbeans.util.Reflections;
/**
@@ -62,7 +58,16 @@
*/
public class ObserverImpl<T> implements Observer<T>
{
- private Bean<?> eventBean;
+ /**
+ * The known transactional phases a transactional event observer can be
+ * interested in
+ */
+ protected enum TransactionObservationPhase
+ {
+ NONE, BEFORE_COMPLETION, AFTER_COMPLETION, AFTER_FAILURE, AFTER_SUCCESS
+ }
+
+ private Bean<?> observerBean;
private final AnnotatedMethod<Object> observerMethod;
private TransactionObservationPhase transactionObservationPhase;
private boolean conditional;
@@ -80,7 +85,7 @@
public ObserverImpl(final AnnotatedMethod<Object> observer, final Bean<?>
observerBean, final ManagerImpl manager)
{
this.manager = manager;
- this.eventBean = observerBean;
+ this.observerBean = observerBean;
this.observerMethod = observer;
validateObserverMethod();
initTransactionObservationPhase();
@@ -92,19 +97,19 @@
List<TransactionObservationPhase> observationPhases = new
ArrayList<TransactionObservationPhase>();
if
(!observerMethod.getAnnotatedParameters(BeforeTransactionCompletion.class).isEmpty())
{
- observationPhases.add(BEFORE_COMPLETION);
+ observationPhases.add(TransactionObservationPhase.BEFORE_COMPLETION);
}
if
(!observerMethod.getAnnotatedParameters(AfterTransactionCompletion.class).isEmpty())
{
- observationPhases.add(AFTER_COMPLETION);
+ observationPhases.add(TransactionObservationPhase.AFTER_COMPLETION);
}
if
(!observerMethod.getAnnotatedParameters(AfterTransactionFailure.class).isEmpty())
{
- observationPhases.add(AFTER_FAILURE);
+ observationPhases.add(TransactionObservationPhase.AFTER_FAILURE);
}
if
(!observerMethod.getAnnotatedParameters(AfterTransactionSuccess.class).isEmpty())
{
- observationPhases.add(AFTER_SUCCESS);
+ observationPhases.add(TransactionObservationPhase.AFTER_SUCCESS);
}
if (observationPhases.size() > 1)
{
@@ -116,7 +121,7 @@
}
else
{
- transactionObservationPhase = NONE;
+ transactionObservationPhase = TransactionObservationPhase.NONE;
}
}
@@ -171,7 +176,14 @@
{
try
{
- observerMethod.invokeWithSpecialValue(instance, Observes.class, event,
manager);
+ if ( isTransactional() && isTransactionActive() )
+ {
+ deferEvent(event);
+ }
+ else
+ {
+ observerMethod.invokeWithSpecialValue(instance, Observes.class, event,
manager);
+ }
}
catch (ExecutionException e)
{
@@ -202,10 +214,43 @@
protected Object getInstance(boolean create)
{
// Return the most specialized instance of the component
- return manager.getMostSpecializedInstance(eventBean, create);
+ return manager.getMostSpecializedInstance(observerBean, create);
}
/**
+ * Checks if there is currently a transaction active
+ *
+ * @return True if there is one, false otherwise
+ */
+ private boolean isTransactionActive()
+ {
+ UserTransaction userTransaction =
manager.getInstanceByType(UserTransaction.class);
+ try
+ {
+ return userTransaction!=null && userTransaction.getStatus() ==
Status.STATUS_ACTIVE;
+ }
+ catch (SystemException e)
+ {
+ return false;
+ }
+ }
+
+ /**
+ * Defers an event for processing in a later phase of the current transaction.
+ *
+ * Gets the transaction listener, creates a deferred event representation and
+ * registers the deferred event.
+ *
+ * @param event The event type
+ */
+ private void deferEvent(T event)
+ {
+ UserTransaction userTransaction =
manager.getInstanceByType(UserTransaction.class);
+ DeferredEventNotification<T> deferredEvent = new
DeferredEventNotification<T>(event, this);
+ userTransaction.registerSynchronization(deferredEvent);
+ }
+
+ /**
* Indicates if the observer is transactional
*
* @return True if transactional, false otherwise
@@ -241,7 +286,7 @@
{
StringBuilder builder = new StringBuilder();
builder.append("Observer Implentation: \n");
- builder.append(" Observer (Declaring) bean: " + eventBean);
+ builder.append(" Observer (Declaring) bean: " + observerBean);
builder.append(" Observer method: " + observerMethod);
return builder.toString();
}