/**
   * Initializes the sql query environment from the SqlResources file. Will look for
   * conf/sqlResources.xml.
   *
   * @param conn The connection for accessing the database
   * @param sqlFileUrl The url which we use to get the sql file
   * @throws Exception If any error occurs
   */
  private void initSqlQueries(Connection conn, String sqlFileUrl) throws Exception {
    try {

      File sqlFile;

      try {
        sqlFile = fileSystem.getFile(sqlFileUrl);
        sqlFileUrl = null;
      } catch (Exception e) {
        serviceLog.error(e.getMessage(), e);
        throw e;
      }

      sqlQueries.init(sqlFile.getCanonicalFile(), "GreyList", conn, sqlParameters);

      selectQuery = sqlQueries.getSqlString("selectQuery", true);
      insertQuery = sqlQueries.getSqlString("insertQuery", true);
      deleteQuery = sqlQueries.getSqlString("deleteQuery", true);
      deleteAutoWhiteListQuery = sqlQueries.getSqlString("deleteAutoWhitelistQuery", true);
      updateQuery = sqlQueries.getSqlString("updateQuery", true);

    } finally {
      theJDBCUtil.closeJDBCConnection(conn);
    }
  }
  private boolean createTable(
      Connection conn, String tableNameSqlStringName, String createSqlStringName)
      throws SQLException {
    String tableName = sqlQueries.getSqlString(tableNameSqlStringName, true);

    DatabaseMetaData dbMetaData = conn.getMetaData();

    // Try UPPER, lower, and MixedCase, to see if the table is there.
    if (theJDBCUtil.tableExists(dbMetaData, tableName)) {
      return false;
    }

    PreparedStatement createStatement = null;

    try {
      createStatement = conn.prepareStatement(sqlQueries.getSqlString(createSqlStringName, true));
      createStatement.execute();

      StringBuffer logBuffer;
      logBuffer =
          new StringBuffer(64)
              .append("Created table '")
              .append(tableName)
              .append("' using sqlResources string '")
              .append(createSqlStringName)
              .append("'.");
      log(logBuffer.toString());

    } finally {
      theJDBCUtil.closeJDBCStatement(createStatement);
    }

    return true;
  }
  /** Initializes the mailet. */
  public void init() throws MessagingException {
    automaticInsert = Boolean.valueOf(getInitParameter("automaticInsert"));
    log("automaticInsert: " + automaticInsert);

    displayFlag = getInitParameter("displayFlag");
    insertFlag = getInitParameter("insertFlag");
    removeFlag = getInitParameter("removeFlag");

    String whitelistManagerAddressString = getInitParameter("whitelistManagerAddress");
    if (whitelistManagerAddressString != null) {
      whitelistManagerAddressString = whitelistManagerAddressString.trim();
      log("whitelistManagerAddress: " + whitelistManagerAddressString);
      try {
        whitelistManagerAddress = new MailAddress(whitelistManagerAddressString);
      } catch (javax.mail.internet.ParseException pe) {
        throw new MessagingException("Bad whitelistManagerAddress", pe);
      }

      if (displayFlag != null) {
        displayFlag = displayFlag.trim();
        log("displayFlag: " + displayFlag);
      } else {
        log("displayFlag is null");
      }
      if (insertFlag != null) {
        insertFlag = insertFlag.trim();
        log("insertFlag: " + insertFlag);
      } else {
        log("insertFlag is null");
      }
      if (removeFlag != null) {
        removeFlag = removeFlag.trim();
        log("removeFlag: " + removeFlag);
      } else {
        log("removeFlag is null");
      }
    } else {
      log("whitelistManagerAddress is null; will ignore commands");
    }

    String repositoryPath = getInitParameter("repositoryPath");
    if (repositoryPath != null) {
      log("repositoryPath: " + repositoryPath);
    } else {
      throw new MessagingException("repositoryPath is null");
    }

    try {
      initSqlQueries(datasource.getConnection(), getMailetContext());
    } catch (Exception e) {
      throw new MessagingException("Exception initializing queries", e);
    }

    selectByPK = sqlQueries.getSqlString("selectByPK", true);
    selectBySender = sqlQueries.getSqlString("selectBySender", true);
    insert = sqlQueries.getSqlString("insert", true);
    deleteByPK = sqlQueries.getSqlString("deleteByPK", true);
  }
  /**
   * Initializes the sql query environment from the SqlResources file. Will look for
   * conf/sqlResources.xml.
   *
   * @param conn The connection for accessing the database
   * @param mailetContext The current mailet context, for finding the conf/sqlResources.xml file
   * @throws Exception If any error occurs
   */
  private void initSqlQueries(Connection conn, org.apache.mailet.MailetContext mailetContext)
      throws Exception {
    try {
      if (conn.getAutoCommit()) {
        conn.setAutoCommit(false);
      }

      /*
       Holds value of property sqlFile.
      */
      File sqlFile =
          new File((String) mailetContext.getAttribute("confDir"), "sqlResources.xml")
              .getCanonicalFile();
      sqlQueries.init(sqlFile, "WhiteList", conn, getSqlParameters());

      checkTables(conn);
    } finally {
      theJDBCUtil.closeJDBCConnection(conn);
    }
  }