Search in sources :

Example 1 with InterceptorInvoker

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

the class ManagedBeanManagerImpl method createManagedBean.

/**
 * @param desc can be null if JCDI enabled bundle.
 * @param managedBeanClass
 * @return
 * @throws Exception
 */
@Override
public <T> T createManagedBean(ManagedBeanDescriptor desc, Class<T> managedBeanClass) throws Exception {
    JCDIService jcdiService = habitat.getService(JCDIService.class);
    BundleDescriptor bundleDescriptor = null;
    if (desc == null) {
        bundleDescriptor = getBundle();
    } else {
        bundleDescriptor = desc.getBundleDescriptor();
    }
    if (bundleDescriptor == null) {
        throw new IllegalStateException("Class " + managedBeanClass + " is not a valid EE ManagedBean class");
    }
    T callerObject = null;
    if ((jcdiService != null) && jcdiService.isJCDIEnabled(bundleDescriptor)) {
        // Have 299 create, inject, and call PostConstruct on managed bean
        JCDIService.JCDIInjectionContext<?> jcdiContext = jcdiService.createManagedObject(managedBeanClass, bundleDescriptor);
        callerObject = (T) jcdiContext.getInstance();
        // Need to keep track of context in order to destroy properly
        Map<Object, JCDIService.JCDIInjectionContext<?>> bundleNonManagedObjs = jcdiManagedBeanInstanceMap.get(bundleDescriptor);
        bundleNonManagedObjs.put(callerObject, jcdiContext);
    } else {
        if (desc == null) {
            throw new IllegalArgumentException("CDI not enabled and no managed bean found for class: " + managedBeanClass.getName());
        }
        JavaEEInterceptorBuilder interceptorBuilder = (JavaEEInterceptorBuilder) desc.getInterceptorBuilder();
        InterceptorInvoker interceptorInvoker = interceptorBuilder.createInvoker(null);
        // This is the object passed back to the caller.
        callerObject = (T) interceptorInvoker.getProxy();
        Object[] interceptorInstances = interceptorInvoker.getInterceptorInstances();
        // Inject interceptor instances
        for (int i = 0; i < interceptorInstances.length; i++) {
            inject(interceptorInstances[i], desc);
        }
        interceptorInvoker.invokeAroundConstruct();
        // This is the managed bean class instance
        Object managedBean = interceptorInvoker.getTargetInstance();
        inject(managedBean, desc);
        interceptorInvoker.invokePostConstruct();
        desc.addBeanInstanceInfo(managedBean, interceptorInvoker);
    }
    return callerObject;
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) InterceptorInvoker(com.sun.enterprise.container.common.spi.InterceptorInvoker) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) JavaEEInterceptorBuilder(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder)

Example 2 with InterceptorInvoker

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

the class ACCJCDIServiceImpl method createEEManagedObject.

private <T> T createEEManagedObject(ManagedBeanDescriptor desc) throws Exception {
    JavaEEInterceptorBuilder interceptorBuilder = (JavaEEInterceptorBuilder) desc.getInterceptorBuilder();
    InterceptorInvoker interceptorInvoker = interceptorBuilder.createInvoker(null);
    Object[] interceptorInstances = interceptorInvoker.getInterceptorInstances();
    // Inject interceptor instances
    for (int i = 0; i < interceptorInstances.length; i++) {
        injectionMgr.injectInstance(interceptorInstances[i], desc.getGlobalJndiName(), false);
    }
    interceptorInvoker.invokeAroundConstruct();
    // This is the managed bean class instance
    @SuppressWarnings("unchecked") T managedBean = (T) interceptorInvoker.getTargetInstance();
    injectionMgr.injectInstance(managedBean, desc);
    interceptorInvoker.invokePostConstruct();
    desc.addBeanInstanceInfo(managedBean, interceptorInvoker);
    return managedBean;
}
Also used : JavaEEInterceptorBuilder(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder) InterceptorInvoker(com.sun.enterprise.container.common.spi.InterceptorInvoker)

Example 3 with InterceptorInvoker

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

the class ManagedBeanManagerImpl method destroyManagedBean.

@Override
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;
            PrivilegedExceptionAction<Void> action = () -> {
                if (!finalF.isAccessible()) {
                    finalF.setAccessible(true);
                }
                return null;
            };
            AccessController.doPrivileged(action);
            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 4 with InterceptorInvoker

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

the class ManagedBeanManagerImpl method createManagedBean.

/**
 * @param desc can be null if JCDI enabled bundle.
 * @param managedBeanClass
 * @param invokePostConstruct
 * @return
 * @throws Exception
 */
