forked from unclebob/fitnesse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created PluginClasspathLoader to dynamically add jars in the plugins …
…folder to the classpath at runtime (Lighthouse unclebob#63)
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
not really a jar, just a file for the PluginsClasspathLoaderTest.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
not really a jar, just a file for the PluginsClasspathLoaderTest.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |