@Override
  public <T> T get(final Contextual<T> component, final CreationalContext<T> creationalContext) {
    assertActive();

    T instance = get(component);
    if (instance == null) {
      if (creationalContext != null) {
        Map<Contextual<?>, Object> componentInstanceMap = getComponentInstanceMap();
        Map<Contextual<?>, CreationalContext<?>> creationalContextMap = getCreationalInstanceMap();

        synchronized (componentInstanceMap) {
          instance = (T) componentInstanceMap.get(component);
          if (instance == null) {
            instance = component.create(creationalContext);
            if (instance != null) {
              componentInstanceMap.put(component, instance);
              creationalContextMap.put(component, creationalContext);
            }
          }
        }
      }
    }

    return instance;
  }
  /**
   * @param bean
   * @param creationalContext
   * @param <T>
   * @return
   */
  public <T> T createContextualInstance(
      Contextual<T> bean, CreationalContext<T> creationalContext) {
    Object beanKey = getBeanKey(bean);
    if (isConcurrent()) {
      // locked approach
      ContextualInstanceInfo<T> instanceInfo = new ContextualInstanceInfo<T>();

      ConcurrentHashMap<Object, ContextualInstanceInfo<?>> concurrentMap =
          (ConcurrentHashMap<Object, ContextualInstanceInfo<?>>) contextualInstances;

      ContextualInstanceInfo<T> oldInstanceInfo =
          (ContextualInstanceInfo<T>) concurrentMap.putIfAbsent(beanKey, instanceInfo);

      if (oldInstanceInfo != null) {
        instanceInfo = oldInstanceInfo;
      }
      synchronized (instanceInfo) {
        T instance = instanceInfo.getContextualInstance();
        if (instance == null) {
          instance = bean.create(creationalContext);
          instanceInfo.setContextualInstance(instance);
          instanceInfo.setCreationalContext(creationalContext);
        }

        return instance;
      }

    } else {
      // simply create the contextual instance
      ContextualInstanceInfo<T> instanceInfo = new ContextualInstanceInfo<T>();
      instanceInfo.setCreationalContext(creationalContext);
      instanceInfo.setContextualInstance(bean.create(creationalContext));

      contextualInstances.put(beanKey, instanceInfo);

      return instanceInfo.getContextualInstance();
    }
  }
Ejemplo n.º 3
0
  public <T> T get(Scope scope, Contextual<T> contextual, CreationalContext<T> creationalContext) {
    RequestContext ctx = currentContext.get();
    if (ctx == null) throw new ContextNotActiveException();

    if (!scope.isActive(ctx)) throw new ContextNotActiveException();

    Object o = ctx.getContextualValue(scope, contextual);
    if (o == null) {
      if (creationalContext != null) {
        o = contextual.create(creationalContext);
        ctx.setContextualValue(scope, contextual, o);
      }
    }
    return (T) o;
  }
  public <T> T get(Contextual<T> component, CreationalContext<T> creationalContext) {
    Map<Contextual, TransactionBeanEntry> transactionBeanEntryMap =
        getBeanStorage().getActiveTransactionContext();

    if (transactionBeanEntryMap == null) {
      throw new ContextNotActiveException(
          "Not accessed within a transactional method - use @" + Transactional.class.getName());
    }

    TransactionBeanEntry transactionBeanEntry = transactionBeanEntryMap.get(component);
    if (transactionBeanEntry != null) {
      return (T) transactionBeanEntry.getContextualInstance();
    }

    // if it doesn't yet exist, we need to create it now!
    T instance = component.create(creationalContext);
    transactionBeanEntry = new TransactionBeanEntry(component, instance, creationalContext);
    transactionBeanEntryMap.put(component, transactionBeanEntry);

    return instance;
  }
Ejemplo n.º 5
0
  /**
   * Create the bean.
   *
   * @param <T> the type.
   * @param facesContext the faces context.
   * @param contextual the contextual.
   * @param creational the creational.
   * @return the value or null if not found.
   */
  public <T> T createBean(
      FacesContext facesContext, Contextual<T> contextual, CreationalContext<T> creational) {
    if (LOGGER.isLoggable(Level.FINEST)) {
      LOGGER.log(Level.FINEST, "Creating @ViewScoped CDI bean using contextual: {0}", contextual);
    }

    if (!(contextual instanceof PassivationCapable)) {
      throw new IllegalArgumentException(
          "ViewScoped bean " + contextual.toString() + " must be PassivationCapable, but is not.");
    }

    T result = contextual.create(creational);

    if (result != null) {
      String name = getName(result);
      facesContext.getViewRoot().getViewMap(true).put(name, result);
      String passivationCapableId = ((PassivationCapable) contextual).getId();

      getContextMap(facesContext)
          .put(passivationCapableId, new ViewScopeContextObject(passivationCapableId, name));
    }

    return result;
  }