Skip to content

Commit

Permalink
Refactored event implementation and updated event APIs to latest spec…
Browse files Browse the repository at this point in the history
…ification revision.

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@117 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Oct 15, 2008
1 parent 2c32c4f commit 41d7cfd
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 56 deletions.
@@ -0,0 +1,50 @@
/*
* 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 javax.webbeans;

/**
* This exception is thrown whenever more than one binding type instance of the
* same type is used with the API.
*
* @author David Allen
*/
public class DuplicateBindingTypeException extends RuntimeException
{

private static final long serialVersionUID = 4194175477451120383L;

public DuplicateBindingTypeException()
{
super();
}

public DuplicateBindingTypeException(String message, Throwable cause)
{
super(message, cause);
}

public DuplicateBindingTypeException(String message)
{
super(message);
}

public DuplicateBindingTypeException(Throwable cause)
{
super(cause);
}

}
4 changes: 4 additions & 0 deletions webbeans-api/src/main/java/javax/webbeans/Event.java
Expand Up @@ -19,6 +19,8 @@

import java.lang.annotation.Annotation;

import javax.webbeans.manager.Observer;

/**
*
* @author Pete Muir
Expand All @@ -29,4 +31,6 @@ public interface Event<T>

public void fire(T event, Annotation... bindings);

public void observes(Observer<T> observe, Annotation... bindings);

}
107 changes: 88 additions & 19 deletions webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
Expand Up @@ -2,54 +2,123 @@

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

import javax.webbeans.BindingType;
import javax.webbeans.Current;
import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Event;
import javax.webbeans.manager.Manager;

import org.jboss.webbeans.BeanImpl;
import org.jboss.webbeans.model.EventComponentModel;
import javax.webbeans.manager.Observer;

/**
* Implementation of an event as a simple component.
* 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.
*
* @author David Allen
*
*
*/
public class EventImpl<T> extends BeanImpl<T> implements Event<T>
public class EventImpl<T> implements Event<T>
{
private Collection<? extends Annotation> eventBindings;

// The current WB manager
@Current
protected Manager webBeansManager;

/**
* Creates a simple implementation of {@link Event} with no default
* event bindings.
*/
public EventImpl(EventComponentModel<T> componentMetaModel) {
super(componentMetaModel);
public EventImpl(Annotation... eventBindings)
{
this.eventBindings = Arrays.asList(eventBindings);
}

/* (non-Javadoc)
* @see javax.webbeans.Event#fire(java.lang.Object, java.lang.annotation.Annotation[])

/*
* (non-Javadoc)
*
* @see javax.webbeans.Event#fire(java.lang.Object,
* java.lang.annotation.Annotation[])
*/
public void fire(T event, Annotation... bindings)
{
// Combine the annotations passed here with the annotations (event bindings)
// 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));

// 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]));
webBeansManager
.fireEvent(event, eventBindings.toArray(new Annotation[0]));
}

public void observes(Observer<T> observe, Annotation... bindings)
{
// TODO Auto-generated method stub

}

/**
* Adds each of the annotation bindings to the set, but if any binding
* already exists in the set, a {@link DuplicateBindingTypeException} is
* thrown.
*
* @param bindingsSet
* @param bindings
* @throws DuplicateBindingTypeException
* if any of bindings are duplicates
*/
private void addAnnotationBindings(Set<Annotation> bindingsSet,
Annotation[] bindings)
{
if (bindings != null)
{
Set<Class<? extends Annotation>> bindingTypes = new HashSet<Class<? extends Annotation>>();
for (Annotation annotation : bindings)
{
// 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());
}
}
bindingsSet.addAll(Arrays.asList(bindings));
}

}

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

// TODO Remove the setter for the manager once WB injection is working
public void setManager(Manager manager)
{
this.webBeansManager = manager;
}

}
Expand Up @@ -9,7 +9,7 @@

