X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.route%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Froute%2Finternal%2FActivator.java;fp=org.simantics.district.route%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Froute%2Finternal%2FActivator.java;h=f26d0cd75c829ba2277dac55b47dd36f9d96d5d9;hb=00103b12908c5071e641ca2bea2c42030ff67770;hp=0000000000000000000000000000000000000000;hpb=2313c99933d1a9351c86da8ea5c648508508e5e2;p=simantics%2Fdistrict.git 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 index 00000000..f26d0cd7 --- /dev/null +++ b/org.simantics.district.route/src/org/simantics/district/route/internal/Activator.java @@ -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 routeServiceTracker; + private ServiceTracker 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; + } + +}