Skip to content

Commit

Permalink
Removed unnecessary code from EventObserver wrapper and cleaned up mo…
Browse files Browse the repository at this point in the history
…re event classes and unit tests; deferred event notifications are parameterized again.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@186 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Oct 27, 2008
1 parent b37205c commit 989ea4f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 66 deletions.
Expand Up @@ -10,10 +10,10 @@
* @author David Allen
*
*/
public class DeferredEventNotification implements Synchronization
public class DeferredEventNotification<T> implements Synchronization
{
private Observer<?> observer;
private Object event;
private Observer<T> observer;
private T event;

/**
* Creates a new deferred event notifier.
Expand All @@ -22,7 +22,7 @@ public class DeferredEventNotification implements Synchronization
* @param observer The observer to be notified
* @param event The event being fired
*/
public DeferredEventNotification(Object event, Observer<?> observer)
public DeferredEventNotification(T event, Observer<T> observer)
{
this.observer = observer;
this.event = event;
Expand All @@ -31,7 +31,7 @@ public DeferredEventNotification(Object event, Observer<?> observer)
/**
* @return the observer
*/
public final Observer<?> getObserver()
public final Observer<T> getObserver()
{
return observer;
}
Expand All @@ -44,8 +44,7 @@ public void afterCompletion(int arg0)
public void beforeCompletion()
{
// Execute the observer method on the event
// TODO It is impossible to use a template variable for this observer, so notify cannot be called
//observer.notify(event);
observer.notify(event);
}

}
10 changes: 5 additions & 5 deletions webbeans-ri/src/main/java/org/jboss/webbeans/event/EventBus.java
Expand Up @@ -40,7 +40,7 @@ public EventBus()
/**
* Adds an observer to the event bus so that it receives event notifications.
*
* @param o
* @param observer
* The observer that should receive events
*/
public <T> void addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
Expand All @@ -64,22 +64,22 @@ public <T> void addObserver(Observer<T> observer, Class<T> eventType, Annotation
* The WebBeans container
* @param event
* The event object to deliver
* @param o
* @param observer
* The observer to receive the event
* @throws SystemException
* @throws IllegalStateException
* @throws RollbackException
*/
public void deferEvent(Object event, Observer<?> o)
public <T> void deferEvent(T event, Observer<T> observer)
throws SystemException, IllegalStateException, RollbackException
{
if (transactionManager != null)
{
// Get the current transaction associated with the thread
Transaction t = transactionManager.getTransaction();
if (t != null)
t.registerSynchronization(new DeferredEventNotification(
event, o));
t.registerSynchronization(new DeferredEventNotification<T>(
event, observer));
}
}

Expand Down
22 changes: 4 additions & 18 deletions webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
Expand Up @@ -16,6 +16,8 @@
import javax.webbeans.Observer;
import javax.webbeans.manager.Manager;

import org.jboss.webbeans.util.Reflections;

/**
* Implementation of the {@link Event} interface used for the container provided
* Web Bean to be injected for an observable event. See section 7.4 of the JSR
Expand Down Expand Up @@ -72,28 +74,12 @@ public void fire(T event, Annotation... bindings)
public void observe(Observer<T> observer, Annotation... bindings)
{
// Register the observer with the web beans manager
Class<T> eventArgumentClazz = null;
try
{
// TODO Fix me: Reflection can only get type variables, not the arguments used elsewhere
Field eventTypeField = this.getClass().getDeclaredField("eventType");
ParameterizedType genericType = (ParameterizedType) eventTypeField.getGenericType();
Type[] eventTypeArguments = genericType.getActualTypeArguments();
eventArgumentClazz = (Class<T>) eventTypeArguments[0];
} catch (SecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

Set<Annotation> eventBindings = new HashSet<Annotation>();
eventBindings.addAll(this.getBindingTypes());
addAnnotationBindings(eventBindings, bindings);
webBeansManager.addObserver(observer, eventArgumentClazz, bindings);
Type[] observerTypeArguments = Reflections.getActualTypeArguements(observer.getClass());
webBeansManager.addObserver(observer, (Class<T>)observerTypeArguments[0], bindings);
}

/**
Expand Down
Expand Up @@ -12,19 +12,19 @@
* code or by the Web Beans Manager for annotated observer methods. In all
* cases, this wrapper provides consistent object identification and hashing
* based on the type of event being observed and any event binding types
* specified.
* specified. It also provides a query method to quickly determine if a
* set of event bindings are exactly what the observer is interested in receiving.
* </p>
*
* @author David Allen
*
*/
public class EventObserver<T>
{

// TODO This probably should be an injectable or annotated item

private final Class<T> eventType;
private final Annotation[] eventBindings;
private final Observer<T> observer;
private final Class<T> eventType;
private final List<Annotation> eventBindings;
private final Observer<T> observer;

/**
* Constructs a new wrapper for an observer.
Expand All @@ -41,7 +41,7 @@ public EventObserver(final Observer<T> observer, final Class<T> eventType,
{
this.observer = observer;
this.eventType = eventType;
this.eventBindings = eventBindings;
this.eventBindings = Arrays.asList(eventBindings);
}

/**
Expand All @@ -55,7 +55,7 @@ public final Class<T> getEventType()
/**
* @return the eventBindings
*/
public final Annotation[] getEventBindings()
public final List<Annotation> getEventBindings()
{
return eventBindings;
}
Expand All @@ -77,32 +77,12 @@ public final Observer<T> getObserver()
*/
public boolean isObserverInterested(Annotation... bindings)
{
// TODO This logic needs to be in injectable
// Simply check that all event bindings specified by the observer are
// in the list provided.
List<Annotation> bindingsArray = Arrays.asList(bindings);
boolean result = true;
// Check each binding specified by this observer against those provided
if (this.eventBindings.length > 0)
{
if ((bindings != null) && (bindings.length > 0))
{
List<Annotation> bindingsArray = Arrays.asList(bindings);
for (Annotation annotation : this.eventBindings)
{
int eventBindingIndex = bindingsArray.indexOf(annotation);
if (eventBindingIndex >= 0)
{
//result = annotationsMatch(annotation, bindingsArray.get(eventBindingIndex));
result = annotation.equals(bindingsArray.get(eventBindingIndex));
} else
{
result = false;
break;
}
}
} else
{
result = false;
}
}
if (!this.eventBindings.isEmpty())
result = bindingsArray.containsAll(this.eventBindings);
return result;
}

Expand Down
Expand Up @@ -80,7 +80,7 @@ public final void testBeforeCompletion() throws Exception
Observer<Event> observer = new MockObserverImpl<Event>(tuna, om, Event.class);
((MockObserverImpl<Event>) observer).setInstance(observerInstance);
Event event = new Event();
DeferredEventNotification deferredNotification = new DeferredEventNotification(event, observer);
DeferredEventNotification<Event> deferredNotification = new DeferredEventNotification<Event>(event, observer);
deferredNotification.beforeCompletion();
assert observerInstance.notified;
}
Expand Down
Expand Up @@ -38,15 +38,15 @@ public void testIsObserverInterested()
{
Observer<DangerCall> observer = new AnObserver<DangerCall>();
EventObserver<DangerCall> wrappedObserver = new EventObserver<DangerCall>(observer, DangerCall.class, new TameAnnotationLiteral());
assert wrappedObserver.getEventBindings().length == 1;
assert wrappedObserver.getEventBindings().size() == 1;
assert wrappedObserver.isObserverInterested(new TameAnnotationLiteral());
assert !wrappedObserver.isObserverInterested(new AnimalStereotypeAnnotationLiteral());
assert !wrappedObserver.isObserverInterested();
assert wrappedObserver.isObserverInterested(new TameAnnotationLiteral(), new RoleBinding("Admin"));

// Perform some tests with binding values (7.7.1)
wrappedObserver = new EventObserver<DangerCall>(observer, DangerCall.class, new RoleBinding("Admin"));
assert wrappedObserver.getEventBindings().length == 1;
assert wrappedObserver.getEventBindings().size() == 1;
assert wrappedObserver.isObserverInterested(new RoleBinding("Admin"));
assert !wrappedObserver.isObserverInterested(new RoleBinding("User"));
}
Expand Down

0 comments on commit 989ea4f

Please sign in to comment.