Skip to content

Commit

Permalink
use predestroy callback to note bean instance was removed
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@1989 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Mar 14, 2009
1 parent 962047c commit bcdc2c4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
Expand Up @@ -238,6 +238,10 @@ public void destroy(T instance)
{
throw new IllegalArgumentException("instance to destroy cannot be null");
}
if (!(instance instanceof EnterpriseBeanInstance))
{
throw new IllegalArgumentException("Cannot destroy session bean instance not created by the container");
}
EnterpriseBeanInstance enterpiseBeanInstance = (EnterpriseBeanInstance) instance;
if (enterpiseBeanInstance.isDestroyed())
{
Expand Down
Expand Up @@ -16,11 +16,9 @@
*/
package org.jboss.webbeans.bean.proxy;

import java.util.Collection;

/**
* Interface implemented by all enterprise bean proxies to determine if
* the enterprise bean has already had a remove method called by the application
* Interface implemented by all enterprise bean proxies to query/control the proxy
*
* @author Pete Muir
*
Expand All @@ -35,10 +33,6 @@ public interface EnterpriseBeanInstance
*/
public boolean isDestroyed();

/**
* The unproxied version of this object
* @return
*/
public Collection<Object> getUnproxiedInstances();
public void setDestroyed(boolean destroyed);

}
Expand Up @@ -61,6 +61,7 @@ protected Set<Class<?>> initialValue()

}

// TODO Surely we can do this better!
public static boolean isContextualInstance(Class<?> beanClass)
{
return contextualInstance.get().contains(beanClass);
Expand Down Expand Up @@ -127,6 +128,24 @@ public Object invoke(Object self, Method method, Method proceed, Object[] args)
{
return destroyed;
}
else if ("setDestroyed".equals(method.getName()))
{
if (args.length != 1)
{
throw new IllegalArgumentException("enterpriseBeanInstance.setDestroyed() called with >1 argument");
}
if (!args[0].getClass().equals(boolean.class))
{
throw new IllegalArgumentException("enterpriseBeanInstance.setDestroyed() called with non-boolean argument");
}
destroyed = ((Boolean) args[0]).booleanValue();
}

if (destroyed)
{
return null;
}

Class<?> businessInterface = method.getDeclaringClass();
Object proxiedInstance = proxiedInstances.get(businessInterface);
if (proxiedInstance == null)
Expand All @@ -148,17 +167,6 @@ public Object invoke(Object self, Method method, Method proceed, Object[] args)
proxiedInstances.put(businessInterface, proxiedInstance);
}
Method proxiedMethod = Reflections.lookupMethod(method, proxiedInstance);
if (removeMethods.contains(proxiedMethod))
{
if (canCallRemoveMethods)
{
destroyed = true;
}
else
{
throw new UnsupportedOperationException("Remove method can't be called directly on non-dependent scoped Enterprise Beans");
}
}
try
{
setContextualInstance(beanClass, true);
Expand All @@ -172,4 +180,5 @@ public Object invoke(Object self, Method method, Method proceed, Object[] args)
}

}

}
Expand Up @@ -23,6 +23,7 @@

import org.jboss.webbeans.CurrentManager;
import org.jboss.webbeans.bean.EnterpriseBean;
import org.jboss.webbeans.bean.proxy.EnterpriseBeanInstance;
import org.jboss.webbeans.bean.proxy.EnterpriseBeanProxyMethodHandler;

/**
Expand Down Expand Up @@ -65,6 +66,7 @@ public void preDestroy(InvocationContext invocationContext) throws Exception
{
enterpriseBean.preDestroy(target);
}
getEnterpriseBeanInstance(enterpriseBean).setDestroyed(true);
invocationContext.proceed();
}

Expand All @@ -78,12 +80,28 @@ private static <T> EnterpriseBean<T> getBean(Class<? extends T> beanClass)
{
if (EnterpriseBeanProxyMethodHandler.isContextualInstance(beanClass))
{
// Access all non-new enterprise beans.
// TODO Deal with XML defined enterprise beans!
return (EnterpriseBean<T>) CurrentManager.rootManager().getEnterpriseBeanMap().get(beanClass);
}
else
{
// Access all @New enterprise beans
return (EnterpriseBean<T>) CurrentManager.rootManager().getNewEnterpriseBeanMap().get(beanClass);
}
}

private static <T> EnterpriseBeanInstance getEnterpriseBeanInstance(EnterpriseBean<T> bean)
{
T instance = CurrentManager.rootManager().getContext(bean.getScopeType()).get(bean);
if (instance instanceof EnterpriseBeanInstance)
{
return (EnterpriseBeanInstance) instance;
}
else
{
throw new IllegalStateException("Contextual instance not an session bean created by the container");
}
}

}

0 comments on commit bcdc2c4

Please sign in to comment.