Sun, 26 Nov 2017 18:09:23 +0100
adds module manager
1.1 --- a/nbproject/project.properties Sun Nov 26 16:51:57 2017 +0100 1.2 +++ b/nbproject/project.properties Sun Nov 26 18:09:23 2017 +0100 1.3 @@ -66,6 +66,7 @@ 1.4 lib.dir=${web.docbase.dir}/WEB-INF/lib 1.5 persistence.xml.dir=${conf.dir} 1.6 platform.active=default_platform 1.7 +project.license=default 1.8 resource.dir=setup 1.9 run.test.classpath=\ 1.10 ${javac.test.classpath}:\
2.1 --- a/src/java/de/uapcore/lightpit/Constants.java Sun Nov 26 16:51:57 2017 +0100 2.2 +++ b/src/java/de/uapcore/lightpit/Constants.java Sun Nov 26 18:09:23 2017 +0100 2.3 @@ -28,6 +28,9 @@ 2.4 */ 2.5 package de.uapcore.lightpit; 2.6 2.7 +/** 2.8 + * Contains all constants used by the this application. 2.9 + */ 2.10 public abstract class Constants { 2.11 public static final String JSP_PATH_PREFIX = "/WEB-INF/view/"; 2.12 }
3.1 --- a/src/java/de/uapcore/lightpit/HomeServlet.java Sun Nov 26 16:51:57 2017 +0100 3.2 +++ b/src/java/de/uapcore/lightpit/HomeServlet.java Sun Nov 26 18:09:23 2017 +0100 3.3 @@ -32,10 +32,18 @@ 3.4 import javax.servlet.RequestDispatcher; 3.5 import javax.servlet.ServletContext; 3.6 import javax.servlet.ServletException; 3.7 +import javax.servlet.annotation.WebServlet; 3.8 import javax.servlet.http.HttpServlet; 3.9 import javax.servlet.http.HttpServletRequest; 3.10 import javax.servlet.http.HttpServletResponse; 3.11 3.12 +/** 3.13 + * Entry point for the application. 3.14 + */ 3.15 +@WebServlet( 3.16 + name = "HomeServlet", 3.17 + urlPatterns = {"/", "/home"} 3.18 +) 3.19 public class HomeServlet extends HttpServlet { 3.20 3.21 private String jspPath(String filename) {
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/java/de/uapcore/lightpit/LightPITModule.java Sun Nov 26 18:09:23 2017 +0100 4.3 @@ -0,0 +1,44 @@ 4.4 +/* 4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 4.6 + * 4.7 + * Copyright 2017 Mike Becker. All rights reserved. 4.8 + * 4.9 + * Redistribution and use in source and binary forms, with or without 4.10 + * modification, are permitted provided that the following conditions are met: 4.11 + * 4.12 + * 1. Redistributions of source code must retain the above copyright 4.13 + * notice, this list of conditions and the following disclaimer. 4.14 + * 4.15 + * 2. Redistributions in binary form must reproduce the above copyright 4.16 + * notice, this list of conditions and the following disclaimer in the 4.17 + * documentation and/or other materials provided with the distribution. 4.18 + * 4.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 4.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 4.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4.29 + * POSSIBILITY OF SUCH DAMAGE. 4.30 + * 4.31 + */ 4.32 +package de.uapcore.lightpit; 4.33 + 4.34 +import java.lang.annotation.ElementType; 4.35 +import java.lang.annotation.Retention; 4.36 +import java.lang.annotation.RetentionPolicy; 4.37 +import java.lang.annotation.Target; 4.38 + 4.39 + 4.40 +/** 4.41 + * Contains information about a LightPIT module. 4.42 + */ 4.43 +@Retention(RetentionPolicy.RUNTIME) 4.44 +@Target(ElementType.TYPE) 4.45 +public @interface LightPITModule { 4.46 + 4.47 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/java/de/uapcore/lightpit/ModuleManager.java Sun Nov 26 18:09:23 2017 +0100 5.3 @@ -0,0 +1,103 @@ 5.4 +/* 5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 5.6 + * 5.7 + * Copyright 2017 Mike Becker. All rights reserved. 5.8 + * 5.9 + * Redistribution and use in source and binary forms, with or without 5.10 + * modification, are permitted provided that the following conditions are met: 5.11 + * 5.12 + * 1. Redistributions of source code must retain the above copyright 5.13 + * notice, this list of conditions and the following disclaimer. 5.14 + * 5.15 + * 2. Redistributions in binary form must reproduce the above copyright 5.16 + * notice, this list of conditions and the following disclaimer in the 5.17 + * documentation and/or other materials provided with the distribution. 5.18 + * 5.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 5.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 5.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 5.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 5.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 5.29 + * POSSIBILITY OF SUCH DAMAGE. 5.30 + * 5.31 + */ 5.32 +package de.uapcore.lightpit; 5.33 + 5.34 +import java.util.logging.Level; 5.35 +import java.util.logging.Logger; 5.36 +import javax.servlet.ServletContext; 5.37 +import javax.servlet.ServletContextEvent; 5.38 +import javax.servlet.ServletContextListener; 5.39 +import javax.servlet.ServletRegistration; 5.40 +import javax.servlet.annotation.WebListener; 5.41 + 5.42 +/** 5.43 + * Scans registered servlets for LightPIT modules. 5.44 + */ 5.45 +@WebListener 5.46 +public class ModuleManager implements ServletContextListener { 5.47 + 5.48 + private static final Logger LOG = Logger.getLogger(ModuleManager.class.getName()); 5.49 + 5.50 + /** 5.51 + * The attribute name in the servlet context under which an instance of this class can be found. 5.52 + */ 5.53 + public static final String SC_ATTR_NAME = ModuleManager.class.getName(); 5.54 + 5.55 + private ServletContext sc; 5.56 + 5.57 + @Override 5.58 + public void contextInitialized(ServletContextEvent sce) { 5.59 + sc = sce.getServletContext(); 5.60 + reloadAll(); 5.61 + sc.setAttribute(SC_ATTR_NAME, this); 5.62 + LOG.info("Module manager injected into ServletContext."); 5.63 + } 5.64 + 5.65 + @Override 5.66 + public void contextDestroyed(ServletContextEvent sce) { 5.67 + assert sc != null && sc.equals(sce.getServletContext()); 5.68 + sc.removeAttribute(SC_ATTR_NAME); 5.69 + unloadAll(); 5.70 + } 5.71 + 5.72 + private boolean isRegisteredAsModule(ServletRegistration sr) { 5.73 + try { 5.74 + final Class scclass = Class.forName(sr.getClassName()); 5.75 + return scclass.isAnnotationPresent(LightPITModule.class); 5.76 + } catch (ClassNotFoundException ex) { 5.77 + LOG.log(Level.SEVERE, 5.78 + "Servlet registration refers to a class which cannot be found by the class loader: {0}", 5.79 + sr.getClassName()); 5.80 + return false; 5.81 + } 5.82 + } 5.83 + 5.84 + private void handleServletRegistration(String name, ServletRegistration sr) { 5.85 + if (isRegisteredAsModule(sr)) { 5.86 + LOG.log(Level.CONFIG, "Module detected: {0}", name); 5.87 + } else { 5.88 + LOG.log(Level.FINE, "Servlet {0} is no module, skipping.", name); 5.89 + } 5.90 + } 5.91 + 5.92 + /** 5.93 + * Scans for modules and reloads them all. 5.94 + */ 5.95 + public void reloadAll() { 5.96 + sc.getServletRegistrations().forEach(this::handleServletRegistration); 5.97 + LOG.info("Modules loaded."); 5.98 + } 5.99 + 5.100 + /** 5.101 + * Unloads all found modules. 5.102 + */ 5.103 + public void unloadAll() { 5.104 + LOG.info("All modules unloaded."); 5.105 + } 5.106 +}
6.1 --- a/web/WEB-INF/web.xml Sun Nov 26 16:51:57 2017 +0100 6.2 +++ b/web/WEB-INF/web.xml Sun Nov 26 18:09:23 2017 +0100 6.3 @@ -1,14 +1,5 @@ 6.4 <?xml version="1.0" encoding="UTF-8"?> 6.5 <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"> 6.6 - <servlet> 6.7 - <servlet-name>HomeServlet</servlet-name> 6.8 - <servlet-class>de.uapcore.lightpit.HomeServlet</servlet-class> 6.9 - </servlet> 6.10 - <servlet-mapping> 6.11 - <servlet-name>HomeServlet</servlet-name> 6.12 - <url-pattern>/</url-pattern> 6.13 - <url-pattern>/home</url-pattern> 6.14 - </servlet-mapping> 6.15 <session-config> 6.16 <session-timeout> 6.17 30