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.
used URLClassLoader instead of changing java.class.path to dynamicall…
…y load plugin jars (Lighthouse unclebob#63)
- Loading branch information
Showing
9 changed files
with
110 additions
and
78 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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); | ||
} | ||
} | ||
|
||
|
||
} |
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,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()); | ||
} | ||
} | ||
} | ||
|
||
} |
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