Moved the logic to the plugin loader which makes the context listener obsolete

This commit is contained in:
Nanne Baars 2015-09-30 23:08:10 +02:00
parent 219b38315b
commit 487bc71df1
2 changed files with 13 additions and 39 deletions

View File

@ -1,31 +0,0 @@
package org.owasp.webgoat.plugins;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.nio.file.Paths;
/**
* Copy the plugins to the WEB-INF/lib directory to take advantage of the automatic reloading of an application
* server.
*/
@WebListener
public class PluginContextListener implements ServletContextListener {
private static boolean alreadyLoaded = false;
@Override
public void contextInitialized(ServletContextEvent event) {
String pluginPath = event.getServletContext().getRealPath("plugin_lessons");
String targetPath = event.getServletContext().getRealPath("plugin_extracted");
if (!alreadyLoaded) {
new PluginsLoader(Paths.get(pluginPath), Paths.get(targetPath)).copyJars();
alreadyLoaded = true;
}
}
@Override
public void contextDestroyed(ServletContextEvent event) {
}
}

View File

@ -32,6 +32,7 @@ import java.util.concurrent.Executors;
public class PluginsLoader { public class PluginsLoader {
private static final String WEBGOAT_PLUGIN_EXTENSION = "jar"; private static final String WEBGOAT_PLUGIN_EXTENSION = "jar";
private static boolean alreadyLoaded = false;
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final Path pluginSource; private final Path pluginSource;
private Path pluginTarget; private Path pluginTarget;
@ -52,18 +53,21 @@ public class PluginsLoader {
*/ */
public void copyJars() { public void copyJars() {
try { try {
WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader(); if (!alreadyLoaded) {
cl.setAntiJARLocking(true); WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader();
cl.setAntiJARLocking(true);
List<URL> jars = listJars(); List<URL> jars = listJars();
Path webInfLib = pluginTarget.getParent().resolve(cl.getJarPath().replaceFirst("\\/", "")); Path webInfLib = pluginTarget.getParent().resolve(cl.getJarPath().replaceFirst("\\/", ""));
for (URL jar : jars) { for (URL jar : jars) {
Path sourceJarFile = Paths.get(jar.toURI()); Path sourceJarFile = Paths.get(jar.toURI());
FileUtils.copyFileToDirectory(sourceJarFile.toFile(), webInfLib.toFile()); FileUtils.copyFileToDirectory(sourceJarFile.toFile(), webInfLib.toFile());
}
alreadyLoaded = true;
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Loading plugins failed", e); logger.error("Copying plugins failed", e);
} }
} }
@ -73,6 +77,7 @@ public class PluginsLoader {
* @return a {@link java.util.List} object. * @return a {@link java.util.List} object.
*/ */
public List<Plugin> loadPlugins() { public List<Plugin> loadPlugins() {
copyJars();
List<Plugin> plugins = Lists.newArrayList(); List<Plugin> plugins = Lists.newArrayList();
try { try {