Search in sources :

Example 6 with JCDIService

use of com.sun.enterprise.container.common.spi.JCDIService in project Payara by payara.

the class ManagedBeanManagerImpl method destroyManagedBean.

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);
        // in a failure scenario it's possible that bundleNonManagedObjs is null
        if (bundleNonManagedObjs == 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());
        } else {
            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());
        }
        InterceptorInvoker invoker = (InterceptorInvoker) desc.getSupportingInfoForBeanInstance(managedBeanInstance);
        try {
            invoker.invokePreDestroy();
        } catch (Exception e) {
            _logger.log(Level.FINE, "Managed bean " + desc.getBeanClassName() + " PreDestroy", e);
        }
        desc.clearBeanInstanceInfo(managedBeanInstance);
    }
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) InterceptorInvoker(com.sun.enterprise.container.common.spi.InterceptorInvoker) ManagedBeanDescriptor(com.sun.enterprise.deployment.ManagedBeanDescriptor) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) Field(java.lang.reflect.Field) Proxy(java.lang.reflect.Proxy) NamingObjectProxy(org.glassfish.api.naming.NamingObjectProxy)

Example 7 with JCDIService

use of com.sun.enterprise.container.common.spi.JCDIService in project Payara by payara.

the class ManagedBeanManagerImpl method loadManagedBeans.

public void loadManagedBeans(Application app) {
    JCDIService jcdiService = habitat.getService(JCDIService.class);
    for (BundleDescriptor bundle : app.getBundleDescriptors()) {
        if (!bundleEligible(bundle)) {
            continue;
        }
        boolean isCDIBundle = (jcdiService != null && jcdiService.isJCDIEnabled(bundle));
        for (ManagedBeanDescriptor next : bundle.getManagedBeans()) {
            try {
                Set<String> interceptorClasses = next.getAllInterceptorClasses();
                Class targetClass = bundle.getClassLoader().loadClass(next.getBeanClassName());
                InterceptorInfo interceptorInfo = new InterceptorInfo();
                interceptorInfo.setTargetClass(targetClass);
                interceptorInfo.setInterceptorClassNames(interceptorClasses);
                interceptorInfo.setAroundConstructInterceptors(next.getAroundConstructCallbackInterceptors(targetClass, getConstructor(targetClass, isCDIBundle)));
                interceptorInfo.setPostConstructInterceptors(next.getCallbackInterceptors(LifecycleCallbackDescriptor.CallbackType.POST_CONSTRUCT));
                interceptorInfo.setPreDestroyInterceptors(next.getCallbackInterceptors(LifecycleCallbackDescriptor.CallbackType.PRE_DESTROY));
                if (next.hasAroundInvokeMethod()) {
                    interceptorInfo.setHasTargetClassAroundInvoke(true);
                }
                Map<Method, List> interceptorChains = new HashMap<Method, List>();
                for (Method m : targetClass.getMethods()) {
                    interceptorChains.put(m, next.getAroundInvokeInterceptors(m));
                }
                interceptorInfo.setAroundInvokeInterceptorChains(interceptorChains);
                // TODO can optimize this out for the non-JAXRS, non-application specified interceptor case
                interceptorInfo.setSupportRuntimeDelegate(true);
                JavaEEInterceptorBuilderFactory interceptorBuilderFactory = habitat.getService(JavaEEInterceptorBuilderFactory.class);
                JavaEEInterceptorBuilder builder = interceptorBuilderFactory.createBuilder(interceptorInfo);
                next.setInterceptorBuilder(builder);
                compEnvManager.bindToComponentNamespace(next);
                String jndiName = next.getGlobalJndiName();
                ManagedBeanNamingProxy namingProxy = new ManagedBeanNamingProxy(next, habitat);
                if (processType.isServer()) {
                    namingManager.publishObject(jndiName, namingProxy, true);
                } else {
                    // Can't store them in server's global naming service so keep
                    // them in local map.
                    appClientManagedBeans.put(jndiName, namingProxy);
                }
            } catch (Exception e) {
                throw new RuntimeException("Error binding ManagedBean " + next.getBeanClassName() + " with name = " + next.getName(), e);
            }
        }
        jcdiManagedBeanInstanceMap.put(bundle, Collections.synchronizedMap(new HashMap<Object, JCDIService.JCDIInjectionContext>()));
    }
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) HashMap(java.util.HashMap) InterceptorInfo(com.sun.enterprise.container.common.spi.util.InterceptorInfo) ManagedBeanDescriptor(com.sun.enterprise.deployment.ManagedBeanDescriptor) Method(java.lang.reflect.Method) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) JavaEEInterceptorBuilderFactory(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilderFactory) JavaEEInterceptorBuilder(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder) List(java.util.List)

Example 8 with JCDIService

use of com.sun.enterprise.container.common.spi.JCDIService in project Payara by payara.

the class InjectionManagerImpl method createManagedObject.

