コード例 #1
0
  /**
   * Creates a {@link UrlAction} object from the supplied {@link URLAction} annotation
   *
   * @param actionAnnotation The annotation
   * @param method The method that was annotated
   * @param classMappingIds the mapping IDs of the current class
   */
  private void processPrettyActionAnnotation(
      final URLAction actionAnnotation, final Method method, final String[] classMappingIds) {

    // Create ActionSpec
    ActionSpec actionSpec = new ActionSpec();
    actionSpec.setMethod(method);
    actionSpec.setOnPostback(actionAnnotation.onPostback());
    actionSpec.setInheritable(actionAnnotation.inheritable());
    actionSpec.setPhaseId(actionAnnotation.phaseId());

    // check which mapping the action belongs to
    if (!isBlank(actionAnnotation.mappingId())) {
      // action belongs to the mapping mentioned with mappingId attribute
      actionSpec.setMappingIds(new String[] {actionAnnotation.mappingId().trim()});
    } else if ((classMappingIds != null) && (classMappingIds.length > 0)) {
      // use the mapping found on the class
      actionSpec.setMappingIds(classMappingIds);
    } else {
      // No mapping found... throw an exception..
      throw new IllegalArgumentException(
          "Unable to find a suitable mapping "
              + "for the action definied on method '"
              + method.getName()
              + "' in class '"
              + method.getDeclaringClass().getName()
              + "'. Either place a @URLMapping annotation on the "
              + "class or reference a foreign mapping using the 'mappingId' attribute.");
    }

    // add action to list of actions
    urlActions.add(actionSpec);
  }
コード例 #2
0
  /**
   * This methods adds all mappings found to the supplied {@link PrettyConfigBuilder}. It should be
   * called after all classes has been scanned via {@link #processClass(Class)}.
   *
   * @param builder The builder to add the mappings to
   */
  public void build(final PrettyConfigBuilder builder) {

    // process all actions found
    for (ActionSpec actionSpec : urlActions) {

      // create an action for each referenced mapping
      for (String mappingId : actionSpec.getMappingIds()) {

        // Get the mapping references by the action
        UrlMapping mapping = urlMappings.get(mappingId);

        /*
         * Fail for unresolved mappings. This may happen when the user places
         * invalid mapping IDs in the mappingId attribute of
         *
         * @URLAction or @URLQueryParameter
         */
        if (mapping == null) {
          throw new IllegalArgumentException(
              "Unable to find the mapping '"
                  + mappingId
                  + "' referenced at method '"
                  + actionSpec.getMethod().getName()
                  + "' in class '"
                  + actionSpec.getMethod().getDeclaringClass().getName()
                  + "'.");
        }

        // build UrlMapping
        UrlAction urlAction = new UrlAction();
        urlAction.setPhaseId(actionSpec.getPhaseId());
        urlAction.setOnPostback(actionSpec.isOnPostback());
        urlAction.setInheritable(actionSpec.isInheritable());

        // try to get bean name
        Class clazz = actionSpec.getMethod().getDeclaringClass();

        // build expression
        PrettyExpression expression =
            buildPrettyExpression(clazz, actionSpec.getMethod().getName());
        urlAction.setAction(expression);

        // trace
        if (log.isTraceEnabled()) {
          log.trace(
              "Adding action expression '"
                  + urlAction.getAction()
                  + "' to mapping: "
                  + mapping.getId());
        }

        // register this action
        mapping.addAction(urlAction);
      }
    }

    for (QueryParamSpec queryParamSpec : queryParamList) {

      // create a query param for each referenced mapping
      for (String mappingId : queryParamSpec.getMappingIds()) {

        // Get the mapping references by the query param
        UrlMapping mapping = urlMappings.get(mappingId);

        // fail for unresolved mappings
        if (mapping == null) {
          throw new IllegalArgumentException(
              "Unable to find the mapping '"
                  + mappingId
                  + "' referenced at field '"
                  + queryParamSpec.getFieldName()
                  + "' in class '"
                  + queryParamSpec.getOwnerClass().getName()
                  + "'.");
        }

        // build UrlMapping
        QueryParameter queryParam = new QueryParameter();
        queryParam.setName(queryParamSpec.getName());
        queryParam.setOnError(queryParamSpec.getOnError());
        queryParam.setConverterId(StringUtils.trimToNull(queryParamSpec.getConverterId()));
        queryParam.setValidatorIds(join(queryParamSpec.getValidatorIds(), " "));
        queryParam.setOnPostback(queryParamSpec.isOnPostback());

        // optional validator method
        if (!isBlank(queryParamSpec.getValidator())) {
          queryParam.setValidatorExpression(new ConstantExpression(queryParamSpec.getValidator()));
        }

        // try to get bean name
        Class<?> clazz = queryParamSpec.getOwnerClass();

        // build expression
        PrettyExpression expression = buildPrettyExpression(clazz, queryParamSpec.getFieldName());
        queryParam.setExpression(expression);

        // trace
        if (log.isTraceEnabled()) {
          log.trace(
              "Registered query-param '"
                  + queryParam.getName()
                  + "' to '"
                  + expression
                  + "' in mapping: "
                  + mapping.getId());
        }

        // register this action
        mapping.addQueryParam(queryParam);
      }
    }

    // finally register all mappings
    for (UrlMapping mapping : urlMappings.values()) {
      builder.addMapping(mapping);
    }
  }