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

Sun, 26 Nov 2017 18:09:23 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 26 Nov 2017 18:09:23 +0100
changeset 6
da61a1646eba
child 7
598670d5b0b8
permissions
-rw-r--r--

adds module manager

6
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2017 Mike Becker. All rights reserved.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 package de.uapcore.lightpit;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 import java.util.logging.Level;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 import java.util.logging.Logger;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 import javax.servlet.ServletContext;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 import javax.servlet.ServletContextEvent;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 import javax.servlet.ServletContextListener;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 import javax.servlet.ServletRegistration;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 import javax.servlet.annotation.WebListener;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 /**
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 * Scans registered servlets for LightPIT modules.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 @WebListener
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 public class ModuleManager implements ServletContextListener {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 private static final Logger LOG = Logger.getLogger(ModuleManager.class.getName());
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 /**
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 * The attribute name in the servlet context under which an instance of this class can be found.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 public static final String SC_ATTR_NAME = ModuleManager.class.getName();
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 private ServletContext sc;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 @Override
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 public void contextInitialized(ServletContextEvent sce) {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 sc = sce.getServletContext();
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 reloadAll();
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 sc.setAttribute(SC_ATTR_NAME, this);
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 LOG.info("Module manager injected into ServletContext.");
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 @Override
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 public void contextDestroyed(ServletContextEvent sce) {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 assert sc != null && sc.equals(sce.getServletContext());
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 sc.removeAttribute(SC_ATTR_NAME);
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 unloadAll();
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 private boolean isRegisteredAsModule(ServletRegistration sr) {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 try {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 final Class scclass = Class.forName(sr.getClassName());
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 return scclass.isAnnotationPresent(LightPITModule.class);
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 } catch (ClassNotFoundException ex) {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 LOG.log(Level.SEVERE,
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 "Servlet registration refers to a class which cannot be found by the class loader: {0}",
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 sr.getClassName());
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 return false;
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 private void handleServletRegistration(String name, ServletRegistration sr) {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 if (isRegisteredAsModule(sr)) {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 LOG.log(Level.CONFIG, "Module detected: {0}", name);
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 } else {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 LOG.log(Level.FINE, "Servlet {0} is no module, skipping.", name);
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 /**
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 * Scans for modules and reloads them all.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 public void reloadAll() {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 sc.getServletRegistrations().forEach(this::handleServletRegistration);
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 LOG.info("Modules loaded.");
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 /**
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 * Unloads all found modules.
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 */
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 public void unloadAll() {
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 LOG.info("All modules unloaded.");
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 }
da61a1646eba adds module manager
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 }

mercurial