Skip to content

Commit

Permalink
WELD-262
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuir committed Jan 13, 2010
1 parent 674d064 commit 36db00a
Show file tree
Hide file tree
Showing 34 changed files with 578 additions and 498 deletions.
5 changes: 2 additions & 3 deletions impl/src/main/java/org/jboss/weld/Container.java
Expand Up @@ -41,7 +41,7 @@
public class Container
{

private final static Singleton<Container> instance;
private static Singleton<Container> instance;

static
{
Expand Down Expand Up @@ -95,6 +95,7 @@ public Container(BeanManagerImpl deploymentManager, ServiceRegistry deploymentSe
this.managers.put(deploymentManager.getId(), deploymentManager);
this.beanDeploymentArchives = new ConcurrentHashMap<BeanDeploymentArchive, BeanManagerImpl>();
this.deploymentServices = deploymentServices;

}

/**
Expand All @@ -105,13 +106,11 @@ public void cleanup()
{
// TODO We should probably cleanup the bean managers for activities?
managers.clear();

for (BeanManagerImpl beanManager : beanDeploymentArchives.values())
{
beanManager.cleanup();
}
beanDeploymentArchives.clear();

deploymentServices.cleanup();
deploymentManager.cleanup();
LoggerFactory.cleanup();
Expand Down
Expand Up @@ -58,9 +58,10 @@
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.IllegalProductException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.DummyInjectionPoint;
import org.jboss.weld.introspector.WeldMember;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.manager.DummyInjectionPoint;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.reflection.Reflections;
Expand Down Expand Up @@ -254,7 +255,7 @@ else if (instance != null)
{
throw new IllegalProductException(NON_SERIALIZABLE_PRODUCT_ERROR, getProducer());
}
InjectionPoint injectionPoint = beanManager.getCurrentInjectionPoint();
InjectionPoint injectionPoint = Container.instance().services().get(CurrentInjectionPoint.class).peek();
if (injectionPoint == null || injectionPoint.equals(DummyInjectionPoint.INSTANCE))
{
return;
Expand Down
14 changes: 13 additions & 1 deletion impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
Expand Up @@ -52,6 +52,7 @@
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.DeploymentException;
import org.jboss.weld.exceptions.ForbiddenStateException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.injection.InjectionContextImpl;
import org.jboss.weld.injection.WeldInjectionPoint;
import org.jboss.weld.introspector.WeldClass;
Expand Down Expand Up @@ -144,7 +145,18 @@ protected InjectionPoint attachCorrectInjectionPoint()
{
throw new ForbiddenStateException(DELEGATE_INJECTION_POINT_NOT_FOUND, decorator);
}
return getBeanManager().replaceOrPushCurrentInjectionPoint(outerDelegateInjectionPoint);
CurrentInjectionPoint currentInjectionPoint = Container.instance().services().get(CurrentInjectionPoint.class);
if (currentInjectionPoint.peek() != null)
{
InjectionPoint originalInjectionPoint = currentInjectionPoint.pop();
currentInjectionPoint.push(outerDelegateInjectionPoint);
return originalInjectionPoint;
}
else
{
currentInjectionPoint.push(outerDelegateInjectionPoint);
return null;
}
}

/**
Expand Down
Expand Up @@ -23,6 +23,8 @@
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.Container;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.slf4j.cal10n.LocLogger;

Expand All @@ -38,7 +40,7 @@ protected AbstractFacadeBean(String idSuffix, BeanManagerImpl manager)

public T create(CreationalContext<T> creationalContext)
{
InjectionPoint injectionPoint = this.getBeanManager().getCurrentInjectionPoint();
InjectionPoint injectionPoint = Container.instance().services().get(CurrentInjectionPoint.class).peek();
if (injectionPoint != null)
{
return newInstance(injectionPoint);
Expand Down
Expand Up @@ -22,6 +22,8 @@
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.Container;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.collections.Arrays2;

Expand Down Expand Up @@ -52,7 +54,7 @@ public InjectionPointBean(BeanManagerImpl manager)

public InjectionPoint create(CreationalContext<InjectionPoint> creationalContext)
{
return getBeanManager().getCurrentInjectionPoint();
return Container.instance().services().get(CurrentInjectionPoint.class).peek();
}

public void destroy(InjectionPoint instance, CreationalContext<InjectionPoint> creationalContext)
Expand Down
Expand Up @@ -34,7 +34,9 @@
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.util.TypeLiteral;

import org.jboss.weld.Container;
import org.jboss.weld.exceptions.InvalidObjectException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resolution.ResolvableWeldClass;
import org.jboss.weld.util.Beans;
Expand Down Expand Up @@ -68,14 +70,14 @@ public T get()
// Push in an empty CC to ensure that we don't get the CC of whatever is injecting the bean containing the Instance injection point
try
{
getBeanManager().pushDummyInjectionPoint();
Container.instance().services().get(CurrentInjectionPoint.class).pushDummy();
@SuppressWarnings("unchecked")
T instance = (T) getBeanManager().getReference(bean, getType(), getBeanManager().createCreationalContext(bean));
return instance;
}
finally
{
getBeanManager().popDummyInjectionPoint();
Container.instance().services().get(CurrentInjectionPoint.class).popDummy();
}
}

Expand Down
Expand Up @@ -30,10 +30,10 @@
import javax.enterprise.inject.spi.Bean;

import org.jboss.weld.Container;
import org.jboss.weld.context.CreationalContextImpl;
import org.jboss.weld.context.WeldCreationalContext;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
import org.slf4j.cal10n.LocLogger;

Expand All @@ -57,8 +57,6 @@ public class ClientProxyMethodHandler implements MethodHandler, Serializable
// The bean index in the manager
private final String id;

private final BeanManagerImpl manager;

private static final ThreadLocal<WeldCreationalContext<?>> currentCreationalContext = new ThreadLocal<WeldCreationalContext<?>>();

/**
Expand All @@ -67,11 +65,10 @@ public class ClientProxyMethodHandler implements MethodHandler, Serializable
* @param bean The bean to proxy
* @param beanIndex The index to the bean in the manager bean list
*/
public ClientProxyMethodHandler(Bean<?> bean, BeanManagerImpl manager, String id)
public ClientProxyMethodHandler(Bean<?> bean, String id)
{
this.bean = bean;
this.id = id;
this.manager = manager;
log.trace("Created method handler for bean " + bean + " identified as " + id);
}

Expand Down Expand Up @@ -128,7 +125,7 @@ private <T> T getProxiedInstance(Bean<T> bean)
boolean outer;
if (currentCreationalContext.get() == null)
{
creationalContext = manager.createCreationalContext(bean);
creationalContext = new CreationalContextImpl<T>(bean);
currentCreationalContext.set(creationalContext);
outer = true;
}
Expand All @@ -139,14 +136,14 @@ private <T> T getProxiedInstance(Bean<T> bean)
}
try
{
Context context = manager.getContext(bean.getScope());
Context context = Container.instance().deploymentManager().getContext(bean.getScope());
// Ensure that there is no injection point associated
manager.pushDummyInjectionPoint();
Container.instance().services().get(CurrentInjectionPoint.class).pushDummy();
return context.get(bean, creationalContext);
}
finally
{
manager.popDummyInjectionPoint();
Container.instance().services().get(CurrentInjectionPoint.class).popDummy();
if (outer)
{
currentCreationalContext.remove();
Expand Down
Expand Up @@ -21,19 +21,19 @@
import static org.jboss.weld.logging.messages.BeanMessage.PROXY_INSTANTIATION_FAILED;

import java.io.Serializable;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;

import javax.enterprise.inject.spi.Bean;

import org.jboss.weld.Container;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.logging.messages.BeanMessage;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.serialization.spi.ContextualStore;
import org.jboss.weld.util.Proxies;
import org.jboss.weld.util.Proxies.TypeInfo;
import org.jboss.weld.util.collections.ConcurrentCache;

import com.google.common.base.Function;
import com.google.common.collect.MapMaker;

/**
* A proxy pool for holding scope adaptors (client proxies)
Expand All @@ -44,21 +44,34 @@
*/
public class ClientProxyProvider
{
private static final long serialVersionUID = 9029999149357529341L;

private static final Function<Bean<Object>, Object> CREATE_CLIENT_PROXY = new Function<Bean<Object>, Object> ()
{

public Object apply(Bean<Object> from)
{
String id = Container.instance().services().get(ContextualStore.class).putIfAbsent(from);
if (id == null)
{
throw new DefinitionException(BEAN_ID_CREATION_FAILED, from);
}
return createClientProxy(from, id);
}
};

/**
* A container/cache for previously created proxies
*
* @author Nicklas Karlsson
*/
private final ConcurrentCache<Bean<? extends Object>, Object> pool;
private final ConcurrentMap<Bean<Object>, Object> pool;

/**
* Constructor
*/
public ClientProxyProvider()
{
this.pool = new ConcurrentCache<Bean<? extends Object>, Object>();
this.pool = new MapMaker().makeComputingMap(CREATE_CLIENT_PROXY);
}

/**
Expand All @@ -74,11 +87,11 @@ public ClientProxyProvider()
* @throws InstantiationException When the proxy couldn't be created
* @throws IllegalAccessException When the proxy couldn't be created
*/
private static <T> T createClientProxy(Bean<T> bean, BeanManagerImpl manager, String id) throws RuntimeException
private static <T> T createClientProxy(Bean<T> bean, String id) throws RuntimeException
{
try
{
return Proxies.<T>createProxy(new ClientProxyMethodHandler(bean, manager, id), TypeInfo.of(bean.getTypes()).add(Serializable.class));
return Proxies.<T>createProxy(new ClientProxyMethodHandler(bean, id), TypeInfo.of(bean.getTypes()).add(Serializable.class));
}
catch (InstantiationException e)
{
Expand All @@ -99,23 +112,9 @@ private static <T> T createClientProxy(Bean<T> bean, BeanManagerImpl manager, St
* @param bean The bean to get a proxy to
* @return the client proxy for the bean
*/
public <T> T getClientProxy(final BeanManagerImpl manager, final Bean<T> bean)
public <T> T getClientProxy(final Bean<T> bean)
{
T instance = pool.putIfAbsent(bean, new Callable<T>()
{

public T call() throws Exception
{
String id = Container.instance().services().get(ContextualStore.class).putIfAbsent(bean);
if (id == null)
{
throw new DefinitionException(BEAN_ID_CREATION_FAILED, bean);
}
return createClientProxy(bean, manager, id);
}

});
return instance;
return (T) pool.get(bean);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions impl/src/main/java/org/jboss/weld/bootstrap/Validator.java
Expand Up @@ -94,8 +94,8 @@
import org.jboss.weld.util.reflection.Reflections;

import com.google.common.base.Supplier;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;

/**
* Checks a list of beans for DeploymentExceptions and their subclasses
Expand Down Expand Up @@ -322,7 +322,7 @@ public void validateBeans(Collection<? extends Bean<?>> beans, Collection<RIBean

public void validateBeanNames(BeanManagerImpl beanManager)
{
Multimap<String, Bean<?>> namedAccessibleBeans = Multimaps.newSetMultimap(new HashMap<String, Collection<Bean<?>>>(), new Supplier<Set<Bean<?>>>()
SetMultimap<String, Bean<?>> namedAccessibleBeans = Multimaps.newSetMultimap(new HashMap<String, Collection<Bean<?>>>(), new Supplier<Set<Bean<?>>>()
{

public Set<Bean<?>> get()
Expand Down
Expand Up @@ -71,6 +71,7 @@
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.exceptions.ForbiddenArgumentException;
import org.jboss.weld.exceptions.ForbiddenStateException;
import org.jboss.weld.injection.CurrentInjectionPoint;
import org.jboss.weld.jsf.JsfApiAbstraction;
import org.jboss.weld.logging.messages.VersionMessage;
import org.jboss.weld.manager.BeanManagerImpl;
Expand Down Expand Up @@ -319,6 +320,7 @@ private ServiceRegistry getImplementationServices(ResourceLoader resourceLoader)
services.add(ContextualStore.class, new ContextualStoreImpl());
services.add(ServiceLoaderFactory.class, new DefaultServiceLoaderFactory());
services.add(JavassistCleaner.class, new JavassistCleaner());
services.add(CurrentInjectionPoint.class, new CurrentInjectionPoint());
return services;
}

Expand Down

0 comments on commit 36db00a

Please sign in to comment.