Skip to content

Commit

Permalink
Initial wiring of creational context (needs tidying up)
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1262 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Jan 28, 2009
1 parent 2c29aca commit e3cf83d
Show file tree
Hide file tree
Showing 41 changed files with 884 additions and 452 deletions.
64 changes: 51 additions & 13 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -63,12 +63,12 @@
import org.jboss.webbeans.ejb.spi.EjbResolver;
import org.jboss.webbeans.event.EventManager;
import org.jboss.webbeans.event.ObserverImpl;
import org.jboss.webbeans.injection.AnnotatedInjectionPoint;
import org.jboss.webbeans.injection.InjectionPointProvider;
import org.jboss.webbeans.introspector.AnnotatedClass;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.AnnotatedMethod;
import org.jboss.webbeans.introspector.jlr.AnnotatedClassImpl;
import org.jboss.webbeans.lookup.Resolver;
import org.jboss.webbeans.resources.spi.NamingContext;
import org.jboss.webbeans.resources.spi.ResourceLoader;
import org.jboss.webbeans.util.Beans;
Expand Down Expand Up @@ -129,7 +129,9 @@ public class ManagerImpl implements Manager, Serializable
// The Naming (JNDI) access
private transient final NamingContext namingContext;

private final Map<Bean<?>, Bean<?>> specializedBeans;
private final transient Map<Bean<?>, Bean<?>> specializedBeans;

private final transient ThreadLocal<Map<Bean<?>, ?>> incompleteInstances;

/**
* Create a new manager
Expand All @@ -153,7 +155,16 @@ public ManagerImpl(NamingContext namingContext, EjbResolver ejbResolver, Resourc
this.ejbDescriptorCache = new EjbDescriptorCache();
this.injectionPointProvider = new InjectionPointProvider();
this.specializedBeans = new HashMap<Bean<?>, Bean<?>>();

this.incompleteInstances = new ThreadLocal<Map<Bean<?>,?>>()
{

@Override
protected Map<Bean<?>, ?> initialValue()
{
return new HashMap<Bean<?>, Object>();
}

};
List<Class<? extends Annotation>> defaultEnabledDeploymentTypes = new ArrayList<Class<? extends Annotation>>();
defaultEnabledDeploymentTypes.add(0, Standard.class);
defaultEnabledDeploymentTypes.add(1, Production.class);
Expand Down Expand Up @@ -553,6 +564,18 @@ public <T> T getInstance(Bean<T> bean)
return getInstance(bean, true);
}

public <T> T getInstance(Bean<T> bean, boolean create)
{
if (create)
{
return getInstance(bean, new CreationalContextImpl<T>(bean));
}
else
{
return getInstance(bean, null);
}
}

/**
* Returns an instance of a bean
*
Expand All @@ -561,36 +584,51 @@ public <T> T getInstance(Bean<T> bean)
*
* @see javax.inject.manager.Manager#getInstance(javax.inject.manager.Bean)
*/
public <T> T getInstance(Bean<T> bean, boolean create)
private <T> T getInstance(Bean<T> bean, CreationalContextImpl<T> creationalContext)
{
if (specializedBeans.containsKey(bean))
{
return getInstance((Bean<T>) specializedBeans.get(bean), create);
return getInstance((Bean<T>) specializedBeans.get(bean), creationalContext);
}
else if (MetaDataCache.instance().getScopeModel(bean.getScopeType()).isNormal())
{
return (T) proxyPool.getClientProxy(bean, create);
return (T) proxyPool.getClientProxy(bean, creationalContext != null);
}
else
{
return getContext(bean.getScopeType()).get(bean, new CreationalContextImpl<T>());
return getContext(bean.getScopeType()).get(bean, creationalContext);
}
}

public <T> T getInstanceToInject(InjectionPoint injectionPoint)
{
throw new UnsupportedOperationException();
return getInstanceToInject(AnnotatedClassImpl.of((Class<T>) injectionPoint.getType(), injectionPoint.getBindings().toArray(new Annotation[0])), null);
}

public <T> T getInstanceToInject(InjectionPoint injectionPoint, CreationalContext<?> creationalContext)
{
throw new UnsupportedOperationException();
return getInstanceToInject(AnnotatedClassImpl.of((Class<T>) injectionPoint.getType(), injectionPoint.getBindings().toArray(new Annotation[0])), creationalContext);
}

