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 {
23 public static final String PLUGIN_ID = "org.simantics.district.route"; //$NON-NLS-1$
25 private static BundleContext context;
26 private static Activator instance;
28 static BundleContext getContext() {
32 public static Activator getInstance() {
36 private RouteServiceImpl routeService;
37 private ServiceRegistration<?> routeServiceRegistration;
38 private ServiceTracker<RouteService, ?> routeServiceTracker;
39 private ServiceTracker<Router, ?> routerTracker;
42 public void start(BundleContext context) throws Exception {
43 Activator.instance = this;
44 Activator.context = context;
45 routeService = new RouteServiceImpl();
47 // register the service
48 routeServiceRegistration = context.registerService(RouteService.class.getName(), routeService, new Hashtable<>());
50 // create a tracker and track the service
51 routeServiceTracker = new ServiceTracker<>(context, RouteService.class.getName(), null);
52 routeServiceTracker.open();
54 // Register initially availble routers
55 routerTracker = new ServiceTracker<>(context, Router.class.getName(), null);
57 Object[] routers = routerTracker.getServices();
58 if (routers != null) {
59 for (Object router : routers)
60 routeService.registerRouter((Router) router);
63 // have a service listener to implement the whiteboard pattern
64 context.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + Router.class.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
67 routeService = (RouteServiceImpl) routeServiceTracker.getService();
71 public void stop(BundleContext context) throws Exception {
72 // close the service tracker
73 routeServiceTracker.close();
74 routeServiceTracker = null;
76 routeServiceRegistration.unregister();
84 public void serviceChanged(ServiceEvent ev) {
85 ServiceReference<?> sr = ev.getServiceReference();
86 switch (ev.getType()) {
87 case ServiceEvent.REGISTERED:
88 routeService.registerRouter((Router) context.getService(sr));
90 case ServiceEvent.UNREGISTERING:
91 routeService.unregisterRouter((Router) context.getService(sr));
96 public RouteService getRouteService() {