/**
   * Fetches the localized name for a form field if one exists in the form field resource bundle. If
   * for any reason a localized value cannot be found (e.g. the bundle cannot be found, or does not
   * contain the required properties) then null will be returned.
   *
   * <p>Looks first for a property called {@code beanClassFQN.fieldName} in the resource bundle. If
   * that is undefined, it next looks for {@code actionPath.fieldName} and if not defined, looks for
   * a property called {@code fieldName}. Will strip any indexing from the field name prior to using
   * it to construct property names (e.g. foo[12] will become simply foo).
   *
   * @param fieldName The name of the field whose localized name to look up
   * @param actionPath The action path of the form in which the field is nested. If for some reason
   *     this is not available, null may be passed without causing errors.
   * @param locale The desired locale of the looked up name.
   * @return a localized field name if one can be found, or null otherwise.
   */
  public static String getLocalizedFieldName(
      String fieldName, String actionPath, Class<? extends ActionBean> beanclass, Locale locale) {

    ParameterName parameterName = new ParameterName(fieldName);
    String strippedName = parameterName.getStrippedName();
    String localizedValue = null;
    ResourceBundle bundle = null;

    try {
      bundle =
          StripesFilter.getConfiguration()
              .getLocalizationBundleFactory()
              .getFormFieldBundle(locale);
    } catch (MissingResourceException mre) {
      log.error(mre);
      return null;
    }

    // First with the bean class
    if (beanclass != null) {
      try {
        localizedValue = bundle.getString(beanclass.getName() + "." + strippedName);
      } catch (MissingResourceException mre) {
        /* do nothing */
      }
    }

    // Then all by itself
    if (localizedValue == null) {
      try {
        localizedValue = bundle.getString(strippedName);
      } catch (MissingResourceException mre2) {
        /* do nothing */
      }
    }

    // Lastly, check @Validate on the ActionBean property
    if (localizedValue == null && beanclass != null) {
      ValidationMetadata validate =
          StripesFilter.getConfiguration()
              .getValidationMetadataProvider()
              .getValidationMetadata(beanclass, parameterName);
      if (validate != null && validate.label() != null && !"".equals(validate.label())) {
        localizedValue = validate.label();
      }
    }

    return localizedValue;
  }
 /**
  * Looks up the specified key in the error message resource bundle. If the bundle is missing or if
  * the resource cannot be found, will return null instead of throwing an exception.
  *
  * @param locale the locale in which to lookup the resource
  * @param key the exact resource key to lookup
  * @return the resource String or null
  */
 public static String getErrorMessage(Locale locale, String key) {
   try {
     Configuration config = StripesFilter.getConfiguration();
     ResourceBundle bundle = config.getLocalizationBundleFactory().getErrorMessageBundle(locale);
     return bundle.getString(key);
   } catch (MissingResourceException mre) {
     return null;
   }
 }
Ejemplo n.º 3
0
  /**
   * calling injectors post creation on Action beans and Interceptors
   *
   * @param context
   * @return
   * @throws Exception
   */
  public Resolution intercept(ExecutionContext context) throws Exception {
    Configuration conf = StripesFilter.getConfiguration();

    FilterConfig filterConfig = conf.getBootstrapPropertyResolver().getFilterConfig();
    String className = filterConfig.getInitParameter(INJECTOR_FACTORY_CLASS);
    String methodName = filterConfig.getInitParameter(INJECTOR_FACTORY_METHOD);

    Injector injector = null;
    try {
      Class clazz = Class.forName(className);
      Method method = clazz.getMethod(methodName);
      injector = (Injector) method.invoke(null);
    } catch (ClassNotFoundException e) {
      log.error("Injector factory class not found - " + className, e);
      throw new Exception("Injector factory class not found - " + methodName, e);
    } catch (NoSuchMethodException e) {
      log.error("Injector factory method not found - " + methodName + " in class " + className, e);
      throw new Exception(
          "Injector factory method not found - " + methodName + " in class " + className, e);
    } catch (SecurityException e) {
      log.error(
          "SecurityException when trying to invoke - " + methodName + " in class " + className, e);
      throw new Exception(
          "SecurityException when trying to invoke - " + methodName + " in class " + className, e);
    } catch (IllegalAccessException e) {
      log.error(
          "IllegalAccessException when trying to invoke - " + methodName + " in class " + className,
          e);
      throw new Exception(
          "IllegalAccessException when trying to invoke - " + methodName + " in class " + className,
          e);
    } catch (IllegalArgumentException e) {
      log.error(
          "IllegalArgumentException when trying to invoke - "
              + methodName
              + " in class "
              + className,
          e);
      throw new Exception(
          "IllegalArgumentException when trying to invoke - "
              + methodName
              + " in class "
              + className,
          e);
    } catch (InvocationTargetException e) {
      log.error(
          "InvocationTargetException when trying to invoke - "
              + methodName
              + " in class "
              + className,
          e);
      throw new Exception(
          "InvocationTargetException when trying to invoke - "
              + methodName
              + " in class "
              + className,
          e);
    }

    //    if (context.getActionBean() != null) injector.injectMembers(context.getActionBean());
    if (context.getActionBeanContext() != null)
      injector.injectMembers(context.getActionBeanContext());

    for (Interceptor interceptor : conf.getInterceptors(context.getLifecycleStage())) {
      if (interceptor != null) injector.injectMembers(interceptor);
    }

    return context.proceed();
  }