Example #1
0
  /**
   * Implement the decorator pattern for the factory implementation.
   *
   * <p>
   *
   * <p>If <code>previousImpl</code> is non-<code>null</code> and the class named by the argument
   * <code>implName</code> has a one arg contstructor of type <code>factoryName</code>, instantiate
   * it, passing previousImpl to the constructor.
   *
   * <p>
   *
   * <p>Otherwise, we just instantiate and return <code>implName</code>.
   *
   * @param classLoader the ClassLoader from which to load the class
   * @param factoryName the fully qualified class name of the factory.
   * @param implName the fully qualified class name of a class that implements the factory.
   * @param previousImpl if non-<code>null</code>, the factory instance to be passed to the
   *     constructor of the new factory.
   */
  private static Object getImplGivenPreviousImpl(
      ClassLoader classLoader, String factoryName, String implName, Object previousImpl) {
    Class clazz;
    Class factoryClass = null;
    Class[] getCtorArg;
    Object[] newInstanceArgs = new Object[1];
    Constructor ctor;
    Object result = null;
    InjectionProvider provider = null;

    // if we have a previousImpl and the appropriate one arg ctor.
    if ((null != previousImpl) && (null != (factoryClass = getFactoryClass(factoryName)))) {
      try {
        clazz = Class.forName(implName, false, classLoader);
        getCtorArg = new Class[1];
        getCtorArg[0] = factoryClass;
        ctor = clazz.getConstructor(getCtorArg);
        newInstanceArgs[0] = previousImpl;
        result = ctor.newInstance(newInstanceArgs);

        FactoryManager fm = FACTORIES_CACHE.getApplicationFactoryManager(classLoader);
        provider = fm.getInjectionProvider();
        if (null != provider) {
          provider.inject(result);
          provider.invokePostConstruct(result);
        } else {
          if (LOGGER.isLoggable(Level.SEVERE)) {
            LOGGER.log(
                Level.SEVERE,
                "Unable to inject {0} because no InjectionProvider can be found. Does this container implement the Mojarra Injection SPI?",
                result);
          }
        }

      } catch (NoSuchMethodException nsme) {
        // fall through to "zero-arg-ctor" case
        factoryClass = null;
      } catch (Exception e) {
        throw new FacesException(implName, e);
      }
    }
    if (null == previousImpl || null == factoryClass) {
      // we have either no previousImpl or no appropriate one arg
      // ctor.
      try {
        clazz = Class.forName(implName, false, classLoader);
        // since this is the hard coded implementation default,
        // there is no preceding implementation, so don't bother
        // with a non-zero-arg ctor.
        result = clazz.newInstance();
      } catch (Exception e) {
        throw new FacesException(implName, e);
      }
    }
    return result;
  }
Example #2
0
  public void destroy(InjectionProvider injectionProvider, Object bean) {

    if (isInjectible) {
      try {
        injectionProvider.invokePreDestroy(bean);
      } catch (InjectionProviderException ipe) {
        if (LOGGER.isLoggable(Level.SEVERE)) {
          LOGGER.log(Level.SEVERE, ipe.getMessage(), ipe);
        }
      }
    }
  }
Example #3
0
  protected void invokePostConstruct(Object bean, InjectionProvider injectionProvider) {

    if (isInjectible) {
      try {
        injectionProvider.invokePostConstruct(bean);
      } catch (InjectionProviderException ipe) {
        String message =
            MessageUtils.getExceptionMessageString(
                MessageUtils.MANAGED_BEAN_INJECTION_ERROR_ID, beanInfo.getName());
        throw new ManagedBeanCreationException(message, ipe);
      }
    }
  }
Example #4
0
  /**
   * <span class="changed_modified_2_0">Release</span> any references to factory instances
   * associated with the class loader for the calling web application. <span
   * class="changed_modified_2_0">This method must be called during of web application
   * shutdown.</span>
   *
   * @throws FacesException if the web application class loader cannot be identified
   */
  public static void releaseFactories() throws FacesException {

    // Identify the web application class loader
    ClassLoader cl = getClassLoader();

    if (!FACTORIES_CACHE.applicationMap.isEmpty()) {

      FactoryManager fm = FACTORIES_CACHE.getApplicationFactoryManager(cl);
      InjectionProvider provider = fm.getInjectionProvider();
      if (null != provider) {
        Collection factories = null;
        for (Map.Entry<FactoryManagerCacheKey, FactoryManager> entry :
            FACTORIES_CACHE.applicationMap.entrySet()) {
          factories = entry.getValue().getFactories();
          for (Object curFactory : factories) {
            try {
              provider.invokePreDestroy(curFactory);
            } catch (Exception ex) {
              if (LOGGER.isLoggable(Level.SEVERE)) {
                String message =
                    MessageFormat.format(
                        "Unable to invoke @PreDestroy annotated methods on {0}.", curFactory);
                LOGGER.log(Level.SEVERE, message, ex);
              }
            }
          }
        }
      } else {
        if (LOGGER.isLoggable(Level.SEVERE)) {
          LOGGER.log(
              Level.SEVERE,
              "Unable to call @PreDestroy annotated methods because no InjectionProvider can be found. Does this container implement the Mojarra Injection SPI?");
        }
      }
    }

    FACTORIES_CACHE.removeApplicationFactoryManager(cl);
  }