Search in sources :

Example 6 with Context

use of javax.enterprise.context.spi.Context in project core by weld.

the class InstanceImpl method destroy.

@Override
public void destroy(T instance) {
    checkNotNull(instance);
    // Attempt to destroy instance which is either a client proxy or a dependent session bean proxy
    if (instance instanceof ProxyObject) {
        ProxyObject proxy = (ProxyObject) instance;
        if (proxy.getHandler() instanceof ProxyMethodHandler) {
            ProxyMethodHandler handler = (ProxyMethodHandler) proxy.getHandler();
            Bean<?> bean = handler.getBean();
            if (isSessionBeanProxy(instance) && Dependent.class.equals(bean.getScope())) {
                // Destroy internal reference to a dependent session bean
                destroyDependentInstance(instance);
                return;
            } else {
                // Destroy contextual instance of a normal-scoped bean
                Context context = getBeanManager().getContext(bean.getScope());
                if (context instanceof AlterableContext) {
                    AlterableContext alterableContext = (AlterableContext) context;
                    alterableContext.destroy(bean);
                    return;
                } else {
                    throw BeanLogger.LOG.destroyUnsupported(context);
                }
            }
        }
    }
    // Attempt to destroy dependent instance which is neither a client proxy nor a dependent session bean proxy
    destroyDependentInstance(instance);
}
Also used : Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) WeldCreationalContext(org.jboss.weld.contexts.WeldCreationalContext) AlterableContext(javax.enterprise.context.spi.AlterableContext) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) Dependent(javax.enterprise.context.Dependent) AlterableContext(javax.enterprise.context.spi.AlterableContext) ProxyMethodHandler(org.jboss.weld.bean.proxy.ProxyMethodHandler)

Example 7 with Context

use of javax.enterprise.context.spi.Context in project HotswapAgent by HotswapProjects.

the class BeanClassRefreshAgent method doReloadBeanInBeanContexts.

private static void doReloadBeanInBeanContexts(BeanManagerImpl beanManager, Class<?> beanClass, InjectionTargetBean<?> bean) {
    try {
        Map<Class<? extends Annotation>, Context> singleContextMap = getSingleContextMap(beanManager);
        Context context = singleContextMap.get(bean.getScope());
        if (context != null) {
            doReloadBeanInContext(beanManager, beanClass, bean, context);
        } else {
            Map<Class<? extends Annotation>, List<Context>> allContexts = getContextMap(beanManager);
            List<Context> ctxList = allContexts.get(bean.getScope());
            if (ctxList != null) {
                for (Context ctx : ctxList) {
                    doReloadBeanInContext(beanManager, beanClass, bean, ctx);
                }
            } else {
                LOGGER.debug("No active contexts for bean '{}' in scope '{}'", beanClass.getName(), bean.getScope());
            }
        }
    } catch (ContextNotActiveException e) {
        LOGGER.warning("No active contexts for bean '{}'", e, beanClass.getName());
    } catch (Exception e) {
        LOGGER.warning("Context for '{}' failed to reload", e, beanClass.getName());
    }
}
Also used : Context(javax.enterprise.context.spi.Context) WebBeansContext(org.apache.webbeans.config.WebBeansContext) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) List(java.util.List) Annotation(java.lang.annotation.Annotation) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) IOException(java.io.IOException)

Example 8 with Context

use of javax.enterprise.context.spi.Context in project HotswapAgent by HotswapProjects.

the class BeanReloadExecutor method doReloadBeanInBeanContexts.

private static void doReloadBeanInBeanContexts(BeanManagerImpl beanManager, Class<?> beanClass, ManagedBean<?> managedBean) {
    try {
        Map<Class<? extends Annotation>, List<Context>> contexts = getContextMap(beanManager);
        List<Context> ctxList = contexts.get(managedBean.getScope());
        if (ctxList != null) {
            for (Context context : ctxList) {
                doReloadBeanInContext(beanManager, beanClass, managedBean, context);
            }
        } else {
            LOGGER.debug("No active contexts for bean '{}' in scope '{}'", beanClass.getName(), managedBean.getScope());
        }
    } catch (ContextNotActiveException e) {
        LOGGER.warning("No active contexts for bean '{}'", e, beanClass.getName());
    } catch (Exception e) {
        LOGGER.warning("Context for '{}' failed to reload", e, beanClass.getName());
    }
}
Also used : Context(javax.enterprise.context.spi.Context) ContextNotActiveException(org.jboss.weld.context.ContextNotActiveException) List(java.util.List) Annotation(java.lang.annotation.Annotation) ContextNotActiveException(org.jboss.weld.context.ContextNotActiveException)

Example 9 with Context

use of javax.enterprise.context.spi.Context in project AngularBeans by bessemHmidi.

the class AngularBeansCDIExtension method registerContext.

/**
 * <p>
 * Invoked by the container once all the annotated types has bean discovered, then registers
 * the NGSessionScopeContext (and the NGSessionScoped custom CDI scope)
 * </p>
 *
 * @see javax.enterprise.inject.spi.AfterBeanDiscovery
 * @see javax.enterprise.inject.spi.BeanManager
 * @see angularBeans.context.NGSessionScoped
 * @see angularBeans.context.NGSessionScopeContext
 */
