src/main/java/de/uapcore/lightpit/ModuleManager.java

Sun, 10 May 2020 10:11:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 10 May 2020 10:11:10 +0200
changeset 35
4fa33bfa8fb9
parent 34
824d4042c857
child 36
0f4f8f255c32
permissions
-rw-r--r--

fixes wrong location of log4j2.properties

6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
8137ec335416 updates copyright header
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
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
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 package de.uapcore.lightpit;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
31 import de.uapcore.lightpit.entities.Module;
30
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
32 import org.slf4j.Logger;
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
33 import org.slf4j.LoggerFactory;
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
34
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
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 import javax.servlet.ServletContext;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 import javax.servlet.ServletContextEvent;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 import javax.servlet.ServletContextListener;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 import javax.servlet.annotation.WebListener;
30
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
40 import java.sql.Connection;
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
41 import java.sql.SQLException;
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
42 import java.util.*;
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
43 import java.util.concurrent.atomic.AtomicBoolean;
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
44 import java.util.stream.Collectors;
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 /**
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 * Scans registered servlets for LightPIT modules.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 @WebListener
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
50 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
51
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
52 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
53
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 /**
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 * The attribute name in the servlet context under which an instance of this class can be found.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 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
58 private ServletContext sc;
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
59
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
60 /**
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
61 * Maps class names to module information.
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
62 */
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
63 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
64
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
65 /**
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
66 * This flag is true, when synchronization is needed.
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
67 */
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
68 private final AtomicBoolean dirty = new AtomicBoolean(true);
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
69
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 @Override
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 public void contextInitialized(ServletContextEvent sce) {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 sc = sce.getServletContext();
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 reloadAll();
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 sc.setAttribute(SC_ATTR_NAME, this);
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 LOG.info("Module manager injected into ServletContext.");
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 @Override
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 public void contextDestroyed(ServletContextEvent sce) {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 unloadAll();
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
82
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
83 private Optional<LightPITModule> getModuleInfo(Registration reg) {
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 try {
33
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
85 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
86
9
20a9b2bc9063 makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
87 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
88 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
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 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 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
93 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
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 } 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
96 LOG.warn(
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
97 "{} 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
98 reg.getClassName(),
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
99 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
100 );
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
101 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
102
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
103 if (lpservlet && lpmodule) {
33
fd8c40ff78c3 fixes several warnings
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
104 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
105 return Optional.of(moduleInfo);
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
106 } else {
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
107 return Optional.empty();
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
108 }
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 } 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
110 LOG.error(
9
20a9b2bc9063 makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
111 "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
112 reg.getClassName(),
20a9b2bc9063 makes LightPITServlet abstract
Mike Becker <universe@uap-core.de>
parents: 8
diff changeset
113 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
114 );
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
115 return Optional.empty();
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
116 }
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
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);
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
121 if (moduleInfo.isPresent()) {
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
122 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
123 LOG.info("Module detected: {}", name);
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 } 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
125 LOG.debug("Servlet {} is no module, skipping.", name);
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
128
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 /**
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 * Scans for modules and reloads them all.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 public void reloadAll() {
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
133 registeredModules.clear();
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 sc.getServletRegistrations().forEach(this::handleServletRegistration);
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
135
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
136 // TODO: implement dependency resolver
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
137
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
138 dirty.set(true);
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 LOG.info("Modules loaded.");
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 }
30
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
141
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 /**
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
143 * Synchronizes module information with the database.
30
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
144 * <p>
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
145 * This must be called from the {@link AbstractLightPITServlet}.
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
146 * Admittedly the call will perform the synchronization once after reload
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
147 * and be a no-op, afterwards.
30
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
148 * However, since the DatabaseFacade might be loaded after the module
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
149 * manager, we must defer the synchronization to the first request
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
150 * handled by the Servlet.
30
fb30f7b78f9b moves DAO classes to different package
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
151 *
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
152 * @param db interface to the database
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
153 */
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
154 public void syncWithDatabase(DatabaseFacade db) {
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
155 if (dirty.compareAndSet(true, false)) {
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
156 if (db.getDataSource().isPresent()) {
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
157 try (Connection conn = db.getDataSource().get().getConnection()) {
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
158 db.getDataAccessObjects()
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
159 .getModuleDao()
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
160 .syncRegisteredModuleClasses(conn, registeredModules.entrySet());
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
161 } catch (SQLException ex) {
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
162 LOG.error("Unexpected SQL Exception", ex);
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
163 }
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
164 } else {
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
165 LOG.error("No datasource present. Cannot sync module information with database.");
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
166 }
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
167 } else {
22
5a91fb7067af minor changes to logging output
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
168 LOG.trace("Module information clean - no synchronization required.");
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
169 }
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
170 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
171
20
bd1a76c91d5b module synchronization with database
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
172 /**
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 * Unloads all found modules.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 public void unloadAll() {
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
176 registeredModules.clear();
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 LOG.info("All modules unloaded.");
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 }
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
179
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
180 /**
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
181 * Returns the main menu.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
182 *
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
183 * @param db the interface to the database
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
184 * @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
185 */
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
186 public List<Menu> getMainMenu(DatabaseFacade db) {
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
187 // TODO: user specific menu
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
188
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
189 if (db.getDataSource().isPresent()) {
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
190 try (Connection conn = db.getDataSource().get().getConnection()) {
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
191 final List<Module> modules = db.getDataAccessObjects().getModuleDao().list(conn);
31
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
192
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
193 return modules
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
194 .stream()
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
195 .filter(Module::isVisible)
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
196 .sorted(new Module.PriorityComparator())
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
197 .map(mod -> new Menu(
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
198 mod.getClassname(),
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
199 new ResourceKey(
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
200 registeredModules.get(mod.getClassname()).bundleBaseName(),
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
201 registeredModules.get(mod.getClassname()).menuKey()),
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
202 registeredModules.get(mod.getClassname()).modulePath()))
58f78f0142e8 adds module priorities
Mike Becker <universe@uap-core.de>
parents: 30
diff changeset
203 .collect(Collectors.toList());
27
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
204 } catch (SQLException ex) {
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
205 LOG.error("Unexpected SQLException when loading the main menu", ex);
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
206 return Collections.emptyList();
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
207 }
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
208 } else {
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
209 return Collections.emptyList();
1f2a96efa69f removes caching of main menu
Mike Becker <universe@uap-core.de>
parents: 24
diff changeset
210 }
10
89e3e6e28b69 implements automatic menu generation from module information
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
211 }
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
212
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
213 /**
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
214 * Returns an unmodifiable map of all registered modules.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
215 * <p>
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
216 * The key is the classname of the module.
34
824d4042c857 cleanup and simplification of database access layer
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
217 *
21
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
218 * @return the map of registered modules
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
219 */
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
220 public Map<String, LightPITModule> getRegisteredModules() {
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
221 return Collections.unmodifiableMap(registeredModules);
b213fef2539e adds first part of a module manager UI
Mike Becker <universe@uap-core.de>
parents: 20
diff changeset
222 }
6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 }

mercurial