Tue, 28 Nov 2017 18:59:13 +0100
core functionality should also use the modules system, changed the code structure accordingly
universe@6 | 1 | /* |
universe@6 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@6 | 3 | * |
universe@6 | 4 | * Copyright 2017 Mike Becker. All rights reserved. |
universe@6 | 5 | * |
universe@6 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@6 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@6 | 8 | * |
universe@6 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@6 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@6 | 11 | * |
universe@6 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@6 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@6 | 14 | * documentation and/or other materials provided with the distribution. |
universe@6 | 15 | * |
universe@6 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@6 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@6 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@6 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@6 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@6 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@6 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@6 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@6 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@6 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@6 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@6 | 27 | * |
universe@6 | 28 | */ |
universe@6 | 29 | package de.uapcore.lightpit; |
universe@6 | 30 | |
universe@6 | 31 | import java.util.logging.Level; |
universe@6 | 32 | import java.util.logging.Logger; |
universe@7 | 33 | import javax.servlet.Registration; |
universe@6 | 34 | import javax.servlet.ServletContext; |
universe@6 | 35 | import javax.servlet.ServletContextEvent; |
universe@6 | 36 | import javax.servlet.ServletContextListener; |
universe@6 | 37 | import javax.servlet.annotation.WebListener; |
universe@6 | 38 | |
universe@6 | 39 | /** |
universe@6 | 40 | * Scans registered servlets for LightPIT modules. |
universe@6 | 41 | */ |
universe@6 | 42 | @WebListener |
universe@6 | 43 | public class ModuleManager implements ServletContextListener { |
universe@6 | 44 | |
universe@6 | 45 | private static final Logger LOG = Logger.getLogger(ModuleManager.class.getName()); |
universe@6 | 46 | |
universe@6 | 47 | /** |
universe@6 | 48 | * The attribute name in the servlet context under which an instance of this class can be found. |
universe@6 | 49 | */ |
universe@6 | 50 | public static final String SC_ATTR_NAME = ModuleManager.class.getName(); |
universe@6 | 51 | |
universe@6 | 52 | private ServletContext sc; |
universe@6 | 53 | |
universe@6 | 54 | @Override |
universe@6 | 55 | public void contextInitialized(ServletContextEvent sce) { |
universe@6 | 56 | sc = sce.getServletContext(); |
universe@6 | 57 | reloadAll(); |
universe@6 | 58 | sc.setAttribute(SC_ATTR_NAME, this); |
universe@6 | 59 | LOG.info("Module manager injected into ServletContext."); |
universe@6 | 60 | } |
universe@6 | 61 | |
universe@6 | 62 | @Override |
universe@6 | 63 | public void contextDestroyed(ServletContextEvent sce) { |
universe@6 | 64 | assert sc != null && sc.equals(sce.getServletContext()); |
universe@6 | 65 | sc.removeAttribute(SC_ATTR_NAME); |
universe@6 | 66 | unloadAll(); |
universe@6 | 67 | } |
universe@6 | 68 | |
universe@7 | 69 | private boolean isRegisteredAsModule(Registration reg) { |
universe@6 | 70 | try { |
universe@7 | 71 | final Class scclass = Class.forName(reg.getClassName()); |
universe@7 | 72 | |
universe@7 | 73 | final boolean lpservlet = LightPITServlet.class.isAssignableFrom(scclass); |
universe@7 | 74 | final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class); |
universe@7 | 75 | |
universe@7 | 76 | if (lpservlet && !lpmodule) { |
universe@7 | 77 | LOG.log(Level.WARNING, |
universe@7 | 78 | "Servlet is a LightPITServlet but is missing the module annotation: {0}", |
universe@7 | 79 | reg.getClassName()); |
universe@7 | 80 | } else if (!lpservlet && lpmodule) { |
universe@7 | 81 | LOG.log(Level.WARNING, |
universe@7 | 82 | "Servlet is annotated as a LightPITModule but does not extend LightPITServlet: {0}", |
universe@7 | 83 | reg.getClassName()); |
universe@7 | 84 | } |
universe@7 | 85 | |
universe@7 | 86 | return lpservlet && lpmodule; |
universe@6 | 87 | } catch (ClassNotFoundException ex) { |
universe@6 | 88 | LOG.log(Level.SEVERE, |
universe@6 | 89 | "Servlet registration refers to a class which cannot be found by the class loader: {0}", |
universe@7 | 90 | reg.getClassName()); |
universe@6 | 91 | return false; |
universe@6 | 92 | } |
universe@6 | 93 | } |
universe@6 | 94 | |
universe@7 | 95 | private void handleServletRegistration(String name, Registration reg) { |
universe@7 | 96 | if (isRegisteredAsModule(reg)) { |
universe@6 | 97 | LOG.log(Level.CONFIG, "Module detected: {0}", name); |
universe@6 | 98 | } else { |
universe@6 | 99 | LOG.log(Level.FINE, "Servlet {0} is no module, skipping.", name); |
universe@6 | 100 | } |
universe@6 | 101 | } |
universe@6 | 102 | |
universe@6 | 103 | /** |
universe@6 | 104 | * Scans for modules and reloads them all. |
universe@6 | 105 | */ |
universe@6 | 106 | public void reloadAll() { |
universe@6 | 107 | sc.getServletRegistrations().forEach(this::handleServletRegistration); |
universe@6 | 108 | LOG.info("Modules loaded."); |
universe@6 | 109 | } |
universe@6 | 110 | |
universe@6 | 111 | /** |
universe@6 | 112 | * Unloads all found modules. |
universe@6 | 113 | */ |
universe@6 | 114 | public void unloadAll() { |
universe@6 | 115 | LOG.info("All modules unloaded."); |
universe@6 | 116 | } |
universe@6 | 117 | } |