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