public <T> T getInstanceToInject(AnnotatedInjectionPoint<T, ?> injectionPoint, CreationalContext<?> creationalContext)
private <T> T getInstanceToInject(AnnotatedItem<T, ?> element, CreationalContext<?> creationalContext)
{
Bean<T> bean = getBeanByType(injectionPoint, injectionPoint.getBindings().toArray(EMPTY_ANNOTATION_ARRAY));
return getInstance(bean);
Bean<T> bean = getBeanByType(element, element.getBindingTypesAsArray());
if (creationalContext instanceof CreationalContextImpl)
{
CreationalContextImpl<?> ctx = (CreationalContextImpl<?>) creationalContext;
if (ctx.containsIncompleteInstance(bean))
{
return ctx.getIncompleteInstance(bean);
}
else
{
return getInstance(bean, ctx.getCreationalContext(bean));
}
}
else
{
return getInstance(bean);
}
}

/**
Expand Down Expand Up @@ -658,7 +696,7 @@ public <T> T getInstanceByType(TypeLiteral<T> type, Annotation... bindings)
* @param bindingTypes The binding types to match
* @return An instance of the bean
*/
public <T> T getInstanceByType(AnnotatedItem<T, ?> element, Annotation... bindings)
private <T> T getInstanceByType(AnnotatedItem<T, ?> element, Annotation... bindings)
{
return getInstance(getBeanByType(element, bindings));
}
Expand Down
Expand Up @@ -21,6 +21,7 @@
import java.util.HashSet;
import java.util.Set;

import javax.context.CreationalContext;
import javax.context.Dependent;
import javax.context.ScopeType;
import javax.event.Observes;
Expand All @@ -35,9 +36,12 @@
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.injection.FieldInjectionPoint;
import org.jboss.webbeans.injection.InjectionPointProvider;
import org.jboss.webbeans.injection.MethodInjectionPoint;
import org.jboss.webbeans.injection.ParameterInjectionPoint;
import org.jboss.webbeans.introspector.AnnotatedClass;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedMethod;
import org.jboss.webbeans.introspector.AnnotatedParameter;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
import org.jboss.webbeans.util.Reflections;
Expand All @@ -60,7 +64,7 @@ public abstract class AbstractClassBean<T> extends AbstractBean<T, Class<T>>
// The injectable fields
private Set<FieldInjectionPoint<?>> injectableFields;
// The initializer methods
private Set<AnnotatedMethod<?>> initializerMethods;
private Set<MethodInjectionPoint<?>> initializerMethods;

/**
* Constructor
Expand Down Expand Up @@ -92,22 +96,35 @@ protected void init()
*
* @param instance The instance to inject into
*/
protected void injectBoundFields(T instance)
protected void injectBoundFields(T instance, CreationalContext<T> creationalContext)
{
InjectionPointProvider injectionPointProvider = manager.getInjectionPointProvider();
for (FieldInjectionPoint<?> injectableField : injectableFields)
{
injectionPointProvider.pushInjectionPoint(injectableField);
try
{
injectableField.inject(instance, manager);
injectableField.inject(instance, manager, creationalContext);
}
finally
{
injectionPointProvider.popInjectionPoint();
}
}
}

/**
* Calls all initializers of the bean
*
* @param instance The bean instance
*/
protected void callInitializers(T instance, CreationalContext<T> creationalContext)
{
for (MethodInjectionPoint<?> initializer : getInitializerMethods())
{
initializer.invoke(instance, manager, creationalContext);
}
}

