]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.route/src/org/simantics/district/route/internal/Activator.java
Fixed NPE from Activator
[simantics/district.git] / org.simantics.district.route / src / org / simantics / district / route / internal / Activator.java
1 package org.simantics.district.route.internal;
2
3 import java.util.Hashtable;
4
5 import org.osgi.framework.BundleActivator;
6 import org.osgi.framework.BundleContext;
7 import org.osgi.framework.Constants;
8 import org.osgi.framework.ServiceEvent;
9 import org.osgi.framework.ServiceListener;
10 import org.osgi.framework.ServiceReference;
11 import org.osgi.framework.ServiceRegistration;
12 import org.osgi.util.tracker.ServiceTracker;
13 import org.simantics.district.route.RouteService;
14 import org.simantics.district.route.Router;
15
16 /**
17  * @author Tuukka Lehtonen
18  * @since 6.09
19  */
20 public class Activator implements BundleActivator, ServiceListener {
21
22     private static BundleContext context;
23     private static Activator instance;
24
25     static BundleContext getContext() {
26         return context;
27     }
28
29     public static Activator getInstance() {
30         return instance;
31     }
32
33     private RouteServiceImpl                routeService;
34     private ServiceRegistration<?>          routeServiceRegistration;
35     private ServiceTracker<RouteService, ?> routeServiceTracker;
36     private ServiceTracker<Router, ?>       routerTracker;
37
38     @Override
39     public void start(BundleContext context) throws Exception {
40         Activator.instance = this;
41         Activator.context = context;
42         routeService = new RouteServiceImpl();
43
44         // register the service
45         routeServiceRegistration = context.registerService(RouteService.class.getName(), routeService, new Hashtable<>());
46
47         // create a tracker and track the service
48         routeServiceTracker = new ServiceTracker<>(context, RouteService.class.getName(), null);
49         routeServiceTracker.open();
50
51         // Register initially availble routers
52         routerTracker = new ServiceTracker<>(context, Router.class.getName(), null);
53         routerTracker.open();
54         Object[] routers = routerTracker.getServices();
55         if (routers != null) {
56             for (Object router : routers)
57                 routeService.registerRouter((Router) router);
58         }
59
60         // have a service listener to implement the whiteboard pattern
61         context.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + Router.class.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
62
63         // grab the service
64         routeService = (RouteServiceImpl) routeServiceTracker.getService();
65     }
66
67     @Override
68     public void stop(BundleContext context) throws Exception {
69         // close the service tracker
70         routeServiceTracker.close();
71         routeServiceTracker = null;
72
73         routeServiceRegistration.unregister();
74
75         routeService.close();
76         routeService = null;
77         context = null;
78         instance = null;
79     }
80
81     public void serviceChanged(ServiceEvent ev) {
82         ServiceReference<?> sr = ev.getServiceReference();
83         switch (ev.getType()) {
84             case ServiceEvent.REGISTERED:
85                 routeService.registerRouter((Router) context.getService(sr));
86                 break;
87             case ServiceEvent.UNREGISTERING:
88                 routeService.unregisterRouter((Router) context.getService(sr));
89                 break;
90         }
91     }
92
93     public RouteService getRouteService() {
94         return routeService;
95     }
96
97 }