Sun, 10 Dec 2017 16:10:14 +0100
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
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 | ||
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
|
31 | import javax.servlet.Registration; |
6 | 32 | import javax.servlet.ServletContext; |
33 | import javax.servlet.ServletContextEvent; | |
34 | import javax.servlet.ServletContextListener; | |
35 | import javax.servlet.annotation.WebListener; | |
8
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
36 | import org.slf4j.Logger; |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
37 | import org.slf4j.LoggerFactory; |
6 | 38 | |
39 | /** | |
40 | * Scans registered servlets for LightPIT modules. | |
41 | */ | |
42 | @WebListener | |
43 | public class ModuleManager implements ServletContextListener { | |
44 | ||
8
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
45 | private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class); |
6 | 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) { |
8
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
77 | LOG.warn( |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
78 | "Servlet is a LightPITServlet but is missing the module annotation: {}", |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
79 | reg.getClassName() |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
80 | ); |
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
|
81 | } else if (!lpservlet && lpmodule) { |
8
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
82 | LOG.warn( |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
83 | "Servlet is annotated as a LightPITModule but does not extend LightPITServlet: {}", |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
84 | reg.getClassName() |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
85 | ); |
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
|
86 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
87 | |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
88 | return lpservlet && lpmodule; |
6 | 89 | } catch (ClassNotFoundException ex) { |
8
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
90 | LOG.error( |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
91 | "Servlet registration refers to a class which cannot be found by the class loader: {}", |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
92 | reg.getClassName() |
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
93 | ); |
6 | 94 | return false; |
95 | } | |
96 | } | |
97 | ||
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
|
98 | 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
|
99 | if (isRegisteredAsModule(reg)) { |
8
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
100 | LOG.info("Module detected: {}", name); |
6 | 101 | } else { |
8
2dfdb79b5344
adds slf4j to project as netbeans library (make sure to have it configured in your environment)
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
102 | LOG.debug("Servlet {} is no module, skipping.", name); |
6 | 103 | } |
104 | } | |
105 | ||
106 | /** | |
107 | * Scans for modules and reloads them all. | |
108 | */ | |
109 | public void reloadAll() { | |
110 | sc.getServletRegistrations().forEach(this::handleServletRegistration); | |
111 | LOG.info("Modules loaded."); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Unloads all found modules. | |
116 | */ | |
117 | public void unloadAll() { | |
118 | LOG.info("All modules unloaded."); | |
119 | } | |
120 | } |