Skip to content

Commit

Permalink
EventBean/EventImpl hammering
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@388 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Dec 2, 2008
1 parent 8a75c14 commit 2a495ac
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 273 deletions.
121 changes: 60 additions & 61 deletions webbeans-ri/src/main/java/org/jboss/webbeans/bean/EventBean.java
Expand Up @@ -26,7 +26,6 @@
import org.jboss.webbeans.event.EventImpl;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.jlr.AnnotatedFieldImpl;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;

Expand All @@ -42,9 +41,18 @@ public class EventBean<T> extends AbstractBean<EventImpl<T>, Field>

private static LogProvider log = Logging.getLogProvider(EventBean.class);

// The debug location
private String location;
// The underlying annotated item
private AnnotatedField<EventImpl<T>> annotatedItem;

/**
* Constructor
*
* @param field The underlying field abstraction
* @param declaringBean
* @param manager The Web Beans manager
*/
@SuppressWarnings("unchecked")
public EventBean(AnnotatedField<T> field, ManagerImpl manager)
{
Expand All @@ -58,106 +66,97 @@ public EventBean(AnnotatedField<T> field, ManagerImpl manager)
*
* Calls super method and validates the annotated item
*/
protected void init() {
protected void init()
{
super.init();
checkAnnotatedItem();
}

/**
* Validates the annotated item
*/
private void checkAnnotatedItem() {
// TODO: checks
}

/**
* Caches the constructor for this type of bean to avoid future reflections
* during use.
*/
@SuppressWarnings("unused")
private void initConstructor()
private void checkAnnotatedItem()
{
try
{
// constructor = new SimpleConstructor<T>((Constructor<T>)
// EventImpl.class.getConstructor((Class[])null));
}
catch (Exception e)
{
log.warn("Unable to get constructor for build-in Event implementation", e);
}
// TODO: checks
}

/*
* public BeanConstructor<T> getConstructor() { return constructor; }
/**
* @see org.jboss.webbeans.bean.AbstractBean#initScopeType()
*/

public String getLocation()
{
if (location == null)
{
location = "type: Event Bean;";
}
return location;
}

@Override
public String toString()
protected void initScopeType()
{
return "EventBean[" + getType().getName() + "]";
this.scopeType = Dependent.class;
}

/**
* @see org.jboss.webbeans.bean.AbstractBean#initDeploymentType()
*/
@Override
protected void initType()
protected void initDeploymentType()
{
log.trace("Bean type specified in Java");
this.type = annotatedItem.getType();
this.deploymentType = Standard.class;
}

/**
* @see org.jboss.webbeans.bean.AbstractBean#getAnnotatedItem()
*/
@Override
protected AnnotatedItem<EventImpl<T>, Field> getAnnotatedItem()
{
return annotatedItem;
}

/**
* @see org.jboss.webbeans.bean.AbstractBean#getDefaultName()
*/
@Override
protected String getDefaultName()
{
// No name per 7.4
return null;
}

/**
* @see org.jboss.webbeans.bean.AbstractBean#initType()
*/
@Override
protected void initDeploymentType()
{
// This is always @Standard per 7.4
this.deploymentType = Standard.class;
}

@Override
protected void checkDeploymentType()
{
// No - op
}

@Override
protected void initName()
protected void initType()
{
// No name per 7.4
this.name = null;
try
{
if (getAnnotatedItem() != null)
{
this.type = getAnnotatedItem().getType();
}
}
catch (ClassCastException e)
{
// TODO: Expand error
throw new IllegalArgumentException("Type mismatch");
}
}

@Override
protected void initScopeType()
/**
* Gets the debug location
*
* @return A string describing the location
*/
private String getLocation()
{
// This is always @Dependent per 7.4
this.scopeType = Dependent.class;
if (location == null)
{
location = "type: Event Bean;";
}
return location;
}

/**
* @see javax.webbeans.manager.Bean#create()
*/
@Override
public EventImpl<T> create()
{
return new EventImpl<T>();
return new EventImpl<T>(getManager(), annotatedItem.getBindingTypesAsArray());
}

}
169 changes: 56 additions & 113 deletions webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
Expand Up @@ -18,157 +18,100 @@
package org.jboss.webbeans.event;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.webbeans.BindingType;
import javax.webbeans.Current;
import javax.webbeans.Dependent;
import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Event;
import javax.webbeans.Observable;
import javax.webbeans.Observer;
import javax.webbeans.Standard;
import javax.webbeans.manager.Manager;

import org.jboss.webbeans.ManagerImpl;

/**
* 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
* for more details on how this bean is provided by the container and used.
* Implementation of the Event interface
*
* @author David Allen
*
* @param <T>
* @see javax.webbeans.Event
*/
@Standard
@Dependent
public class EventImpl<T> implements Event<T>
{
private Collection<? extends Annotation> eventBindings;
// The set of binding types
private Set<? extends Annotation> bindingTypes;
// The event type
private Class<T> eventType;

// The current WB manager
@Current
protected Manager webBeansManager;
// The Web Beans manager
protected ManagerImpl manager;

/**
* Used to set the event bindings for this type of event after it is
* constructed with the default constructor.
*
* @param eventBindings Annotations that are bindings for the event
*/
public void setEventBindings(Annotation... eventBindings)
{
// TODO Use constructor injection
Set<Annotation> newEventBindings = new HashSet<Annotation>();
addAnnotationBindings(newEventBindings, eventBindings);
this.eventBindings = newEventBindings;
}

/*
* (non-Javadoc)
* Constructor
*
* @see javax.webbeans.Event#fire(java.lang.Object,
* java.lang.annotation.Annotation[])
* @param manager The Web Beans manager
* @param bindingTypes The binding types
*/
public void fire(T event, Annotation... bindings)
public EventImpl(ManagerImpl manager, Annotation... bindingTypes)
{
// Combine the annotations passed here with the annotations (event
// bindings)
// specified on the @Observable object.
Set<Annotation> eventBindings = new HashSet<Annotation>();
eventBindings.addAll(this.getBindingTypes());
// eventBindings.addAll(Arrays.asList(bindings));
addAnnotationBindings(eventBindings, bindings);

// Invoke the container method to fire the event per 7.2
webBeansManager.fireEvent(event, eventBindings.toArray(new Annotation[0]));
}

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);
webBeansManager.addObserver(observer, eventType, bindings);
this.manager = manager;
this.bindingTypes = checkBindingTypes(bindingTypes);
}

