]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.route/src/org/simantics/district/route/internal/Activator.java
Enable routing problem UI feedback for users
[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     // The plug-in ID
23     public static final String PLUGIN_ID = "org.simantics.district.route"; //$NON-NLS-1$
24
25     private static BundleContext context;
26     private static Activator instance;
27
28     static BundleContext getContext() {
29         return context;
30     }
31
32     public static Activator getInstance() {
33         return instance;
34     }
35
36     private RouteServiceImpl                routeService;
37     private ServiceRegistration<?>          routeServiceRegistration;
38     private ServiceTracker<RouteService, ?> routeServiceTracker;
39     private ServiceTracker<Router, ?>       routerTracker;
40
41     @Override
42     public void start(BundleContext context) throws Exception {
43         Activator.instance = this;
44         Activator.context = context;
45         routeService = new RouteServiceImpl();
46
47         // register the service
48         routeServiceRegistration = context.registerService(RouteService.class.getName(), routeService, new Hashtable<>());
49
50         // create a tracker and track the service
51         routeServiceTracker = new ServiceTracker<>(context, RouteService.class.getName(), null);
52         routeServiceTracker.open();
53
54         // Register initially availble routers
55         routerTracker = new ServiceTracker<>(context, Router.class.getName(), null);
56         routerTracker.open();
57         Object[] routers = routerTracker.getServices();
58         if (routers != null) {
59             for (Object router : routers)
60                 routeService.registerRouter((Router) router);
61         }
62
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$
65
66         // grab the service
67         routeService = (RouteServiceImpl) routeServiceTracker.getService();
68     }
69
70     @Override
71     public void stop(BundleContext context) throws Exception {
72         // close the service tracker
73         routeServiceTracker.close();
74         routeServiceTracker = null;
75
76         routeServiceRegistration.unregister();
77
78         routeService.close();
79         routeService = null;
80         context = null;
81         instance = null;
82     }
83
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));
89                 break;
90             case ServiceEvent.UNREGISTERING:
91                 routeService.unregisterRouter((Router) context.getService(sr));
92                 break;
93         }
94     }
95
96     public RouteService getRouteService() {
97         return routeService;
98     }
99
100 }