destroyManagedBean(managedBean, true);
}
public void destroyManagedBean(Object managedBean, boolean validate) {
BundleDescriptor bundle = getBundle();
JCDIService jcdiService = habitat.getService(JCDIService.class);
if( (jcdiService != null) && jcdiService.isJCDIEnabled(bundle)) {
Map<Object, JCDIService.JCDIInjectionContext> bundleNonManagedObjs =
jcdiManagedBeanInstanceMap.get(bundle);
JCDIService.JCDIInjectionContext context =
bundleNonManagedObjs.remove(managedBean);
if( context == null ) {
if (validate) {
throw new IllegalStateException("Unknown JCDI-enabled managed bean " +
managedBean + " of class " + managedBean.getClass());
}
_logger.log(Level.FINE, "Unknown JCDI-enabled managed bean " +
managedBean + " of class " + managedBean.getClass());
return;
}
// Call PreDestroy and cleanup
context.cleanup(true);
} else {
Object managedBeanInstance = null;
try {
Field proxyField = managedBean.getClass().getDeclaredField("__ejb31_delegate");
final Field finalF = proxyField;
java.security.AccessController.doPrivileged(
new java.security.PrivilegedExceptionAction() {
public java.lang.Object run() throws Exception {
if( !finalF.isAccessible() ) {
finalF.setAccessible(true);
}
return null;
}
});
Proxy proxy = (Proxy) proxyField.get(managedBean);
InterceptorInvoker invoker = (InterceptorInvoker) Proxy.getInvocationHandler(proxy);
managedBeanInstance = invoker.getTargetInstance();
} catch(Exception e) {
throw new IllegalArgumentException("invalid managed bean " + managedBean, e);
}
ManagedBeanDescriptor desc = bundle.getManagedBeanByBeanClass(
managedBeanInstance.getClass().getName());
if( desc == null ) {
throw new IllegalStateException("Could not retrieve managed bean descriptor for " +
managedBean + " of class " + managedBean.getClass());