Skip to content

Commit

Permalink
docs + merging of similar code in FacadeImpl
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@468 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Dec 8, 2008
1 parent ff35602 commit cf95d52
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 44 deletions.
34 changes: 33 additions & 1 deletion webbeans-ri/src/main/java/org/jboss/webbeans/CurrentManager.java
@@ -1,19 +1,51 @@
/*
* 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;

import org.jboss.webbeans.contexts.ApplicationContext;
import org.jboss.webbeans.contexts.RequestContext;
import org.jboss.webbeans.contexts.SessionContext;

/**
* Access point for getting/setting current Managager
*
* @author Gavin King
*/
public class CurrentManager
{

// The root manager instance
protected static ManagerImpl rootManager;

/**
* Gets the root manager
*
* @return The root manager
*/
public static ManagerImpl rootManager()
{
return rootManager;
}

/**
* Sets the root manager
*
* @param rootManager The root manager
*/
public static void setRootManager(ManagerImpl rootManager)
{
CurrentManager.rootManager = rootManager;
Expand Down
115 changes: 72 additions & 43 deletions webbeans-ri/src/main/java/org/jboss/webbeans/FacadeImpl.java
@@ -1,79 +1,108 @@
/*
* 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;

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

import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Observable;

import org.jboss.webbeans.util.Reflections;

public class FacadeImpl<T> {

/**
* Common implementation for binding-type-based helpers
*
* @author Gavin King
*
* @param <T>
*/
public abstract class FacadeImpl<T>
{
// The binding types the helper operates on
protected final Set<? extends Annotation> bindingTypes;
// The Web Beans manager
protected final ManagerImpl manager;
// The type of the operation
protected final Class<T> type;

/**
* Validates the binding types
* Constructor
*
* Removes @Observable from the list
*
* @param annotations The annotations to validate
* @return A set of binding type annotations (minus @Observable, if it was
* present)
* @param manager The Web Beans manager
* @param eventType The event type
* @param bindingTypes The binding types
*/
protected static Set<Annotation> getBindingTypes(Annotation... annotations) {
Set<Annotation> result = new HashSet<Annotation>();
for (Annotation annotation : annotations)
{
if (!Reflections.isBindingType(annotation))
{
throw new IllegalArgumentException(annotation + " is not a binding type");
}
if (!annotation.annotationType().equals(Observable.class))
{
result.add(annotation);
}
}
return result;
}

protected FacadeImpl(ManagerImpl manager, Class<T> eventType, Annotation... bindingTypes) {
protected FacadeImpl(ManagerImpl manager, Class<T> eventType, Annotation... bindingTypes)
{
this.manager = manager;
this.type = eventType;
this.bindingTypes = getBindingTypes(bindingTypes);
}
this.bindingTypes = mergeBindingTypes(new HashSet<Annotation>(), bindingTypes);
}

/**
* Validates the binding types and checks for duplicates among the annotations.
* Merges and validates the current and new bindings
*
* Checkes with an abstract method for annotations to exclude
*
* @param annotations The annotations to validate
* @return A set of unique binding type annotations
* @param currentBindings Existing bindings
* @param newBindings New bindings
* @return The union of the bindings
*/
protected Set<Annotation> checkBindingTypes(Annotation... annotations) {
protected Set<Annotation> mergeBindingTypes(Set<? extends Annotation> currentBindings, Annotation... newBindings)
{
Set<Annotation> result = new HashSet<Annotation>();
for (Annotation annotation : annotations)
result.addAll(currentBindings);
for (Annotation newAnnotation : newBindings)
{
if (!Reflections.isBindingType(annotation))
if (!Reflections.isBindingType(newAnnotation))
{
throw new IllegalArgumentException(newAnnotation + " is not a binding type for " + this);
}
if (result.contains(newAnnotation))
{
throw new IllegalArgumentException(annotation + " is not a binding type for " + this);
throw new DuplicateBindingTypeException(newAnnotation + " is already present in the bindings list for " + this);
}
if (result.contains(annotation) || this.bindingTypes.contains(annotation))
if (!getFilteredAnnotations().contains(newAnnotation.annotationType()))
{
throw new DuplicateBindingTypeException(annotation + " is already present in the bindings list for " + this);
result.add(newAnnotation);
}
result.add(annotation);
}
return result;
}

protected Annotation[] mergeBindings(Annotation... bindingTypes) {
Set<Annotation> bindingParameters = checkBindingTypes(bindingTypes);
bindingParameters.addAll(this.bindingTypes);
Annotation[] bindings = bindingParameters.toArray(new Annotation[0]);
return bindings;
/**
* Gets a set of annotation classes to ignore
*
* @return A set of annotation classes to ignore
*/
protected abstract Set<Class<? extends Annotation>> getFilteredAnnotations();

/**
* Merges the binding this helper operates upon with the parameters
*
* @param bindingTypes The bindings to merge
*
* @return The union of the binding types
*/
protected Annotation[] mergeBindings(Annotation... newBindingTypes)
{
return mergeBindingTypes(bindingTypes, newBindingTypes).toArray(new Annotation[0]);
}

}
Expand Up @@ -18,6 +18,8 @@
package org.jboss.webbeans;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Set;

import javax.webbeans.Instance;

Expand Down Expand Up @@ -57,4 +59,10 @@ public String toString()
return buffer.toString();
}

@Override
protected Set<Class<? extends Annotation>> getFilteredAnnotations()
{
return Collections.emptySet();
}

}
13 changes: 13 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/event/EventImpl.java
Expand Up @@ -18,8 +18,12 @@
package org.jboss.webbeans.event;

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

import javax.webbeans.Event;
import javax.webbeans.Observable;
import javax.webbeans.Observer;

import org.jboss.webbeans.FacadeImpl;
Expand All @@ -36,6 +40,9 @@
*/
public class EventImpl<T> extends FacadeImpl<T> implements Event<T>
{
@SuppressWarnings("unchecked")
private static final Set<Class<? extends Annotation>> FILTERED_ANNOTATIONS = new HashSet<Class<? extends Annotation>>(Arrays.asList(Observable.class));

/**
* Constructor
*
Expand Down Expand Up @@ -78,4 +85,10 @@ public String toString()
return buffer.toString();
}

@Override
protected Set<Class<? extends Annotation>> getFilteredAnnotations()
{
return FILTERED_ANNOTATIONS;
}

}

0 comments on commit cf95d52

Please sign in to comment.