public void registerContext(@Observes final AfterBeanDiscovery event, BeanManager manager) {
    Context context = NGSessionScopeContext.getINSTANCE();
    event.addContext(context);
}
Also used : Context(javax.enterprise.context.spi.Context)

Example 10 with Context

use of javax.enterprise.context.spi.Context in project tomee by apache.

the class CdiPlugin method getSessionBeanProxy.

@Override
public Object getSessionBeanProxy(final Bean<?> inBean, final Class<?> interfce, final CreationalContext<?> creationalContext) {
    Object instance = cacheProxies.get(inBean);
    if (instance != null) {
        return instance;
    }
    synchronized (inBean) {
        // singleton for the app so safe to sync on it
        instance = cacheProxies.get(inBean);
        if (instance != null) {
            return instance;
        }
        final Class<? extends Annotation> scopeClass = inBean.getScope();
        final CdiEjbBean<Object> cdiEjbBean = (CdiEjbBean<Object>) inBean;
        final CreationalContext<Object> cc = (CreationalContext<Object>) creationalContext;
        if (scopeClass == null || Dependent.class == scopeClass) {
            // no need to add any layer, null = @New
            return cdiEjbBean.createEjb(cc);
        }
        // only stateful normally
        final InstanceBean<Object> bean = new InstanceBean<>(cdiEjbBean);
        if (webBeansContext.getBeanManagerImpl().isNormalScope(scopeClass)) {
            final BeanContext beanContext = cdiEjbBean.getBeanContext();
            final Provider provider = webBeansContext.getNormalScopeProxyFactory().getInstanceProvider(beanContext.getClassLoader(), cdiEjbBean);
            if (!beanContext.isLocalbean()) {
                final List<Class> interfaces = new ArrayList<>();
                final InterfaceType type = beanContext.getInterfaceType(interfce);
                if (type != null) {
                    interfaces.addAll(beanContext.getInterfaces(type));
                } else {
                    // can happen when looked up from impl instead of API in OWB -> default to business local
                    interfaces.addAll(beanContext.getInterfaces(InterfaceType.BUSINESS_LOCAL));
                }
                interfaces.add(Serializable.class);
                interfaces.add(IntraVmProxy.class);
                if (BeanType.STATEFUL.equals(beanContext.getComponentType()) || BeanType.MANAGED.equals(beanContext.getComponentType())) {
                    interfaces.add(BeanContext.Removable.class);
                }
                try {
                    instance = ProxyManager.newProxyInstance(interfaces.toArray(new Class<?>[interfaces.size()]), new InvocationHandler() {

                        @Override
                        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
                            try {
                                return method.invoke(provider.get(), args);
                            } catch (final InvocationTargetException ite) {
                                throw ite.getCause();
                            }
                        }
                    });
                } catch (final IllegalAccessException e) {
                    throw new OpenEJBRuntimeException(e);
                }
            } else {
                final NormalScopeProxyFactory normalScopeProxyFactory = webBeansContext.getNormalScopeProxyFactory();
                final Class<?> proxyClass = normalScopeProxyFactory.createProxyClass(beanContext.getClassLoader(), beanContext.getBeanClass());
                instance = normalScopeProxyFactory.createProxyInstance(proxyClass, provider);
            }
            cacheProxies.put(inBean, instance);
        } else {
            final Context context = webBeansContext.getBeanManagerImpl().getContext(scopeClass);
            instance = context.get(bean, cc);
        }
        bean.setOwbProxy(instance);
        return instance;
    }
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) NormalScopeProxyFactory(org.apache.webbeans.proxy.NormalScopeProxyFactory) ArrayList(java.util.ArrayList) Dependent(javax.enterprise.context.Dependent) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) Provider(javax.inject.Provider) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) CreationalContext(javax.enterprise.context.spi.CreationalContext) BeanContext(org.apache.openejb.BeanContext) InterfaceType(org.apache.openejb.InterfaceType)

Aggregations

Context (javax.enterprise.context.spi.Context)20 CreationalContext (javax.enterprise.context.spi.CreationalContext)8 WebBeansContext (org.apache.webbeans.config.WebBeansContext)4 Test (org.junit.Test)4 Annotation (java.lang.annotation.Annotation)3 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)3 WeldCreationalContext (org.jboss.weld.contexts.WeldCreationalContext)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Dependent (javax.enterprise.context.Dependent)2 RequestScoped (javax.enterprise.context.RequestScoped)2 AlterableContext (javax.enterprise.context.spi.AlterableContext)2 InvocationContext (javax.interceptor.InvocationContext)2 BeanContext (org.apache.openejb.BeanContext)2 ContextsService (org.apache.webbeans.spi.ContextsService)2 ProxyObject (org.jboss.weld.bean.proxy.ProxyObject)2 JoynrJeeMessageScoped (io.joynr.jeeintegration.api.JoynrJeeMessageScoped)1 JoynrJeeMessageContext (io.joynr.jeeintegration.context.JoynrJeeMessageContext)1 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1