Skip to content

Commit

Permalink
Renamed the EventBus as EventManager and fixed the EventImpl to work …
Browse files Browse the repository at this point in the history
…properly with event types.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@345 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Nov 20, 2008
1 parent f1b4961 commit 3e1eed7
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 232 deletions.
22 changes: 11 additions & 11 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -52,7 +52,7 @@
import org.jboss.webbeans.contexts.RequestContext;
import org.jboss.webbeans.contexts.SessionContext;
import org.jboss.webbeans.ejb.DefaultEnterpriseBeanLookup;
import org.jboss.webbeans.event.EventBus;
import org.jboss.webbeans.event.EventManager;
import org.jboss.webbeans.exceptions.NameResolutionLocation;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.AnnotatedMethod;
Expand All @@ -72,7 +72,7 @@ public class ManagerImpl implements Manager
{
private List<Class<? extends Annotation>> enabledDeploymentTypes;
private MetaDataCache metaDataCache;
private EventBus eventBus;
private EventManager eventManager;
private Resolver resolver;
private ContextMap contextMap;
private ProxyPool proxyPool;
Expand All @@ -85,7 +85,7 @@ public ManagerImpl()
{
this.metaDataCache = new MetaDataCache();
this.beans = new CopyOnWriteArrayList<Bean<?>>();
this.eventBus = new EventBus(this);
this.eventManager = new EventManager();
this.resolver = new Resolver(this);
this.proxyPool = new ProxyPool(this);
this.decorators = new HashSet<Decorator>();
Expand Down Expand Up @@ -200,7 +200,7 @@ public <T> Set<AnnotatedMethod<Object>> resolveDisposalMethods(Class<T> apiType,
*/
public <T> Set<Observer<T>> resolveObservers(T event, Annotation... bindings)
{
return eventBus.getObservers(event, bindings);
return eventManager.getObservers(event, bindings);
}

/**
Expand Down Expand Up @@ -366,7 +366,7 @@ public Manager addInterceptor(Interceptor interceptor)
*/
public <T> Manager addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
{
this.eventBus.addObserver(observer, eventType, bindings);
this.eventManager.addObserver(observer, eventType, bindings);
return this;
}

Expand All @@ -386,7 +386,7 @@ public <T> Manager addObserver(Observer<T> observer, TypeLiteral<T> eventType, A
{
// TODO Using the eventType TypeLiteral<T>, the Class<T> object must be
// retrieved
this.eventBus.addObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
this.eventManager.addObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
return this;
}

Expand All @@ -411,7 +411,7 @@ public void fireEvent(Object event, Annotation... bindings)
// parameterized, this
// method is not, so we have to use Observer<Object> for observers.
Set<Observer<Object>> observers = this.resolveObservers(event, bindings);
this.eventBus.notifyObservers(observers, event);
this.eventManager.notifyObservers(observers, event);
}

/**
Expand Down Expand Up @@ -580,7 +580,7 @@ else if (beans.size() > 1)
*/
public <T> Manager removeObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
{
this.eventBus.removeObserver(observer, eventType, bindings);
this.eventManager.removeObserver(observer, eventType, bindings);
return this;
}

Expand All @@ -600,7 +600,7 @@ public <T> Manager removeObserver(Observer<T> observer, TypeLiteral<T> eventType
{
// TODO The Class<T> for the event type must be retrieved from the
// TypeLiteral<T> instance
this.eventBus.removeObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
this.eventManager.removeObserver(observer, (Class<T>) Reflections.getActualTypeArguments(eventType.getClass())[0], bindings);
return this;
}

Expand Down Expand Up @@ -669,8 +669,8 @@ public String toString()
buffer.append(" " + deploymentType.getName() + "\n");
}

buffer.append("Event bus:\n");
buffer.append(eventBus.toString());
buffer.append("Event manager:\n");
buffer.append(eventManager.toString());

buffer.append("Metadata cache:\n");
buffer.append(metaDataCache.toString());
Expand Down
@@ -1,7 +1,6 @@
package org.jboss.webbeans.event;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
Expand All @@ -16,8 +15,6 @@
import javax.webbeans.Standard;
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 All @@ -31,6 +28,7 @@
public class EventImpl<T> implements Event<T>
{
private Collection<? extends Annotation> eventBindings;
private Class<T> eventType;

// The current WB manager
@Current
Expand Down Expand Up @@ -72,16 +70,14 @@ public void fire(T event, Annotation... bindings)
.fireEvent(event, eventBindings.toArray(new Annotation[0]));
}

@SuppressWarnings("unchecked")
public void observe(Observer<T> observer, Annotation... bindings)
{
// Register the observer with the web beans manager

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

/**
Expand Down Expand Up @@ -156,5 +152,11 @@ public void setManager(Manager manager)
{
this.webBeansManager = manager;
}

// TODO Use constructor injection
public void setEventType(Class<T> eventType)
{
this.eventType = eventType;
}

}
@@ -1,3 +1,20 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.webbeans.event;

import java.lang.annotation.Annotation;
Expand All @@ -14,39 +31,37 @@
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.webbeans.Current;
import javax.webbeans.Observer;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.util.JNDI;

/**
* The event bus is where observers are registered and events are fired.
*
* @author David Allen
*
*/
public class EventBus
public class EventManager
{
private ManagerImpl manager;
private final Map<Class<?>, CopyOnWriteArrayList<EventObserver<?>>> registeredObservers;

@Current
private TransactionManager transactionManager;

/**
* Initializes a new instance of the EventBus. This includes looking up the
* Initializes a new instance of the EventManager. This includes looking up the
* transaction manager which is needed to defer events till the end of a
* transaction.
* transaction.
*/
public EventBus(ManagerImpl manager)
public EventManager()
{
this.manager = manager;
transactionManager = (TransactionManager) JNDI.lookup("java:/TransactionManager");
registeredObservers = new ConcurrentHashMap<Class<?>, CopyOnWriteArrayList<EventObserver<?>>>();
}

/**
* Adds an observer to the event bus so that it receives event notifications.
*
* @param observer The observer that should receive events
* @param observer
* The observer that should receive events
*/
public <T> void addObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
{
Expand Down Expand Up @@ -126,29 +141,23 @@ public <T> void notifyObservers(Set<Observer<T>> observers, T event)
try
{
transaction = transactionManager.getTransaction();
}
catch (SystemException e)
} catch (SystemException e)
{
}
if (transaction != null)
{
try
{
deferEvent(event, observer);
}
catch (IllegalStateException e)
} catch (IllegalStateException e)
{
}
catch (SystemException e)
} catch (SystemException e)
{
}
catch (RollbackException e)
} catch (RollbackException e)
{
// TODO If transaction is being rolled back, perhaps notification
// should terminate now
// TODO If transaction is being rolled back, perhaps notification should terminate now
}
}
else
} else
{
// Notify observer immediately in the same context as this method
observer.notify(event);
Expand All @@ -159,7 +168,8 @@ public <T> void notifyObservers(Set<Observer<T>> observers, T event)
/**
* Removes an observer from the event bus.
*
* @param observer The observer to remove
* @param observer
* The observer to remove
*/
public <T> void removeObserver(Observer<T> observer, Class<T> eventType, Annotation... bindings)
{
Expand Down

0 comments on commit 3e1eed7

Please sign in to comment.