Skip to content

Commit

Permalink
created PluginClasspathLoader to dynamically add jars in the plugins …
Browse files Browse the repository at this point in the history
…folder to the classpath at runtime (Lighthouse unclebob#63)
  • Loading branch information
timander committed Oct 9, 2009
1 parent 8ee3e3a commit b325ede
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/testJar1.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not really a jar, just a file for the PluginsClasspathLoaderTest.java
1 change: 1 addition & 0 deletions plugins/testJar2.jar
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not really a jar, just a file for the PluginsClasspathLoaderTest.java
29 changes: 29 additions & 0 deletions src/fitnesse/components/PluginClasspathLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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");
}

}
47 changes: 47 additions & 0 deletions src/fitnesse/components/PluginClasspathLoaderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 comments on commit b325ede

Please sign in to comment.