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

changeset 34
824d4042c857
parent 33
fd8c40ff78c3
child 36
0f4f8f255c32
     1.1 --- a/src/main/java/de/uapcore/lightpit/ModuleManager.java	Sat May 09 15:19:21 2020 +0200
     1.2 +++ b/src/main/java/de/uapcore/lightpit/ModuleManager.java	Sat May 09 17:01:29 2020 +0200
     1.3 @@ -1,8 +1,8 @@
     1.4  /*
     1.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - * 
     1.7 + *
     1.8   * Copyright 2018 Mike Becker. All rights reserved.
     1.9 - * 
    1.10 + *
    1.11   * Redistribution and use in source and binary forms, with or without
    1.12   * modification, are permitted provided that the following conditions are met:
    1.13   *
    1.14 @@ -24,12 +24,10 @@
    1.15   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.16   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.17   * POSSIBILITY OF SUCH DAMAGE.
    1.18 - * 
    1.19 + *
    1.20   */
    1.21  package de.uapcore.lightpit;
    1.22  
    1.23 -import de.uapcore.lightpit.dao.CoreDAOFactory;
    1.24 -import de.uapcore.lightpit.dao.ModuleDao;
    1.25  import de.uapcore.lightpit.entities.Module;
    1.26  import org.slf4j.Logger;
    1.27  import org.slf4j.LoggerFactory;
    1.28 @@ -50,25 +48,25 @@
    1.29   */
    1.30  @WebListener
    1.31  public final class ModuleManager implements ServletContextListener {
    1.32 -    
    1.33 +
    1.34      private static final Logger LOG = LoggerFactory.getLogger(ModuleManager.class);
    1.35 -    
    1.36 +
    1.37      /**
    1.38       * The attribute name in the servlet context under which an instance of this class can be found.
    1.39       */
    1.40      public static final String SC_ATTR_NAME = ModuleManager.class.getName();
    1.41      private ServletContext sc;
    1.42 -    
    1.43 +
    1.44      /**
    1.45       * Maps class names to module information.
    1.46       */
    1.47      private final Map<String, LightPITModule> registeredModules = new HashMap<>();
    1.48 -    
    1.49 +
    1.50      /**
    1.51       * This flag is true, when synchronization is needed.
    1.52       */
    1.53      private final AtomicBoolean dirty = new AtomicBoolean(true);
    1.54 -    
    1.55 +
    1.56      @Override
    1.57      public void contextInitialized(ServletContextEvent sce) {
    1.58          sc = sce.getServletContext();
    1.59 @@ -81,27 +79,27 @@
    1.60      public void contextDestroyed(ServletContextEvent sce) {
    1.61          unloadAll();
    1.62      }
    1.63 -    
    1.64 +
    1.65      private Optional<LightPITModule> getModuleInfo(Registration reg) {
    1.66          try {
    1.67              final Class<?> scclass = Class.forName(reg.getClassName());
    1.68 -            
    1.69 +
    1.70              final boolean lpservlet = AbstractLightPITServlet.class.isAssignableFrom(scclass);
    1.71              final boolean lpmodule = scclass.isAnnotationPresent(LightPITModule.class);
    1.72 -            
    1.73 +
    1.74              if (lpservlet && !lpmodule) {
    1.75                  LOG.warn(
    1.76 -                    "{} is a LightPIT Servlet but is missing the module annotation.",
    1.77 -                    reg.getClassName()
    1.78 +                        "{} is a LightPIT Servlet but is missing the module annotation.",
    1.79 +                        reg.getClassName()
    1.80                  );
    1.81              } else if (!lpservlet && lpmodule) {
    1.82                  LOG.warn(
    1.83 -                    "{} is annotated as a LightPIT Module but does not extend {}.",
    1.84 -                    reg.getClassName(),
    1.85 -                    AbstractLightPITServlet.class.getSimpleName()
    1.86 +                        "{} is annotated as a LightPIT Module but does not extend {}.",
    1.87 +                        reg.getClassName(),
    1.88 +                        AbstractLightPITServlet.class.getSimpleName()
    1.89                  );
    1.90              }
    1.91 -            
    1.92 +
    1.93              if (lpservlet && lpmodule) {
    1.94                  final LightPITModule moduleInfo = scclass.getAnnotation(LightPITModule.class);
    1.95                  return Optional.of(moduleInfo);
    1.96 @@ -115,28 +113,28 @@
    1.97                      ex.getMessage()
    1.98              );
    1.99              return Optional.empty();
   1.100 -        }        
   1.101 +        }
   1.102      }
   1.103 -    
   1.104 +
   1.105      private void handleServletRegistration(String name, Registration reg) {
   1.106          final Optional<LightPITModule> moduleInfo = getModuleInfo(reg);
   1.107          if (moduleInfo.isPresent()) {
   1.108 -            registeredModules.put(reg.getClassName(), moduleInfo.get());            
   1.109 +            registeredModules.put(reg.getClassName(), moduleInfo.get());
   1.110              LOG.info("Module detected: {}", name);
   1.111          } else {
   1.112              LOG.debug("Servlet {} is no module, skipping.", name);
   1.113          }
   1.114      }
   1.115 -    
   1.116 +
   1.117      /**
   1.118       * Scans for modules and reloads them all.
   1.119       */
   1.120      public void reloadAll() {
   1.121          registeredModules.clear();
   1.122          sc.getServletRegistrations().forEach(this::handleServletRegistration);
   1.123 -        
   1.124 +
   1.125          // TODO: implement dependency resolver
   1.126 -        
   1.127 +
   1.128          dirty.set(true);
   1.129          LOG.info("Modules loaded.");
   1.130      }
   1.131 @@ -157,8 +155,9 @@
   1.132          if (dirty.compareAndSet(true, false)) {
   1.133              if (db.getDataSource().isPresent()) {
   1.134                  try (Connection conn = db.getDataSource().get().getConnection()) {
   1.135 -                    final ModuleDao moduleDao = CoreDAOFactory.getModuleDao(db.getSQLDialect());
   1.136 -                    moduleDao.syncRegisteredModuleClasses(conn, registeredModules.entrySet());
   1.137 +                    db.getDataAccessObjects()
   1.138 +                            .getModuleDao()
   1.139 +                            .syncRegisteredModuleClasses(conn, registeredModules.entrySet());
   1.140                  } catch (SQLException ex) {
   1.141                      LOG.error("Unexpected SQL Exception", ex);
   1.142                  }
   1.143 @@ -169,7 +168,7 @@
   1.144              LOG.trace("Module information clean - no synchronization required.");
   1.145          }
   1.146      }
   1.147 -    
   1.148 +
   1.149      /**
   1.150       * Unloads all found modules.
   1.151       */
   1.152 @@ -180,17 +179,16 @@
   1.153  
   1.154      /**
   1.155       * Returns the main menu.
   1.156 -     * 
   1.157 +     *
   1.158       * @param db the interface to the database
   1.159       * @return a list of menus belonging to the main menu
   1.160       */
   1.161      public List<Menu> getMainMenu(DatabaseFacade db) {
   1.162          // TODO: user specific menu
   1.163 -        
   1.164 +
   1.165          if (db.getDataSource().isPresent()) {
   1.166              try (Connection conn = db.getDataSource().get().getConnection()) {
   1.167 -                final ModuleDao dao = CoreDAOFactory.getModuleDao(db.getSQLDialect());
   1.168 -                final List<Module> modules = dao.listAll(conn);
   1.169 +                final List<Module> modules = db.getDataAccessObjects().getModuleDao().list(conn);
   1.170  
   1.171                  return modules
   1.172                          .stream()
   1.173 @@ -211,12 +209,12 @@
   1.174              return Collections.emptyList();
   1.175          }
   1.176      }
   1.177 -    
   1.178 +
   1.179      /**
   1.180       * Returns an unmodifiable map of all registered modules.
   1.181 -     * 
   1.182 +     * <p>
   1.183       * The key is the classname of the module.
   1.184 -     * 
   1.185 +     *
   1.186       * @return the map of registered modules
   1.187       */
   1.188      public Map<String, LightPITModule> getRegisteredModules() {

mercurial