Skip to content

Commit

Permalink
Refactor - move context creation logic to bootstrap/servlet lifecycle…
Browse files Browse the repository at this point in the history
…, mock it out for tests, clean up literals

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@516 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Dec 14, 2008
1 parent 923de22 commit 20dc117
Show file tree
Hide file tree
Showing 60 changed files with 478 additions and 837 deletions.
15 changes: 0 additions & 15 deletions webbeans-ri/src/main/java/org/jboss/webbeans/CurrentManager.java
Expand Up @@ -17,9 +17,6 @@

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
Expand Down Expand Up @@ -50,17 +47,5 @@ public static void setRootManager(ManagerImpl rootManager)
{
CurrentManager.rootManager = rootManager;
}

/**
* Set up the root manager.
* TODO: move this to Bootstrap
*/
static
{
rootManager = new ManagerImpl();
rootManager.addContext(RequestContext.INSTANCE);
rootManager.addContext(SessionContext.INSTANCE);
rootManager.addContext(ApplicationContext.INSTANCE);
}

}
64 changes: 25 additions & 39 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -32,7 +32,6 @@
import javax.webbeans.AmbiguousDependencyException;
import javax.webbeans.BindingType;
import javax.webbeans.ContextNotActiveException;
import javax.webbeans.Dependent;
import javax.webbeans.DeploymentException;
import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.Observer;
Expand All @@ -47,13 +46,9 @@
import javax.webbeans.manager.Interceptor;
import javax.webbeans.manager.Manager;

import org.jboss.webbeans.bean.AbstractBean;
import org.jboss.webbeans.bean.ManagerBean;
import org.jboss.webbeans.bean.SimpleBean;
import org.jboss.webbeans.bean.proxy.ProxyPool;
import org.jboss.webbeans.contexts.ContextMap;
import org.jboss.webbeans.contexts.DependentContext;
import org.jboss.webbeans.ejb.DefaultEnterpriseBeanLookup;
import org.jboss.webbeans.event.EventManager;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.AnnotatedMethod;
Expand Down Expand Up @@ -94,6 +89,9 @@ public class ManagerImpl implements Manager

/**
* Constructor
*
* @param enabledDeploymentTypes any enabled deployment types, an empty set
* if none are specified
*/
@SuppressWarnings("unchecked")
public ManagerImpl()
Expand All @@ -105,18 +103,11 @@ public ManagerImpl()
this.decorators = new HashSet<Decorator>();
this.interceptors = new HashSet<Interceptor>();
this.contextMap = new ContextMap();
initEnabledDeploymentTypes();
initStandardBeans();
addContext(new DependentContext());
}

/**
* Add any beans provided by the Web Beans RI to the registry
*/
protected void initStandardBeans()
{
addBean(new SimpleBean<DefaultEnterpriseBeanLookup>(DefaultEnterpriseBeanLookup.class, this));
addBean(new ManagerBean(this));

List<Class<? extends Annotation>> defaultEnabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
defaultEnabledDeploymentTypes.add(0, Standard.class);
defaultEnabledDeploymentTypes.add(1, Production.class);
setEnabledDeploymentTypes(defaultEnabledDeploymentTypes);
}

/**
Expand All @@ -126,24 +117,11 @@ protected void initStandardBeans()
* @param enabledDeploymentTypes The enabled deployment types from
* web-beans.xml
*/
protected void initEnabledDeploymentTypes(Class<? extends Annotation>... enabledDeploymentTypes)
protected void checkEnabledDeploymentTypes()
{
this.enabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
if (enabledDeploymentTypes.length == 0)
if (!this.enabledDeploymentTypes.get(0).equals(Standard.class))
{
this.enabledDeploymentTypes.add(0, Standard.class);
this.enabledDeploymentTypes.add(1, Production.class);
}
else
{
for (Class<? extends Annotation> enabledDeploymentType : enabledDeploymentTypes)
{
this.enabledDeploymentTypes.add(enabledDeploymentType);
}
if (!this.enabledDeploymentTypes.get(0).equals(Standard.class))
{
throw new DeploymentException("@Standard must be the lowest precedence deployment type");
}
throw new DeploymentException("@Standard must be the lowest precedence deployment type");
}
}

Expand Down Expand Up @@ -194,14 +172,24 @@ public <T> Set<Observer<T>> resolveObservers(T event, Annotation... bindings)
}

/**
* A strongly ordered, unmodifyable list of enabled deployment types
* A strongly ordered, unmodifiable list of enabled deployment types
*
* @return The ordered enabled deployment types known to the manager
*/
public List<Class<? extends Annotation>> getEnabledDeploymentTypes()
{
return Collections.unmodifiableList(enabledDeploymentTypes);
}

/**
* Set the enabled deployment types
* @param enabledDeploymentTypes
*/
public void setEnabledDeploymentTypes(List<Class<? extends Annotation>> enabledDeploymentTypes)
{
this.enabledDeploymentTypes = enabledDeploymentTypes;
checkEnabledDeploymentTypes();
}

/**
* Resolves beans by API type and binding types
Expand Down Expand Up @@ -276,14 +264,12 @@ public <T> Set<Bean<T>> resolveByType(AnnotatedItem<T, ?> element, Annotation...
* @param beans The set of beans to add
* @return A reference to the manager
*/
public Manager setBeans(Set<AbstractBean<?, ?>> beans)
public void setBeans(Set<Bean<?>> beans)
{
synchronized (beans)
{
this.beans = new CopyOnWriteArrayList<Bean<?>>(beans);
resolver.clear();
initStandardBeans();
return this;
}
}

