From 487bc71df135092d73953fdb15dcba92efabc39f Mon Sep 17 00:00:00 2001 From: Nanne Baars Date: Wed, 30 Sep 2015 23:08:10 +0200 Subject: [PATCH] Moved the logic to the plugin loader which makes the context listener obsolete --- .../plugins/PluginContextListener.java | 31 ------------------- .../owasp/webgoat/plugins/PluginsLoader.java | 21 ++++++++----- 2 files changed, 13 insertions(+), 39 deletions(-) delete mode 100644 webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginContextListener.java diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginContextListener.java b/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginContextListener.java deleted file mode 100644 index 6de0c9a0a..000000000 --- a/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginContextListener.java +++ /dev/null @@ -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) { - } -} diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginsLoader.java b/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginsLoader.java index ef9453403..c14bf7817 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginsLoader.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginsLoader.java @@ -32,6 +32,7 @@ import java.util.concurrent.Executors; public class PluginsLoader { private static final String WEBGOAT_PLUGIN_EXTENSION = "jar"; + private static boolean alreadyLoaded = false; private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Path pluginSource; private Path pluginTarget; @@ -52,18 +53,21 @@ public class PluginsLoader { */ public void copyJars() { try { - WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader(); - cl.setAntiJARLocking(true); + if (!alreadyLoaded) { + WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader(); + cl.setAntiJARLocking(true); - List jars = listJars(); + List jars = listJars(); - Path webInfLib = pluginTarget.getParent().resolve(cl.getJarPath().replaceFirst("\\/", "")); - for (URL jar : jars) { - Path sourceJarFile = Paths.get(jar.toURI()); - FileUtils.copyFileToDirectory(sourceJarFile.toFile(), webInfLib.toFile()); + Path webInfLib = pluginTarget.getParent().resolve(cl.getJarPath().replaceFirst("\\/", "")); + for (URL jar : jars) { + Path sourceJarFile = Paths.get(jar.toURI()); + FileUtils.copyFileToDirectory(sourceJarFile.toFile(), webInfLib.toFile()); + } + alreadyLoaded = true; } } 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. */ public List loadPlugins() { + copyJars(); List plugins = Lists.newArrayList(); try {