private <T> T getProxiedInstance(Bean<T> bean) {
   WeldCreationalContext<T> creationalContext;
   boolean outer;
   if (currentCreationalContext.get() == null) {
     creationalContext = new CreationalContextImpl<T>(bean);
     currentCreationalContext.set(creationalContext);
     outer = true;
   } else {
     creationalContext = currentCreationalContext.get().getCreationalContext(bean);
     outer = false;
   }
   Context context = Container.instance().deploymentManager().getContext(bean.getScope());
   try {
     // Ensure that there is no injection point associated
     Container.instance()
         .services()
         .get(CurrentInjectionPoint.class)
         .push(SimpleInjectionPoint.EMPTY_INJECTION_POINT);
     return context.get(bean, creationalContext);
   } finally {
     Container.instance().services().get(CurrentInjectionPoint.class).pop();
     if (outer) {
       currentCreationalContext.remove();
     }
   }
 }
Пример #2
0
 /**
  * @param bean
  * @param beanManager
  * @return
  */
 static Object findContextualInstance(Bean<?> bean, BeanManagerImpl beanManager) {
   Context context;
   try {
     context = beanManager.getContext(bean.getScope());
   } catch (ContextNotActiveException e) {
     return null;
   }
   return context.get(bean);
 }
Пример #3
0
  /** Returns true if given scope is active in current context. */
  public static <S extends Annotation> boolean isScopeActive(Class<S> scope) {
    BeanManager beanManager = Util.getCdiBeanManager(FacesContext.getCurrentInstance());

    try {
      Context context = beanManager.getContext(scope);
      return context.isActive();
    } catch (ContextNotActiveException ignore) {
      return false;
    }
  }
  protected Object getInstance() {
    Bean<X> bean = getParentBean();
    Class<?> type = bean.getBeanClass();

    if (_isIfExists) {
      Class scopeType = bean.getScope();
      Context context = _beanManager.getContext(scopeType);

      if (context != null) return context.get(bean);
      else return null;
    }

    CreationalContext env = _beanManager.createCreationalContext(getParentBean());

    return _beanManager.getReference(getParentBean(), type, env);
  }
Пример #5
0
  /**
   * Returns concrete (non-proxied) bean instance of given class in current context.
   *
   * @param type the required bean type the instance must have
   * @param create whether to auto-create bean if not exist
   * @return a bean instance adhering to the required type
   */
  public static <T> T getBeanInstance(Class<T> type, boolean create) {
    BeanManager beanManager = Util.getCdiBeanManager(FacesContext.getCurrentInstance());
    @SuppressWarnings("unchecked")
    Bean<T> bean = (Bean<T>) beanManager.resolve(beanManager.getBeans(type));

    if (bean != null) {
      Context context = beanManager.getContext(bean.getScope());

      if (create) {
        return context.get(bean, beanManager.createCreationalContext(bean));
      } else {
        return context.get(bean);
      }
    } else {
      return null;
    }
  }
  @Test
  @SpecAssertions({
    @SpecAssertion(section = CONTEXT, id = "q"),
    @SpecAssertion(section = BEAN, id = "aa")
  })
  public void testDestroyedInstanceMustNotBeReturnedByGet() {
    assert getBeans(MySessionBean.class).size() == 1;
    Bean<MySessionBean> mySessionBean = getBeans(MySessionBean.class).iterator().next();
    CreationalContext<MySessionBean> sessionCreationalContext =
        getCurrentManager().createCreationalContext(mySessionBean);
    MySessionBean beanInstance = mySessionBean.create(sessionCreationalContext);
    assert beanInstance != null;
    Context sessionContext = getCurrentManager().getContext(SessionScoped.class);
    destroyContext(sessionContext);
    setContextActive(sessionContext);

    beanInstance = sessionContext.get(mySessionBean);
    assert beanInstance == null;
  }
Пример #7
0
 @Test
 @SpecAssertions({
   @SpecAssertion(section = CONTEXTUAL_INSTANCE, id = "a"),
   @SpecAssertion(section = CONTEXTUAL_INSTANCE, id = "b")
 })
 public void testContextCreatesNewInstanceForInjection() {
   Context requestContext = getCurrentManager().getContext(RequestScoped.class);
   Bean<Tuna> tunaBean = getBeans(Tuna.class).iterator().next();
   assertNull(requestContext.get(tunaBean));
   TunaFarm tunaFarm =
       getCurrentConfiguration()
           .getEl()
           .evaluateValueExpression(getCurrentManager(), "#{tunaFarm}", TunaFarm.class);
   assertNotNull(tunaFarm.tuna);
   long timestamp = tunaFarm.tuna.getTimestamp();
   // Lookup once again - do not create new instance - contextual instance already exists
   Tuna tuna = requestContext.get(tunaBean);
   assertNotNull(tuna);
   assertEquals(timestamp, tuna.getTimestamp());
 }
 @Override
 public void addContext(Context context) {
   checkWithinObserverNotification();
   Preconditions.checkArgumentNotNull(context, "context");
   Class<? extends Annotation> scope = context.getScope();
   if (scope == null) {
     throw ContextLogger.LOG.contextHasNullScope(context);
   }
   if (!getBeanManager().isScope(scope)) {
     MetadataLogger.LOG.contextGetScopeIsNotAScope(scope, context);
   }
   if (scope == ApplicationScoped.class || scope == Dependent.class) {
     throw ContextLogger.LOG.cannotRegisterContext(scope, context);
   }
   getBeanManager().addContext(context);
   BootstrapLogger.LOG.addContext(getReceiver(), context);
 }