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;
|
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
|
@WebListener
|
||||||
public class PluginContextListener implements ServletContextListener {
|
public class PluginContextListener implements ServletContextListener {
|
||||||
|
|
||||||
|
private static boolean alreadyLoaded = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void contextInitialized(ServletContextEvent event) {
|
public void contextInitialized(ServletContextEvent event) {
|
||||||
String pluginPath = event.getServletContext().getRealPath("plugin_lessons");
|
String pluginPath = event.getServletContext().getRealPath("plugin_lessons");
|
||||||
String targetPath = event.getServletContext().getRealPath("plugin_extracted");
|
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();
|
new PluginsLoader(Paths.get(pluginPath), Paths.get(targetPath)).copyJars();
|
||||||
|
alreadyLoaded = true;
|
||||||
}
|
}
|
||||||
event.getServletContext().setInitParameter("plugins_loaded", "");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void contextDestroyed(ServletContextEvent event) {
|
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.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.nio.file.SimpleFileVisitor;
|
import java.nio.file.SimpleFileVisitor;
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -30,7 +29,7 @@ import java.util.concurrent.Executors;
|
|||||||
*
|
*
|
||||||
* @version $Id: $Id
|
* @version $Id: $Id
|
||||||
*/
|
*/
|
||||||
public class PluginsLoader implements Runnable {
|
public class PluginsLoader {
|
||||||
|
|
||||||
private static final String WEBGOAT_PLUGIN_EXTENSION = "jar";
|
private static final String WEBGOAT_PLUGIN_EXTENSION = "jar";
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
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");
|
this.pluginTarget = Objects.requireNonNull(pluginTarget, "plugin target cannot be null");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copy jars to the lib directory
|
||||||
|
*/
|
||||||
public void copyJars() {
|
public void copyJars() {
|
||||||
try {
|
try {
|
||||||
WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader();
|
WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader();
|
||||||
@ -55,12 +57,10 @@ public class PluginsLoader implements Runnable {
|
|||||||
|
|
||||||
List<URL> jars = listJars();
|
List<URL> jars = listJars();
|
||||||
|
|
||||||
cl.closeJARs(true);
|
|
||||||
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());
|
||||||
Files.copy(sourceJarFile, webInfLib.resolve(sourceJarFile.getFileName()),
|
FileUtils.copyFileToDirectory(sourceJarFile.toFile(), webInfLib.toFile());
|
||||||
StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Loading plugins failed", e);
|
logger.error("Loading plugins failed", e);
|
||||||
@ -74,7 +74,6 @@ public class PluginsLoader implements Runnable {
|
|||||||
*/
|
*/
|
||||||
public List<Plugin> loadPlugins() {
|
public List<Plugin> loadPlugins() {
|
||||||
List<Plugin> plugins = Lists.newArrayList();
|
List<Plugin> plugins = Lists.newArrayList();
|
||||||
WebappClassLoader cl = (WebappClassLoader) Thread.currentThread().getContextClassLoader();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
PluginFileUtils.createDirsIfNotExists(pluginTarget);
|
PluginFileUtils.createDirsIfNotExists(pluginTarget);
|
||||||
@ -88,13 +87,12 @@ public class PluginsLoader implements Runnable {
|
|||||||
return plugins;
|
return plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void cleanupExtractedPluginsDirectory() {
|
private void cleanupExtractedPluginsDirectory() {
|
||||||
Path i18nDirectory = pluginTarget.resolve("plugin/i18n/");
|
Path i18nDirectory = pluginTarget.resolve("plugin/i18n/");
|
||||||
FileUtils.deleteQuietly(i18nDirectory.toFile());
|
FileUtils.deleteQuietly(i18nDirectory.toFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<URL> listJars() throws IOException {
|
private List<URL> listJars() throws IOException {
|
||||||
final List<URL> jars = Lists.newArrayList();
|
final List<URL> jars = Lists.newArrayList();
|
||||||
Files.walkFileTree(pluginSource, new SimpleFileVisitor<Path>() {
|
Files.walkFileTree(pluginSource, new SimpleFileVisitor<Path>() {
|
||||||
|
|
||||||
@ -143,12 +141,4 @@ public class PluginsLoader implements Runnable {
|
|||||||
}
|
}
|
||||||
return extractorCallables;
|
return extractorCallables;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
loadPlugins();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user