Skip to content

Commit

Permalink
WBRI-159, WBRI-165,
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1991 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Mar 14, 2009
1 parent fc3be9a commit b749199
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 184 deletions.
25 changes: 7 additions & 18 deletions impl/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -65,7 +65,7 @@
import org.jboss.webbeans.context.ContextMap;
import org.jboss.webbeans.context.CreationalContextImpl;
import org.jboss.webbeans.ejb.EjbDescriptorCache;
import org.jboss.webbeans.ejb.spi.EjbResolver;
import org.jboss.webbeans.ejb.spi.EjbServices;
import org.jboss.webbeans.event.EventManager;
import org.jboss.webbeans.event.ObserverImpl;
import org.jboss.webbeans.injection.ResolvableAnnotatedClass;
Expand Down Expand Up @@ -125,14 +125,13 @@ public class ManagerImpl implements WebBeansManager, Serializable
private transient List<Bean<?>> beans;
// The registered beans, mapped by implementation class
private transient final Map<Class<?>, EnterpriseBean<?>> newEnterpriseBeanMap;
private transient final Map<Class<?>, EnterpriseBean<?>> enterpriseBeanMap;
// The registered decorators
private transient final Set<Decorator> decorators;
// The registered interceptors
private transient final Set<Interceptor> interceptors;

// The EJB resolver provided by the container
private transient final EjbResolver ejbResolver;
private transient final EjbServices ejbServices;

private transient final EjbDescriptorCache ejbDescriptorCache;

Expand All @@ -151,17 +150,16 @@ public class ManagerImpl implements WebBeansManager, Serializable
/**
* Create a new manager
*
* @param ejbResolver the ejbResolver to use
* @param ejbServices the ejbResolver to use
*/
public ManagerImpl(NamingContext namingContext, EjbResolver ejbResolver, ResourceLoader resourceLoader, TransactionServices transactionServices)
public ManagerImpl(NamingContext namingContext, EjbServices ejbServices, ResourceLoader resourceLoader, TransactionServices transactionServices)
{
this.ejbResolver = ejbResolver;
this.ejbServices = ejbServices;
this.namingContext = namingContext;
this.resourceLoader = resourceLoader;
this.transactionServices = transactionServices;
this.beans = new CopyOnWriteArrayList<Bean<?>>();
this.newEnterpriseBeanMap = new ConcurrentHashMap<Class<?>, EnterpriseBean<?>>();
this.enterpriseBeanMap = new ConcurrentHashMap<Class<?>, EnterpriseBean<?>>();
this.resolver = new Resolver(this);
this.clientProxyProvider = new ClientProxyProvider();
this.decorators = new HashSet<Decorator>();
Expand Down Expand Up @@ -381,10 +379,6 @@ public void setBeans(Set<RIBean<?>> beans)
{
newEnterpriseBeanMap.put(bean.getType(), (EnterpriseBean<?>) bean);
}
else if (bean instanceof EnterpriseBean)
{
enterpriseBeanMap.put(bean.getType(), (EnterpriseBean<?>) bean);
}
}
resolver.clear();
}
Expand All @@ -399,11 +393,6 @@ public Map<Class<?>, EnterpriseBean<?>> getNewEnterpriseBeanMap()
{
return newEnterpriseBeanMap;
}

public Map<Class<?>, EnterpriseBean<?>> getEnterpriseBeanMap()
{
return enterpriseBeanMap;
}

