/** * Checks if scope is registered * * @param scope the scope * @return true, if is registered scope */ public static boolean isRegisteredScope(Class<? extends Annotation> scope) { return getContextClass(scope) != null; }
/** * Gets the context class for specified scope. * * @param scope the scope * @return the context class */ public static Class<? extends Context> getContextClass(Class<? extends Annotation> scope) { return getCurrentScopeToContextMap().get(scope); }
/** * Add custom tracker field to session context * * @param ctClass the class * @throws NotFoundException the not found exception * @throws CannotCompileException the cannot compile exception */ @OnClassLoadEvent(classNameRegexp = "org.jboss.weld.context.AbstractContext") public static void transformHttpSessionContext(ClassPool classPool, CtClass ctClass) throws NotFoundException, CannotCompileException { HaCdiCommons.transformContext(classPool, ctClass); } }
/** * Add bean registry field to context, register bean instances in get(...) methods * * @param classPool the class pool * @param ctClass the ct class * @throws CannotCompileException the cannot compile exception * @throws NotFoundException the not found exception */ public static void transformContext(ClassPool classPool, CtClass ctClass) throws CannotCompileException, NotFoundException { addBeanRegistryToContext(classPool, ctClass); transformGet1(classPool, ctClass); transformGet2(classPool, ctClass); LOGGER.debug(ctClass.getName() + " - patched by bean registration."); }
@SuppressWarnings("rawtypes") private static void doReinjectRegisteredBeanInstances(BeanManagerImpl beanManager, AbstractClassBean bean) { for (Object instance: HaCdiCommons.getBeanInstances(bean)) { if (instance != null) { doCallInject(beanManager, bean, instance); } } }
private static void doReinjectBean(BeanManagerImpl beanManager, AbstractClassBean<?> bean) { try { if (!bean.getScope().equals(ApplicationScoped.class) && HaCdiCommons.isRegisteredScope(bean.getScope())) { doReinjectRegisteredBeanInstances(beanManager, bean); } else { doReinjectBeanInstance(beanManager, bean, beanManager.getContext(bean.getScope())); } } catch (Exception e) { if (e.getClass().getSimpleName().equals("ContextNotActiveException")) { LOGGER.info("No active contexts for bean '{}'", bean.getBeanClass().getName()); } else { throw e; } } }
/** * Return all bean instances. * * @param bean the bean * @return the bean instances */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static List<Object> getBeanInstances(Bean<?> bean) { List<Object> result = new ArrayList<>(); Class<? extends Context> contextClass = getContextClass(bean.getScope()); Map beanRegistry = (Map) getBeanRegistry(contextClass); if (beanRegistry != null) { Map m = (Map) beanRegistry.get(bean.getBeanClass().getName()); if (m != null) { result.addAll(m.keySet()); } else { LOGGER.debug("BeanRegistry is empty for bean class '{}'", bean.getBeanClass().getName()); } } else { LOGGER.error("BeanRegistry field not found in context class '{}'", contextClass.getName()); } return result; }
/** * Transform 1 argument get method : * <code>public <T> T get(Contextual<T> contextual);</code> * * @param classPool the class pool * @param ctClass the ct class * @throws CannotCompileException the cannot compile exception * @throws NotFoundException the not found exception */ public static void transformGet1(ClassPool classPool, CtClass ctClass) throws CannotCompileException, NotFoundException { CtMethod methGet1 = ctClass.getDeclaredMethod("get", new CtClass[] { classPool.get("javax.enterprise.context.spi.Contextual") }); methGet1.insertAfter(getRegistrationCode()); }
@OnClassLoadEvent(classNameRegexp = "org.apache.deltaspike.core.util.context.AbstractContext") public static void patchWindowContext(ClassPool classPool, CtClass ctClass) throws CannotCompileException, NotFoundException { HaCdiCommons.transformContext(classPool, ctClass); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static void doReinjectRegisteredBeanInstances(BeanManagerImpl beanManager, InjectionTargetBean bean) { for (Object instance: HaCdiCommons.getBeanInstances(bean)) { if (instance != null) { bean.getProducer().inject(instance, beanManager.createCreationalContext(bean)); LOGGER.info("Bean '{}' injection points was reinjected.", bean.getBeanClass().getName()); } else { LOGGER.info("Unexpected 'null' bean instance in registry. bean='{}'", bean.getBeanClass().getName()); } } }
private static void doReinjectBean(BeanManagerImpl beanManager, InjectionTargetBean<?> bean) { try { if (!bean.getScope().equals(ApplicationScoped.class) && HaCdiCommons.isRegisteredScope(bean.getScope())) { doReinjectRegisteredBeanInstances(beanManager, bean); } else { doReinjectBeanInstance(beanManager, bean, beanManager.getContext(bean.getScope())); } } catch (ContextNotActiveException e) { LOGGER.info("No active contexts for bean '{}'", bean.getBeanClass().getName()); } }
/** * Register context class. * * @param scope the scope * @param contextClass the context class */ public static void registerContextClass(Class<? extends Annotation> scope, Class<? extends Context> contextClass) { Map<Class<? extends Annotation>, Class<? extends Context>> currentScopeToContextMap = getCurrentScopeToContextMap(); if (!currentScopeToContextMap.containsKey(scope)) { LOGGER.debug("Registering scope '{}' to scopeToContextMap@{}", scope.getName(), System.identityHashCode(currentScopeToContextMap)); currentScopeToContextMap.put(scope, contextClass); } }
/** * Transform 2 arguments get method : * <code>public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext);</code> * * @param classPool the class pool * @param ctClass the ct class * @throws CannotCompileException the cannot compile exception * @throws NotFoundException the not found exception */ public static void transformGet2(ClassPool classPool, CtClass ctClass) throws CannotCompileException, NotFoundException { CtMethod methGet2 = ctClass.getDeclaredMethod("get", new CtClass[] { classPool.get("javax.enterprise.context.spi.Contextual"), classPool.get("javax.enterprise.context.spi.CreationalContext"), }); methGet2.insertAfter(getRegistrationCode()); }
@OnClassLoadEvent(classNameRegexp = "org.apache.myfaces.cdi.view.ViewScopeContextImpl") public static void patchViewScopeContext(ClassPool classPool, CtClass ctClass) throws CannotCompileException, NotFoundException { HaCdiCommons.transformContext(classPool, ctClass); }
@OnClassLoadEvent(classNameRegexp = "org.omnifaces.cdi.viewscope.ViewScopeContext") public static void patchViewScopeContext(ClassPool classPool, CtClass ctClass) throws CannotCompileException, NotFoundException { HaCdiCommons.transformContext(classPool, ctClass); }
@OnClassLoadEvent(classNameRegexp = "org.apache.webbeans.context.AbstractContext") public static void transformAbstractContext(ClassPool classPool, CtClass ctClass) throws NotFoundException, CannotCompileException { HaCdiCommons.transformContext(classPool, ctClass); }
@OnClassLoadEvent(classNameRegexp = "com.sun.faces.application.view.ViewScopeContext") public static void patchViewScopeContext(ClassPool classPool, CtClass ctClass) throws CannotCompileException, NotFoundException { HaCdiCommons.transformContext(classPool, ctClass); }