1 package org.simantics.district.route.internal;
3 import java.util.Hashtable;
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;
17 * @author Tuukka Lehtonen
20 public class Activator implements BundleActivator, ServiceListener {
22 private static BundleContext context;
23 private static Activator instance;
25 static BundleContext getContext() {
29 public static Activator getInstance() {
33 private RouteServiceImpl routeService;
34 private ServiceRegistration<?> routeServiceRegistration;
35 private ServiceTracker<RouteService, ?> routeServiceTracker;
36 private ServiceTracker<Router, ?> routerTracker;
39 public void start(BundleContext context) throws Exception {
40 Activator.instance = this;
41 Activator.context = context;
42 routeService = new RouteServiceImpl();
44 // register the service
45 routeServiceRegistration = context.registerService(RouteService.class.getName(), routeService, new Hashtable<>());
47 // create a tracker and track the service
48 routeServiceTracker = new ServiceTracker<>(context, RouteService.class.getName(), null);
49 routeServiceTracker.open();
51 // Register initially availble routers
52 routerTracker = new ServiceTracker<>(context, Router.class.getName(), null);
54 Object[] routers = routerTracker.getServices();
55 if (routers != null) {
56 for (Object router : routers)
57 routeService.registerRouter((Router) router);
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$
64 routeService = (RouteServiceImpl) routeServiceTracker.getService();
68 public void stop(BundleContext context) throws Exception {
69 // close the service tracker
70 routeServiceTracker.close();
71 routeServiceTracker = null;
73 routeServiceRegistration.unregister();
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));
87 case ServiceEvent.UNREGISTERING:
88 routeService.unregisterRouter((Router) context.getService(sr));
93 public RouteService getRouteService() {