@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;
  }
Exemplo n.º 2
0
  /**
   * Get the value from the view map (or null if not found).
   *
   * @param <T> the type.
   * @param facesContext the faces context.
   * @param contextual the contextual.
   * @return the value or null if not found.
   */
  public <T> T getBean(FacesContext facesContext, Contextual<T> contextual) {
    T result = null;
    Map<String, ViewScopeContextObject> contextMap = getContextMap(facesContext);

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

      ViewScopeContextObject contextObject =
          contextMap.get(((PassivationCapable) contextual).getId());

      if (contextObject != null) {
        if (LOGGER.isLoggable(Level.FINEST)) {
          LOGGER.log(
              Level.FINEST,
              "Getting value for @ViewScoped bean with name: {0}",
              contextObject.getName());
        }
        result = (T) facesContext.getViewRoot().getViewMap(true).get(contextObject.getName());
      }
    }

    return result;
  }
Exemplo n.º 3
0
  @Override
  public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext) {
    if (creationalContext == null) {
      return null;
    }

    checkActive();

    if (passivatingScope) {
      if (!(bean instanceof PassivationCapable)) {
        throw new IllegalStateException(
            bean.toString() + " doesn't implement " + PassivationCapable.class.getName());
      }
    }

    ContextualStorage storage = getContextualStorage(bean, true);

    Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
    ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));

    if (contextualInstanceInfo != null) {
      @SuppressWarnings("unchecked")
      final T instance = (T) contextualInstanceInfo.getContextualInstance();

      if (instance != null) {
        return instance;
      }
    }

    return storage.createContextualInstance(bean, creationalContext);
  }
  /**
   * We get PreDestroyViewMapEvent events from the JSF servlet and destroy our contextual instances.
   * This should (theoretically!) also get fired if the webapp closes, so there should be no need to
   * manually track all view scopes and destroy them at a shutdown.
   *
   * @see javax.faces.event.SystemEventListener#processEvent(javax.faces.event.SystemEvent)
   */
  @Override
  public void processEvent(final SystemEvent event) {
    if (event instanceof PreDestroyViewMapEvent) {
      Map<Contextual<?>, Object> componentInstanceMap = getComponentInstanceMap();
      Map<Contextual<?>, CreationalContext<?>> creationalContextMap = getCreationalInstanceMap();

      if (componentInstanceMap != null) {
        for (Map.Entry<Contextual<?>, Object> componentEntry : componentInstanceMap.entrySet()) {
          /*
           * No way to inform the compiler of type <T> information, so
           * it has to be abandoned here :(
           */
          Contextual contextual = componentEntry.getKey();
          Object instance = componentEntry.getValue();
          CreationalContext creational = creationalContextMap.get(contextual);

          contextual.destroy(instance, creational);
        }
      }
    }
  }
  /**
   * @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();
    }
  }
Exemplo n.º 6
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;
  }
Exemplo n.º 7
0
  /**
   * Destroy the view scoped beans for the given view and context map.
   *
   * @param viewMap the view map.
   * @param contextMap the context map.
   */
  private void destroyBeans(
      Map<String, Object> viewMap, Map<String, ViewScopeContextObject> contextMap) {
    ArrayList<String> removalNameList = new ArrayList<String>();

    if (contextMap != null) {
      for (Map.Entry<String, ViewScopeContextObject> entry : contextMap.entrySet()) {
        String passivationCapableId = entry.getKey();
        Contextual contextual = beanManager.getPassivationCapableBean(passivationCapableId);

        ViewScopeContextObject contextObject = entry.getValue();
        CreationalContext creationalContext = beanManager.createCreationalContext(contextual);
        // We can no longer get this from the contextObject. Instead we must call
        // beanManager.createCreationalContext(contextual)
        contextual.destroy(viewMap.get(contextObject.getName()), creationalContext);
        removalNameList.add(contextObject.getName());
      }

      Iterator<String> removalNames = removalNameList.iterator();
      while (removalNames.hasNext()) {
        String name = removalNames.next();
        viewMap.remove(name);
      }
    }
  }
Exemplo n.º 8
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;
  }
  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;
  }
Exemplo n.º 10
0
 public static void destroyBean(
     Contextual bean, ContextualInstanceInfo<?> contextualInstanceInfo) {
   bean.destroy(
       contextualInstanceInfo.getContextualInstance(),
       contextualInstanceInfo.getCreationalContext());
 }