@Override
public <T> T createManagedBean(ManagedBeanDescriptor desc, Class<T> managedBeanClass, boolean invokePostConstruct) throws Exception {
    JCDIService jcdiService = habitat.getService(JCDIService.class);
    BundleDescriptor bundleDescriptor = null;
    if (desc == null) {
        bundleDescriptor = getBundle();
    } else {
        bundleDescriptor = desc.getBundleDescriptor();
    }
    if (bundleDescriptor == null) {
        throw new IllegalStateException("Class " + managedBeanClass + " is not a valid EE ManagedBean class");
    }
    T callerObject = null;
    if ((jcdiService != null) && jcdiService.isJCDIEnabled(bundleDescriptor)) {
        // Have 299 create, inject, and call PostConstruct (if desired) on managed bean
        JCDIService.JCDIInjectionContext<?> jcdiContext = jcdiService.createManagedObject(managedBeanClass, bundleDescriptor, invokePostConstruct);
        callerObject = (T) jcdiContext.getInstance();
        // Need to keep track of context in order to destroy properly
        Map<Object, JCDIService.JCDIInjectionContext<?>> bundleNonManagedObjs = jcdiManagedBeanInstanceMap.get(bundleDescriptor);
        bundleNonManagedObjs.put(callerObject, jcdiContext);
    } else {
        if (desc == null) {
            throw new IllegalArgumentException("CDI not enabled and no managed bean found for class: " + managedBeanClass.getName());
        }
        JavaEEInterceptorBuilder interceptorBuilder = (JavaEEInterceptorBuilder) desc.getInterceptorBuilder();
        // This is the managed bean class instance
        T managedBean = managedBeanClass.newInstance();
        InterceptorInvoker interceptorInvoker = interceptorBuilder.createInvoker(managedBean);
        // This is the object passed back to the caller.
        callerObject = (T) interceptorInvoker.getProxy();
        Object[] interceptorInstances = interceptorInvoker.getInterceptorInstances();
        inject(managedBean, desc);
        // Inject interceptor instances
        for (int i = 0; i < interceptorInstances.length; i++) {
            inject(interceptorInstances[i], desc);
        }
        interceptorInvoker.invokePostConstruct();
        desc.addBeanInstanceInfo(managedBean, interceptorInvoker);
    }
    return callerObject;
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) InterceptorInvoker(com.sun.enterprise.container.common.spi.InterceptorInvoker) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) JavaEEInterceptorBuilder(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder)

Example 5 with InterceptorInvoker

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

the class ManagedBeanManagerImpl method unloadManagedBeans.

@Override
public void unloadManagedBeans(Application app) {
    for (BundleDescriptor bundle : app.getBundleDescriptors()) {
        if (!bundleEligible(bundle)) {
            continue;
        }
        Map<Object, JCDIService.JCDIInjectionContext<?>> jcdiInstances = jcdiManagedBeanInstanceMap.remove(bundle);
        if (jcdiInstances != null) {
            for (JCDIService.JCDIInjectionContext<?> next : jcdiInstances.values()) {
                try {
                    next.cleanup(true);
                } catch (Exception e) {
                    _logger.log(Level.FINE, "Exception during JCDI cleanup for " + next, e);
                }
            }
        }
        for (ManagedBeanDescriptor next : bundle.getManagedBeans()) {
            for (Object instance : next.getBeanInstances()) {
                InterceptorInvoker invoker = (InterceptorInvoker) next.getSupportingInfoForBeanInstance(instance);
                try {
                    invoker.invokePreDestroy();
                } catch (Exception e) {
                    _logger.log(Level.FINE, "Managed bean " + next.getBeanClassName() + " PreDestroy", e);
                }
            }
            com.sun.enterprise.container.common.spi.util.ComponentEnvManager compEnvManager = habitat.getService(com.sun.enterprise.container.common.spi.util.ComponentEnvManager.class);
            try {
                compEnvManager.unbindFromComponentNamespace(next);
            } catch (javax.naming.NamingException ne) {
                _logger.log(Level.FINE, "Managed bean " + next.getBeanClassName() + " unbind", ne);
            }
            org.glassfish.api.naming.GlassfishNamingManager namingManager = habitat.getService(org.glassfish.api.naming.GlassfishNamingManager.class);
            String jndiName = next.getGlobalJndiName();
            if (processType.isServer()) {
                try {
                    namingManager.unpublishObject(jndiName);
                } catch (javax.naming.NamingException ne) {
                    _logger.log(Level.FINE, "Error unpubishing managed bean " + next.getBeanClassName() + " with jndi name " + jndiName, ne);
                }
            } else {
                appClientManagedBeans.remove(jndiName);
            }
            next.clearAllBeanInstanceInfo();
        }
    }
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) InterceptorInvoker(com.sun.enterprise.container.common.spi.InterceptorInvoker) ManagedBeanDescriptor(com.sun.enterprise.deployment.ManagedBeanDescriptor) ComponentEnvManager(com.sun.enterprise.container.common.spi.util.ComponentEnvManager) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) GlassfishNamingManager(org.glassfish.api.naming.GlassfishNamingManager)

Aggregations

InterceptorInvoker (com.sun.enterprise.container.common.spi.InterceptorInvoker)5 JCDIService (com.sun.enterprise.container.common.spi.JCDIService)4 BundleDescriptor (com.sun.enterprise.deployment.BundleDescriptor)4 EjbBundleDescriptor (com.sun.enterprise.deployment.EjbBundleDescriptor)4 WebBundleDescriptor (com.sun.enterprise.deployment.WebBundleDescriptor)4 JavaEEInterceptorBuilder (com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder)3 ManagedBeanDescriptor (com.sun.enterprise.deployment.ManagedBeanDescriptor)2 ComponentEnvManager (com.sun.enterprise.container.common.spi.util.ComponentEnvManager)1 Field (java.lang.reflect.Field)1 Proxy (java.lang.reflect.Proxy)1 GlassfishNamingManager (org.glassfish.api.naming.GlassfishNamingManager)1 NamingObjectProxy (org.glassfish.api.naming.NamingObjectProxy)1