Search in sources :

Example 1 with NormalScopeProxyFactory

use of org.apache.webbeans.proxy.NormalScopeProxyFactory in project HotswapAgent by HotswapProjects.

the class ProxyRefreshAgent method doRecreateProxy.

private static void doRecreateProxy(ClassLoader appClassLoader, Class<?> beanClass) {
    ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        ProxyClassLoadingDelegate.beginProxyRegeneration();
        Thread.currentThread().setContextClassLoader(appClassLoader);
        WebBeansContext wbc = WebBeansContext.currentInstance();
        NormalScopeProxyFactory proxyFactory = wbc.getNormalScopeProxyFactory();
        // Clear proxy class cache
        Map cachedProxyClasses = (Map) ReflectionHelper.get(proxyFactory, "cachedProxyClasses");
        Set<Bean<?>> beans = wbc.getBeanManagerImpl().getBeans(beanClass);
        if (beans != null) {
            boolean recreateIt = false;
            for (Bean<?> bean : beans) {
                if (cachedProxyClasses.containsKey(bean)) {
                    cachedProxyClasses.remove(bean);
                    recreateIt = true;
                }
            }
            if (recreateIt) {
                proxyFactory.createProxyClass(appClassLoader, beanClass);
            }
        }
    } catch (Exception e) {
        LOGGER.error("Proxy redefinition failed {}.", e, e.getMessage());
    } finally {
        Thread.currentThread().setContextClassLoader(oldContextClassLoader);
        ProxyClassLoadingDelegate.endProxyRegeneration();
    }
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) NormalScopeProxyFactory(org.apache.webbeans.proxy.NormalScopeProxyFactory) Map(java.util.Map) IOException(java.io.IOException) Bean(javax.enterprise.inject.spi.Bean)

Example 2 with NormalScopeProxyFactory

use of org.apache.webbeans.proxy.NormalScopeProxyFactory 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

WebBeansContext (org.apache.webbeans.config.WebBeansContext)2 NormalScopeProxyFactory (org.apache.webbeans.proxy.NormalScopeProxyFactory)2 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Dependent (javax.enterprise.context.Dependent)1 Context (javax.enterprise.context.spi.Context)1 CreationalContext (javax.enterprise.context.spi.CreationalContext)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 Bean (javax.enterprise.inject.spi.Bean)1 ObserverMethod (javax.enterprise.inject.spi.ObserverMethod)1 Provider (javax.inject.Provider)1 BeanContext (org.apache.openejb.BeanContext)1 InterfaceType (org.apache.openejb.InterfaceType)1 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)1