Sat, 16 May 2020 15:45:06 +0200
fixes bug where displaying an error page for missing data source would also require that data source (error pages don't try to get database connections now)
also improves error pages in general
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; | |
45
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
39 | import java.util.ArrayList; |
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
40 | import java.util.Collections; |
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
41 | import java.util.List; |
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
42 | import java.util.Optional; |
6 | 43 | |
44 | /** | |
45 | * Scans registered servlets for LightPIT modules. | |
46 | */ | |
47 | @WebListener | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
48 | 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
|
49 | |
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
|
50 | 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
|
51 | |
6 | 52 | /** |
53 | * The attribute name in the servlet context under which an instance of this class can be found. | |
54 | */ | |
55 | 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
|
56 | private ServletContext sc; |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
57 | |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
58 | /** |
27
1f2a96efa69f
removes caching of main menu
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
59 | * Maps class names to module information. |
1f2a96efa69f
removes caching of main menu
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
60 | */ |
45
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
61 | private final List<LightPITModule> registeredModules = new ArrayList<>(); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
62 | |
27
1f2a96efa69f
removes caching of main menu
Mike Becker <universe@uap-core.de>
parents:
24
diff
changeset
|
63 | /** |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
64 | * Contains the menu entries for the loaded modules. |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
65 | */ |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
66 | 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
|
67 | |
6 | 68 | @Override |
69 | public void contextInitialized(ServletContextEvent sce) { | |
70 | sc = sce.getServletContext(); | |
71 | reloadAll(); | |
72 | sc.setAttribute(SC_ATTR_NAME, this); | |
73 | LOG.info("Module manager injected into ServletContext."); | |
74 | } | |
75 | ||
76 | @Override | |
77 | public void contextDestroyed(ServletContextEvent sce) { | |
78 | unloadAll(); | |
79 | } | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
80 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
81 | private Optional<LightPITModule> getModuleInfo(Registration reg) { |
6 | 82 | try { |
33 | 83 | 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
|
84 | |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
85 | 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
|
86 | 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
|
87 | |
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
|
88 | 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
|
89 | LOG.warn( |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
90 | "{} 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
|
91 | 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
|
92 | ); |
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
|
93 | } 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
|
94 | LOG.warn( |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
95 | "{} 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
|
96 | reg.getClassName(), |
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
97 | 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
|
98 | ); |
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
|
99 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
100 | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
101 | if (lpservlet && lpmodule) { |
33 | 102 | 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
|
103 | return Optional.of(moduleInfo); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
104 | } else { |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
105 | return Optional.empty(); |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
106 | } |
6 | 107 | } 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
|
108 | LOG.error( |
9
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
109 | "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
|
110 | reg.getClassName(), |
20a9b2bc9063
makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents:
8
diff
changeset
|
111 | 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
|
112 | ); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
113 | return Optional.empty(); |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
114 | } |
6 | 115 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
116 | |
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
|
117 | 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
|
118 | final Optional<LightPITModule> moduleInfo = getModuleInfo(reg); |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
119 | if (moduleInfo.isPresent()) { |
45
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
120 | registeredModules.add(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
|
121 | LOG.info("Module detected: {}", name); |
6 | 122 | } 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
|
123 | LOG.debug("Servlet {} is no module, skipping.", name); |
6 | 124 | } |
125 | } | |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
126 | |
6 | 127 | /** |
128 | * Scans for modules and reloads them all. | |
129 | */ | |
130 | public void reloadAll() { | |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
131 | registeredModules.clear(); |
6 | 132 | 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
|
133 | createMainMenu(); |
30
fb30f7b78f9b
moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents:
29
diff
changeset
|
134 | |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
135 | LOG.info("Modules loaded."); |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
136 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
137 | |
20
bd1a76c91d5b
module synchronization with database
Mike Becker <universe@uap-core.de>
parents:
18
diff
changeset
|
138 | /** |
6 | 139 | * Unloads all found modules. |
140 | */ | |
141 | public void unloadAll() { | |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
142 | registeredModules.clear(); |
6 | 143 | LOG.info("All modules unloaded."); |
144 | } | |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
145 | |
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
146 | /** |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
147 | * 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
|
148 | */ |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
149 | 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
|
150 | mainMenu.clear(); |
45
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
151 | registeredModules |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
152 | .stream() |
45
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
153 | .filter(mod -> !mod.systemModule()) |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
154 | .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
|
155 | new ResourceKey( |
45
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
156 | mod.bundleBaseName(), |
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
157 | mod.menuKey()), |
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
158 | mod.modulePath(), |
cc7f082c5ef3
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
159 | mod.defaultPriority())) |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
160 | .sorted() |
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
161 | .forEachOrdered(mainMenu::add); |
10
89e3e6e28b69
implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
162 | } |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
163 | |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
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 | * Returns the main menu. |
34
824d4042c857
cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents:
33
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 | * @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
|
168 | */ |
36
0f4f8f255c32
removes features that are not (and probably will not) used anyway
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
169 | 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
|
170 | return Collections.unmodifiableList(mainMenu); |
21
b213fef2539e
adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents:
20
diff
changeset
|
171 | } |
6 | 172 | } |