private static void doReloadInjectionTargetBean(BeanManagerImpl beanManager, InjectionTargetBean<?> bean, Map<String, String> oldSignatures, BeanReloadStrategy reloadStrategy) { String signatureByStrategy = OwbClassSignatureHelper.getSignatureByStrategy(reloadStrategy, bean.getBeanClass()); String oldSignature = oldSignatures.get(bean.getBeanClass().getName()); if (reloadStrategy == BeanReloadStrategy.CLASS_CHANGE || (reloadStrategy != BeanReloadStrategy.NEVER && signatureByStrategy != null && !signatureByStrategy.equals(oldSignature))) { // Reload bean in contexts - invalidates existing instances doReloadBeanInBeanContexts(beanManager, bean); } else { // keep beans in contexts, reinitialize bean injection points doReinjectBean(beanManager, bean); } }
@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 doReloadBeanInBeanContexts(BeanManagerImpl beanManager, InjectionTargetBean<?> bean) { try { Map<Class<? extends Annotation>, Context> singleContextMap = getSingleContextMap(beanManager); Context context = singleContextMap.get(bean.getScope()); if (context != null) { doReloadBeanInContext(beanManager, 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, bean, ctx); } } else { LOGGER.debug("No active contexts for bean '{}' in scope '{}'", bean.getBeanClass().getName(), bean.getScope()); } } } catch (ContextNotActiveException e) { LOGGER.warning("No active contexts for bean '{}'", e, bean.getBeanClass().getName()); } catch (Exception e) { LOGGER.warning("Context for '{}' failed to reload", e, bean.getBeanClass().getName()); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private static void doReinjectBeanInstance(BeanManagerImpl beanManager, InjectionTargetBean bean, Context context) { Object instance = context.get(bean); if (instance != null) { bean.getProducer().inject(instance, beanManager.createCreationalContext(bean)); LOGGER.info("Bean '{}' injection points was reinjected.", 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()); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) private static void createAnnotatedTypeForExistingBeanClass(BeanManagerImpl beanManager, InjectionTargetBean bean) { WebBeansContext wbc = beanManager.getWebBeansContext(); AnnotatedElementFactory annotatedElementFactory = wbc.getAnnotatedElementFactory(); // Clear AnnotatedElementFactory caches annotatedElementFactory.clear(); AnnotatedType annotatedType = annotatedElementFactory.newAnnotatedType(bean.getBeanClass()); ReflectionHelper.set(bean, InjectionTargetBean.class, "annotatedType", annotatedType); // Updated members that were set by bean attributes BeanAttributesImpl attributes = BeanAttributesBuilder.forContext(wbc).newBeanAttibutes(annotatedType).build(); ReflectionHelper.set(bean, BeanAttributesImpl.class, "types", attributes.getTypes()); ReflectionHelper.set(bean, BeanAttributesImpl.class, "qualifiers", attributes.getQualifiers()); ReflectionHelper.set(bean, BeanAttributesImpl.class, "scope", attributes.getScope()); ReflectionHelper.set(bean, BeanAttributesImpl.class, "name", attributes.getName()); ReflectionHelper.set(bean, BeanAttributesImpl.class, "stereotypes", attributes.getStereotypes()); ReflectionHelper.set(bean, BeanAttributesImpl.class, "alternative", attributes.isAlternative()); InjectionTargetFactory factory = new InjectionTargetFactoryImpl(annotatedType, bean.getWebBeansContext()); InjectionTarget injectionTarget = factory.createInjectionTarget(bean); ReflectionHelper.set(bean, InjectionTargetBean.class, "injectionTarget", injectionTarget); LOGGER.debug("New annotated type created for bean '{}'", bean.getBeanClass()); }
/** * Create a new instance. * * @param returnType bean type info */ public AbstractProducerBean(InjectionTargetBean<?> ownerComponent, WebBeansType webBeansType, BeanAttributes<T> beanAttributes, Class<T> returnType, ProducerFactory<?> producerFactory) { super(ownerComponent.webBeansContext, webBeansType, beanAttributes, ownerComponent.getBeanClass(), !returnType.isPrimitive()); this.returnType = returnType; this.ownerComponent = ownerComponent; producer = producerFactory.createProducer(this); }
@Override protected String providedId() { return webBeansContext.getBeanManagerImpl().getId(Class.class.cast(getBeanClass()), annotatedType); }