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

changeset 79
f64255a88d66
parent 78
bb4c52bf3439
child 80
27a25f32048e
     1.1 --- a/src/main/java/de/uapcore/lightpit/ModuleManager.java	Sat May 23 13:52:04 2020 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,172 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2018 Mike Becker. All rights reserved.
     1.8 - *
     1.9 - * Redistribution and use in source and binary forms, with or without
    1.10 - * modification, are permitted provided that the following conditions are met:
    1.11 - *
    1.12 - *   1. Redistributions of source code must retain the above copyright
    1.13 - *      notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *   2. Redistributions in binary form must reproduce the above copyright
    1.16 - *      notice, this list of conditions and the following disclaimer in the
    1.17 - *      documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 - * POSSIBILITY OF SUCH DAMAGE.
    1.30 - *
    1.31 - */
    1.32 -package de.uapcore.lightpit;
    1.33 -
    1.34 -import org.slf4j.Logger;
    1.35 -import org.slf4j.LoggerFactory;
    1.36 -
    1.37 -import javax.servlet.Registration;
    1.38 -import javax.servlet.ServletContext;
    1.39 -import javax.servlet.ServletContextEvent;
    1.40 -import javax.servlet.ServletContextListener;
    1.41 -import javax.servlet.annotation.WebListener;
    1.42 -import java.util.ArrayList;
    1.43 -import java.util.Collections;
    1.44 -import java.util.List;
    1.45 -import java.util.Optional;
    1.46 -
    1.47 -/**
    1.48 - * Scans registered servlets for LightPIT modules.
    1.49 - */
    1.50 -@WebListener
    1.51 -public final class ModuleManager implements ServletContextListener {
    1.52 -
    1.53 -    private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class);
    1.54 -
    1.55 -    /**
    1.56 -     * The attribute name in the servlet context under which an instance of this class can be found.
    1.57 -     */
    1.58 -    public static final String SC_ATTR_NAME = ModuleManager.class.getName();
    1.59 -    private ServletContext sc;
    1.60 -
    1.61 -    /**
    1.62 -     * Maps class names to module information.
    1.63 -     */
    1.64 -    private final List<LightPITModule> registeredModules = new ArrayList<>();
    1.65 -
    1.66 -    /**
    1.67 -     * Contains the menu entries for the loaded modules.
    1.68 -     */
    1.69 -    private final List<MenuEntry> mainMenu = new ArrayList<>();
    1.70 -
    1.71 -    @Override
    1.72 -    public void contextInitialized(ServletContextEvent sce) {
    1.73 -        sc = sce.getServletContext();
    1.74 -        reloadAll();
    1.75 -        sc.setAttribute(SC_ATTR_NAME, this);
    1.76 -        LOG.info("Module manager injected into ServletContext.");
    1.77 -    }
    1.78 -
    1.79 -    @Override
    1.80 -    public void contextDestroyed(ServletContextEvent sce) {
    1.81 -        unloadAll();
    1.82 -    }
    1.83 -
    1.84 -    private Optional<LightPITModule> getModuleInfo(Registration reg) {
    1.85 -        try {
    1.86 -            final Class<?> scclass = Class.forName(reg.getClassName());
    1.87 -
    1.88 -            final boolean lpservlet = AbstractLightPITServlet.class.isAssignableFrom(scclass);
    1.89 -            final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class);
    1.90 -
    1.91 -            if (lpservlet && !lpmodule) {
    1.92 -                LOG.warn(
    1.93 -                        "{} is a LightPIT Servlet but is missing the module annotation.",
    1.94 -                        reg.getClassName()
    1.95 -                );
    1.96 -            } else if (!lpservlet && lpmodule) {
    1.97 -                LOG.warn(
    1.98 -                        "{} is annotated as a LightPIT Module but does not extend {}.",
    1.99 -                        reg.getClassName(),
   1.100 -                        AbstractLightPITServlet.class.getSimpleName()
   1.101 -                );
   1.102 -            }
   1.103 -
   1.104 -            if (lpservlet && lpmodule) {
   1.105 -                final LightPITModule moduleInfo = scclass.getAnnotation(LightPITModule.class);
   1.106 -                return Optional.of(moduleInfo);
   1.107 -            } else {
   1.108 -                return Optional.empty();
   1.109 -            }
   1.110 -        } catch (ClassNotFoundException ex) {
   1.111 -            LOG.error(
   1.112 -                    "Servlet registration refers to class {} which cannot be found by the class loader (Reason: {})",
   1.113 -                    reg.getClassName(),
   1.114 -                    ex.getMessage()
   1.115 -            );
   1.116 -            return Optional.empty();
   1.117 -        }
   1.118 -    }
   1.119 -
   1.120 -    private void handleServletRegistration(String name, Registration reg) {
   1.121 -        final Optional<LightPITModule> moduleInfo = getModuleInfo(reg);
   1.122 -        if (moduleInfo.isPresent()) {
   1.123 -            registeredModules.add(moduleInfo.get());
   1.124 -            LOG.info("Module detected: {}", name);
   1.125 -        } else {
   1.126 -            LOG.debug("Servlet {} is no module, skipping.", name);
   1.127 -        }
   1.128 -    }
   1.129 -
   1.130 -    /**
   1.131 -     * Scans for modules and reloads them all.
   1.132 -     */
   1.133 -    public void reloadAll() {
   1.134 -        registeredModules.clear();
   1.135 -        sc.getServletRegistrations().forEach(this::handleServletRegistration);
   1.136 -        createMainMenu();
   1.137 -
   1.138 -        LOG.info("Modules loaded.");
   1.139 -    }
   1.140 -
   1.141 -    /**
   1.142 -     * Unloads all found modules.
   1.143 -     */
   1.144 -    public void unloadAll() {
   1.145 -        registeredModules.clear();
   1.146 -        LOG.info("All modules unloaded.");
   1.147 -    }
   1.148 -
   1.149 -    /**
   1.150 -     * Populates the main menu based on the registered modules.
   1.151 -     */
   1.152 -    private void createMainMenu() {
   1.153 -        mainMenu.clear();
   1.154 -        registeredModules
   1.155 -                .stream()
   1.156 -                .filter(mod -> !mod.systemModule())
   1.157 -                .map(mod -> new MenuEntry(
   1.158 -                        new ResourceKey(
   1.159 -                                mod.bundleBaseName(),
   1.160 -                                "menuLabel"),
   1.161 -                        mod.modulePath() + "/",
   1.162 -                        mod.defaultPriority()))
   1.163 -                .sorted()
   1.164 -                .forEachOrdered(mainMenu::add);
   1.165 -    }
   1.166 -
   1.167 -    /**
   1.168 -     * Returns the main menu.
   1.169 -     *
   1.170 -     * @return a list of menu items
   1.171 -     */
   1.172 -    public List<MenuEntry> getMainMenu() {
   1.173 -        return Collections.unmodifiableList(mainMenu);
   1.174 -    }
   1.175 -}

mercurial