/**
* Adds each of the annotation bindings to the set, but if any binding
* already exists in the set, a {@link DuplicateBindingTypeException} is
* thrown.
* Validates the binding types
*
* Removes @Observable from the list
*
* @param bindingsSet The set of annotation binding objects
* @param bindings An array of annotation bindings to add to the set
* @throws DuplicateBindingTypeException if any of bindings are duplicates
* @throws IllegalArgumentException if any annotation is not a binding type
* @param annotations The annotations to validate
* @return A set of unique binding type annotations (minus @Observable, if it
* was present)
*/
private void addAnnotationBindings(Set<Annotation> bindingsSet, Annotation[] bindings)
private Set<Annotation> checkBindingTypes(Annotation... annotations)
{
if (bindings != null)
Set<Annotation> uniqueAnnotations = new HashSet<Annotation>();
for (Annotation annotation : annotations)
{
Set<Class<? extends Annotation>> bindingTypes = new HashSet<Class<? extends Annotation>>();
// Add the bindings types that are already in the set being added to.
// This will
// provide detection of duplicates across construction and later
// invocations.
for (Annotation annotation : bindingsSet)
if (!annotation.annotationType().isAnnotationPresent(BindingType.class))
{
bindingTypes.add(annotation.annotationType());
throw new IllegalArgumentException(annotation + " is not a binding type");
}

// Now go through the new annotations being added to make sure these
// are binding
// types and are not duplicates
for (Annotation annotation : bindings)
if (uniqueAnnotations.contains(annotation))
{
// Check that the binding type is indeed a binding type
Annotation[] bindingAnnotations = annotation.annotationType().getAnnotations();
boolean isBindingType = false;
for (Annotation bindingAnnotation : bindingAnnotations)
{
if (bindingAnnotation.annotationType().equals(BindingType.class))
{
isBindingType = true;
}
}
if (!isBindingType)
throw new IllegalArgumentException("Annotation " + annotation + " is not a binding type");

// Check that no binding type was specified more than once in the
// annotations
if (bindingTypes.contains(annotation.annotationType()))
{
throw new DuplicateBindingTypeException();
}
else
{
bindingTypes.add(annotation.annotationType());
}
throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list");
}
if (!annotation.annotationType().equals(Observable.class))
{
uniqueAnnotations.add(annotation);
}
bindingsSet.addAll(Arrays.asList(bindings));
}

}

private Collection<? extends Annotation> getBindingTypes()
{
// Get the binding types directly from the model for the bean
return this.eventBindings;
return uniqueAnnotations;
}

// TODO Remove the setter for the manager once WB injection is working
public void setManager(Manager manager)
/**
* Fires an event
*
* @param event The event object
* @param bindingTypes Additional binding types
*/
public void fire(T event, Annotation... bindingTypes)
{
this.webBeansManager = manager;
Set<Annotation> bindingParameters = checkBindingTypes(bindingTypes);
bindingParameters.addAll(this.bindingTypes);
manager.fireEvent(event, bindingParameters.toArray(new Annotation[0]));
}

// TODO Use constructor injection
public void setEventType(Class<T> eventType)
/**
* Registers an observer
*
* @param observer
* @param bindingTypes Additional binding types
*/
public void observe(Observer<T> observer, Annotation... bindingTypes)
{
this.eventType = eventType;
Set<Annotation> bindingParameters = checkBindingTypes(bindingTypes);
bindingParameters.addAll(this.bindingTypes);
manager.addObserver(observer, eventType, bindingParameters.toArray(new Annotation[0]));
}

}
}

0 comments on commit 2a495ac

Please sign in to comment.