/**
* Web Beans component meta model for the container instantiated, injectable,
* observable events (Section 7.2).
* observable events (Section 7.4).
*
* @author David Allen
*
Expand Down Expand Up @@ -56,7 +56,7 @@ public String toString()
@Override
protected void initType()
{
// TODO Type is null but maybe should be EventImpl
// TODO Get the class for Event and use it for the type
this.type = null;
}

Expand All @@ -69,7 +69,7 @@ protected AnnotatedItem<Object> getAnnotatedItem()
@Override
protected String getDefaultName()
{
// TODO Auto-generated method stub
// No name per 7.4
return null;
}

Expand All @@ -79,22 +79,13 @@ protected AnnotatedItem<Object> getXmlAnnotatedItem()
return this.xmlAnnotatedItem;
}

/* (non-Javadoc)
* @see org.jboss.webbeans.model.AbstractComponentModel#checkDeploymentType()
*/
@Override
protected void checkDeploymentType()
{
// TODO Let super class check deployment type once initType() is fixed.
}

/* (non-Javadoc)
* @see org.jboss.webbeans.model.AbstractComponentModel#initDeploymentType(org.jboss.webbeans.ManagerImpl)
*/
@Override
protected void initDeploymentType(ManagerImpl container)
{
// This is always @Standard per 7.2
// This is always @Standard per 7.4
this.deploymentType = new StandardAnnotationLiteral();
}

Expand All @@ -104,7 +95,7 @@ protected void initDeploymentType(ManagerImpl container)
@Override
protected void initName()
{
// No name per 7.2
// No name per 7.4
this.name = null;
}

Expand All @@ -114,7 +105,7 @@ protected void initName()
@Override
protected void initScopeType()
{
// This is always @Dependent per 7.2
// This is always @Dependent per 7.4
this.scopeType = new DependentAnnotationLiteral();
}

Expand Down
@@ -0,0 +1,96 @@
package org.jboss.webbeans.test;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import javax.webbeans.Dependent;
import javax.webbeans.Event;
import javax.webbeans.Standard;

import org.jboss.webbeans.bindings.StandardAnnotationLiteral;
import org.jboss.webbeans.injectable.ComponentConstructor;
import org.jboss.webbeans.introspector.SimpleAnnotatedItem;
import org.jboss.webbeans.model.EventComponentModel;
import org.jboss.webbeans.test.bindings.AnotherDeploymentTypeAnnotationLiteral;
import org.jboss.webbeans.test.mock.MockContainerImpl;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
* Unit tests for the component model used only for the container supplied
* Event component.
*
* @author David Allen
*
*/
public class EventComponentModelTest
{
private MockContainerImpl manager = null;
private EventComponentModel<Event<? extends Object>> eventComponentModel = null;

@BeforeMethod
public void before() throws Exception
{
List<Annotation> enabledDeploymentTypes = new ArrayList<Annotation>();
enabledDeploymentTypes.add(new StandardAnnotationLiteral());
enabledDeploymentTypes.add(new AnotherDeploymentTypeAnnotationLiteral());
manager = new MockContainerImpl(enabledDeploymentTypes);
eventComponentModel = new EventComponentModel<Event<? extends Object>>(
new SimpleAnnotatedItem<Object>(
new HashMap<Class<? extends Annotation>, Annotation>()),
new SimpleAnnotatedItem<Object>(
new HashMap<Class<? extends Annotation>, Annotation>()),
manager);

}

/**
* The name should always be null since this type of component is not allowed to have a name.
*/
@Test(groups = "eventbus")
public void testName()
{
assert eventComponentModel.getName() == null;
}

/**
* The scope type should always be @Dependent
*/
@Test(groups = "eventbus")
public void testScopeType()
{
assert eventComponentModel.getScopeType().annotationType().equals(Dependent.class);
}

/**
* The deployment type should always be @Standard
*/
@Test(groups = "eventbus")
public void testDeploymentType()
{
assert eventComponentModel.getDeploymentType().annotationType().equals(Standard.class);
}

@Test(groups = "eventbus")
public void testApiTypes()
{
Set<Class> apis = eventComponentModel.getApiTypes();
assert apis.size() >= 1;
for (Class api : apis)
{
api.equals(Event.class);
}
}

@Test(groups = "eventbus")
public void testConstructor()
{
ComponentConstructor<Event<? extends Object>> constructor = eventComponentModel.getConstructor();
assert constructor != null;
Event<? extends Object> event = constructor.invoke(manager);
assert event != null;
}
}

0 comments on commit 41d7cfd

Please sign in to comment.