Search in sources :

Example 21 with BundleDescriptor

use of com.sun.enterprise.deployment.BundleDescriptor in project Payara by payara.

the class ValidationNamingProxy method obtainBeanManager.

/**
 * Obtain the BeanManagerNamingProxy from hk2, so the BeanManager can be looked up
 *
 * @throws NamingException
 */
private synchronized BeanManager obtainBeanManager() throws NamingException {
    BeanManager beanManager = null;
    // Use invocation context to find applicable BeanDeploymentArchive.
    ComponentInvocation inv = invocationManager.getCurrentInvocation();
    if (inv != null) {
        JndiNameEnvironment componentEnv = compEnvManager.getJndiNameEnvironment(inv.getComponentId());
        if (componentEnv != null) {
            BundleDescriptor bundle = null;
            if (componentEnv instanceof EjbDescriptor) {
                bundle = (BundleDescriptor) ((EjbDescriptor) componentEnv).getEjbBundleDescriptor().getModuleDescriptor().getDescriptor();
            } else if (componentEnv instanceof WebBundleDescriptor) {
                bundle = (BundleDescriptor) componentEnv;
            }
            if (bundle != null) {
                BeanDeploymentArchive bda = weldDeployer.getBeanDeploymentArchiveForBundle(bundle);
                if (bda != null) {
                    WeldBootstrap bootstrap = weldDeployer.getBootstrapForApp(bundle.getApplication());
                    beanManager = bootstrap.getManager(bda);
                }
            }
        }
    }
    return beanManager;
}
Also used : WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) JndiNameEnvironment(com.sun.enterprise.deployment.JndiNameEnvironment) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) BeanManager(javax.enterprise.inject.spi.BeanManager) EjbDescriptor(com.sun.enterprise.deployment.EjbDescriptor)

Example 22 with BundleDescriptor

use of com.sun.enterprise.deployment.BundleDescriptor in project Payara by payara.

the class JCDIServiceImpl method isCurrentModuleJCDIEnabled.

@Override
public boolean isCurrentModuleJCDIEnabled() {
    BundleDescriptor bundle = null;
    ComponentInvocation inv = invocationManager.getCurrentInvocation();
    if (inv == null) {
        return false;
    }
    JndiNameEnvironment componentEnv = compEnvManager.getJndiNameEnvironment(inv.getComponentId());
    if (componentEnv != null) {
        if (componentEnv instanceof BundleDescriptor) {
            bundle = (BundleDescriptor) componentEnv;
        } else if (componentEnv instanceof EjbDescriptor) {
            bundle = ((EjbDescriptor) componentEnv).getEjbBundleDescriptor();
        }
    }
    return (bundle != null) ? isJCDIEnabled(bundle) : false;
}
Also used : BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) JndiNameEnvironment(com.sun.enterprise.deployment.JndiNameEnvironment) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation) EjbDescriptor(com.sun.enterprise.deployment.EjbDescriptor)

Example 23 with BundleDescriptor

use of com.sun.enterprise.deployment.BundleDescriptor in project Payara by payara.

the class JCDIServiceImpl method createInterceptorInstance.

/**
 * @param <T> interceptor type
 * @param interceptorClass The interceptor class.
 * @param ejb The ejb descriptor.
 * @param ejbContext The ejb jcdi context.  This context is only used to store any contexts for interceptors
 *                   not bound to the ejb.  Nothing else in this context will be used in this method as they are
 *                   most likely null.
 * @param ejbInterceptors All of the ejb interceptors for the ejb.
 *
 * @return The interceptor instance.
 */
