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

Sat, 31 Mar 2018 19:35:04 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 31 Mar 2018 19:35:04 +0200
changeset 20
bd1a76c91d5b
parent 18
a94b172c3a93
child 21
b213fef2539e
permissions
-rw-r--r--

module synchronization with database

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2017 Mike Becker. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package de.uapcore.lightpit;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.Registration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Scans registered servlets for LightPIT modules.
 */
@WebListener
public final class ModuleManager implements ServletContextListener {
    
    private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class);
    
    /**
     * The attribute name in the servlet context under which an instance of this class can be found.
     */
    public static final String SC_ATTR_NAME = ModuleManager.class.getName();
    private ServletContext sc;
    
    /**
     * This flag is true, when synchronization is needed.
     */
    private AtomicBoolean dirty = new AtomicBoolean(true);
    
    private final CopyOnWriteArrayList<Menu> mainMenu = new CopyOnWriteArrayList<>();
    private final List<Menu> immutableMainMenu = Collections.unmodifiableList(mainMenu);
    
    /**
     * Maps class names to module information.
     */
    private final Map<String, LightPITModule> registeredModules = new HashMap<>();
    
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        sc = sce.getServletContext();
        reloadAll();
        sc.setAttribute(SC_ATTR_NAME, this);
        LOG.info("Module manager injected into ServletContext.");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        unloadAll();
    }
    
    private Optional<LightPITModule> getModuleInfo(Registration reg) {
        try {
            final Class scclass = Class.forName(reg.getClassName());
            
            final boolean lpservlet = AbstractLightPITServlet.class.isAssignableFrom(scclass);
            final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class);
            
            if (lpservlet && !lpmodule) {
                LOG.warn(
                    "{} is a LightPIT Servlet but is missing the module annotation.",
                    reg.getClassName()
                );
            } else if (!lpservlet && lpmodule) {
                LOG.warn(
                    "{} is annotated as a LightPIT Module but does not extend {}.",
                    reg.getClassName(),
                    AbstractLightPITServlet.class.getSimpleName()
                );
            }
            
            if (lpservlet && lpmodule) {
                final Class<? extends AbstractLightPITServlet> clazz = scclass;
                final LightPITModule moduleInfo = clazz.getAnnotation(LightPITModule.class);
                return Optional.of(moduleInfo);
            } else {
                return Optional.empty();
            }
        } catch (ClassNotFoundException ex) {
            LOG.error(
                    "Servlet registration refers to class {} which cannot be found by the class loader (Reason: {})",
                    reg.getClassName(),
                    ex.getMessage()
            );
            return Optional.empty();
        }        
    }
    
    private void handleServletRegistration(String name, Registration reg) {
        final Optional<LightPITModule> moduleInfo = getModuleInfo(reg);
        if (moduleInfo.isPresent()) {
            registeredModules.put(reg.getClassName(), moduleInfo.get());            
            LOG.info("Module detected: {}", name);
        } else {
            LOG.debug("Servlet {} is no module, skipping.", name);
        }
    }
    
    /**
     * Scans for modules and reloads them all.
     */
    public void reloadAll() {
        sc.getServletRegistrations().forEach(this::handleServletRegistration);
        
        // TODO: implement dependency resolver
        
        LOG.info("Modules loaded.");
    }
    
    /**
     * Synchronizes module information with the database.
     * 
     * @param db interface to the database
     */
    public void syncWithDatabase(DatabaseFacade db) {
        if (dirty.compareAndSet(true, false)) {
            if (db.getDataSource().isPresent()) {
                try (Connection conn = db.getDataSource().get().getConnection()) {
                    PreparedStatement
                            check = conn.prepareStatement("SELECT visible FROM lpitcore_module WHERE classname = ?"),
                            insert = conn.prepareStatement("INSERT INTO lpitcore_module (classname, visible) VALUES (?, ?)");
                    insert.setBoolean(2, true);
                    // update/delete not required, we do this in the module management UI
                    
                    final List<Menu> updatedMenu = new ArrayList<>();
                    
                    for (Entry<String, LightPITModule> mod : registeredModules.entrySet()) {
                        if (mod.getValue().systemModule()) continue;
                        
                        check.setString(1, mod.getKey());
                        try (ResultSet r = check.executeQuery()) {
                            final boolean addToMenu;
                            if (r.next()) {
                                addToMenu = r.getBoolean(1);
                            } else {
                                insert.setString(1, mod.getKey());
                                insert.executeUpdate();
                                addToMenu = !mod.getValue().menuKey().isEmpty();
                            }
                            if (addToMenu) {
                                updatedMenu.add(new Menu(
                                        mod.getKey(),
                                        new ResourceKey(mod.getValue().bundleBaseName(), mod.getValue().menuKey()),
                                        mod.getValue().modulePath()
                                ));
                            }
                        }
                    }
                    
                    mainMenu.removeIf((e) -> !updatedMenu.contains(e));
                    mainMenu.addAllAbsent(updatedMenu);
                } catch (SQLException ex) {
                    LOG.error("Unexpected SQL Exception", ex);
                }
            } else {
                LOG.warn("No datasource present. Cannot sync module information with database.");
            }
        } else {
            LOG.debug("Module information clean - no synchronization required.");
        }
    }
    
    /**
     * Unloads all found modules.
     */
    public void unloadAll() {
        mainMenu.clear();
        LOG.info("All modules unloaded.");
    }

    /**
     * Returns the main menu.
     * @return a list of menus belonging to the main menu
     */
    public List<Menu> getMainMenu() {
        return immutableMainMenu;
    }
}

mercurial