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 for (Object router : routerTracker.getServices()) {
55 routeService.registerRouter((Router) router);
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$
62 routeService = (RouteServiceImpl) routeServiceTracker.getService();
66 public void stop(BundleContext context) throws Exception {
67 // close the service tracker
68 routeServiceTracker.close();
69 routeServiceTracker = null;
71 routeServiceRegistration.unregister();
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));
85 case ServiceEvent.UNREGISTERING:
86 routeService.unregisterRouter((Router) context.getService(sr));
91 public RouteService getRouteService() {