Expand Down Expand Up @@ -465,7 +451,7 @@ public <T> T getInstance(Bean<T> bean)
{
try
{
contextMap.getBuiltInContext(Dependent.class).setActive(true);
DependentContext.INSTANCE.setActive(true);
if (MetaDataCache.instance().getScopeModel(bean.getScopeType()).isNormal())
{
return (T) proxyPool.getClientProxy(bean);
Expand All @@ -477,7 +463,7 @@ public <T> T getInstance(Bean<T> bean)
}
finally
{
contextMap.getBuiltInContext(Dependent.class).setActive(false);
DependentContext.INSTANCE.setActive(false);
}
}

Expand Down
Expand Up @@ -38,7 +38,7 @@
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bindings.CurrentAnnotationLiteral;
import org.jboss.webbeans.bindings.CurrentBinding;
import org.jboss.webbeans.ejb.DefaultEnterpriseBeanLookup;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.AnnotatedMethod;
Expand Down Expand Up @@ -166,7 +166,7 @@ protected void initBindingTypes()
else if (bindingTypes.size() == 0)
{
log.trace("Adding default @Current binding type");
this.bindingTypes.add(new CurrentAnnotationLiteral());
this.bindingTypes.add(new CurrentBinding());
}
else
{
Expand Down
Expand Up @@ -37,7 +37,7 @@
* @param <S>
* @param <P>
*/
public abstract class FacadeBean<T, S, P> extends AbstractBean<T, S>
public abstract class AbstractFacadeBean<T, S, P> extends AbstractBean<T, S>
{
// The underlying item
protected AnnotatedItem<T, S> annotatedItem;
Expand All @@ -48,7 +48,7 @@ public abstract class FacadeBean<T, S, P> extends AbstractBean<T, S>
* @param field The facaded field
* @param manager The Web Beans manager
*/
public FacadeBean(AnnotatedItem<T, S> field, ManagerImpl manager)
public AbstractFacadeBean(AnnotatedItem<T, S> field, ManagerImpl manager)
{
super(manager);
this.annotatedItem = field;
Expand Down
Expand Up @@ -36,7 +36,7 @@
* @param <T>
* @param <S>
*/
public abstract class ProducerBean<T, S> extends AbstractBean<T, S>
public abstract class AbstractProducerBean<T, S> extends AbstractBean<T, S>
{
// The declaring bean
protected AbstractClassBean<?> declaringBean;
Expand All @@ -47,7 +47,7 @@ public abstract class ProducerBean<T, S> extends AbstractBean<T, S>
* @param declaringBean The declaring bean
* @param manager The Web Beans manager
*/
public ProducerBean(AbstractClassBean<?> declaringBean, ManagerImpl manager)
public AbstractProducerBean(AbstractClassBean<?> declaringBean, ManagerImpl manager)
{
super(manager);
this.declaringBean = declaringBean;
Expand Down
Expand Up @@ -33,7 +33,7 @@
* @param <T>
* @param <S>
*/
public class EventBean<T, S> extends FacadeBean<Event<T>, S, T>
public class EventBean<T, S> extends AbstractFacadeBean<Event<T>, S, T>
{

/**
Expand Down
Expand Up @@ -32,7 +32,7 @@
* @param <T>
* @param <S>
*/
public class InstanceBean<T, S> extends FacadeBean<Instance<T>, S, T>
public class InstanceBean<T, S> extends AbstractFacadeBean<Instance<T>, S, T>
{

/**
Expand Down
Expand Up @@ -27,21 +27,21 @@
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Manager;

import org.jboss.webbeans.bindings.CurrentAnnotationLiteral;
import org.jboss.webbeans.bindings.CurrentBinding;
import org.jboss.webbeans.util.Reflections;

/**
* Helper bean for accessing the Manager
*
* @author Gavin King
* @author Pete Muir
*
*/
public class ManagerBean extends Bean<Manager>
{
// The API types of the manager
private static Set<Class<?>> types = Reflections.getTypeHierachy(Manager.class);
// The binding types of the manager
private static final Set<Annotation> BINDING = new HashSet<Annotation>(Arrays.asList(new CurrentAnnotationLiteral()));
private static final Set<Annotation> BINDING = new HashSet<Annotation>(Arrays.asList(new CurrentBinding()));

/**
* Constructor
Expand Down
Expand Up @@ -31,7 +31,7 @@
*
* @param <T>
*/
public class ProducerFieldBean<T> extends ProducerBean<T, Field>
public class ProducerFieldBean<T> extends AbstractProducerBean<T, Field>
{
// The underlying field
private AnnotatedField<T> field;
Expand Down
Expand Up @@ -39,7 +39,7 @@
*
* @param <T>
*/
public class ProducerMethodBean<T> extends ProducerBean<T, Method>
public class ProducerMethodBean<T> extends AbstractProducerBean<T, Method>
{
// The underlying method
private AnnotatedMethod<T> method;
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -25,4 +25,4 @@
*
* @author Pete Muir
*/
public class CurrentAnnotationLiteral extends AnnotationLiteral<Current> implements Current {}
public class CurrentBinding extends AnnotationLiteral<Current> implements Current {}

This file was deleted.

0 comments on commit 20dc117

Please sign in to comment.