/* * Sonar, open source software quality management tool. * Copyright (C) 2008-2012 SonarSource * mailto:contact AT sonarsource DOT com * * Sonar is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * Sonar 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Sonar; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 */ package org.sonar.batch.bootstrap; import com.google.common.annotations.VisibleForTesting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; import org.sonar.api.utils.SonarException; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.List; /** * Contains and provides class loader extended with the JDBC Driver hosted on the server-side. */ public class JdbcDriverHolder { private static final Logger LOG = LoggerFactory.getLogger(JdbcDriverHolder.class); private TempDirectories tempDirectories; private ServerClient serverClient; private Settings settings; // initialized in start() private JdbcDriverClassLoader classLoader = null; public JdbcDriverHolder(Settings settings, TempDirectories tempDirectories, ServerClient serverClient) { this.tempDirectories = tempDirectories; this.serverClient = serverClient; this.settings = settings; } public void start() { if (!settings.getBoolean(CoreProperties.DRY_RUN)) { LOG.info("Install JDBC driver"); File jdbcDriver = new File(tempDirectories.getRoot(), "jdbc-driver.jar"); serverClient.download("/deploy/jdbc-driver.jar", jdbcDriver); classLoader = initClassloader(jdbcDriver); } } @VisibleForTesting JdbcDriverClassLoader getClassLoader() { return classLoader; } @VisibleForTesting static JdbcDriverClassLoader initClassloader(File jdbcDriver) { JdbcDriverClassLoader classLoader; try { ClassLoader parentClassLoader = JdbcDriverHolder.class.getClassLoader(); classLoader = new JdbcDriverClassLoader(jdbcDriver.toURI().toURL(), parentClassLoader); } catch (MalformedURLException e) { throw new SonarException("Fail to get URL of : " + jdbcDriver.getAbsolutePath(), e); } // set as the current context classloader for hibernate, else it does not find the JDBC driver. Thread.currentThread().setContextClassLoader(classLoader); return classLoader; } /** * This method automatically invoked by PicoContainer and unregisters JDBC drivers, which were forgotten. *

* Dynamically loaded JDBC drivers can not be simply used and this is a well known problem of {@link java.sql.DriverManager}, * so workaround is to use proxy. * However DriverManager also contains memory leak, thus not only proxy, but also original driver must be unregistered, * otherwise our class loader would be kept in memory. *

*

* This operation contains unnecessary complexity because: *