Esempio n. 1
0
 private static long extractLong(
     ResourceProfile profile, String key, long minimumValue, long defaultValue) {
   assert profile != null;
   assert key != null;
   String valueString = extract(profile, key, null);
   long value;
   try {
     if (valueString == null || valueString.isEmpty()) {
       value = defaultValue;
     } else {
       value = Integer.parseInt(valueString);
     }
   } catch (NumberFormatException e) {
     WGLOG.error("E00001", profile.getName(), key, valueString);
     throw new IllegalArgumentException(
         MessageFormat.format(
             "The \"{1}\" must be a valid number: {2} (resource={0})",
             profile.getName(), key, valueString),
         e);
   }
   if (value < minimumValue) {
     WGLOG.error("E00001", profile.getName(), key, valueString);
     throw new IllegalArgumentException(
         MessageFormat.format(
             "The \"{1}\" must be > 0: {2} (resource={0})",
             profile.getName(), value, valueString));
   }
   return value;
 }
Esempio n. 2
0
 @Override
 public void prepare() throws IOException {
   LOG.debug(
       "Preparing JDBC resource source (resource={}, table={})",
       profile.getResourceName(),
       script.getTableName());
   try {
     this.resultSet = prepareResultSet();
   } catch (SQLException e) {
     for (SQLException ex = e; ex != null; ex = ex.getNextException()) {
       WGLOG.error(
           ex,
           "E03001",
           profile.getResourceName(),
           script.getName(),
           script.getTableName(),
           script.getColumnNames());
     }
     throw new IOException(
         MessageFormat.format(
             "Failed to prepare JDBC source (resource={0}, table={1}, columns={2})",
             profile.getResourceName(), script.getTableName(), script.getColumnNames()),
         e);
   }
   LOG.debug(
       "Creating ResultSet support {} for {}",
       script.getSupport().getClass().getName(),
       script.getColumnNames());
   support = script.getSupport().createResultSetSupport(resultSet, script.getColumnNames());
 }
Esempio n. 3
0
 private static String extract(ResourceProfile profile, String configKey) {
   assert profile != null;
   assert configKey != null;
   String value = extract(profile, configKey, null);
   if (value == null) {
     WGLOG.error("E00001", profile.getName(), configKey, null);
     throw new IllegalArgumentException(
         MessageFormat.format(
             "Resource \"{0}\" must declare \"{1}\"", profile.getName(), configKey));
   }
   return value.trim();
 }
Esempio n. 4
0
 private void prepareNextStream() throws IOException {
   streamProvider.next();
   currentPath = streamProvider.getCurrentPath();
   WGLOG.info("I04001", resourceName, processName, currentPath);
   try {
     currentStream = streamProvider.openStream();
     currentWriter = streamSupport.createWriter(currentPath, currentStream);
   } catch (IOException e) {
     WGLOG.error(e, "E04001", resourceName, processName, currentPath);
     throw e;
   }
 }
Esempio n. 5
0
 private void closeCurrentStream() throws IOException {
   currentWriter.flush();
   WGLOG.info("I04002", resourceName, processName, currentPath, currentStream.getCount());
   bytesCount += currentStream.getCount();
   try {
     currentStream.close();
   } catch (IOException e) {
     WGLOG.error(e, "E04002", resourceName, processName, currentPath);
     throw e;
   }
   currentPath = null;
   currentStream = null;
   currentWriter = null;
 }
