df49a5e91a49ddb56f93baa44bfbc762278ae123
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / dlux / loader / impl / src / main / java / org / opendaylight / dlux / loader / implementation / DluxLoader.java
1 /**
2  * Copyright (c) 2014 Inocybe Technologies, and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.dlux.loader.implementation;
10
11 import javax.servlet.ServletException;
12
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.dlux.loader.DluxModuleLoader;
15 import org.opendaylight.dlux.loader.Module;
16 import org.opendaylight.dlux.loader.exception.DluxLoaderException;
17 import org.osgi.service.http.HttpService;
18 import org.osgi.service.http.NamespaceException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 public class DluxLoader implements DluxModuleLoader {
26
27     private DluxLoaderIndexServlet index;
28     private static Logger logger = LoggerFactory.getLogger(DluxLoader.class);
29
30     /**
31      * List of modules registered with dlux
32      */
33     private List<Module> modules = new ArrayList<>();
34
35     private String RESOURCE_URL = "/";
36
37     private String RESOURCE_DIRECTORY = "/dlux";
38
39     private String SERVLET_URL = "/index.html";
40
41     @Override
42     public void addModule(Module module){
43         modules.add(module);
44     }
45
46     @Override
47     public void removeModule(Module module) {
48         modules.remove(module);
49     }
50
51     public List<Module> getModules() {
52         return modules;
53     }
54
55     public void onUnbindService(HttpService httpService) {
56         httpService.unregister(SERVLET_URL);
57         httpService.unregister(RESOURCE_URL);
58         index = null;
59     }
60
61     public void onBindService(HttpService httpService) throws ServletException, NamespaceException, DluxLoaderException {
62         Preconditions.checkNotNull(httpService,
63             "Unable to inject HttpService into DluxLoader. dlux modules won't work without httpService");
64
65         index = new DluxLoaderIndexServlet(this);
66         httpService.registerServlet(SERVLET_URL, index, null, null);
67         httpService.registerResources(RESOURCE_URL, RESOURCE_DIRECTORY, null);
68         logger.info("DluxLoader Service initialization complete.");
69     }
70
71 }