Sun, 01 Apr 2018 18:16:47 +0200
adds first part of a module manager UI
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;
31 import de.uapcore.lightpit.entities.CoreDAOFactory;
32 import de.uapcore.lightpit.entities.ModuleDao;
33 import java.sql.Connection;
34 import java.sql.SQLException;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Map.Entry;
41 import java.util.Optional;
42 import java.util.concurrent.CopyOnWriteArrayList;
43 import java.util.concurrent.atomic.AtomicBoolean;
44 import java.util.stream.Collectors;
45 import javax.servlet.Registration;
46 import javax.servlet.ServletContext;
47 import javax.servlet.ServletContextEvent;
48 import javax.servlet.ServletContextListener;
49 import javax.servlet.annotation.WebListener;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 /**
54 * Scans registered servlets for LightPIT modules.
55 */
56 @WebListener
57 public final class ModuleManager implements ServletContextListener {
59 private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class);
61 /**
62 * The attribute name in the servlet context under which an instance of this class can be found.
63 */
64 public static final String SC_ATTR_NAME = ModuleManager.class.getName();
65 private ServletContext sc;
67 /**
68 * This flag is true, when synchronization is needed.
69 */
70 private final AtomicBoolean dirty = new AtomicBoolean(true);
72 private final CopyOnWriteArrayList<Menu> mainMenu = new CopyOnWriteArrayList<>();
73 private final List<Menu> immutableMainMenu = Collections.unmodifiableList(mainMenu);
75 /**
76 * Maps class names to module information.
77 */
78 private final Map<String, LightPITModule> registeredModules = new HashMap<>();
80 @Override
81 public void contextInitialized(ServletContextEvent sce) {
82 sc = sce.getServletContext();
83 reloadAll();
84 sc.setAttribute(SC_ATTR_NAME, this);
85 LOG.info("Module manager injected into ServletContext.");
86 }
88 @Override
89 public void contextDestroyed(ServletContextEvent sce) {
90 unloadAll();
91 }
93 private Optional<LightPITModule> getModuleInfo(Registration reg) {
94 try {
95 final Class scclass = Class.forName(reg.getClassName());
97 final boolean lpservlet = AbstractLightPITServlet.class.isAssignableFrom(scclass);
98 final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class);
100 if (lpservlet && !lpmodule) {
101 LOG.warn(
102 "{} is a LightPIT Servlet but is missing the module annotation.",
103 reg.getClassName()
104 );
105 } else if (!lpservlet && lpmodule) {
106 LOG.warn(
107 "{} is annotated as a LightPIT Module but does not extend {}.",
108 reg.getClassName(),
109 AbstractLightPITServlet.class.getSimpleName()
110 );
111 }
113 if (lpservlet && lpmodule) {
114 final Class<? extends AbstractLightPITServlet> clazz = scclass;
115 final LightPITModule moduleInfo = clazz.getAnnotation(LightPITModule.class);
116 return Optional.of(moduleInfo);
117 } else {
118 return Optional.empty();
119 }
120 } catch (ClassNotFoundException ex) {
121 LOG.error(
122 "Servlet registration refers to class {} which cannot be found by the class loader (Reason: {})",
123 reg.getClassName(),
124 ex.getMessage()
125 );
126 return Optional.empty();
127 }
128 }
130 private void handleServletRegistration(String name, Registration reg) {
131 final Optional<LightPITModule> moduleInfo = getModuleInfo(reg);
132 if (moduleInfo.isPresent()) {
133 registeredModules.put(reg.getClassName(), moduleInfo.get());
134 LOG.info("Module detected: {}", name);
135 } else {
136 LOG.debug("Servlet {} is no module, skipping.", name);
137 }
138 }
140 /**
141 * Scans for modules and reloads them all.
142 */
143 public void reloadAll() {
144 registeredModules.clear();
145 sc.getServletRegistrations().forEach(this::handleServletRegistration);
147 // TODO: implement dependency resolver
149 dirty.set(true);
150 LOG.info("Modules loaded.");
151 }
153 /**
154 * Synchronizes module information with the database.
155 *
156 * @param db interface to the database
157 */
158 public void syncWithDatabase(DatabaseFacade db) {
159 if (dirty.compareAndSet(true, false)) {
160 if (db.getDataSource().isPresent()) {
161 try (Connection conn = db.getDataSource().get().getConnection()) {
162 final ModuleDao moduleDao = CoreDAOFactory.getModuleDao(db.getSQLDialect());
164 final List<Entry<String, LightPITModule>> visibleModules =
165 moduleDao.syncRegisteredModuleClasses(conn, registeredModules.entrySet());
167 final List<Menu> updatedMenu = visibleModules
168 .stream()
169 .collect(Collectors.mapping(
170 (mod) -> new Menu(
171 mod.getKey(),
172 new ResourceKey(mod.getValue().bundleBaseName(), mod.getValue().menuKey()),
173 mod.getValue().modulePath()),
174 Collectors.toList())
175 );
177 mainMenu.removeIf((e) -> !updatedMenu.contains(e));
178 mainMenu.addAllAbsent(updatedMenu);
179 } catch (SQLException ex) {
180 LOG.error("Unexpected SQL Exception", ex);
181 }
182 } else {
183 LOG.warn("No datasource present. Cannot sync module information with database.");
184 }
185 } else {
186 LOG.debug("Module information clean - no synchronization required.");
187 }
188 }
190 /**
191 * Unloads all found modules.
192 */
193 public void unloadAll() {
194 mainMenu.clear();
195 registeredModules.clear();
196 LOG.info("All modules unloaded.");
197 }
199 /**
200 * Returns the main menu.
201 * @return a list of menus belonging to the main menu
202 */
203 public List<Menu> getMainMenu() {
204 return immutableMainMenu;
205 }
207 /**
208 * Returns an unmodifiable map of all registered modules.
209 *
210 * The key is the classname of the module.
211 *
212 * @return the map of registered modules
213 */
214 public Map<String, LightPITModule> getRegisteredModules() {
215 return Collections.unmodifiableMap(registeredModules);
216 }
217 }