Ejemplo n.º 1
0
  /**
   * Processes the given runtime arguments, using the specified properties list as default. All
   * arguments which could not be matched will be returned.
   *
   * <p>Arguments are expected to either take the form
   *
   * <pre>-<paramName> <paramValue></pre>
   *
   * or
   *
   * <pre>-<paramName>=<paramValue></pre>
   *
   * @param properties a list of default values, may be null.
   * @param arguments the list of arguments to process.
   * @return the list of unmatched runtime arguments.
   * @throws ParameterValueException If one of the parameter values was illegal.
   */
  public Collection<String> processArguments(final Properties properties, final String... arguments)
      throws ParameterValueException {
    final LinkedList<String> unmatchedArguments = new LinkedList<String>();
    if (properties != null) {
      /* Process the property file */
      for (Object o : properties.keySet()) {
        final String s = (String) o;
        final ApplicationParameter param = getParameter(s);
        if (param == null) continue;
        param.setValueAsString(properties.getProperty(s));
      }
    }

    /* Process the runtime arguments */
    for (int i = 0; i < arguments.length; i++) {
      final String s = arguments[i];
      /* Check if this argument should be processed or passed down to the application */
      if (!s.startsWith("-")) unmatchedArguments.add(s);
      else {
        final boolean directAssignment = s.contains("=");
        final String key = directAssignment ? s.substring(1, s.indexOf('=')) : s.substring(1);
        final ApplicationParameter parameter = _parameterKeyMap.get(key);
        if (parameter == null) {
          unmatchedArguments.add(s);
          continue;
        }

        final String value;
        if (parameter.getType() == ApplicationParameter.Type.BOOLEAN)
          value = directAssignment ? s.substring(s.indexOf('=') + 1, s.length()) : "true";
        else
          value = directAssignment ? s.substring(s.indexOf('=') + 1, s.length()) : arguments[++i];

        parameter.setValueAsString(value);
        parameter.setPresent(true);
      }
    }

    return unmatchedArguments;
  }