]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.route/src/org/simantics/district/route/internal/Activator.java
Merge "Initial version of the district network Routes view."
[simantics/district.git] / org.simantics.district.route / src / org / simantics / district / route / internal / Activator.java
diff --git a/org.simantics.district.route/src/org/simantics/district/route/internal/Activator.java b/org.simantics.district.route/src/org/simantics/district/route/internal/Activator.java
new file mode 100644 (file)
index 0000000..f26d0cd
--- /dev/null
@@ -0,0 +1,95 @@
+package org.simantics.district.route.internal;
+
+import java.util.Hashtable;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.util.tracker.ServiceTracker;
+import org.simantics.district.route.RouteService;
+import org.simantics.district.route.Router;
+
+/**
+ * @author Tuukka Lehtonen
+ * @since 6.09
+ */
+public class Activator implements BundleActivator, ServiceListener {
+
+    private static BundleContext context;
+    private static Activator instance;
+
+    static BundleContext getContext() {
+        return context;
+    }
+
+    public static Activator getInstance() {
+        return instance;
+    }
+
+    private RouteServiceImpl                routeService;
+    private ServiceRegistration<?>          routeServiceRegistration;
+    private ServiceTracker<RouteService, ?> routeServiceTracker;
+    private ServiceTracker<Router, ?>       routerTracker;
+
+    @Override
+    public void start(BundleContext context) throws Exception {
+        Activator.instance = this;
+        Activator.context = context;
+        routeService = new RouteServiceImpl();
+
+        // register the service
+        routeServiceRegistration = context.registerService(RouteService.class.getName(), routeService, new Hashtable<>());
+
+        // create a tracker and track the service
+        routeServiceTracker = new ServiceTracker<>(context, RouteService.class.getName(), null);
+        routeServiceTracker.open();
+
+        // Register initially availble routers
+        routerTracker = new ServiceTracker<>(context, Router.class.getName(), null);
+        routerTracker.open();
+        for (Object router : routerTracker.getServices()) {
+            routeService.registerRouter((Router) router);
+        }
+
+        // have a service listener to implement the whiteboard pattern
+        context.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + Router.class.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
+
+        // grab the service
+        routeService = (RouteServiceImpl) routeServiceTracker.getService();
+    }
+
+    @Override
+    public void stop(BundleContext context) throws Exception {
+        // close the service tracker
+        routeServiceTracker.close();
+        routeServiceTracker = null;
+
+        routeServiceRegistration.unregister();
+
+        routeService.close();
+        routeService = null;
+        context = null;
+        instance = null;
+    }
+
+    public void serviceChanged(ServiceEvent ev) {
+        ServiceReference<?> sr = ev.getServiceReference();
+        switch (ev.getType()) {
+            case ServiceEvent.REGISTERED:
+                routeService.registerRouter((Router) context.getService(sr));
+                break;
+            case ServiceEvent.UNREGISTERING:
+                routeService.unregisterRouter((Router) context.getService(sr));
+                break;
+        }
+    }
+
+    public RouteService getRouteService() {
+        return routeService;
+    }
+
+}