|
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; |
|
30 |
|
31 import java.util.logging.Level; |
|
32 import java.util.logging.Logger; |
|
33 import javax.servlet.ServletContext; |
|
34 import javax.servlet.ServletContextEvent; |
|
35 import javax.servlet.ServletContextListener; |
|
36 import javax.servlet.ServletRegistration; |
|
37 import javax.servlet.annotation.WebListener; |
|
38 |
|
39 /** |
|
40 * Scans registered servlets for LightPIT modules. |
|
41 */ |
|
42 @WebListener |
|
43 public class ModuleManager implements ServletContextListener { |
|
44 |
|
45 private static final Logger LOG = Logger.getLogger(ModuleManager.class.getName()); |
|
46 |
|
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(); |
|
51 |
|
52 private ServletContext sc; |
|
53 |
|
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 } |
|
61 |
|
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 } |
|
68 |
|
69 private boolean isRegisteredAsModule(ServletRegistration sr) { |
|
70 try { |
|
71 final Class scclass = Class.forName(sr.getClassName()); |
|
72 return scclass.isAnnotationPresent(LightPITModule.class); |
|
73 } catch (ClassNotFoundException ex) { |
|
74 LOG.log(Level.SEVERE, |
|
75 "Servlet registration refers to a class which cannot be found by the class loader: {0}", |
|
76 sr.getClassName()); |
|
77 return false; |
|
78 } |
|
79 } |
|
80 |
|
81 private void handleServletRegistration(String name, ServletRegistration sr) { |
|
82 if (isRegisteredAsModule(sr)) { |
|
83 LOG.log(Level.CONFIG, "Module detected: {0}", name); |
|
84 } else { |
|
85 LOG.log(Level.FINE, "Servlet {0} is no module, skipping.", name); |
|
86 } |
|
87 } |
|
88 |
|
89 /** |
|
90 * Scans for modules and reloads them all. |
|
91 */ |
|
92 public void reloadAll() { |
|
93 sc.getServletRegistrations().forEach(this::handleServletRegistration); |
|
94 LOG.info("Modules loaded."); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Unloads all found modules. |
|
99 */ |
|
100 public void unloadAll() { |
|
101 LOG.info("All modules unloaded."); |
|
102 } |
|
103 } |