--- a/src/main/java/de/uapcore/lightpit/ModuleManager.java Sat May 09 15:19:21 2020 +0200 +++ b/src/main/java/de/uapcore/lightpit/ModuleManager.java Sat May 09 17:01:29 2020 +0200 @@ -1,8 +1,8 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * + * * Copyright 2018 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: * @@ -24,12 +24,10 @@ * 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 de.uapcore.lightpit.dao.CoreDAOFactory; -import de.uapcore.lightpit.dao.ModuleDao; import de.uapcore.lightpit.entities.Module; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -50,25 +48,25 @@ */ @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; - + /** * Maps class names to module information. */ private final Map<String, LightPITModule> registeredModules = new HashMap<>(); - + /** * This flag is true, when synchronization is needed. */ private final AtomicBoolean dirty = new AtomicBoolean(true); - + @Override public void contextInitialized(ServletContextEvent sce) { sc = sce.getServletContext(); @@ -81,27 +79,27 @@ 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() + "{} 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() + "{} is annotated as a LightPIT Module but does not extend {}.", + reg.getClassName(), + AbstractLightPITServlet.class.getSimpleName() ); } - + if (lpservlet && lpmodule) { final LightPITModule moduleInfo = scclass.getAnnotation(LightPITModule.class); return Optional.of(moduleInfo); @@ -115,28 +113,28 @@ 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()); + 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() { registeredModules.clear(); sc.getServletRegistrations().forEach(this::handleServletRegistration); - + // TODO: implement dependency resolver - + dirty.set(true); LOG.info("Modules loaded."); } @@ -157,8 +155,9 @@ if (dirty.compareAndSet(true, false)) { if (db.getDataSource().isPresent()) { try (Connection conn = db.getDataSource().get().getConnection()) { - final ModuleDao moduleDao = CoreDAOFactory.getModuleDao(db.getSQLDialect()); - moduleDao.syncRegisteredModuleClasses(conn, registeredModules.entrySet()); + db.getDataAccessObjects() + .getModuleDao() + .syncRegisteredModuleClasses(conn, registeredModules.entrySet()); } catch (SQLException ex) { LOG.error("Unexpected SQL Exception", ex); } @@ -169,7 +168,7 @@ LOG.trace("Module information clean - no synchronization required."); } } - + /** * Unloads all found modules. */ @@ -180,17 +179,16 @@ /** * Returns the main menu. - * + * * @param db the interface to the database * @return a list of menus belonging to the main menu */ public List<Menu> getMainMenu(DatabaseFacade db) { // TODO: user specific menu - + if (db.getDataSource().isPresent()) { try (Connection conn = db.getDataSource().get().getConnection()) { - final ModuleDao dao = CoreDAOFactory.getModuleDao(db.getSQLDialect()); - final List<Module> modules = dao.listAll(conn); + final List<Module> modules = db.getDataAccessObjects().getModuleDao().list(conn); return modules .stream() @@ -211,12 +209,12 @@ return Collections.emptyList(); } } - + /** * Returns an unmodifiable map of all registered modules. - * + * <p> * The key is the classname of the module. - * + * * @return the map of registered modules */ public Map<String, LightPITModule> getRegisteredModules() {