package de.skuzzle.tinyplugz.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import de.skuzzle.tinyplugz.TinyPlugz;
import de.skuzzle.tinyplugz.TinyPlugzConfigurator;
import de.skuzzle.tinyplugz.TinyPlugzConfigurator.DefineProperties;
import de.skuzzle.tinyplugz.TinyPlugzConfigurator.DeployTinyPlugz;
import de.skuzzle.tinyplugz.util.Require;

/**
 * ServletContextListener for configuring TinyPlugz for a web application. Users
 * need to extend this class to provide the actual configuration for TinyPlugz
 * by implementing {@link #configure(DefineProperties, ServletContext)}. You then need to
 * register the listener in your web.xml:
 *
 * <pre>
 * &lt;listener&gt;
 *     &lt;listener-class&gt;com.your.domain.TinyPlugzServletContextListenerImpl&lt;/listener-class&gt;
 * &lt;/listener&gt;
 * </pre>
 *
 * TinyPlugz will then be deployed when this listener receives the
 * contextInitialized event and will be undeployed when the listener receives
 * the contextDestroyed event.
 * @author Simon Taddiken
 */
public abstract class TinyPlugzServletContextListener implements ServletContextListener {

    /**
     * Provides settings for deploying TinyPlugz. Sample implementation of this method:
     *
     * <pre>
     * &#064;Override
     * protected final DeployTinyPlugz configure(DefineProperties props,
     *         ServletContext context) {
     *     final String pathString = context.getRealPath("WEB-INF/plugins");
     *     final Path pluginDir = Paths.get(pathString);
     *
     *     return props.withProperty(Options.FAIL_ON_MULTIPLE_PROVIDERS)
     *             .withPlugins(source -&gt; source.addAllPluginJars(pluginDir));
     * }
     * </pre>
     *
     * @param props Builder object for specifying configuration properties.
     * @param context The current servlet context.
     * @return The {@link DeployTinyPlugz} instance which is returned by the
     *         given builder's
     *         {@link DefineProperties#withPlugins(java.util.function.Consumer)
     *         withPlugins} method.
     */
    protected abstract DeployTinyPlugz configure(DefineProperties props,
            ServletContext context);

    @Override
    public final void contextInitialized(ServletContextEvent sce) {
        final ServletContext ctx = sce.getServletContext();
        final ClassLoader webAppCl = getClass().getClassLoader();
        final DefineProperties props = TinyPlugzConfigurator.setupUsingParent(webAppCl);

        final DeployTinyPlugz config = Require.nonNullResult(
                configure(props, ctx),
                "TinyPlugzServletContextListener.configure");

        config.deploy();
    }

    @Override
    public final void contextDestroyed(ServletContextEvent sce) {
        TinyPlugz.getInstance().undeploy();
    }
}

@@ -665,16 +665,19 @@
 t)%7D. You
+%0A *
  then ne
@@ -681,19 +681,16 @@
  need to
-%0A *
  registe
@@ -1067,16 +1067,19 @@
  event.%0A
+ *%0A
  * @auth
@@ -1278,16 +1278,23 @@
  of this
+%0A     *
  method:
@@ -1507,17 +1507,22 @@
 ealPath(
-%22
+&quot;
 WEB-INF/
@@ -1528,17 +1528,22 @@
 /plugins
-%22
+&quot;
 );%0A     
@@ -2822,32 +2822,74 @@
 extEvent sce) %7B%0A
+        if (TinyPlugz.isDeployed()) %7B%0A    
         TinyPlug
@@ -2912,20 +2912,30 @@
 undeploy();%0A
+        %7D%0A
     %7D%0A%7D%0A

Add deployment check before undeploying