/**
* Initializes the bean type
Expand Down Expand Up @@ -141,35 +158,42 @@ protected void initInjectionPoints()
super.injectionPoints.add(fieldInjectionPoint);
}
}
for (AnnotatedMethod<?> initializer : getInitializerMethods())
{
for (AnnotatedParameter<?> parameter : initializer.getParameters())
{
injectionPoints.add(ParameterInjectionPoint.of(this, parameter));
}
}
}

/**
* Initializes the initializer methods
*/
protected void initInitializerMethods()
{
initializerMethods = new HashSet<AnnotatedMethod<?>>();
for (AnnotatedMethod<?> annotatedMethod : annotatedItem.getAnnotatedMethods(Initializer.class))
initializerMethods = new HashSet<MethodInjectionPoint<?>>();
for (AnnotatedMethod<?> method : annotatedItem.getAnnotatedMethods(Initializer.class))
{
if (annotatedMethod.isStatic())
if (method.isStatic())
{
throw new DefinitionException("Initializer method " + annotatedMethod.toString() + " cannot be static");
throw new DefinitionException("Initializer method " + method.toString() + " cannot be static");
}
else if (annotatedMethod.getAnnotation(Produces.class) != null)
else if (method.getAnnotation(Produces.class) != null)
{
throw new DefinitionException("Initializer method " + annotatedMethod.toString() + " cannot be annotated @Produces");
throw new DefinitionException("Initializer method " + method.toString() + " cannot be annotated @Produces");
}
else if (annotatedMethod.getAnnotatedParameters(Disposes.class).size() > 0)
else if (method.getAnnotatedParameters(Disposes.class).size() > 0)
{
throw new DefinitionException("Initializer method " + annotatedMethod.toString() + " cannot have parameters annotated @Disposes");
throw new DefinitionException("Initializer method " + method.toString() + " cannot have parameters annotated @Disposes");
}
else if (annotatedMethod.getAnnotatedParameters(Observes.class).size() > 0)
else if (method.getAnnotatedParameters(Observes.class).size() > 0)
{
throw new DefinitionException("Initializer method " + annotatedMethod.toString() + " cannot be annotated @Observes");
throw new DefinitionException("Initializer method " + method.toString() + " cannot be annotated @Observes");
}
else
{
initializerMethods.add(annotatedMethod);
initializerMethods.add(MethodInjectionPoint.of(this, method));
}
}
}
Expand Down Expand Up @@ -308,7 +332,7 @@ protected String getDefaultName()
*
* @return The set of annotated methods
*/
public Set<AnnotatedMethod<?>> getInitializerMethods()
public Set<? extends MethodInjectionPoint<?>> getInitializerMethods()
{
return initializerMethods;
}
Expand Down
Expand Up @@ -305,7 +305,7 @@ public T create(CreationalContext<T> creationalContext)
DependentContext.INSTANCE.setCurrentInjectionInstance(dependentCollector);
}
DependentContext.INSTANCE.setActive(true);
T instance = produceInstance();
T instance = produceInstance(creationalContext);
checkReturnValue(instance);
return instance;
}
Expand Down Expand Up @@ -333,7 +333,7 @@ public void destroy(T instance)
}
}

protected abstract T produceInstance();
protected abstract T produceInstance(CreationalContext<T> creationalContext);

/**
* Gets a string representation
Expand Down
Expand Up @@ -126,15 +126,6 @@ protected void init()
checkObserverMethods();
}

/**
* Initializes the injection points
*/
@Override
protected void initInjectionPoints()
{
super.initInjectionPoints();
}

protected void initTypes()
{
types = new LinkedHashSet<Type>();
Expand Down Expand Up @@ -217,6 +208,7 @@ public T create(CreationalContext<T> creationalContext)
{
DependentContext.INSTANCE.setActive(true);
T instance = proxyClass.newInstance();
creationalContext.push(instance);
((ProxyObject) instance).setHandler(new EnterpriseBeanProxyMethodHandler(this, ejbDescriptor.getRemoveMethods()));
if (log.isTraceEnabled())
log.trace("Enterprise bean instance created for bean " + this);
Expand Down Expand Up @@ -254,18 +246,7 @@ public void destroy(T instance)
}
}

/**
* Calls all initializers of the bean
*
* @param instance The bean instance
*/
protected void callInitializers(T instance)
{
for (AnnotatedMethod<?> initializer : getInitializerMethods())
{
initializer.invoke(manager, instance);
}
}


/**
* Injects EJBs and common fields
Expand Down Expand Up @@ -314,19 +295,23 @@ public void postConstruct(T instance)
{
try
{
manager.getInjectionPointProvider().pushBean(this);
CreationalContext<T> creationalContext = new CreationalContext<T>()
{

public void push(T incompleteInstance) {};

};
DependentContext.INSTANCE.setCurrentInjectionInstance(instance);
DependentContext.INSTANCE.setActive(true);
bindDecorators();
bindInterceptors();
injectEjbAndCommonFields();
injectBoundFields(instance);
callInitializers(instance);
injectBoundFields(instance, creationalContext);
callInitializers(instance, creationalContext);
}
finally
{
DependentContext.INSTANCE.clearCurrentInjectionInstance(instance);
manager.getInjectionPointProvider().popBean();
DependentContext.INSTANCE.setActive(false);
}

Expand Down
Expand Up @@ -19,6 +19,8 @@

import java.lang.reflect.Field;

import javax.context.CreationalContext;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.jlr.AnnotatedFieldImpl;
Expand Down Expand Up @@ -78,7 +80,7 @@ protected ProducerFieldBean(AnnotatedField<T> field, AbstractClassBean<?> declar


@Override
public T produceInstance()
protected T produceInstance(CreationalContext<T> creationalContext)
{
return field.get(getReceiver());
}
Expand Down

0 comments on commit e3cf83d

Please sign in to comment.