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 {
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,6 +53,7 @@ public class PluginsLoader {
*/
public void copyJars() {
try {
if (!alreadyLoaded) {
WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader();
cl.setAntiJARLocking(true);
@ -62,8 +64,10 @@ public class PluginsLoader {
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<Plugin> loadPlugins() {
copyJars();
List<Plugin> plugins = Lists.newArrayList();
try {