Tue, 26 Dec 2017 19:18:32 +0100
changed logger implementation to log4j2
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 | ||
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
31 | import java.util.ArrayList; |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
32 | import java.util.Collections; |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
33 | import java.util.List; |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
34 | import java.util.Optional; |
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
|
35 | import javax.servlet.Registration; |
6 | 36 | import javax.servlet.ServletContext; |
37 | import javax.servlet.ServletContextEvent; | |
38 | import javax.servlet.ServletContextListener; | |
39 | 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
|
40 | 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
|
41 | import org.slf4j.LoggerFactory; |
6 | 42 | |
43 | /** | |
44 | * Scans registered servlets for LightPIT modules. | |
45 | */ | |
46 | @WebListener | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
47 | public final class ModuleManager implements ServletContextListener { |
6 | 48 | |
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
|
49 | private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class); |
6 | 50 | |
51 | /** | |
52 | * The attribute name in the servlet context under which an instance of this class can be found. | |
53 | */ | |
54 | public static final String SC_ATTR_NAME = ModuleManager.class.getName(); | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
55 | private ServletContext sc; |
6 | 56 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
57 | private final List<Menu> mainMenu = new ArrayList<>(); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
58 | private final List<Menu> immutableMainMenu = Collections.unmodifiableList(mainMenu); |
6 | 59 | |
60 | @Override | |
61 | public void contextInitialized(ServletContextEvent sce) { | |
62 | sc = sce.getServletContext(); | |
63 | reloadAll(); | |
64 | sc.setAttribute(SC_ATTR_NAME, this); | |
65 | LOG.info("Module manager injected into ServletContext."); | |
66 | } | |
67 | ||
68 | @Override | |
69 | public void contextDestroyed(ServletContextEvent sce) { | |
70 | unloadAll(); | |
71 | } | |
72 | ||
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
73 | private Optional<LightPITModule> getModuleInfo(Registration reg) { |
6 | 74 | 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
|
75 | 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
|
76 | |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
77 | final boolean lpservlet = AbstractLightPITServlet.class.isAssignableFrom(scclass); |
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
|
78 | 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
|
79 | |
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 | 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
|
81 | LOG.warn( |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
82 | "{} is a LightPIT Servlet but is missing the module annotation.", |
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
|
83 | 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
|
84 | ); |
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
|
85 | } 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
|
86 | LOG.warn( |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
87 | "{} is annotated as a LightPIT Module but does not extend {}.", |
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
88 | reg.getClassName(), |
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
89 | AbstractLightPITServlet.class.getSimpleName() |
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 | ); |
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
|
91 | } |
598670d5b0b8
core functionality should also use the modules system, changed the code structure accordingly
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
92 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
93 | if (lpservlet && lpmodule) { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
94 | final Class<? extends AbstractLightPITServlet> clazz = scclass; |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
95 | final LightPITModule moduleInfo = clazz.getAnnotation(LightPITModule.class); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
96 | return Optional.of(moduleInfo); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
97 | } else { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
98 | return Optional.empty(); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
99 | } |
6 | 100 | } 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
|
101 | LOG.error( |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
102 | "Servlet registration refers to class {} which cannot be found by the class loader (Reason: {})", |
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
103 | reg.getClassName(), |
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
104 | ex.getMessage() |
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
|
105 | ); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
106 | return Optional.empty(); |
6 | 107 | } |
108 | } | |
109 | ||
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
110 | private void addModuleToMenu(String moduleClassName, LightPITModule moduleInfo) { |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
111 | final Menu menu = new Menu( |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
112 | moduleClassName, |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
113 | new ResourceKey(moduleInfo.bundleBaseName(), moduleInfo.menuKey()), |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
114 | moduleInfo.modulePath() |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
115 | ); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
116 | mainMenu.add(menu); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
117 | } |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
118 | |
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
|
119 | private void handleServletRegistration(String name, Registration reg) { |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
120 | final Optional<LightPITModule> moduleInfo = getModuleInfo(reg); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
121 | if (moduleInfo.isPresent()) { |
11
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
122 | |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
123 | // TODO: remove this call and add the module to some dependency resolver, first |
737ab27e37b3
implements simple request mapper
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
124 | addModuleToMenu(reg.getClassName(), moduleInfo.get()); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
125 | |
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
|
126 | LOG.info("Module detected: {}", name); |
6 | 127 | } 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
|
128 | LOG.debug("Servlet {} is no module, skipping.", name); |
6 | 129 | } |
130 | } | |
131 | ||
132 | /** | |
133 | * Scans for modules and reloads them all. | |
134 | */ | |
135 | public void reloadAll() { | |
136 | sc.getServletRegistrations().forEach(this::handleServletRegistration); | |
137 | LOG.info("Modules loaded."); | |
138 | } | |
139 | ||
140 | /** | |
141 | * Unloads all found modules. | |
142 | */ | |
143 | public void unloadAll() { | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
144 | mainMenu.clear(); |
6 | 145 | LOG.info("All modules unloaded."); |
146 | } | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
147 | |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
148 | /** |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
149 | * Returns the main menu. |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
150 | * @return a list of menus belonging to the main menu |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
151 | */ |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
152 | public List<Menu> getMainMenu() { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
153 | return immutableMainMenu; |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
154 | } |
6 | 155 | } |