/** Called by the extension object when gvsig terminates. */
  public void closeAllBeforeTerminate() {

    boolean ok = true;
    String key = "";
    ConnectionWithParams cwp = null;
    Iterator iter = connections.keySet().iterator();
    while (iter.hasNext()) {
      key = (String) iter.next();
      cwp = (ConnectionWithParams) connections.get(key);

      if (cwp.getConnection() == null) continue;

      try {
        cwp.getConnection().close();
      } catch (DBException se) {
        ok = false;
        logger.error("While closing connection: " + se.getMessage(), se);
      }
    }

    connections.clear();

    if (ok) {
      logger.info("Successfully closed all connections.");
    } else {
      logger.warn("Problems while closing all connections.");
    }
  }
  /**
   * Utility metho to find a connection with its parameters given the connection object.
   *
   * @param co the connection object
   * @return
   */
  public ConnectionWithParams findConnection(IConnection co) {

    Iterator iter = connections.keySet().iterator();
    while (iter.hasNext()) {
      String keyitem = (String) iter.next();
      ConnectionWithParams cwp = (ConnectionWithParams) connections.get(keyitem);
      if (cwp.getConnection() == co) {
        return cwp;
      }
    }
    return null;
  }
  /**
   * Closes and removes a connection with params object
   *
   * @param _cwp
   * @return whether the connection was actually closed (false if the connection was not open at the
   *     start)
   */
  public boolean closeAndRemove(ConnectionWithParams _cwp) {

    boolean it_was_open = true;

    try {
      it_was_open = (_cwp.getConnection() != null) && (!_cwp.getConnection().isClosed());
      if (_cwp.getConnection() != null) _cwp.getConnection().close();
      removeConnectionWP(_cwp);
    } catch (Exception se) {
      logger.error("While closing connection: " + se.getMessage(), se);
      return false;
    }
    logger.info("Connection successfully closed.");
    return it_was_open;
  }
  /**
   * Gets available open connections.
   *
   * @return array of open connections with parameters
   */
  public ConnectionWithParams[] getConnectedConnections() {
    Iterator iter = connections.keySet().iterator();
    if (!iter.hasNext()) return null;

    ArrayList aux = new ArrayList();

    while (iter.hasNext()) {
      ConnectionWithParams _cwp = (ConnectionWithParams) connections.get(iter.next());
      if (_cwp.isConnected()) {
        aux.add(_cwp);
      }
    }

    ConnectionWithParams[] resp = new ConnectionWithParams[aux.size()];
    for (int i = 0; i < aux.size(); i++) {
      resp[i] = (ConnectionWithParams) aux.get(i);
    }
    return resp;
  }
  /**
   * Creates a new connection with its parameters if not created yet.
   *
   * @param _drvName driver name
   * @param _user user name
   * @param _pw password
   * @param _name connection name
   * @param _host host url
   * @param _port port number as string
   * @param _db database name
   * @param _schema schema name
   * @param _connected whether or not to connect the connection
   * @return the connection with parameters object
   * @throws SQLException
   */
  public ConnectionWithParams getConnection(
      String _drvName,
      String _user,
      String _pw,
      String _name,
      String _host,
      String _port,
      String _db,
      String _schema,
      boolean _connected)
      throws DBException {

    // IVectorialDatabaseDriver drv =
    Driver _drv = getInstanceFromName(_drvName);

    if (_drv == null) return null;

    String conn_str = getConnStringForDriver(_drv, _host, _port, _db, _user, _pw);
    String key = getConnectionKey(_drvName, _host, _db, _port, _user);

    if (!connections.containsKey(key)) {

      ConnectionWithParams cwp = null;

      if (_connected) {
        IConnection new_connection;

        try {
          new_connection = getConnectionForDriver(_drv, conn_str, _host, _port, _db, _user, _pw);
        } catch (Exception e) {
          throw new DBException(e);
        }

        cwp =
            new ConnectionWithParams(
                conn_str,
                new_connection,
                _drvName,
                _user,
                _pw,
                _name,
                _host,
                _port,
                _db,
                _schema,
                true);
      } else {

        cwp =
            new ConnectionWithParams(
                conn_str, null, _drvName, _user, null, _name, _host, _port, _db, _schema, false);
      }
      connections.put(key, cwp);
    }

    ConnectionWithParams _cwp = (ConnectionWithParams) connections.get(key);

    if (_cwp.getName().compareTo(_name) != 0) {
      // connections.remove(key);
      _cwp.setName(_name);
      connections.put(key, _cwp);
    }

    if ((!_cwp.isConnected()) && (_connected)) {
      _cwp.connect(_pw);
    }

    return _cwp;
  }