Plugins are now reloaded
This commit is contained in:
parent
33d251a147
commit
6a00d66f8b
@ -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
|
||||
/**
|
||||
* <p>PluginBackgroundLoader class.</p>
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
}
|
@ -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();
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
@ -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<URL> 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<Plugin> loadPlugins() {
|
||||
List<Plugin> 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<URL> listJars() throws IOException {
|
||||
private List<URL> listJars() throws IOException {
|
||||
final List<URL> jars = Lists.newArrayList();
|
||||
Files.walkFileTree(pluginSource, new SimpleFileVisitor<Path>() {
|
||||
|
||||
@ -143,12 +141,4 @@ public class PluginsLoader implements Runnable {
|
||||
}
|
||||
return extractorCallables;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
loadPlugins();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user