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

Tue, 28 Nov 2017 18:59:13 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Nov 2017 18:59:13 +0100
changeset 7
598670d5b0b8
parent 6
da61a1646eba
child 8
2dfdb79b5344
permissions
-rw-r--r--

core functionality should also use the modules system, changed the code structure accordingly

     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.util.logging.Level;
    32 import java.util.logging.Logger;
    33 import javax.servlet.Registration;
    34 import javax.servlet.ServletContext;
    35 import javax.servlet.ServletContextEvent;
    36 import javax.servlet.ServletContextListener;
    37 import javax.servlet.annotation.WebListener;
    39 /**
    40  * Scans registered servlets for LightPIT modules.
    41  */
    42 @WebListener
    43 public class ModuleManager implements ServletContextListener {
    45     private static final Logger LOG = Logger.getLogger(ModuleManager.class.getName());
    47     /**
    48      * The attribute name in the servlet context under which an instance of this class can be found.
    49      */
    50     public static final String SC_ATTR_NAME = ModuleManager.class.getName();
    52     private ServletContext sc;
    54     @Override
    55     public void contextInitialized(ServletContextEvent sce) {
    56         sc = sce.getServletContext();
    57         reloadAll();
    58         sc.setAttribute(SC_ATTR_NAME, this);
    59         LOG.info("Module manager injected into ServletContext.");
    60     }
    62     @Override
    63     public void contextDestroyed(ServletContextEvent sce) {
    64         assert sc != null && sc.equals(sce.getServletContext());
    65         sc.removeAttribute(SC_ATTR_NAME);
    66         unloadAll();
    67     }
    69     private boolean isRegisteredAsModule(Registration reg) {
    70         try {
    71             final Class scclass = Class.forName(reg.getClassName());
    73             final boolean lpservlet = LightPITServlet.class.isAssignableFrom(scclass);
    74             final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class);
    76             if (lpservlet && !lpmodule) {
    77                 LOG.log(Level.WARNING,
    78                     "Servlet is a LightPITServlet but is missing the module annotation: {0}",
    79                     reg.getClassName());
    80             } else if (!lpservlet && lpmodule) {
    81                 LOG.log(Level.WARNING,
    82                     "Servlet is annotated as a LightPITModule but does not extend LightPITServlet: {0}",
    83                     reg.getClassName());
    84             }
    86             return lpservlet && lpmodule;
    87         } catch (ClassNotFoundException ex) {
    88             LOG.log(Level.SEVERE,
    89                     "Servlet registration refers to a class which cannot be found by the class loader: {0}",
    90                     reg.getClassName());
    91             return false;
    92         }        
    93     }
    95     private void handleServletRegistration(String name, Registration reg) {
    96         if (isRegisteredAsModule(reg)) {
    97             LOG.log(Level.CONFIG, "Module detected: {0}", name);
    98         } else {
    99             LOG.log(Level.FINE, "Servlet {0} is no module, skipping.", name);
   100         }
   101     }
   103     /**
   104      * Scans for modules and reloads them all.
   105      */
   106     public void reloadAll() {
   107         sc.getServletRegistrations().forEach(this::handleServletRegistration);
   108         LOG.info("Modules loaded.");
   109     }
   111     /**
   112      * Unloads all found modules.
   113      */
   114     public void unloadAll() {
   115         LOG.info("All modules unloaded.");
   116     }
   117 }

mercurial