Wed, 13 May 2020 21:10:23 +0200
simplifies menu generation, adds submenus and removes VersionsModule (versions will be part of the ProjectsModule)
universe@6 | 1 | /* |
universe@6 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@34 | 3 | * |
universe@24 | 4 | * Copyright 2018 Mike Becker. All rights reserved. |
universe@34 | 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@34 | 27 | * |
universe@6 | 28 | */ |
universe@6 | 29 | package de.uapcore.lightpit; |
universe@6 | 30 | |
universe@30 | 31 | import org.slf4j.Logger; |
universe@30 | 32 | import org.slf4j.LoggerFactory; |
universe@30 | 33 | |
universe@7 | 34 | import javax.servlet.Registration; |
universe@6 | 35 | import javax.servlet.ServletContext; |
universe@6 | 36 | import javax.servlet.ServletContextEvent; |
universe@6 | 37 | import javax.servlet.ServletContextListener; |
universe@6 | 38 | import javax.servlet.annotation.WebListener; |
universe@45 | 39 | import java.util.ArrayList; |
universe@45 | 40 | import java.util.Collections; |
universe@45 | 41 | import java.util.List; |
universe@45 | 42 | import java.util.Optional; |
universe@6 | 43 | |
universe@6 | 44 | /** |
universe@6 | 45 | * Scans registered servlets for LightPIT modules. |
universe@6 | 46 | */ |
universe@6 | 47 | @WebListener |
universe@10 | 48 | public final class ModuleManager implements ServletContextListener { |
universe@34 | 49 | |
universe@8 | 50 | private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class); |
universe@34 | 51 | |
universe@6 | 52 | /** |
universe@6 | 53 | * The attribute name in the servlet context under which an instance of this class can be found. |
universe@6 | 54 | */ |
universe@6 | 55 | public static final String SC_ATTR_NAME = ModuleManager.class.getName(); |
universe@10 | 56 | private ServletContext sc; |
universe@34 | 57 | |
universe@20 | 58 | /** |
universe@27 | 59 | * Maps class names to module information. |
universe@27 | 60 | */ |
universe@45 | 61 | private final List<LightPITModule> registeredModules = new ArrayList<>(); |
universe@34 | 62 | |
universe@27 | 63 | /** |
universe@36 | 64 | * Contains the menu entries for the loaded modules. |
universe@20 | 65 | */ |
universe@36 | 66 | private final List<MenuEntry> mainMenu = new ArrayList<>(); |
universe@34 | 67 | |
universe@6 | 68 | @Override |
universe@6 | 69 | public void contextInitialized(ServletContextEvent sce) { |
universe@6 | 70 | sc = sce.getServletContext(); |
universe@6 | 71 | reloadAll(); |
universe@6 | 72 | sc.setAttribute(SC_ATTR_NAME, this); |
universe@6 | 73 | LOG.info("Module manager injected into ServletContext."); |
universe@6 | 74 | } |
universe@6 | 75 | |
universe@6 | 76 | @Override |
universe@6 | 77 | public void contextDestroyed(ServletContextEvent sce) { |
universe@6 | 78 | unloadAll(); |
universe@6 | 79 | } |
universe@34 | 80 | |
universe@10 | 81 | private Optional<LightPITModule> getModuleInfo(Registration reg) { |
universe@6 | 82 | try { |
universe@33 | 83 | final Class<?> scclass = Class.forName(reg.getClassName()); |
universe@34 | 84 | |
universe@9 | 85 | final boolean lpservlet = AbstractLightPITServlet.class.isAssignableFrom(scclass); |
universe@7 | 86 | final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class); |
universe@34 | 87 | |
universe@7 | 88 | if (lpservlet && !lpmodule) { |
universe@8 | 89 | LOG.warn( |
universe@34 | 90 | "{} is a LightPIT Servlet but is missing the module annotation.", |
universe@34 | 91 | reg.getClassName() |
universe@8 | 92 | ); |
universe@7 | 93 | } else if (!lpservlet && lpmodule) { |
universe@8 | 94 | LOG.warn( |
universe@34 | 95 | "{} is annotated as a LightPIT Module but does not extend {}.", |
universe@34 | 96 | reg.getClassName(), |
universe@34 | 97 | AbstractLightPITServlet.class.getSimpleName() |
universe@8 | 98 | ); |
universe@7 | 99 | } |
universe@34 | 100 | |
universe@10 | 101 | if (lpservlet && lpmodule) { |
universe@33 | 102 | final LightPITModule moduleInfo = scclass.getAnnotation(LightPITModule.class); |
universe@10 | 103 | return Optional.of(moduleInfo); |
universe@10 | 104 | } else { |
universe@10 | 105 | return Optional.empty(); |
universe@10 | 106 | } |
universe@6 | 107 | } catch (ClassNotFoundException ex) { |
universe@8 | 108 | LOG.error( |
universe@9 | 109 | "Servlet registration refers to class {} which cannot be found by the class loader (Reason: {})", |
universe@9 | 110 | reg.getClassName(), |
universe@9 | 111 | ex.getMessage() |
universe@8 | 112 | ); |
universe@10 | 113 | return Optional.empty(); |
universe@34 | 114 | } |
universe@6 | 115 | } |
universe@34 | 116 | |
universe@7 | 117 | private void handleServletRegistration(String name, Registration reg) { |
universe@10 | 118 | final Optional<LightPITModule> moduleInfo = getModuleInfo(reg); |
universe@20 | 119 | if (moduleInfo.isPresent()) { |
universe@45 | 120 | registeredModules.add(moduleInfo.get()); |
universe@8 | 121 | LOG.info("Module detected: {}", name); |
universe@6 | 122 | } else { |
universe@8 | 123 | LOG.debug("Servlet {} is no module, skipping.", name); |
universe@6 | 124 | } |
universe@6 | 125 | } |
universe@34 | 126 | |
universe@6 | 127 | /** |
universe@6 | 128 | * Scans for modules and reloads them all. |
universe@6 | 129 | */ |
universe@6 | 130 | public void reloadAll() { |
universe@21 | 131 | registeredModules.clear(); |
universe@6 | 132 | sc.getServletRegistrations().forEach(this::handleServletRegistration); |
universe@36 | 133 | createMainMenu(); |
universe@34 | 134 | |
universe@6 | 135 | LOG.info("Modules loaded."); |
universe@6 | 136 | } |
universe@30 | 137 | |
universe@6 | 138 | /** |
universe@6 | 139 | * Unloads all found modules. |
universe@6 | 140 | */ |
universe@6 | 141 | public void unloadAll() { |
universe@21 | 142 | registeredModules.clear(); |
universe@6 | 143 | LOG.info("All modules unloaded."); |
universe@6 | 144 | } |
universe@10 | 145 | |
universe@10 | 146 | /** |
universe@36 | 147 | * Populates the main menu based on the registered modules. |
universe@10 | 148 | */ |
universe@36 | 149 | private void createMainMenu() { |
universe@36 | 150 | mainMenu.clear(); |
universe@45 | 151 | registeredModules |
universe@36 | 152 | .stream() |
universe@45 | 153 | .filter(mod -> !mod.systemModule()) |
universe@36 | 154 | .map(mod -> new MenuEntry( |
universe@36 | 155 | new ResourceKey( |
universe@45 | 156 | mod.bundleBaseName(), |
universe@45 | 157 | mod.menuKey()), |
universe@45 | 158 | mod.modulePath(), |
universe@45 | 159 | mod.defaultPriority())) |
universe@36 | 160 | .sorted() |
universe@36 | 161 | .forEachOrdered(mainMenu::add); |
universe@10 | 162 | } |
universe@34 | 163 | |
universe@21 | 164 | /** |
universe@36 | 165 | * Returns the main menu. |
universe@34 | 166 | * |
universe@36 | 167 | * @return a list of menu items |
universe@21 | 168 | */ |
universe@36 | 169 | public List<MenuEntry> getMainMenu() { |
universe@36 | 170 | return Collections.unmodifiableList(mainMenu); |
universe@21 | 171 | } |
universe@6 | 172 | } |