All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ms.log4jdbc.utils.drivermanager.ExtendedLog4JdbcDriverManager Maven / Gradle / Ivy

There is a newer version: 0.0.5
Show newest version
/*
 * This file is part of Log4Jdbc.
 *
 * Log4Jdbc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Log4Jdbc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Log4Jdbc.  If not, see .
 *
 */
package fr.ms.log4jdbc.utils.drivermanager;

import java.io.PrintWriter;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;

/**
 * 
 * @see Marco4J
 * 
 * 
 * @author Marco Semiao
 * 
 */
public class ExtendedLog4JdbcDriverManager implements Log4JdbcDriverManager {

  private final static Log4JdbcDriverManager driverManager = new DefaultLog4JdbcDriverManager();

  private final Vector log4jdbcDrivers = new Vector();

  public void setLogWriter(final PrintWriter out) {
    driverManager.setLogWriter(out);
  }

  public Enumeration getDrivers() {
    final Vector result = new Vector();

    java.sql.Driver d;
    for (int i = 0; i < log4jdbcDrivers.size(); i++) {
      final DriverInfo di = (DriverInfo) log4jdbcDrivers.elementAt(i);
      result.addElement(di.driver);
    }

    final Enumeration driverManagerDrivers = driverManager.getDrivers();
    while (driverManagerDrivers.hasMoreElements()) {
      d = (java.sql.Driver) driverManagerDrivers.nextElement();

      result.addElement(d);
    }

    return result.elements();
  }

  public synchronized void registerDriver(final Driver driver) throws SQLException {

    if (!Driver.class.equals(driver.getClass())) {
      final DriverInfo driverInfo = new DriverInfo(driver);
      if (!log4jdbcDrivers.contains(driverInfo)) {
        log4jdbcDrivers.addElement(driverInfo);
      }
    }
    driverManager.registerDriver(driver);
  }

  private final static class DriverInfo {
    private final Driver driver;
    private final Class driverClass;

    public DriverInfo(final Driver driver) {
      this.driver = driver;
      this.driverClass = driver.getClass();
    }

    public boolean equals(final Object other) {
      return (other instanceof DriverInfo) && this.driverClass == ((DriverInfo) other).driverClass;
    }

    public String toString() {
      return ("driver[className=" + driverClass + "]");
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy