--- a/src/java/de/uapcore/lightpit/ModuleManager.java Sat Mar 31 19:35:04 2018 +0200 +++ b/src/java/de/uapcore/lightpit/ModuleManager.java Sun Apr 01 18:16:47 2018 +0200 @@ -28,11 +28,11 @@ */ package de.uapcore.lightpit; +import de.uapcore.lightpit.entities.CoreDAOFactory; +import de.uapcore.lightpit.entities.ModuleDao; import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.SQLException; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -41,6 +41,7 @@ import java.util.Optional; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; import javax.servlet.Registration; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; @@ -66,7 +67,7 @@ /** * This flag is true, when synchronization is needed. */ - private AtomicBoolean dirty = new AtomicBoolean(true); + private final AtomicBoolean dirty = new AtomicBoolean(true); private final CopyOnWriteArrayList<Menu> mainMenu = new CopyOnWriteArrayList<>(); private final List<Menu> immutableMainMenu = Collections.unmodifiableList(mainMenu); @@ -140,10 +141,12 @@ * 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."); } @@ -156,36 +159,20 @@ 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 ModuleDao moduleDao = CoreDAOFactory.getModuleDao(db.getSQLDialect()); - final List<Menu> updatedMenu = new ArrayList<>(); + final List<Entry<String, LightPITModule>> visibleModules = + moduleDao.syncRegisteredModuleClasses(conn, registeredModules.entrySet()); - 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() - )); - } - } - } + final List<Menu> updatedMenu = visibleModules + .stream() + .collect(Collectors.mapping( + (mod) -> new Menu( + mod.getKey(), + new ResourceKey(mod.getValue().bundleBaseName(), mod.getValue().menuKey()), + mod.getValue().modulePath()), + Collectors.toList()) + ); mainMenu.removeIf((e) -> !updatedMenu.contains(e)); mainMenu.addAllAbsent(updatedMenu); @@ -205,6 +192,7 @@ */ public void unloadAll() { mainMenu.clear(); + registeredModules.clear(); LOG.info("All modules unloaded."); } @@ -215,4 +203,15 @@ public List<Menu> getMainMenu() { return immutableMainMenu; } + + /** + * Returns an unmodifiable map of all registered modules. + * + * The key is the classname of the module. + * + * @return the map of registered modules + */ + public Map<String, LightPITModule> getRegisteredModules() { + return Collections.unmodifiableMap(registeredModules); + } }