コード例 #1
0
 /**
  * ToString implementation.
  *
  * @return String value
  */
 public String toString() {
   String str = "jdbc:mysql://";
   if (!haMode.equals(HaMode.NONE)) {
     str = "jdbc:mysql:" + haMode.toString().toLowerCase() + "://";
   }
   if (addresses != null) {
     str += HostAddress.toString(addresses);
   }
   if (database != null) {
     str += "/" + database;
   }
   return str;
 }
コード例 #2
0
  /*
  Parse ConnectorJ compatible urls
  jdbc:mysql://host:port/database
  Example: jdbc:mysql://localhost:3306/test?user=root&password=passwd
   */
  private static void parseInternal(UrlParser urlParser, String url, Properties properties)
      throws SQLException {
    try {
      int separator = url.indexOf("//");
      if (separator == -1) {
        throw new IllegalArgumentException(
            "url parsing error : '//' is not present in the url " + url);
      }
      String[] baseTokens = url.substring(0, separator).split(":");

      // parse HA mode
      urlParser.haMode = HaMode.NONE;
      if (baseTokens.length > 2) {
        try {
          urlParser.haMode = HaMode.valueOf(baseTokens[2].toUpperCase());
        } catch (IllegalArgumentException i) {
          throw new IllegalArgumentException(
              "url parameter error '"
                  + baseTokens[2]
                  + "' is a unknown parameter in the url "
                  + url);
        }
      }

      url = url.substring(separator + 2);
      String[] tokens = url.split("/");
      String hostAddressesString = tokens[0];
      String additionalParameters =
          (tokens.length > 1) ? url.substring(tokens[0].length() + 1) : null;

      urlParser.addresses = HostAddress.parse(hostAddressesString, urlParser.haMode);

      if (additionalParameters == null) {
        urlParser.database = null;
        urlParser.options = DefaultOptions.parse(urlParser.haMode, "", properties);
      } else {
        int ind = additionalParameters.indexOf('?');
        if (ind > -1) {
          urlParser.database = additionalParameters.substring(0, ind);
          urlParser.options =
              DefaultOptions.parse(
                  urlParser.haMode, additionalParameters.substring(ind + 1), properties);
        } else {
          urlParser.database = additionalParameters;
          urlParser.options = DefaultOptions.parse(urlParser.haMode, "", properties);
        }
      }

      if (urlParser.haMode == HaMode.AURORA) {
        for (HostAddress hostAddress : urlParser.addresses) {
          hostAddress.type = null;
        }
      } else {
        for (HostAddress hostAddress : urlParser.addresses) {
          if (hostAddress.type == null) {
            hostAddress.type = ParameterConstant.TYPE_MASTER;
          }
        }
      }
    } catch (IllegalArgumentException i) {
      throw new SQLException(i.getMessage());
    }
  }