/** * Creates a new invocation handler with supplier which provides a current injected value in proper scope. * * @param supplier provider of the value. */ private ThreadScopeBeanInstance(Supplier<T> supplier, Bean<T> bean, String contextId) { super(bean, new StringBeanIdentifier(((PassivationCapable) bean).getId()), contextId); this.supplier = supplier; }
public Object produce() { if (delegateProduce && bean instanceof ManagedBean) { return ((ManagedBean) bean).getInjectionTarget().produce(context); } else { return injectionTarget.produce(context); } }
private static void doReloadAbstractClassBean(BeanManagerImpl beanManager, AbstractClassBean<?> bean, Map<String, String> oldSignatures, BeanReloadStrategy reloadStrategy) { String signatureByStrategy = WeldClassSignatureHelper.getSignatureByStrategy(reloadStrategy, bean.getBeanClass()); String oldSignature = oldSignatures.get(bean.getBeanClass().getName()); if (bean instanceof ManagedBean && ( reloadStrategy == BeanReloadStrategy.CLASS_CHANGE || (reloadStrategy != BeanReloadStrategy.NEVER && signatureByStrategy != null && !signatureByStrategy.equals(oldSignature))) ) { // Reload bean in contexts - invalidates existing instances doReloadBeanInBeanContexts(beanManager, (ManagedBean<?>) bean); } else { // Reinjects bean instances in aproperiate contexts doReinjectBean(beanManager, bean); } }
/** * Initializes the bean and its metadata */ @Override public void internalInitialize(BeanDeployerEnvironment environment) { getDeclaringBean().initialize(environment); super.internalInitialize(environment); initPassivationCapable(); }
/** * Creates an instance of a NewSimpleBean from an annotated class * * @param clazz The annotated class * @param beanManager The Bean manager * @return a new NewSimpleBean instance */ public static <T> NewManagedBean<T> of(BeanAttributes<T> attributes, EnhancedAnnotatedType<T> clazz, BeanManagerImpl beanManager) { return new NewManagedBean<T>(attributes, clazz, new StringBeanIdentifier(BeanIdentifiers.forNewManagedBean(clazz)), beanManager); }
private void checkDecoratedMethods(EnhancedAnnotatedType<T> type, List<Decorator<?>> decorators) { if (type.isFinal()) { throw BeanLogger.LOG.finalBeanClassWithDecoratorsNotAllowed(this); } checkNoArgsConstructor(type); for (Decorator<?> decorator : decorators) { EnhancedAnnotatedType<?> decoratorClass; if (decorator instanceof DecoratorImpl<?>) { DecoratorImpl<?> decoratorBean = (DecoratorImpl<?>) decorator; decoratorClass = decoratorBean.getBeanManager().getServices().get(ClassTransformer.class) .getEnhancedAnnotatedType(decoratorBean.getAnnotated()); } else if (decorator instanceof CustomDecoratorWrapper<?>) { decoratorClass = ((CustomDecoratorWrapper<?>) decorator).getEnhancedAnnotated(); } else { throw BeanLogger.LOG.nonContainerDecorator(decorator); } for (EnhancedAnnotatedMethod<?, ?> decoratorMethod : decoratorClass.getEnhancedMethods()) { EnhancedAnnotatedMethod<?, ?> method = type.getEnhancedMethod(decoratorMethod.getSignature()); if (method != null && !method.isStatic() && !method.isPrivate() && method.isFinal()) { throw BeanLogger.LOG.finalBeanClassWithInterceptorsNotAllowed(this); } } } }
@SuppressWarnings({ "rawtypes", "unchecked" }) private static void doDefineNewManagedBean(BeanManagerImpl beanManager, String bdaId, Class<?> beanClass) { try { ClassTransformer classTransformer = getClassTransformer(); SlimAnnotatedType<?> annotatedType = classTransformer.getBackedAnnotatedType(beanClass, bdaId); boolean managedBeanOrDecorator = Beans.isTypeManagedBeanOrDecoratorOrInterceptor(annotatedType); if (managedBeanOrDecorator) { EnhancedAnnotatedType eat = EnhancedAnnotatedTypeImpl.of(annotatedType, classTransformer); BeanAttributes attributes = BeanAttributesFactory.forBean(eat, beanManager); ManagedBean<?> bean = ManagedBean.of(attributes, eat, beanManager); ReflectionHelper.set(beanManager, beanManager.getClass(), "beanSet", Collections.synchronizedSet(new HashSet<Bean<?>>())); beanManager.addBean(bean); beanManager.getBeanResolver().clear(); bean.initializeAfterBeanDiscovery(); LOGGER.debug("Bean defined '{}'", beanClass.getName()); } else { // TODO : define session bean LOGGER.warning("Bean NOT? defined '{}', session bean?", beanClass.getName()); } } catch (Exception e) { LOGGER.debug("Bean definition failed.", e); } }
@SuppressWarnings("unchecked") private static void doCallInject(BeanManagerImpl beanManager, AbstractClassBean bean, Object instance) { // In whatever reason, we have to use reflection call for beanManager.createCreationalContext() in weld>=3.0 Method m = null; try { m = beanManager.getClass().getMethod("createCreationalContext", Contextual.class); } catch (Exception e) { LOGGER.error("BeanManager.createCreationalContext() method not found beanManagerClass='{}'", e, bean.getBeanClass().getName()); return; } try { bean.getProducer().inject(instance, (CreationalContext) m.invoke(beanManager, bean)); LOGGER.debug("Bean instance '{}' injection points was reinjected.", instance); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { LOGGER.error("beanManager.createCreationalContext(beanManager, bean) invocation failed beanManagerClass='{}', beanClass='{}'", e, bean.getBeanClass().getName(), bean.getClass().getName()); } }
private static void doReloadBeanInBeanContexts(BeanManagerImpl beanManager, 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, managedBean, context); } } else { LOGGER.debug("No active contexts for bean '{}' in scope '{}'", managedBean.getBeanClass().getName(), managedBean.getScope()); } } catch (Exception e) { if (e.getClass().getSimpleName().equals("ContextNotActiveException")) { LOGGER.warning("No active contexts for bean '{}'", e, managedBean.getBeanClass().getName()); } else { LOGGER.warning("Context for '{}' failed to reload", e, managedBean.getBeanClass().getName()); } } }
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; } } }
/** * Will re-inject any managed beans in the target. Will not call any other life-cycle methods * * @param ctx * @param managedBean */ public static void reinitialize(Context ctx, Contextual<Object> contextual) { try { ManagedBean<Object> managedBean = ManagedBean.class.cast(contextual); LOGGER.debug("Re-Initializing........ {},: {}", managedBean, ctx); Object get = ctx.get(managedBean); if (get != null) { LOGGER.debug("Bean injection points are reinitialized '{}'", managedBean); managedBean.getProducer().inject(get, managedBean.getBeanManager().createCreationalContext(managedBean)); } } catch (Exception e) { LOGGER.error("Error reinitializing bean {},: {}", e, contextual, ctx); } } }
/** * Creates a simple, annotation defined Enterprise Web Bean using the annotations specified on type * * @param <T> The type * @param beanManager the current manager * @param type the AnnotatedType to use * @return An Enterprise Web Bean */ public static <T> SessionBean<T> of(BeanAttributes<T> attributes, InternalEjbDescriptor<T> ejbDescriptor, BeanManagerImpl beanManager, EnhancedAnnotatedType<T> type) { return new SessionBean<T>(attributes, type, ejbDescriptor, new StringBeanIdentifier(BeanIdentifiers.forSessionBean(type, ejbDescriptor)), beanManager); }
EnhancedAnnotatedType eat = createAnnotatedTypeForExistingBeanClass(bdaId, bean); if (!eat.isAbstract() || !eat.getJavaClass().isInterface()) { // injectionTargetCannotBeCreatedForInterface ((AbstractClassBean)bean).setProducer(beanManager.getLocalInjectionTargetFactory(eat).createInjectionTarget(eat, bean, false)); if (isReinjectingContext(bean)) { doReloadAbstractClassBean(beanManager, (AbstractClassBean) bean, oldSignatures, reloadStrategy);
this.injectionTarget = sessionBean.getProducer(); return;
/** * Initializes the bean and its metadata */ @Override public void internalInitialize(BeanDeployerEnvironment environment) { getDeclaringBean().initialize(environment); super.internalInitialize(environment); initPassivationCapable(); }
/** * Creates an instance of a NewSimpleBean from an annotated class * * @param clazz The annotated class * @param beanManager The Bean manager * @return a new NewSimpleBean instance */ public static <T> NewManagedBean<T> of(BeanAttributes<T> attributes, EnhancedAnnotatedType<T> clazz, BeanManagerImpl beanManager) { return new NewManagedBean<T>(attributes, clazz, new StringBeanIdentifier(BeanIdentifiers.forNewManagedBean(clazz)), beanManager); }
/** * Initializes the bean and its metadata */ @Override public void internalInitialize(BeanDeployerEnvironment environment) { getDeclaringBean().initialize(environment); super.internalInitialize(environment); initPassivationCapable(); }
/** * Creates an instance of a NewSimpleBean from an annotated class * * @param clazz The annotated class * @param beanManager The Bean manager * @return a new NewSimpleBean instance */ public static <T> NewManagedBean<T> of(BeanAttributes<T> attributes, EnhancedAnnotatedType<T> clazz, BeanManagerImpl beanManager) { return new NewManagedBean<T>(attributes, clazz, new StringBeanIdentifier(BeanIdentifiers.forNewManagedBean(clazz)), beanManager); }
/** * Initializes the bean and its metadata */ @Override public void internalInitialize(BeanDeployerEnvironment environment) { getDeclaringBean().initialize(environment); super.internalInitialize(environment); initPassivationCapable(); }
/** * Creates an instance of a NewSimpleBean from an annotated class * * @param clazz The annotated class * @param beanManager The Bean manager * @return a new NewSimpleBean instance */ public static <T> NewManagedBean<T> of(BeanAttributes<T> attributes, EnhancedAnnotatedType<T> clazz, BeanManagerImpl beanManager) { return new NewManagedBean<T>(attributes, clazz, new StringBeanIdentifier(BeanIdentifiers.forNewManagedBean(clazz)), beanManager); }