/**
 * Create a managed object for the given class. The object will be injected and any PostConstruct methods will be
 * called. The returned object can be cast to the clazz type but is not necessarily a direct reference to the managed
 * instance. All invocations on the returned object should be on its public methods.
 *
 * It is the responsibility of the caller to destroy the returned object by calling destroyManagedObject(Object
 * managedObject).
 *
 * @param clazz
 *            Class to be instantiated
 * @return managed object
 * @throws InjectionException
 */
public <T> T createManagedObject(Class<T> clazz) throws InjectionException {
    T managedObject = null;
    try {
        ManagedBean managedBeanAnn = clazz.getAnnotation(ManagedBean.class);
        ManagedBeanManager managedBeanMgr = serviceLocator.getService(ManagedBeanManager.class);
        if (managedBeanAnn != null) {
            // EE style @ManagedBean
            // Create , inject, and call PostConstruct via managed bean manager
            managedObject = managedBeanMgr.createManagedBean(clazz);
        } else {
            JCDIService cdiService = serviceLocator.getService(JCDIService.class);
            if (cdiService != null && cdiService.isCurrentModuleJCDIEnabled()) {
                // Create , inject, and call PostConstruct via managed bean manager
                managedObject = managedBeanMgr.createManagedBean(clazz);
            } else {
                // Not in a 299-enabled module and not annoated with @ManagedBean, so
                // just instantiate using new and perform injection
                Constructor<T> noArgCtor = clazz.getConstructor();
                managedObject = noArgCtor.newInstance();
                // Inject and call PostConstruct
                injectInstance(managedObject);
            }
        }
    } catch (Exception e) {
        throw new InjectionException(localStrings.getLocalString("injection-manager.error-creating-managed-object", "Error creating managed object for class: {0}", clazz), e);
    }
    return managedObject;
}
Also used : InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) JCDIService(com.sun.enterprise.container.common.spi.JCDIService) ManagedBean(javax.annotation.ManagedBean) NamingException(javax.naming.NamingException) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ManagedBeanManager(com.sun.enterprise.container.common.spi.ManagedBeanManager)

Example 9 with JCDIService

use of com.sun.enterprise.container.common.spi.JCDIService in project Payara by payara.

the class InjectionManagerImpl method destroyManagedObject.

/**
 * Destroy a managed object that was created via createManagedObject. Any PreDestroy methods will be called.
 *
 * @param managedObject
 * @param validate
 *            if false, do nothing if the instance is not registered
 * @throws InjectionException
 */
public void destroyManagedObject(Object managedObject, boolean validate) throws InjectionException {
    Class managedObjectClass = managedObject.getClass();
    ManagedBean managedBeanAnn = (ManagedBean) managedObjectClass.getAnnotation(ManagedBean.class);
    ManagedBeanManager managedBeanMgr = serviceLocator.getService(ManagedBeanManager.class);
    JCDIService jcdiService = serviceLocator.getService(JCDIService.class);
    if ((jcdiService != null) && jcdiService.isCurrentModuleJCDIEnabled()) {
        // If 299-enabled always delegate to managed bean manager
        managedBeanMgr.destroyManagedBean(managedObject, validate);
    } else {
        // If the object's class has @ManagedBean it's a managed bean. Otherwise, ask
        // managed bean manager.
        boolean isManagedBean = (managedBeanAnn != null) || managedBeanMgr.isManagedBean(managedObject);
        if (isManagedBean) {
            managedBeanMgr.destroyManagedBean(managedObject, validate);
        } else {
            this.invokeInstancePreDestroy(managedObject, validate);
        }
    }
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) ManagedBean(javax.annotation.ManagedBean) ManagedBeanManager(com.sun.enterprise.container.common.spi.ManagedBeanManager)

Aggregations

JCDIService (com.sun.enterprise.container.common.spi.JCDIService)9 BundleDescriptor (com.sun.enterprise.deployment.BundleDescriptor)4 EjbBundleDescriptor (com.sun.enterprise.deployment.EjbBundleDescriptor)4 WebBundleDescriptor (com.sun.enterprise.deployment.WebBundleDescriptor)4 InterceptorInvoker (com.sun.enterprise.container.common.spi.InterceptorInvoker)3 JavaEEInterceptorBuilder (com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder)3 ManagedBeanManager (com.sun.enterprise.container.common.spi.ManagedBeanManager)3 ManagedBean (javax.annotation.ManagedBean)3 NamingException (javax.naming.NamingException)3 InjectionException (com.sun.enterprise.container.common.spi.util.InjectionException)2 ManagedBeanDescriptor (com.sun.enterprise.deployment.ManagedBeanDescriptor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 JavaEEInterceptorBuilderFactory (com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilderFactory)1 InterceptorInfo (com.sun.enterprise.container.common.spi.util.InterceptorInfo)1 EjbInterceptor (com.sun.enterprise.deployment.EjbInterceptor)1 MethodDescriptor (com.sun.enterprise.deployment.MethodDescriptor)1 JarURIPattern (com.sun.enterprise.util.net.JarURIPattern)1 JspProbeEmitterImpl (com.sun.enterprise.web.jsp.JspProbeEmitterImpl)1