/**
  * Adds the specified listener to the list.
  *
  * @see #fireConnectionEvent
  * @see #removeConnectionEventListener
  */
 public synchronized void addConnectionEventListener(ConnectionEventListener listener) {
   // Clone the list of listeners to avoid concurrent modifications. See
   // bug [1113040] Small bug in net.sourceforge.jtds.jdbcx.PooledConnection
   // for a description of how these can occur. The method still needs to
   // be synchronized to prevent race conditions.
   listeners = (ArrayList) listeners.clone();
   // Now add the listener to the new, cloned list
   listeners.add(listener);
 }
  private String findDriverByUrlImpl(String url) {
    for (int i = 0; i < _driverList.size(); i++) {
      try {
        Driver driver = (Driver) _driverList.get(i);

        if (driver.acceptsURL(url)) return driver.getClass().getName();
      } catch (Exception e) {
        log.log(Level.FINE, e.toString(), e);
      }
    }

    return null;
  }
  /**
   * Fires a new connection event on all listeners.
   *
   * @param closed <code>true</code> if <code>close</code> has been called on the connection; <code>
   *     false</code> if the <code>sqlException</code> represents an error where the connection may
   *     not longer be used.
   * @param sqlException the SQLException to pass to the listeners
   */
  public synchronized void fireConnectionEvent(boolean closed, SQLException sqlException) {
    if (_listeners.size() > 0) {
      ConnectionEvent connectionEvent = new ConnectionEvent(this, sqlException);
      Iterator iterator = _listeners.iterator();

      while (iterator.hasNext()) {
        ConnectionEventListener listener = (ConnectionEventListener) iterator.next();

        if (closed) {
          listener.connectionClosed(connectionEvent);
        } else {
          listener.connectionErrorOccurred(connectionEvent);
        }
      }
    }
  }
  /**
   * Fires a new connection event on all listeners.
   *
   * @param closed <code>true</code> if <code>close</code> has been called on the connection; <code>
   *     false</code> if the <code>sqlException</code> represents an error where the connection may
   *     not longer be used.
   * @param sqlException the SQLException to pass to the listeners
   */
  public synchronized void fireConnectionEvent(boolean closed, SQLException sqlException) {
    if (listeners.size() > 0) {
      ConnectionEvent connectionEvent = new ConnectionEvent(this, sqlException);
      Iterator iterator = listeners.iterator();

      while (iterator.hasNext()) {
        ConnectionEventListener listener = (ConnectionEventListener) iterator.next();

        if (closed) {
          listener.connectionClosed(connectionEvent);
        } else {
          try {
            if (connection == null || connection.isClosed()) {
              listener.connectionErrorOccurred(connectionEvent);
            }
          } catch (SQLException ex) {
            // Will never occur
          }
        }
      }
    }
  }
  private void initDriverList() {
    try {
      Thread thread = Thread.currentThread();
      ClassLoader loader = thread.getContextClassLoader();

      Enumeration iter = loader.getResources("META-INF/services/java.sql.Driver");
      while (iter.hasMoreElements()) {
        URL url = (URL) iter.nextElement();

        ReadStream is = null;
        try {
          is = Vfs.lookup(url.toString()).openRead();

          String filename;

          while ((filename = is.readLine()) != null) {
            int p = filename.indexOf('#');

            if (p >= 0) filename = filename.substring(0, p);

            filename = filename.trim();
            if (filename.length() == 0) continue;

            try {
              Class cl = Class.forName(filename, false, loader);
              Driver driver = null;

              if (Driver.class.isAssignableFrom(cl)) driver = (Driver) cl.newInstance();

              if (driver != null) {
                log.fine(L.l("DatabaseManager adding driver '{0}'", driver.getClass().getName()));

                _driverList.add(driver);
              }
            } catch (Exception e) {
              log.log(Level.FINE, e.toString(), e);
            }
          }
        } catch (Exception e) {
          log.log(Level.FINE, e.toString(), e);
        } finally {
          if (is != null) is.close();
        }
      }
    } catch (Exception e) {
      log.log(Level.FINE, e.toString(), e);
    }
  }
  public String find() {

    String back = "";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    Connection con = Helper.getConnection();
    String qq =
        "select r.id,r.asset_id,r.asset_num,r.type,  "
            + " date_format(date,'%m/%d/%Y'),r.location_id,r.weight,r.description "
            + " from recycled_items r ";
    String qw = "";
    if (con == null) {
      back = "Could not connect to DB";
      addError(back);
      return back;
    }
    try {
      if (!location_id.equals("")) {
        if (!qw.equals("")) qw += " and ";
        qw += " r.location_id = ? ";
      }
      if (!asset_id.equals("")) {
        if (!qw.equals("")) qw += " and ";
        qw += " r.asset_id = ? ";
      }
      if (!type.equals("")) {
        if (!qw.equals("")) qw += " and ";
        qw += " r.type = ? ";
      }
      if (!date_from.equals("")) {
        if (!qw.equals("")) qw += " and ";
        qw += " r.date >= str_to_date('" + date_from + "','%m/%d/%Y')";
      }
      if (!date_to.equals("")) {
        if (!qw.equals("")) qw += " and ";
        qw += " r.date <= str_to_date('" + date_to + "','%m/%d/%Y')";
      }
      if (!qw.equals("")) {
        qq = qq + " where " + qw;
      }
      qq = qq + " order by r.date DESC " + limit;
      if (debug) {
        logger.debug(qq);
      }
      pstmt = con.prepareStatement(qq);
      int jj = 1;
      if (!location_id.equals("")) {
        pstmt.setString(jj++, location_id);
      }
      if (!asset_id.equals("")) {
        pstmt.setString(jj++, asset_id);
      }
      if (!type.equals("")) {
        pstmt.setString(jj++, type);
      }
      rs = pstmt.executeQuery();
      while (rs.next()) {
        if (recycledItems == null) recycledItems = new ArrayList<RecycledItem>();
        RecycledItem one =
            new RecycledItem(
                debug,
                rs.getString(1),
                rs.getString(2),
                rs.getString(3),
                rs.getString(4),
                rs.getString(5),
                rs.getString(6),
                rs.getString(7),
                rs.getString(8));
        recycledItems.add(one);
      }
    } catch (Exception ex) {
      back += ex + " : " + qq;
      logger.error(back);
      addError(back);
    } finally {
      Helper.databaseDisconnect(con, pstmt, rs);
    }
    return back;
  }
 /**
  * Adds the specified listener to the list.
  *
  * @see #fireConnectionEvent
  * @see #removeConnectionEventListener
  */
 public synchronized void addConnectionEventListener(ConnectionEventListener listener) {
   _listeners.add(listener);
 }
 /**
  * Removes the specified listener from the list.
  *
  * @see #addConnectionEventListener
  * @see #fireConnectionEvent
  */
 public synchronized void removeConnectionEventListener(ConnectionEventListener listener) {
   _listeners.remove(listener);
 }