Skip to content

Commit

Permalink
minor events stuff
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@357 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Nov 24, 2008
1 parent 2d01871 commit 08eec85
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 58 deletions.
Expand Up @@ -12,32 +12,28 @@
* 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. It also provides a query method to quickly determine if a
* set of event bindings are exactly what the observer is interested in receiving.
* 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>
{
private final Class<T> eventType;

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

/**
* Constructs a new wrapper for an observer.
*
* @param observer
* The observer
* @param eventType
* The class of event being observed
* @param eventBindings
* The array of annotation event bindings, if any
* @param observer The observer
* @param eventType The class of event being observed
* @param eventBindings The array of annotation event bindings, if any
*/
public EventObserver(final Observer<T> observer, final Class<T> eventType,
final Annotation... eventBindings)
public EventObserver(final Observer<T> observer, final Class<T> eventType, final Annotation... eventBindings)
{
this.observer = observer;
this.eventType = eventType;
Expand Down Expand Up @@ -82,57 +78,81 @@ public boolean isObserverInterested(Annotation... bindings)
List<Annotation> bindingsArray = Arrays.asList(bindings);
boolean result = true;
if (!this.eventBindings.isEmpty())
{
result = bindingsArray.containsAll(this.eventBindings);
}
return result;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result
+ ((eventBindings == null) ? 0 : eventBindings.hashCode());
result = prime * result
+ ((eventType == null) ? 0 : eventType.hashCode());
result = prime * result + ((eventBindings == null) ? 0 : eventBindings.hashCode());
result = prime * result + ((eventType == null) ? 0 : eventType.hashCode());
result = prime * result + ((observer == null) ? 0 : observer.hashCode());
return result;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
EventObserver<?> other = (EventObserver<?>) obj;
if (eventBindings == null)
{
if (other.eventBindings != null)
return false;
} else if (!eventBindings.equals(other.eventBindings))
}
else if (!eventBindings.equals(other.eventBindings))
{
return false;

}
if (eventType == null)
{
if (other.eventType != null)
{
return false;
} else if (!eventType.equals(other.eventType))
}
}
else if (!eventType.equals(other.eventType))
{
return false;
}
if (observer == null)
{
if (other.observer != null)
{
return false;
} else if (!observer.equals(other.observer))
}
}
else if (!observer.equals(other.observer))
{
return false;
}
return true;
}

Expand Down
Expand Up @@ -6,7 +6,6 @@
import javax.webbeans.AfterTransactionFailure;
import javax.webbeans.AfterTransactionSuccess;
import javax.webbeans.BeforeTransactionCompletion;
import javax.webbeans.Current;
import javax.webbeans.Observer;

import org.jboss.webbeans.ManagerImpl;
Expand All @@ -16,9 +15,9 @@
/**
* <p>
* Reference implementation for the Observer interface, which represents an
* observer method. Each observer method has an event type which is the class of the
* event object being observed, and event binding types that are annotations applied to
* the event parameter to narrow the event notifications delivered.
* observer method. Each observer method has an event type which is the class of
* the event object being observed, and event binding types that are annotations
* applied to the event parameter to narrow the event notifications delivered.
* </p>
*
* @author David Allen
Expand All @@ -30,36 +29,32 @@ public class ObserverImpl<T> implements Observer<T>
private EventBean<T> eventBean;
private final AnnotatedMethod<Object> observerMethod;
private final Class<T> eventType;

/**
* Injected before notify is called. This can only work with the RI since
* InjectableMethod requires this specific implementation.
* TODO Determine if the impl can be injected here.
*/
@Current
protected ManagerImpl manager;
private boolean transactional;
private ManagerImpl manager;

/**
* Creates an Observer which describes and encapsulates an observer method
* (7.5).
*
* @param componentModel
* The model for the component which defines the observer method
* @param observer
* The observer method to notify
* @param eventType
* The type of event being observed
* @param beanModel The model for the bean which defines the
* @param componentModel The model for the component which defines the
* observer method
* @param observer The observer method to notify
* @param eventType The type of event being observed
* @param beanModel The model for the bean which defines the observer method
* @param observer The observer method to notify
* @param eventType The type of event being observed
*/
public ObserverImpl(final EventBean<T> eventBean,
final AnnotatedMethod<Object> observer, final Class<T> eventType)
public ObserverImpl(ManagerImpl manager, final EventBean<T> eventBean, final AnnotatedMethod<Object> observer, final Class<T> eventType)
{
this.manager = manager;
this.eventBean = eventBean;
this.observerMethod = observer;
this.eventType = eventType;
transactional =
!observerMethod.getAnnotatedParameters(AfterTransactionCompletion.class).isEmpty() ||
!observerMethod.getAnnotatedParameters(AfterTransactionFailure.class).isEmpty() ||
!observerMethod.getAnnotatedParameters(AfterTransactionSuccess.class).isEmpty() ||
!observerMethod.getAnnotatedParameters(BeforeTransactionCompletion.class).isEmpty();
}

/*
Expand Down Expand Up @@ -101,12 +96,9 @@ protected Object getInstance()
// Return the most specialized instance of the component
return manager.getInstanceByType(eventBean.getType(), eventBean.getBindingTypes().toArray(new Annotation[0]));
}

public boolean isTransactional() {
return
!observerMethod.getAnnotatedParameters(AfterTransactionCompletion.class).isEmpty() ||
!observerMethod.getAnnotatedParameters(AfterTransactionFailure.class).isEmpty() ||
!observerMethod.getAnnotatedParameters(AfterTransactionSuccess.class).isEmpty() ||
!observerMethod.getAnnotatedParameters(BeforeTransactionCompletion.class).isEmpty();

public boolean isTransactional()
{
return transactional;
}
}
@@ -1,33 +1,35 @@
package org.jboss.webbeans.test.mock;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bean.EventBean;
import org.jboss.webbeans.event.ObserverImpl;
import org.jboss.webbeans.introspector.AnnotatedMethod;

/**
* An implementation used for unit testing only.
*
* @author David Allen
*
*
*/
public class MockObserverImpl<T> extends ObserverImpl<T> {
public class MockObserverImpl<T> extends ObserverImpl<T>
{

private Object specializedInstance;



public MockObserverImpl(EventBean<T> beanModel,
AnnotatedMethod<Object> observer, Class<T> eventType)
public MockObserverImpl(ManagerImpl manager, EventBean<T> beanModel, AnnotatedMethod<Object> observer, Class<T> eventType)
{
super(beanModel, observer, eventType);
super(manager, beanModel, observer, eventType);
}

@Override
protected final Object getInstance() {
protected final Object getInstance()
{
return specializedInstance;
}

/**
* The most specialized instance of this observer type.
*
* @param instance The instance to use for testing
*/
public final void setInstance(Object instance)
Expand Down

0 comments on commit 08eec85

Please sign in to comment.