Bug 1891 : Fixing the karaf based modularity. Each new tab can be installed as karaf...
[dlux.git] / loader / impl / src / main / java / org / opendaylight / dlux / loader / implementation / DluxLoader.java
index 212046b47761ed8eebbaffd341d14fee26f7ef86..146d504397b973e501456af929c8cc7808bc90ac 100644 (file)
@@ -9,36 +9,60 @@ package org.opendaylight.dlux.loader.implementation;
 
 import javax.servlet.ServletException;
 
+import com.google.common.base.Preconditions;
+import org.opendaylight.dlux.loader.DluxModuleLoader;
 import org.osgi.service.http.HttpService;
 import org.osgi.service.http.NamespaceException;
-import org.opendaylight.dlux.loader.IDluxLoaderRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class DluxLoader implements IDluxLoaderRegistration {
+import java.util.HashMap;
+import java.util.Map;
+
+public class DluxLoader implements DluxModuleLoader {
 
-    private DluxLoaderServlet modules;
     private DluxLoaderIndexServlet index;
     private static Logger logger = LoggerFactory.getLogger(DluxLoader.class);
 
+    /**
+     * List of modules registered with dlux
+     */
+    private Map<String, Module> modules = new HashMap<>();
+
+    private String RESOURCE_URL = "/";
+
+    private String RESOURCE_DIRECTORY = "/dlux";
+
+    private String SERVLET_URL = "/index.html";
+
     @Override
     public void addModule(String bundleName, String url, String requiredJs, String angularJs){
-        modules.addModule(bundleName, url, requiredJs, angularJs);
+        modules.put(bundleName, new Module(bundleName, url, requiredJs, angularJs));
     }
-    public void onUnbindService(HttpService srv) {
-        modules = null;
+
+    @Override
+    public void removeModule(String bundleName) {
+        modules.remove(bundleName);
+    }
+
+    public Map<String, Module> getModules() {
+        return modules;
+    }
+
+    public void onUnbindService(HttpService httpService) {
+        httpService.unregister(SERVLET_URL);
+        httpService.unregister(RESOURCE_URL);
         index = null;
     }
 
-    public void onBindService(HttpService srv) throws ServletException, NamespaceException {
-        if (srv == null) {
-            logger.error("Unable to inject HttpService into DluxBootstrapper.");
-        } else {
-            modules = new DluxLoaderServlet();
-            index = new DluxLoaderIndexServlet();
-            srv.registerServlet("/src/app/modules.js", modules, null, null);
-            srv.registerServlet("/index.html", index, null, null);
-            srv.registerResources("/", "/dlux", null);
-        }
+    public void onBindService(HttpService httpService) throws ServletException, NamespaceException {
+        Preconditions.checkNotNull(httpService,
+            "Unable to inject HttpService into DluxLoader. dlux modules won't work without httpService");
+
+        index = new DluxLoaderIndexServlet(this);
+        httpService.registerServlet(SERVLET_URL, index, null, null);
+        httpService.registerResources(RESOURCE_URL, RESOURCE_DIRECTORY, null);
+        logger.info("DluxLoader Service initialization complete.");
     }
+
 }