Defer loading site libraries until database opens

By deferring the library loading a new MySQL database driver can
be downloaded and the old one removed from the lib/ directory before
it is added to the current URLClassLoader. This avoids seeing the
library twice in the classpath.

Bug: issue 1870
Change-Id: I5600d6cc998aad1c702058512b3f7371d547a95e
This commit is contained in:
Shawn Pearce
2013-04-25 13:58:51 -07:00
parent 8d414da1d9
commit 39f16cbea0
8 changed files with 153 additions and 123 deletions

View File

@@ -41,19 +41,9 @@ import org.eclipse.jgit.lib.Config;
import org.kohsuke.args4j.Option;
import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.sql.DataSource;
@@ -78,77 +68,8 @@ public abstract class SiteProgram extends AbstractProgram {
}
}
/** Load extra JARs from {@code lib/} subdirectory of {@link #getSitePath()} */
protected void loadSiteLib() {
final File libdir = new File(getSitePath(), "lib");
final File[] list = libdir.listFiles(new FileFilter() {
@Override
public boolean accept(File path) {
if (!path.isFile()) {
return false;
}
return path.getName().endsWith(".jar") //
|| path.getName().endsWith(".zip");
}
});
if (list != null && 0 < list.length) {
Arrays.sort(list, new Comparator<File>() {
@Override
public int compare(File a, File b) {
return a.getName().compareTo(b.getName());
}
});
addToClassLoader(list);
}
}
private void addToClassLoader(final File[] additionalLocations) {
final ClassLoader cl = getClass().getClassLoader();
if (!(cl instanceof URLClassLoader)) {
throw noAddURL("Not loaded by URLClassLoader", null);
}
final URLClassLoader ucl = (URLClassLoader) cl;
final Set<URL> have = new HashSet<URL>();
have.addAll(Arrays.asList(ucl.getURLs()));
final Method m;
try {
m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
m.setAccessible(true);
} catch (SecurityException e) {
throw noAddURL("Method addURL not available", e);
} catch (NoSuchMethodException e) {
throw noAddURL("Method addURL not available", e);
}
for (final File path : additionalLocations) {
try {
final URL url = path.toURI().toURL();
if (have.add(url)) {
m.invoke(cl, url);
}
} catch (MalformedURLException e) {
throw noAddURL("addURL " + path + " failed", e);
} catch (IllegalArgumentException e) {
throw noAddURL("addURL " + path + " failed", e);
} catch (IllegalAccessException e) {
throw noAddURL("addURL " + path + " failed", e);
} catch (InvocationTargetException e) {
throw noAddURL("addURL " + path + " failed", e.getCause());
}
}
}
private static UnsupportedOperationException noAddURL(String m, Throwable why) {
final String prefix = "Cannot extend classpath: ";
return new UnsupportedOperationException(prefix + m, why);
}
/** @return provides database connectivity and site path. */
protected Injector createDbInjector(final DataSourceProvider.Context context) {
loadSiteLib();
final File sitePath = getSitePath();
final List<Module> modules = new ArrayList<Module>();
@@ -164,9 +85,10 @@ public abstract class SiteProgram extends AbstractProgram {
@Override
protected void configure() {
bind(DataSourceProvider.Context.class).toInstance(context);
bind(Key.get(DataSource.class, Names.named("ReviewDb"))).toProvider(
DataSourceProvider.class).in(SINGLETON);
listener().to(DataSourceProvider.class);
bind(Key.get(DataSource.class, Names.named("ReviewDb")))
.toProvider(SiteLibraryBasedDataSourceProvider.class)
.in(SINGLETON);
listener().to(SiteLibraryBasedDataSourceProvider.class);
}
});
Module configModule = new GerritServerConfigModule();