diff --git a/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginBackgroundLoader.java b/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginBackgroundLoader.java deleted file mode 100644 index ce0fb0f1e..000000000 --- a/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginBackgroundLoader.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.owasp.webgoat.plugins; - -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; -import javax.servlet.annotation.WebListener; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; - -@WebListener -/** - *

PluginBackgroundLoader class.

- * - * @version $Id: $Id - */ -public class PluginBackgroundLoader implements ServletContextListener { - - private ScheduledExecutorService scheduler; - - /** {@inheritDoc} */ - @Override - public void contextInitialized(ServletContextEvent event) { - String pluginPath = event.getServletContext().getRealPath("plugin_lessons"); - String targetPath = event.getServletContext().getRealPath("plugin_extracted"); - - scheduler = Executors.newSingleThreadScheduledExecutor(); - //scheduler.scheduleAtFixedRate(new PluginsLoader(Paths.get(pluginPath), Paths.get(targetPath)), 10, 5, TimeUnit.MINUTES); - } - - /** {@inheritDoc} */ - @Override - public void contextDestroyed(ServletContextEvent event) { - scheduler.shutdownNow(); - } -} 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 index 43e58d07f..6de0c9a0a 100644 --- a/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginContextListener.java +++ b/webgoat-container/src/main/java/org/owasp/webgoat/plugins/PluginContextListener.java @@ -6,31 +6,26 @@ import javax.servlet.annotation.WebListener; import java.nio.file.Paths; /** - * Created by nanne_000 on 9/29/2015. + * 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 (event.getServletContext().getInitParameter("plugins_loaded") == null) { + if (!alreadyLoaded) { new PluginsLoader(Paths.get(pluginPath), Paths.get(targetPath)).copyJars(); + alreadyLoaded = true; } - event.getServletContext().setInitParameter("plugins_loaded", ""); } @Override public void contextDestroyed(ServletContextEvent event) { -// String targetPath = event.getServletContext().getRealPath("plugin_extracted"); -// WebappClassLoader cl = (WebappClassLoader)Thread.currentThread().getContextClassLoader(); -// cl.closeJARs(true); -// Path webInfLib = Paths.get(targetPath).getParent().resolve(cl.getJarPath().replaceFirst("\\/", "")); -// try { -// FileUtils.cleanDirectory(webInfLib.toFile()); -// } catch (IOException e) { -// e.printStackTrace(); -// } } } 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 930ebbf30..ef9453403 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 @@ -15,7 +15,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; -import java.nio.file.StandardCopyOption; import java.nio.file.attribute.BasicFileAttributes; import java.util.List; import java.util.Objects; @@ -30,7 +29,7 @@ import java.util.concurrent.Executors; * * @version $Id: $Id */ -public class PluginsLoader implements Runnable { +public class PluginsLoader { private static final String WEBGOAT_PLUGIN_EXTENSION = "jar"; private final Logger logger = LoggerFactory.getLogger(this.getClass()); @@ -48,6 +47,9 @@ public class PluginsLoader implements Runnable { this.pluginTarget = Objects.requireNonNull(pluginTarget, "plugin target cannot be null"); } + /** + * Copy jars to the lib directory + */ public void copyJars() { try { WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader(); @@ -55,12 +57,10 @@ public class PluginsLoader implements Runnable { List jars = listJars(); - cl.closeJARs(true); Path webInfLib = pluginTarget.getParent().resolve(cl.getJarPath().replaceFirst("\\/", "")); for (URL jar : jars) { Path sourceJarFile = Paths.get(jar.toURI()); - Files.copy(sourceJarFile, webInfLib.resolve(sourceJarFile.getFileName()), - StandardCopyOption.REPLACE_EXISTING); + FileUtils.copyFileToDirectory(sourceJarFile.toFile(), webInfLib.toFile()); } } catch (Exception e) { logger.error("Loading plugins failed", e); @@ -74,7 +74,6 @@ public class PluginsLoader implements Runnable { */ public List loadPlugins() { List plugins = Lists.newArrayList(); - WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader(); try { PluginFileUtils.createDirsIfNotExists(pluginTarget); @@ -88,13 +87,12 @@ public class PluginsLoader implements Runnable { return plugins; } - private void cleanupExtractedPluginsDirectory() { Path i18nDirectory = pluginTarget.resolve("plugin/i18n/"); FileUtils.deleteQuietly(i18nDirectory.toFile()); } - public List listJars() throws IOException { + private List listJars() throws IOException { final List jars = Lists.newArrayList(); Files.walkFileTree(pluginSource, new SimpleFileVisitor() { @@ -143,12 +141,4 @@ public class PluginsLoader implements Runnable { } return extractorCallables; } - - /** - * {@inheritDoc} - */ - @Override - public void run() { - loadPlugins(); - } }