Exemplo n.º 1
0
  /**
   * Get the system property value if the string is of the format ${sysproperty}
   *
   * <p>You can insert default value when the system property is not set, by separating it at the
   * beginning with ::
   *
   * <p><b>Examples:</b>
   *
   * <p>${idp} should resolve to a value if the system property "idp" is set.
   *
   * <p>${idp::http://localhost:8080} will resolve to http://localhost:8080 if the system property
   * "idp" is not set.
   *
   * @param str
   * @return
   */
  public static String getSystemPropertyAsString(String str) {
    if (str == null) throw logger.nullArgumentError("str");
    if (str.contains("${")) {
      Pattern pattern = Pattern.compile("\\$\\{([^}]+)}");
      Matcher matcher = pattern.matcher(str);

      StringBuffer buffer = new StringBuffer();
      String sysPropertyValue = null;

      while (matcher.find()) {
        String subString = matcher.group(1);
        String defaultValue = "";

        // Look for default value
        if (subString.contains("::")) {
          int index = subString.indexOf("::");
          defaultValue = subString.substring(index + 2);
          subString = subString.substring(0, index);
        }
        sysPropertyValue = SecurityActions.getSystemProperty(subString, defaultValue);
        if (sysPropertyValue.isEmpty()) {
          throw logger.systemPropertyMissingError(matcher.group(1));
        }
        matcher.appendReplacement(buffer, sysPropertyValue);
      }

      matcher.appendTail(buffer);
      str = buffer.toString();
    }
    return str;
  }