Esempio n. 6
0
  /**
   * Creates a new connection using this configuration.
   *
   * @return the created connection
   * @throws IOException if failed to create a new connection
   */
  public Connection openConnection() throws IOException {
    LOG.debug("Opening JDBC connection: {}", url);

    try {
      Class<? extends Driver> driverClass =
          Class.forName(driver, true, classLoader).asSubclass(Driver.class);
      Properties properties = new Properties();
      properties.putAll(getConnectionProperties());
      if (user != null) {
        properties.put("user", user);
      }
      if (password != null) {
        properties.put("password", password);
      }
      Connection conn = null;
      try {
        conn = openConnection(driverClass, properties);
      } catch (Exception first) {
        Exception last = first;
        for (int i = 1, n = getConnectRetryCount(); i <= n; i++) {
          WGLOG.warn(last, "W00001", getResourceName(), url, i, getConnectRetryCount());
          try {
            TimeUnit.SECONDS.sleep(getConnectRetryInterval());
            conn = openConnection(driverClass, properties);
            break;
          } catch (Exception retry) {
            last = retry;
          }
        }
        if (conn == null) {
          throw last;
        }
      }
      boolean succeed = false;
      try {
        conn.setAutoCommit(false);
        succeed = true;
      } finally {
        if (succeed == false) {
          LOG.debug("Disposing JDBC connection: {}", url);
          conn.close();
        }
      }
      return conn;
    } catch (Exception e) {
      WGLOG.error(e, "E00002", getResourceName(), url);
      throw new IOException(MessageFormat.format("Failed to open connection: {0}", url), e);
    }
  }
Esempio n. 7
0
  /**
   * Converts {@link ResourceProfile} into {@link JdbcProfile}.
   *
   * @param profile target profile
   * @return the converted profile
   * @throws IllegalArgumentException if profile is not valid, or any parameter is {@code null}
   */
  public static JdbcProfile convert(ResourceProfile profile) {
    if (profile == null) {
      throw new IllegalArgumentException("profile must not be null"); // $NON-NLS-1$
    }
    String resourceName = profile.getName();
    ClassLoader classLoader = profile.getContext().getClassLoader();
    String driver = extract(profile, KEY_DRIVER);
    String url = extract(profile, KEY_URL);
    String user = extract(profile, KEY_USER, null);
    String password = extract(profile, KEY_PASSWORD, null);
    Map<String, String> connectionProperties =
        PropertiesUtil.createPrefixMap(profile.getConfiguration(), KEY_PREFIX_PROPERTIES);

    JdbcProfile result =
        new JdbcProfile(
            resourceName, classLoader, driver, url, user, password, connectionProperties);

    int batchGetUnit = extractInt(profile, KEY_BATCH_GET_UNIT, 0, DEFAULT_BATCH_GET_UNIT);
    long batchPutUnit = extractLong(profile, KEY_BATCH_PUT_UNIT, 1, DEFAULT_BATCH_PUT_UNIT);
    int connectRetryCount =
        extractInt(profile, KEY_CONNECT_RETRY_COUNT, 0, DEFAULT_CONNECT_RETRY_COUNT);
    int connectRetryInterval =
        extractInt(profile, KEY_CONNECT_RETRY_INTERVAL, 1, DEFAULT_CONNECT_RETRY_INTERVAL);
    String truncateStatement = extract(profile, KEY_TRUNCATE_STATEMENT, DEFAULT_TRUNCATE_STATEMENT);
    try {
      MessageFormat.format(truncateStatement, "dummy");
    } catch (IllegalArgumentException e) {
      WGLOG.error("E00001", profile.getName(), KEY_TRUNCATE_STATEMENT, truncateStatement);
      throw new IllegalArgumentException(
          MessageFormat.format(
              "The \"{1}\" must be a valid MessageFormat: {2} (resource={0})",
              profile.getName(), KEY_TRUNCATE_STATEMENT, truncateStatement),
          e);
    }

    result.setBatchGetUnit(batchGetUnit);
    result.setBatchPutUnit(batchPutUnit);
    result.setConnectRetryCount(connectRetryCount);
    result.setConnectRetryInterval(connectRetryInterval);
    result.setTruncateStatement(truncateStatement);
    return result;
  }
Esempio n. 8
0
 @Override
 public boolean next() throws IOException {
   try {
     sawNext = support.next(object);
     return sawNext;
   } catch (SQLException e) {
     sawNext = false;
     for (SQLException ex = e; ex != null; ex = ex.getNextException()) {
       WGLOG.error(
           ex,
           "E03001",
           profile.getResourceName(),
           script.getName(),
           script.getTableName(),
           script.getColumnNames());
     }
     throw new IOException(
         MessageFormat.format(
             "Failed to fetch next object from JDBC source (resource={0}, table={1})",
             profile.getResourceName(), script.getTableName()),
         e);
   }
 }