adds module manager

Sun, 26 Nov 2017 18:09:23 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Nov 2017 18:09:23 +0100
changeset 6
da61a1646eba
parent 5
131903fc16b8
child 7
598670d5b0b8

adds module manager

nbproject/project.properties file | annotate | diff | comparison | revisions
src/java/de/uapcore/lightpit/Constants.java file | annotate | diff | comparison | revisions
src/java/de/uapcore/lightpit/HomeServlet.java file | annotate | diff | comparison | revisions
src/java/de/uapcore/lightpit/LightPITModule.java file | annotate | diff | comparison | revisions
src/java/de/uapcore/lightpit/ModuleManager.java file | annotate | diff | comparison | revisions
web/WEB-INF/web.xml file | annotate | diff | comparison | revisions
--- a/nbproject/project.properties	Sun Nov 26 16:51:57 2017 +0100
+++ b/nbproject/project.properties	Sun Nov 26 18:09:23 2017 +0100
@@ -66,6 +66,7 @@
 lib.dir=${web.docbase.dir}/WEB-INF/lib
 persistence.xml.dir=${conf.dir}
 platform.active=default_platform
+project.license=default
 resource.dir=setup
 run.test.classpath=\
     ${javac.test.classpath}:\
--- a/src/java/de/uapcore/lightpit/Constants.java	Sun Nov 26 16:51:57 2017 +0100
+++ b/src/java/de/uapcore/lightpit/Constants.java	Sun Nov 26 18:09:23 2017 +0100
@@ -28,6 +28,9 @@
  */
 package de.uapcore.lightpit;
 
+/**
+ * Contains all constants used by the this application.
+ */
 public abstract class Constants {
     public static final String JSP_PATH_PREFIX = "/WEB-INF/view/";
 }
--- a/src/java/de/uapcore/lightpit/HomeServlet.java	Sun Nov 26 16:51:57 2017 +0100
+++ b/src/java/de/uapcore/lightpit/HomeServlet.java	Sun Nov 26 18:09:23 2017 +0100
@@ -32,10 +32,18 @@
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+/**
+ * Entry point for the application.
+ */
+@WebServlet(
+        name = "HomeServlet",
+        urlPatterns = {"/", "/home"}
+)
 public class HomeServlet extends HttpServlet {
     
     private String jspPath(String filename) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java/de/uapcore/lightpit/LightPITModule.java	Sun Nov 26 18:09:23 2017 +0100
@@ -0,0 +1,44 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ * 
+ * Copyright 2017 Mike Becker. All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ * 
+ */
+package de.uapcore.lightpit;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+/**
+ * Contains information about a LightPIT module.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface LightPITModule {
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java/de/uapcore/lightpit/ModuleManager.java	Sun Nov 26 18:09:23 2017 +0100
@@ -0,0 +1,103 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ * 
+ * Copyright 2017 Mike Becker. All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ * 
+ */
+package de.uapcore.lightpit;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletRegistration;
+import javax.servlet.annotation.WebListener;
+
+/**
+ * Scans registered servlets for LightPIT modules.
+ */
+@WebListener
+public class ModuleManager implements ServletContextListener {
+    
+    private static final Logger LOG = Logger.getLogger(ModuleManager.class.getName());
+    
+    /**
+     * The attribute name in the servlet context under which an instance of this class can be found.
+     */
+    public static final String SC_ATTR_NAME = ModuleManager.class.getName();
+    
+    private ServletContext sc;
+    
+    @Override
+    public void contextInitialized(ServletContextEvent sce) {
+        sc = sce.getServletContext();
+        reloadAll();
+        sc.setAttribute(SC_ATTR_NAME, this);
+        LOG.info("Module manager injected into ServletContext.");
+    }
+
+    @Override
+    public void contextDestroyed(ServletContextEvent sce) {
+        assert sc != null && sc.equals(sce.getServletContext());
+        sc.removeAttribute(SC_ATTR_NAME);
+        unloadAll();
+    }
+    
+    private boolean isRegisteredAsModule(ServletRegistration sr) {
+        try {
+            final Class scclass = Class.forName(sr.getClassName());
+            return scclass.isAnnotationPresent(LightPITModule.class);
+        } catch (ClassNotFoundException ex) {
+            LOG.log(Level.SEVERE,
+                    "Servlet registration refers to a class which cannot be found by the class loader: {0}",
+                    sr.getClassName());
+            return false;
+        }        
+    }
+    
+    private void handleServletRegistration(String name, ServletRegistration sr) {
+        if (isRegisteredAsModule(sr)) {
+            LOG.log(Level.CONFIG, "Module detected: {0}", name);
+        } else {
+            LOG.log(Level.FINE, "Servlet {0} is no module, skipping.", name);
+        }
+    }
+    
+    /**
+     * Scans for modules and reloads them all.
+     */
+    public void reloadAll() {
+        sc.getServletRegistrations().forEach(this::handleServletRegistration);
+        LOG.info("Modules loaded.");
+    }
+    
+    /**
+     * Unloads all found modules.
+     */
+    public void unloadAll() {
+        LOG.info("All modules unloaded.");
+    }
+}
--- a/web/WEB-INF/web.xml	Sun Nov 26 16:51:57 2017 +0100
+++ b/web/WEB-INF/web.xml	Sun Nov 26 18:09:23 2017 +0100
@@ -1,14 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
-    <servlet>
-        <servlet-name>HomeServlet</servlet-name>
-        <servlet-class>de.uapcore.lightpit.HomeServlet</servlet-class>
-    </servlet>
-    <servlet-mapping>
-        <servlet-name>HomeServlet</servlet-name>
-        <url-pattern>/</url-pattern>
-        <url-pattern>/home</url-pattern>
-    </servlet-mapping>
     <session-config>
         <session-timeout>
             30

mercurial