Search in sources :

Example 1 with JavaEEInterceptorBuilder

use of com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder 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 JavaEEInterceptorBuilder

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

the class ManagedBeanManagerImpl method loadManagedBeans.

@Override
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<? extends InterceptorDescriptor>> interceptorChains = new HashMap<>();
                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<>()));
    }
}
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) InterceptorDescriptor(com.sun.enterprise.deployment.InterceptorDescriptor) JavaEEInterceptorBuilderFactory(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilderFactory) JavaEEInterceptorBuilder(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder) List(java.util.List)

Example 3 with JavaEEInterceptorBuilder

use of com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder 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 4 with JavaEEInterceptorBuilder

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

the class ManagedBeanManagerImpl method registerRuntimeInterceptor.

/**
 * Apply a runtime interceptor instance to all managed beans in the given module
 * @param interceptorInstance
 * @param bundle bundle descripto
 */
@Override
public void registerRuntimeInterceptor(Object interceptorInstance, BundleDescriptor bundle) {
    for (ManagedBeanDescriptor next : bundle.getManagedBeans()) {
        JavaEEInterceptorBuilder builder = (JavaEEInterceptorBuilder) next.getInterceptorBuilder();
        builder.addRuntimeInterceptor(interceptorInstance);
    }
}
Also used : JavaEEInterceptorBuilder(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder) ManagedBeanDescriptor(com.sun.enterprise.deployment.ManagedBeanDescriptor)

Example 5 with JavaEEInterceptorBuilder

use of com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder 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)

Aggregations

JavaEEInterceptorBuilder (com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder)5 InterceptorInvoker (com.sun.enterprise.container.common.spi.InterceptorInvoker)3 JCDIService (com.sun.enterprise.container.common.spi.JCDIService)3 BundleDescriptor (com.sun.enterprise.deployment.BundleDescriptor)3 EjbBundleDescriptor (com.sun.enterprise.deployment.EjbBundleDescriptor)3 WebBundleDescriptor (com.sun.enterprise.deployment.WebBundleDescriptor)3 ManagedBeanDescriptor (com.sun.enterprise.deployment.ManagedBeanDescriptor)2 JavaEEInterceptorBuilderFactory (com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilderFactory)1 InterceptorInfo (com.sun.enterprise.container.common.spi.util.InterceptorInfo)1 InterceptorDescriptor (com.sun.enterprise.deployment.InterceptorDescriptor)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 List (java.util.List)1