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