Exemple #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;
  }
  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);
      }
    }
  }