Wed, 13 May 2020 18:40:52 +0200
makes every parameter in a handler method optional
6 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
3 | * |
24 | 4 | * Copyright 2018 Mike Becker. All rights reserved. |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
5 | * |
6 | 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. | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
27 | * |
6 | 28 | */ |
29 | package de.uapcore.lightpit; | |
30 | ||
30
fb30f7b78f9b
moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
31 | import org.slf4j.Logger; |
fb30f7b78f9b
moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
32 | import org.slf4j.LoggerFactory; |
fb30f7b78f9b
moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
33 | |
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
|
34 | import javax.servlet.Registration; |
6 | 35 | import javax.servlet.ServletContext; |
36 | import javax.servlet.ServletContextEvent; | |
37 | import javax.servlet.ServletContextListener; | |
38 | import javax.servlet.annotation.WebListener; | |
30
fb30f7b78f9b
moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
39 | import java.util.*; |
6 | 40 | |
41 | /** | |
42 | * Scans registered servlets for LightPIT modules. | |
43 | */ | |
44 | @WebListener | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
45 | public final class ModuleManager implements ServletContextListener { |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
46 | |
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
|
47 | private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
48 | |
6 | 49 | /** |
50 | * The attribute name in the servlet context under which an instance of this class can be found. | |
51 | */ | |
52 | 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
|
53 | private ServletContext sc; |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
54 | |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
55 | /** |
27
1f2a96efa69f
removes caching of main menu
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
56 | * Maps class names to module information. |
1f2a96efa69f
removes caching of main menu
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
57 | */ |
1f2a96efa69f
removes caching of main menu
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
58 | private final Map<String, LightPITModule> registeredModules = new HashMap<>(); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
59 | |
27
1f2a96efa69f
removes caching of main menu
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
60 | /** |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
61 | * Contains the menu entries for the loaded modules. |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
62 | */ |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
63 | private final List<MenuEntry> mainMenu = new ArrayList<>(); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
64 | |
6 | 65 | @Override |
66 | public void contextInitialized(ServletContextEvent sce) { | |
67 | sc = sce.getServletContext(); | |
68 | reloadAll(); | |
69 | sc.setAttribute(SC_ATTR_NAME, this); | |
70 | LOG.info("Module manager injected into ServletContext."); | |
71 | } | |
72 | ||
73 | @Override | |
74 | public void contextDestroyed(ServletContextEvent sce) { | |
75 | unloadAll(); | |
76 | } | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
77 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
78 | private Optional<LightPITModule> getModuleInfo(Registration reg) { |
6 | 79 | try { |
33 | 80 | final Class<?> scclass = Class.forName(reg.getClassName()); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
81 | |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
82 | 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
|
83 | final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
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 | 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( |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
87 | "{} is a LightPIT Servlet but is missing the module annotation.", |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
88 | reg.getClassName() |
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
|
89 | ); |
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 | } 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
|
91 | LOG.warn( |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
92 | "{} is annotated as a LightPIT Module but does not extend {}.", |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
93 | reg.getClassName(), |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
94 | 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
|
95 | ); |
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
|
96 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
97 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
98 | if (lpservlet && lpmodule) { |
33 | 99 | final LightPITModule moduleInfo = scclass.getAnnotation(LightPITModule.class); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
100 | return Optional.of(moduleInfo); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
101 | } else { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
102 | return Optional.empty(); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
103 | } |
6 | 104 | } 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
|
105 | LOG.error( |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
106 | "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
|
107 | reg.getClassName(), |
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
108 | 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
|
109 | ); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
110 | return Optional.empty(); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
111 | } |
6 | 112 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
113 | |
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
|
114 | 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
|
115 | final Optional<LightPITModule> moduleInfo = getModuleInfo(reg); |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
116 | if (moduleInfo.isPresent()) { |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
117 | registeredModules.put(reg.getClassName(), moduleInfo.get()); |
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
|
118 | LOG.info("Module detected: {}", name); |
6 | 119 | } 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
|
120 | LOG.debug("Servlet {} is no module, skipping.", name); |
6 | 121 | } |
122 | } | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
123 | |
6 | 124 | /** |
125 | * Scans for modules and reloads them all. | |
126 | */ | |
127 | public void reloadAll() { | |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
128 | registeredModules.clear(); |
6 | 129 | sc.getServletRegistrations().forEach(this::handleServletRegistration); |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
130 | createMainMenu(); |
30
fb30f7b78f9b
moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
131 | |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
132 | LOG.info("Modules loaded."); |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
133 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
134 | |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
135 | /** |
6 | 136 | * Unloads all found modules. |
137 | */ | |
138 | public void unloadAll() { | |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
139 | registeredModules.clear(); |
6 | 140 | LOG.info("All modules unloaded."); |
141 | } | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
142 | |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
143 | /** |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
144 | * Populates the main menu based on the registered modules. |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
145 | */ |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
146 | private void createMainMenu() { |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
147 | mainMenu.clear(); |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
148 | registeredModules.entrySet() |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
149 | .stream() |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
150 | .filter(mod -> !mod.getValue().systemModule()) |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
151 | .map(mod -> new MenuEntry( |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
152 | mod.getKey(), |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
153 | new ResourceKey( |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
154 | mod.getValue().bundleBaseName(), |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
155 | mod.getValue().menuKey()), |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
156 | mod.getValue().modulePath(), |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
157 | mod.getValue().defaultPriority())) |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
158 | .sorted() |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
159 | .forEachOrdered(mainMenu::add); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
160 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
161 | |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
162 | /** |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
163 | * Returns the main menu. |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
164 | * |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
165 | * @return a list of menu items |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
166 | */ |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
167 | public List<MenuEntry> getMainMenu() { |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
168 | return Collections.unmodifiableList(mainMenu); |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
169 | } |
6 | 170 | } |