]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.route/src/org/simantics/district/route/internal/Activator.java
f26d0cd75c829ba2277dac55b47dd36f9d96d5d9
[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         for (Object router : routerTracker.getServices()) {
55             routeService.registerRouter((Router) router);
56         }
57
58         // have a service listener to implement the whiteboard pattern
59         context.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + Router.class.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
60
61         // grab the service
62         routeService = (RouteServiceImpl) routeServiceTracker.getService();
63     }
64
65     @Override
66     public void stop(BundleContext context) throws Exception {
67         // close the service tracker
68         routeServiceTracker.close();
69         routeServiceTracker = null;
70
71         routeServiceRegistration.unregister();
72
73         routeService.close();
74         routeService = null;
75         context = null;
76         instance = null;
77     }
78
79     public void serviceChanged(ServiceEvent ev) {
80         ServiceReference<?> sr = ev.getServiceReference();
81         switch (ev.getType()) {
82             case ServiceEvent.REGISTERED:
83                 routeService.registerRouter((Router) context.getService(sr));
84                 break;
85             case ServiceEvent.UNREGISTERING:
86                 routeService.unregisterRouter((Router) context.getService(sr));
87                 break;
88         }
89     }
90
91     public RouteService getRouteService() {
92         return routeService;
93     }
94
95 }