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

Sat, 16 Dec 2017 20:19:28 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 16 Dec 2017 20:19:28 +0100
changeset 10
89e3e6e28b69
parent 9
20a9b2bc9063
child 11
737ab27e37b3
permissions
-rw-r--r--

implements automatic menu generation from module information

universe@6 1 /*
universe@6 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@6 3 *
universe@6 4 * Copyright 2017 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@10 31 import java.util.ArrayList;
universe@10 32 import java.util.Collections;
universe@10 33 import java.util.List;
universe@10 34 import java.util.Optional;
universe@7 35 import javax.servlet.Registration;
universe@6 36 import javax.servlet.ServletContext;
universe@6 37 import javax.servlet.ServletContextEvent;
universe@6 38 import javax.servlet.ServletContextListener;
universe@6 39 import javax.servlet.annotation.WebListener;
universe@8 40 import org.slf4j.Logger;
universe@8 41 import org.slf4j.LoggerFactory;
universe@6 42
universe@6 43 /**
universe@6 44 * Scans registered servlets for LightPIT modules.
universe@6 45 */
universe@6 46 @WebListener
universe@10 47 public final class ModuleManager implements ServletContextListener {
universe@6 48
universe@8 49 private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class);
universe@6 50
universe@6 51 /**
universe@6 52 * The attribute name in the servlet context under which an instance of this class can be found.
universe@6 53 */
universe@6 54 public static final String SC_ATTR_NAME = ModuleManager.class.getName();
universe@10 55 private ServletContext sc;
universe@6 56
universe@10 57 private final List<Menu> mainMenu = new ArrayList<>();
universe@10 58 private final List<Menu> immutableMainMenu = Collections.unmodifiableList(mainMenu);
universe@6 59
universe@6 60 @Override
universe@6 61 public void contextInitialized(ServletContextEvent sce) {
universe@6 62 sc = sce.getServletContext();
universe@6 63 reloadAll();
universe@6 64 sc.setAttribute(SC_ATTR_NAME, this);
universe@6 65 LOG.info("Module manager injected into ServletContext.");
universe@6 66 }
universe@6 67
universe@6 68 @Override
universe@6 69 public void contextDestroyed(ServletContextEvent sce) {
universe@6 70 unloadAll();
universe@6 71 }
universe@6 72
universe@10 73 private Optional<LightPITModule> getModuleInfo(Registration reg) {
universe@6 74 try {
universe@7 75 final Class scclass = Class.forName(reg.getClassName());
universe@7 76
universe@9 77 final boolean lpservlet = AbstractLightPITServlet.class.isAssignableFrom(scclass);
universe@7 78 final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class);
universe@7 79
universe@7 80 if (lpservlet && !lpmodule) {
universe@8 81 LOG.warn(
universe@9 82 "{} is a LightPIT Servlet but is missing the module annotation.",
universe@8 83 reg.getClassName()
universe@8 84 );
universe@7 85 } else if (!lpservlet && lpmodule) {
universe@8 86 LOG.warn(
universe@9 87 "{} is annotated as a LightPIT Module but does not extend {}.",
universe@9 88 reg.getClassName(),
universe@9 89 AbstractLightPITServlet.class.getSimpleName()
universe@8 90 );
universe@7 91 }
universe@7 92
universe@10 93 if (lpservlet && lpmodule) {
universe@10 94 final Class<? extends AbstractLightPITServlet> clazz = scclass;
universe@10 95 final LightPITModule moduleInfo = clazz.getAnnotation(LightPITModule.class);
universe@10 96 return Optional.of(moduleInfo);
universe@10 97 } else {
universe@10 98 return Optional.empty();
universe@10 99 }
universe@6 100 } catch (ClassNotFoundException ex) {
universe@8 101 LOG.error(
universe@9 102 "Servlet registration refers to class {} which cannot be found by the class loader (Reason: {})",
universe@9 103 reg.getClassName(),
universe@9 104 ex.getMessage()
universe@8 105 );
universe@10 106 return Optional.empty();
universe@6 107 }
universe@6 108 }
universe@6 109
universe@10 110 private void addModuleToMenu(LightPITModule moduleInfo) {
universe@10 111 final Menu menu = new Menu(
universe@10 112 new ResourceKey(moduleInfo.bundleBaseName(), moduleInfo.menuKey()),
universe@10 113 moduleInfo.modulePath()
universe@10 114 );
universe@10 115 mainMenu.add(menu);
universe@10 116 }
universe@10 117
universe@7 118 private void handleServletRegistration(String name, Registration reg) {
universe@10 119 final Optional<LightPITModule> moduleInfo = getModuleInfo(reg);
universe@10 120 if (moduleInfo.isPresent()) {
universe@10 121 addModuleToMenu(moduleInfo.get());
universe@10 122
universe@8 123 LOG.info("Module detected: {}", name);
universe@6 124 } else {
universe@8 125 LOG.debug("Servlet {} is no module, skipping.", name);
universe@6 126 }
universe@6 127 }
universe@6 128
universe@6 129 /**
universe@6 130 * Scans for modules and reloads them all.
universe@6 131 */
universe@6 132 public void reloadAll() {
universe@6 133 sc.getServletRegistrations().forEach(this::handleServletRegistration);
universe@6 134 LOG.info("Modules loaded.");
universe@6 135 }
universe@6 136
universe@6 137 /**
universe@6 138 * Unloads all found modules.
universe@6 139 */
universe@6 140 public void unloadAll() {
universe@10 141 mainMenu.clear();
universe@6 142 LOG.info("All modules unloaded.");
universe@6 143 }
universe@10 144
universe@10 145 /**
universe@10 146 * Returns the main menu.
universe@10 147 * @return a list of menus belonging to the main menu
universe@10 148 */
universe@10 149 public List<Menu> getMainMenu() {
universe@10 150 return immutableMainMenu;
universe@10 151 }
universe@6 152 }

mercurial