001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.batch.bootstrap;
021    
022    import java.sql.Driver;
023    import java.sql.DriverManager;
024    import java.sql.SQLException;
025    import java.util.ArrayList;
026    import java.util.Enumeration;
027    import java.util.HashSet;
028    import java.util.List;
029    import java.util.Set;
030    
031    /**
032     * Companion of {@link JdbcDriverHolder} and allows it to deregister JDBC drivers.
033     * <p>
034     * Some hacks are involved in the loading of the class - see {@link JdbcDriverHolder#stop()},
035     * so this class can refer to classes only from java.* package and must not be referred from other classes.
036     * Placement and naming of this class and methods are very important, since it loaded and invoked via reflection.
037     * </p>
038     */
039    public class JdbcLeakPrevention {
040    
041      /**
042       * @return names of the drivers that have been unregistered
043       */
044      public List<String> unregisterDrivers() throws SQLException {
045        Set<Driver> registeredDrivers = registeredDrivers();
046    
047        List<String> unregisteredNames = new ArrayList<String>();
048        Enumeration<Driver> drivers = DriverManager.getDrivers();
049        while (drivers.hasMoreElements()) {
050          Driver driver = drivers.nextElement();
051          if (driver.getClass().getClassLoader() != this.getClass().getClassLoader()) {
052            continue;
053          }
054          if (registeredDrivers.contains(driver)) {
055            unregisteredNames.add(driver.getClass().getCanonicalName());
056          }
057          DriverManager.deregisterDriver(driver);
058        }
059        return unregisteredNames;
060      }
061    
062      private Set<Driver> registeredDrivers() {
063        Set<Driver> registeredDrivers = new HashSet<Driver>();
064        Enumeration<Driver> drivers = DriverManager.getDrivers();
065        while (drivers.hasMoreElements()) {
066          Driver driver = drivers.nextElement();
067          registeredDrivers.add(driver);
068        }
069        return registeredDrivers;
070      }
071    
072    }