/**
* The beans registered with the Web Bean manager. For internal use
Expand Down Expand Up @@ -899,9 +888,9 @@ public NamingContext getNaming()
return namingContext;
}

public final EjbResolver getEjbResolver()
public final EjbServices getEjbServices()
{
return ejbResolver;
return ejbServices;
}

public final ResourceLoader getResourceLoader()
Expand Down
10 changes: 9 additions & 1 deletion impl/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java
Expand Up @@ -35,12 +35,14 @@
import javax.inject.DefinitionException;
import javax.interceptor.Interceptor;

import org.jboss.webbeans.CurrentManager;
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bean.proxy.EnterpriseBeanInstance;
import org.jboss.webbeans.bean.proxy.EnterpriseBeanProxyMethodHandler;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.context.DependentStorageRequest;
import org.jboss.webbeans.ejb.InternalEjbDescriptor;
import org.jboss.webbeans.ejb.api.EjbReference;
import org.jboss.webbeans.ejb.spi.BusinessInterfaceDescriptor;
import org.jboss.webbeans.introspector.AnnotatedClass;
import org.jboss.webbeans.introspector.AnnotatedMethod;
Expand Down Expand Up @@ -209,7 +211,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()));
((ProxyObject) instance).setHandler(new EnterpriseBeanProxyMethodHandler(this));
if (log.isTraceEnabled())
log.trace("Enterprise bean instance created for bean " + this);
return instance;
Expand Down Expand Up @@ -375,5 +377,11 @@ public boolean isMethodExistsOnTypes(AnnotatedMethod<?> method)
}
return false;
}

public EjbReference<T> createReference()
{
return getManager().getEjbServices().resolveEJB(getEjbDescriptor(), CurrentManager.rootManager().getNaming());
}

}

16 changes: 8 additions & 8 deletions impl/src/main/java/org/jboss/webbeans/bean/SimpleBean.java
Expand Up @@ -219,12 +219,12 @@ protected void callPostConstruct(T instance)
protected void initEjbInjectionPoints()
{
this.ejbInjectionPoints = new HashSet<AnnotatedInjectionPoint<?, ?>>();
for (AnnotatedField<?> field : annotatedItem.getAnnotatedFields(manager.getEjbResolver().getEJBAnnotation()))
for (AnnotatedField<?> field : annotatedItem.getAnnotatedFields(manager.getEjbServices().getEJBAnnotation()))
{
this.ejbInjectionPoints.add(FieldInjectionPoint.of(this, field));
}

for (AnnotatedMethod<?> method : annotatedItem.getAnnotatedMethods(manager.getEjbResolver().getEJBAnnotation()))
for (AnnotatedMethod<?> method : annotatedItem.getAnnotatedMethods(manager.getEjbServices().getEJBAnnotation()))
{
this.ejbInjectionPoints.add(MethodInjectionPoint.of(this, method));
}
Expand All @@ -233,7 +233,7 @@ protected void initEjbInjectionPoints()
protected void initPersistenceUnitInjectionPoints()
{
this.persistenceUnitInjectionPoints = new HashSet<AnnotatedInjectionPoint<?, ?>>();
for (AnnotatedField<?> field : annotatedItem.getAnnotatedFields(manager.getEjbResolver().getPersistenceContextAnnotation()))
for (AnnotatedField<?> field : annotatedItem.getAnnotatedFields(manager.getEjbServices().getPersistenceContextAnnotation()))
{
if (field.getAnnotation(PersistenceContext.class).type().equals(PersistenceContextType.EXTENDED))
{
Expand All @@ -242,7 +242,7 @@ protected void initPersistenceUnitInjectionPoints()
this.persistenceUnitInjectionPoints.add(FieldInjectionPoint.of(this, field));
}

for (AnnotatedMethod<?> method : annotatedItem.getAnnotatedMethods(manager.getEjbResolver().getPersistenceContextAnnotation()))
for (AnnotatedMethod<?> method : annotatedItem.getAnnotatedMethods(manager.getEjbServices().getPersistenceContextAnnotation()))
{
if (method.getAnnotation(PersistenceContext.class).type().equals(PersistenceContextType.EXTENDED))
{
Expand All @@ -255,7 +255,7 @@ protected void initPersistenceUnitInjectionPoints()
protected void initResourceInjectionPoints()
{
this.resourceInjectionPoints = new HashSet<AnnotatedInjectionPoint<?, ?>>();
for (AnnotatedField<?> field : annotatedItem.getAnnotatedFields(manager.getEjbResolver().getResourceAnnotation()))
for (AnnotatedField<?> field : annotatedItem.getAnnotatedFields(manager.getEjbServices().getResourceAnnotation()))
{
this.resourceInjectionPoints.add(FieldInjectionPoint.of(this, field));
}
Expand All @@ -269,19 +269,19 @@ protected void injectEjbAndCommonFields(T beanInstance)

for (AnnotatedInjectionPoint<?, ?> injectionPoint : ejbInjectionPoints)
{
Object ejbInstance = manager.getEjbResolver().resolveEjb(injectionPoint, manager.getNaming());
Object ejbInstance = manager.getEjbServices().resolveEjb(injectionPoint, manager.getNaming());
injectionPoint.inject(beanInstance, ejbInstance);
}

for (AnnotatedInjectionPoint<?, ?> injectionPoint : persistenceUnitInjectionPoints)
{
Object puInstance = manager.getEjbResolver().resolvePersistenceContext(injectionPoint, manager.getNaming());
Object puInstance = manager.getEjbServices().resolvePersistenceContext(injectionPoint, manager.getNaming());
injectionPoint.inject(beanInstance, puInstance);
}

for (AnnotatedInjectionPoint<?, ?> injectionPoint : resourceInjectionPoints)
{
Object resourceInstance = manager.getEjbResolver().resolveResource(injectionPoint, manager.getNaming());
Object resourceInstance = manager.getEjbServices().resolveResource(injectionPoint, manager.getNaming());
injectionPoint.inject(beanInstance, resourceInstance);
}

Expand Down
Expand Up @@ -18,16 +18,11 @@
package org.jboss.webbeans.bean.proxy;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javassist.util.proxy.MethodHandler;

import org.jboss.webbeans.CurrentManager;
import org.jboss.webbeans.bean.EnterpriseBean;
import org.jboss.webbeans.ejb.api.EjbReference;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
import org.jboss.webbeans.util.Reflections;
Expand All @@ -44,48 +39,27 @@ public class EnterpriseBeanProxyMethodHandler implements MethodHandler
// The log provider
private static final transient LogProvider log = Logging.getLogProvider(EnterpriseBeanProxyMethodHandler.class);

private static final ThreadLocal<Set<Class<?>>> contextualInstance;
private static final ThreadLocal<EnterpriseBean<?>> enterpriseBean;

static
{
contextualInstance = new ThreadLocal<Set<Class<?>>>()
{

@Override
protected Set<Class<?>> initialValue()
{
return new HashSet<Class<?>>();
}

};
enterpriseBean = new ThreadLocal<EnterpriseBean<?>>();

}

// TODO Surely we can do this better!
public static boolean isContextualInstance(Class<?> beanClass)
public static EnterpriseBean<?> getEnterpriseBean()
{
return contextualInstance.get().contains(beanClass);
return enterpriseBean.get();
}

private static void setContextualInstance(Class<?> beanClass, boolean accessing)
private static void setEnterpriseBean(EnterpriseBean<?> bean)
{
if (accessing)
{
contextualInstance.get().add(beanClass);
}
else
{
contextualInstance.get().remove(beanClass);
}
enterpriseBean.set(bean);
}

// The container provided proxy that implements all interfaces
private final Map<Class<?>, Object> proxiedInstances;
private final Map<Class<?>, String> jndiNames;
private final EjbReference<?> reference;
private final Class<?> objectInterface;
private boolean destroyed;
private boolean canCallRemoveMethods;
private final List<Method> removeMethods;
private final Class<?> beanClass;

/**
* Constructor
Expand All @@ -94,14 +68,20 @@ private static void setContextualInstance(Class<?> beanClass, boolean accessing)
*
* @param proxy The generic proxy
*/
public EnterpriseBeanProxyMethodHandler(EnterpriseBean<?> bean, Iterable<Method> removeMethods)
public EnterpriseBeanProxyMethodHandler(EnterpriseBean<?> bean)
{
this.proxiedInstances = new HashMap<Class<?>, Object>();
this.jndiNames = bean.getEjbDescriptor().getLocalBusinessInterfacesJndiNames();
this.canCallRemoveMethods = bean.canCallRemoveMethods();
this.removeMethods = bean.getEjbDescriptor().getRemoveMethods();
this.reference = bean.createReference();
this.destroyed = false;
this.beanClass = bean.getType();
this.objectInterface = bean.getEjbDescriptor().getObjectInterface();
try
{
setEnterpriseBean(bean);
reference.create();
}
finally
{
setEnterpriseBean(null);
}
log.trace("Created enterprise bean proxy method handler for " + bean);
}

