Tue, 28 Nov 2017 18:59:13 +0100
core functionality should also use the modules system, changed the code structure accordingly
6 | 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; | |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
33 | import javax.servlet.Registration; |
6 | 34 | import javax.servlet.ServletContext; |
35 | import javax.servlet.ServletContextEvent; | |
36 | import javax.servlet.ServletContextListener; | |
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 | ||
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
69 | private boolean isRegisteredAsModule(Registration reg) { |
6 | 70 | try { |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
71 | final Class scclass = Class.forName(reg.getClassName()); |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
72 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
73 | final boolean lpservlet = LightPITServlet.class.isAssignableFrom(scclass); |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
74 | final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class); |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
75 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
76 | if (lpservlet && !lpmodule) { |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
77 | LOG.log(Level.WARNING, |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
78 | "Servlet is a LightPITServlet but is missing the module annotation: {0}", |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
79 | reg.getClassName()); |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
80 | } else if (!lpservlet && lpmodule) { |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
81 | LOG.log(Level.WARNING, |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
82 | "Servlet is annotated as a LightPITModule but does not extend LightPITServlet: {0}", |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
83 | reg.getClassName()); |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
84 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
85 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
86 | return lpservlet && lpmodule; |
6 | 87 | } catch (ClassNotFoundException ex) { |
88 | LOG.log(Level.SEVERE, | |
89 | "Servlet registration refers to a class which cannot be found by the class loader: {0}", | |
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
90 | reg.getClassName()); |
6 | 91 | return false; |
92 | } | |
93 | } | |
94 | ||
7
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
95 | private void handleServletRegistration(String name, Registration reg) { |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
96 | if (isRegisteredAsModule(reg)) { |
6 | 97 | LOG.log(Level.CONFIG, "Module detected: {0}", name); |
98 | } else { | |
99 | LOG.log(Level.FINE, "Servlet {0} is no module, skipping.", name); | |
100 | } | |
101 | } | |
102 | ||
103 | /** | |
104 | * Scans for modules and reloads them all. | |
105 | */ | |
106 | public void reloadAll() { | |
107 | sc.getServletRegistrations().forEach(this::handleServletRegistration); | |
108 | LOG.info("Modules loaded."); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Unloads all found modules. | |
113 | */ | |
114 | public void unloadAll() { | |
115 | LOG.info("All modules unloaded."); | |
116 | } | |
117 | } |