@Override
@SuppressWarnings("unchecked")
public <T> T createInterceptorInstance(Class<T> interceptorClass, EjbDescriptor ejb, JCDIService.JCDIInjectionContext<T> ejbContext, Set<EjbInterceptor> ejbInterceptors) {
    BundleDescriptor topLevelBundleDesc = (BundleDescriptor) ejb.getEjbBundleDescriptor().getModuleDescriptor().getDescriptor();
    // First get BeanDeploymentArchive for this ejb
    BeanDeploymentArchive bda = getBDAForBeanClass(topLevelBundleDesc, ejb.getEjbClassName());
    WeldBootstrap bootstrap = weldDeployer.getBootstrapForApp(ejb.getEjbBundleDescriptor().getApplication());
    WeldManager beanManager = bootstrap.getManager(bda);
    org.jboss.weld.ejb.spi.EjbDescriptor<T> ejbDesc = beanManager.getEjbDescriptor(ejb.getName());
    // get or create the ejb's creational context
    CreationalContext<T> creationalContext = ejbContext.getCreationalContext();
    if (creationalContext == null) {
        // We have to do this because interceptors are created before the ejb but in certain cases we must associate
        // the interceptors with the ejb so that they are cleaned up correctly.
        // And we only want to create the ejb's creational context once or we will have a memory
        // leak there too.
        Bean<T> bean = beanManager.getBean(ejbDesc);
        creationalContext = beanManager.createCreationalContext(bean);
        ejbContext.setCreationalContext(creationalContext);
    }
    // first see if there's an Interceptor object defined for the interceptorClass
    // This happens when @Interceptor or @InterceptorBinding is used.
    Interceptor<T> interceptor = findEjbInterceptor(interceptorClass, ejbInterceptors);
    if (interceptor != null) {
        // using the ejb's creationalContext so we don't have to do any cleanup.
        // the cleanup will be handled by weld when it clean's up the ejb.
        Object instance = beanManager.getReference(interceptor, interceptorClass, creationalContext);
        return (T) instance;
    }
    // Check to see if the interceptor was defined as a Bean.
    // This can happen when using @Interceptors to define the interceptors.
    Set<Bean<?>> availableBeans = beanManager.getBeans(interceptorClass);
    if (availableBeans != null && !availableBeans.isEmpty()) {
        // using the ejb's creationalContext so we don't have to do any cleanup.
        // the cleanup will be handled by weld when it clean's up the ejb.
        Bean<?> interceptorBean = beanManager.resolve(availableBeans);
        Object instance = beanManager.getReference(interceptorBean, interceptorClass, creationalContext);
        return (T) instance;
    }
    // There are other interceptors like SessionBeanInterceptor that are
    // defined via code and they are not beans.
    // Cannot use the ejb's creationalContext.
    creationalContext = beanManager.createCreationalContext(null);
    AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(interceptorClass);
    InjectionTarget<T> it = beanManager.getInjectionTargetFactory(annotatedType).createInterceptorInjectionTarget();
    T interceptorInstance = it.produce(creationalContext);
    it.inject(interceptorInstance, creationalContext);
    // make sure the interceptor's cdi objects get cleaned up when the ejb is cleaned up.
    ejbContext.addDependentContext(new JCDIInjectionContextImpl<>(it, creationalContext, interceptorInstance));
    return interceptorInstance;
}
Also used : WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) javax.enterprise.inject.spi(javax.enterprise.inject.spi) WeldManager(org.jboss.weld.manager.api.WeldManager) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)

Example 24 with BundleDescriptor

use of com.sun.enterprise.deployment.BundleDescriptor in project Payara by payara.

the class ManagedBeanManagerImpl method createManagedBean.

public <T> T createManagedBean(Class<T> managedBeanClass, boolean invokePostConstruct) throws Exception {
    ManagedBeanDescriptor managedBeanDesc = null;
    try {
        BundleDescriptor bundle = getBundle();
        managedBeanDesc = bundle.getManagedBeanByBeanClass(managedBeanClass.getName());
    } catch (Exception e) {
    // OK.  Can mean that it's not annotated with @ManagedBean but 299 can handle it.
    }
    return createManagedBean(managedBeanDesc, managedBeanClass, invokePostConstruct);
}
Also used : BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) ManagedBeanDescriptor(com.sun.enterprise.deployment.ManagedBeanDescriptor)

Example 25 with BundleDescriptor

use of com.sun.enterprise.deployment.BundleDescriptor 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
 */
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 {
        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 : BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) JCDIService(com.sun.enterprise.container.common.spi.JCDIService) JavaEEInterceptorBuilder(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder) InterceptorInvoker(com.sun.enterprise.container.common.spi.InterceptorInvoker)

Aggregations

BundleDescriptor (com.sun.enterprise.deployment.BundleDescriptor)51 EjbBundleDescriptor (com.sun.enterprise.deployment.EjbBundleDescriptor)24 WebBundleDescriptor (com.sun.enterprise.deployment.WebBundleDescriptor)24 Application (com.sun.enterprise.deployment.Application)17 EjbDescriptor (com.sun.enterprise.deployment.EjbDescriptor)10 ModuleDescriptor (org.glassfish.deployment.common.ModuleDescriptor)9 ManagedBeanDescriptor (com.sun.enterprise.deployment.ManagedBeanDescriptor)8 JndiNameEnvironment (com.sun.enterprise.deployment.JndiNameEnvironment)7 WeldBootstrap (org.jboss.weld.bootstrap.WeldBootstrap)7 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)7 JCDIService (com.sun.enterprise.container.common.spi.JCDIService)6 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 RootDeploymentDescriptor (org.glassfish.deployment.common.RootDeploymentDescriptor)5 InterceptorInvoker (com.sun.enterprise.container.common.spi.InterceptorInvoker)4 HashSet (java.util.HashSet)4 JavaEEInterceptorBuilder (com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder)3 File (java.io.File)3 IOException (java.io.IOException)3