Expand All @@ -124,6 +104,7 @@ public EnterpriseBeanProxyMethodHandler(EnterpriseBean<?> bean, Iterable<Method>
*/
public Object invoke(Object self, Method method, Method proceed, Object[] args) throws Throwable
{
// EnterpriseBeanInstance methods
if ("isDestroyed".equals(method.getName()))
{
return destroyed;
Expand All @@ -147,38 +128,18 @@ else if ("setDestroyed".equals(method.getName()))
}

Class<?> businessInterface = method.getDeclaringClass();
Object proxiedInstance = proxiedInstances.get(businessInterface);
if (proxiedInstance == null)
if (businessInterface.equals(Object.class))
{
String jndiName = jndiNames.get(businessInterface);
if (jndiName == null)
{
throw new IllegalStateException("Unable to establish jndi name to use to lookup EJB");
}
try
{
setContextualInstance(beanClass, true);
proxiedInstance = CurrentManager.rootManager().getNaming().lookup(jndiName, businessInterface);
}
finally
{
setContextualInstance(beanClass, false);
}
proxiedInstances.put(businessInterface, proxiedInstance);
businessInterface = objectInterface;
}
Object proxiedInstance = reference.get(businessInterface);
Method proxiedMethod = Reflections.lookupMethod(method, proxiedInstance);
try
{
setContextualInstance(beanClass, true);
Object returnValue = Reflections.invokeAndWrap(proxiedMethod, proxiedInstance, args);
log.trace("Executed " + method + " on " + proxiedInstance + " with parameters " + args + " and got return value " + returnValue);
return returnValue;
}
finally
{
setContextualInstance(beanClass, false);
}
Object returnValue = Reflections.invokeAndWrap(proxiedMethod, proxiedInstance, args);
log.trace("Executed " + method + " on " + proxiedInstance + " with parameters " + args + " and got return value " + returnValue);
return returnValue;

}



}
Expand Up @@ -204,6 +204,7 @@ private void createSimpleBean(AnnotatedClass<?> annotatedClass)

private void createEnterpriseBean(AnnotatedClass<?> annotatedClass)
{
// TODO Don't create enterprise bean if it has no local interfaces!
EnterpriseBean<?> bean = EnterpriseBean.of(annotatedClass, manager);
createBean(bean, annotatedClass);
beans.add(NewEnterpriseBean.of(annotatedClass, manager));
Expand Down
Expand Up @@ -77,15 +77,15 @@ public void initialize()
{
throw new IllegalStateException("NamingContext is not set");
}
if (getEjbResolver() == null)
if (getEjbServices() == null)
{
throw new IllegalStateException("EjbResolver is not set");
throw new IllegalStateException("EjbServices is not set");
}
if (getTransactionServices() == null)
{
log.info("Transactional services not available. Transactional observers will be invoked synchronously.");
}
this.manager = new ManagerImpl(getNamingContext(), getEjbResolver(), getResourceLoader(), getTransactionServices());
this.manager = new ManagerImpl(getNamingContext(), getEjbServices(), getResourceLoader(), getTransactionServices());
getManager().getNaming().bind(ManagerImpl.JNDI_KEY, getManager());
CurrentManager.setRootManager(manager);
initializeContexts();
Expand Down

0 comments on commit b749199

Please sign in to comment.