Permalink
Browse files

used URLClassLoader instead of changing java.class.path to dynamicall…

…y load plugin jars (Lighthouse #63)
  • Loading branch information...
1 parent b325ede commit 16e64a10f6493032c1444b5701f9558780d44e34 @timander timander committed Oct 9, 2009
View
Binary file not shown.
View
Binary file not shown.
View
@@ -1 +0,0 @@
-not really a jar, just a file for the PluginsClasspathLoaderTest.java
View
@@ -1 +0,0 @@
-not really a jar, just a file for the PluginsClasspathLoaderTest.java
@@ -1,29 +0,0 @@
-package fitnesse.components;
-
-import java.io.File;
-
-
-public class PluginClasspathLoader {
-
- private String pluginsDirecory = "./plugins";
-
- public void addPluginsToClasspath() {
- File pluginsDir = new File(pluginsDirecory);
- File[] plugins = pluginsDir.listFiles();
- for (File plugin : plugins) {
- if (plugin.getName().endsWith("jar")) {
- System.setProperty("java.class.path", getClasspath() + getPathSeperator() + plugin.getAbsolutePath());
- }
- }
- }
-
- protected String getClasspath() {
- return System.getProperty("java.class.path");
- }
-
-
- protected String getPathSeperator() {
- return System.getProperty("path.separator");
- }
-
-}
@@ -1,47 +0,0 @@
-package fitnesse.components;
-
-import static org.junit.Assert.*;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-
-public class PluginClasspathLoaderTest {
-
- private PluginClasspathLoader loader;
-
- @Before
- public void setUp() throws Exception {
- loader = new PluginClasspathLoader();
- }
-
-
- @Test
- public void loadClassPath() {
- List<String> jars = listOfJarsInClasspath();
- assertFalse(jars.contains("testJar1.jar"));
- assertFalse(jars.contains("testJar2.jar"));
- int originalJarCount = jars.size();
-
- loader.addPluginsToClasspath();
-
- List<String> newListOfJars = listOfJarsInClasspath();
- assertEquals(originalJarCount + 2, newListOfJars.size());
- assertTrue(newListOfJars.contains("testJar1.jar"));
- assertTrue(newListOfJars.contains("testJar2.jar"));
- }
-
- private List<String> listOfJarsInClasspath() {
- List<String> classpathElements = Arrays.asList(loader.getClasspath().split(loader.getPathSeperator()));
- List<String> jars = new ArrayList<String>();
- for (String classpathElement : classpathElements) {
- int slashPosition = classpathElement.lastIndexOf(System.getProperty("file.separator"));
- String jarName = classpathElement.substring(slashPosition + 1);
- jars.add(jarName);
- }
- return jars;
- }
-}
@@ -0,0 +1,53 @@
+package fitnesse.components;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+
+public class PluginsClassLoader {
+
+ protected String pluginsDirectory = "plugins";
+
+ public void addPluginsToClassLoader() {
+ File pluginsDir = new File(pluginsDirectory);
+ if (pluginsDir.exists()) {
+ for (File plugin : pluginsDir.listFiles()) {
+ if (plugin.getName().endsWith("jar")) {
+ addFile(plugin);
+ }
+ }
+ }
+ }
+
+
+ public void addFile(File plugin) {
+ try {
+ addURL(plugin.toURI().toURL());
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+ public void addURL(URL url) {
+ URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
+ Class sysclass = URLClassLoader.class;
+ try {
+ Method method = sysclass.getDeclaredMethod("addURL", new Class[]{URL.class});
+ method.setAccessible(true);
+ method.invoke(sysloader, url);
+ } catch (NoSuchMethodException nsme) {
+ throw new RuntimeException("Error, could not add URL to system classloader", nsme);
+ } catch (InvocationTargetException ite) {
+ throw new RuntimeException("Error, could not add URL to system classloader", ite);
+ } catch (IllegalAccessException iae) {
+ throw new RuntimeException("Error, could not add URL to system classloader", iae);
+ }
+ }
+
+
+}
@@ -0,0 +1,51 @@
+package fitnesse.components;
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+
+public class PluginsClassLoaderTest {
+
+ @Test
+ public void whenPluginsDirectoryDoesNotExist() {
+ try {
+ PluginsClassLoader pluginsClassLoader = new PluginsClassLoader();
+ pluginsClassLoader.pluginsDirectory = "nonExistingPluginsDirectory";
+ pluginsClassLoader.addPluginsToClassLoader();
+ assertTrue("didn't cause exception", true);
+ } catch (Exception e) {
+ fail("if exception occurs when plugins directory does not exist");
+ }
+ }
+
+ @Test
+ public void addPluginsToClassLoader() {
+ String[] dynamicClasses = new String[]{"fitnesse.testing.PluginX", "fitnesse.testing.PluginY"};
+ assertLoadingClassCausesException(dynamicClasses);
+ new PluginsClassLoader().addPluginsToClassLoader();
+ assertLoadingClassWorksNow(dynamicClasses);
+ }
+
+ private void assertLoadingClassWorksNow(String... dynamicClasses) {
+ for (String dynamicClass : dynamicClasses) {
+ try {
+ Class<?> dynamicallyLoadedClass = Class.forName(dynamicClass);
+ assertEquals(dynamicClass, dynamicallyLoadedClass.getName());
+ } catch (ClassNotFoundException e) {
+ fail(e.getMessage());
+ }
+ }
+ }
+
+ private void assertLoadingClassCausesException(String... dynamicClasses) {
+ for (String dynamicClass : dynamicClasses) {
+ try {
+ Class.forName(dynamicClass);
+ fail("plugins are not yet added to the classloader");
+ } catch (ClassNotFoundException e) {
+ assertEquals(dynamicClass, e.getMessage());
+ }
+ }
+ }
+
+}
@@ -2,6 +2,7 @@
import java.io.File;
+import fitnesse.components.PluginsClassLoader;
import util.CommandLine;
import fitnesse.Arguments;
import fitnesse.ComponentFactory;
@@ -35,6 +36,7 @@ public static void main(String[] args) throws Exception {
}
private static void launchFitNesse(Arguments arguments) throws Exception {
+ loadPlugins();
FitNesseContext context = loadContext(arguments);
VelocityFactory.makeVelocityFactory(context);
Updater updater = null;
@@ -46,6 +48,10 @@ private static void launchFitNesse(Arguments arguments) throws Exception {
updateAndLaunch(arguments, context, fitnesse);
}
+ private static void loadPlugins() {
+ new PluginsClassLoader().addPluginsToClassLoader();
+ }
+
static void updateAndLaunch(Arguments arguments, FitNesseContext context,
FitNesse fitnesse) throws Exception {
if (!arguments.isOmittingUpdates())

0 comments on commit 16e64a1

Please sign in to comment.