Search in sources :

Example 6 with ManagedBeanManager

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

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

ManagedBeanManager (com.sun.enterprise.container.common.spi.ManagedBeanManager)7 JCDIService (com.sun.enterprise.container.common.spi.JCDIService)3 ManagedBean (javax.annotation.ManagedBean)3 NamingException (javax.naming.NamingException)3 ComponentEnvManager (com.sun.enterprise.container.common.spi.util.ComponentEnvManager)2 InjectionException (com.sun.enterprise.container.common.spi.util.InjectionException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ApplicationClientDescriptor (com.sun.enterprise.deployment.ApplicationClientDescriptor)1 PersistenceUnitDescriptor (com.sun.enterprise.deployment.PersistenceUnitDescriptor)1 ComponentInvocation (org.glassfish.api.invocation.ComponentInvocation)1 InvocationManager (org.glassfish.api.invocation.InvocationManager)1 ModuleDescriptor (org.glassfish.deployment.common.ModuleDescriptor)1 PersistenceUnitLoader (org.glassfish.persistence.jpa.PersistenceUnitLoader)1