Sat, 31 Mar 2018 19:35:04 +0200
module synchronization with database
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 java.sql.Connection;
32 import java.sql.PreparedStatement;
33 import java.sql.ResultSet;
34 import java.sql.SQLException;
35 import java.util.ArrayList;
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 javax.servlet.Registration;
45 import javax.servlet.ServletContext;
46 import javax.servlet.ServletContextEvent;
47 import javax.servlet.ServletContextListener;
48 import javax.servlet.annotation.WebListener;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 /**
53 * Scans registered servlets for LightPIT modules.
54 */
55 @WebListener
56 public final class ModuleManager implements ServletContextListener {
58 private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class);
60 /**
61 * The attribute name in the servlet context under which an instance of this class can be found.
62 */
63 public static final String SC_ATTR_NAME = ModuleManager.class.getName();
64 private ServletContext sc;
66 /**
67 * This flag is true, when synchronization is needed.
68 */
69 private AtomicBoolean dirty = new AtomicBoolean(true);
71 private final CopyOnWriteArrayList<Menu> mainMenu = new CopyOnWriteArrayList<>();
72 private final List<Menu> immutableMainMenu = Collections.unmodifiableList(mainMenu);
74 /**
75 * Maps class names to module information.
76 */
77 private final Map<String, LightPITModule> registeredModules = new HashMap<>();
79 @Override
80 public void contextInitialized(ServletContextEvent sce) {
81 sc = sce.getServletContext();
82 reloadAll();
83 sc.setAttribute(SC_ATTR_NAME, this);
84 LOG.info("Module manager injected into ServletContext.");
85 }
87 @Override
88 public void contextDestroyed(ServletContextEvent sce) {
89 unloadAll();
90 }
92 private Optional<LightPITModule> getModuleInfo(Registration reg) {
93 try {
94 final Class scclass = Class.forName(reg.getClassName());
96 final boolean lpservlet = AbstractLightPITServlet.class.isAssignableFrom(scclass);
97 final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class);
99 if (lpservlet && !lpmodule) {
100 LOG.warn(
101 "{} is a LightPIT Servlet but is missing the module annotation.",
102 reg.getClassName()
103 );
104 } else if (!lpservlet && lpmodule) {
105 LOG.warn(
106 "{} is annotated as a LightPIT Module but does not extend {}.",
107 reg.getClassName(),
108 AbstractLightPITServlet.class.getSimpleName()
109 );
110 }
112 if (lpservlet && lpmodule) {
113 final Class<? extends AbstractLightPITServlet> clazz = scclass;
114 final LightPITModule moduleInfo = clazz.getAnnotation(LightPITModule.class);
115 return Optional.of(moduleInfo);
116 } else {
117 return Optional.empty();
118 }
119 } catch (ClassNotFoundException ex) {
120 LOG.error(
121 "Servlet registration refers to class {} which cannot be found by the class loader (Reason: {})",
122 reg.getClassName(),
123 ex.getMessage()
124 );
125 return Optional.empty();
126 }
127 }
129 private void handleServletRegistration(String name, Registration reg) {
130 final Optional<LightPITModule> moduleInfo = getModuleInfo(reg);
131 if (moduleInfo.isPresent()) {
132 registeredModules.put(reg.getClassName(), moduleInfo.get());
133 LOG.info("Module detected: {}", name);
134 } else {
135 LOG.debug("Servlet {} is no module, skipping.", name);
136 }
137 }
139 /**
140 * Scans for modules and reloads them all.
141 */
142 public void reloadAll() {
143 sc.getServletRegistrations().forEach(this::handleServletRegistration);
145 // TODO: implement dependency resolver
147 LOG.info("Modules loaded.");
148 }
150 /**
151 * Synchronizes module information with the database.
152 *
153 * @param db interface to the database
154 */
155 public void syncWithDatabase(DatabaseFacade db) {
156 if (dirty.compareAndSet(true, false)) {
157 if (db.getDataSource().isPresent()) {
158 try (Connection conn = db.getDataSource().get().getConnection()) {
159 PreparedStatement
160 check = conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?"),
161 insert = conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)");
162 insert.setBoolean(2, true);
163 // update/delete not required, we do this in the module management UI
165 final List<Menu> updatedMenu = new ArrayList<>();
167 for (Entry<String, LightPITModule> mod : registeredModules.entrySet()) {
168 if (mod.getValue().systemModule()) continue;
170 check.setString(1, mod.getKey());
171 try (ResultSet r = check.executeQuery()) {
172 final boolean addToMenu;
173 if (r.next()) {
174 addToMenu = r.getBoolean(1);
175 } else {
176 insert.setString(1, mod.getKey());
177 insert.executeUpdate();
178 addToMenu = !mod.getValue().menuKey().isEmpty();
179 }
180 if (addToMenu) {
181 updatedMenu.add(new Menu(
182 mod.getKey(),
183 new ResourceKey(mod.getValue().bundleBaseName(), mod.getValue().menuKey()),
184 mod.getValue().modulePath()
185 ));
186 }
187 }
188 }
190 mainMenu.removeIf((e) -> !updatedMenu.contains(e));
191 mainMenu.addAllAbsent(updatedMenu);
192 } catch (SQLException ex) {
193 LOG.error("Unexpected SQL Exception", ex);
194 }
195 } else {
196 LOG.warn("No datasource present. Cannot sync module information with database.");
197 }
198 } else {
199 LOG.debug("Module information clean - no synchronization required.");
200 }
201 }
203 /**
204 * Unloads all found modules.
205 */
206 public void unloadAll() {
207 mainMenu.clear();
208 LOG.info("All modules unloaded.");
209 }
211 /**
212 * Returns the main menu.
213 * @return a list of menus belonging to the main menu
214 */
215 public List<Menu> getMainMenu() {
216 return immutableMainMenu;
217 }
218 }