diff options
| author | Markus Alexander Kuppe | 2010-06-11 12:12:44 +0000 |
|---|---|---|
| committer | Markus Alexander Kuppe | 2010-06-11 12:12:44 +0000 |
| commit | b621f4bdff344211fcc728a3bfa65700bcc6c831 (patch) | |
| tree | 178672c5c4c21f9e3a381f2f1833492b930cac74 | |
| parent | 492b00ab1ec907a83a32a7cace0375057cea7c84 (diff) | |
| download | org.eclipse.ecf-b621f4bdff344211fcc728a3bfa65700bcc6c831.tar.gz org.eclipse.ecf-b621f4bdff344211fcc728a3bfa65700bcc6c831.tar.xz org.eclipse.ecf-b621f4bdff344211fcc728a3bfa65700bcc6c831.zip | |
WIP - bug 314998: [Discovery][DNS-SD] Make provider configurable with Configuration Admin
https://bugs.eclipse.org/bugs/show_bug.cgi?id=314998
| -rw-r--r-- | providers/bundles/org.eclipse.ecf.provider.dnssd/META-INF/MANIFEST.MF | 1 | ||||
| -rw-r--r-- | providers/bundles/org.eclipse.ecf.provider.dnssd/src/org/eclipse/ecf/provider/dnssd/Activator.java | 97 |
2 files changed, 86 insertions, 12 deletions
diff --git a/providers/bundles/org.eclipse.ecf.provider.dnssd/META-INF/MANIFEST.MF b/providers/bundles/org.eclipse.ecf.provider.dnssd/META-INF/MANIFEST.MF index e8c889e1e..f0071ff40 100644 --- a/providers/bundles/org.eclipse.ecf.provider.dnssd/META-INF/MANIFEST.MF +++ b/providers/bundles/org.eclipse.ecf.provider.dnssd/META-INF/MANIFEST.MF @@ -15,6 +15,7 @@ Import-Package: org.eclipse.core.runtime;version="3.4.0", org.eclipse.ecf.discovery;version="3.0.0", org.eclipse.ecf.discovery.identity;version="3.0.0", org.osgi.framework;version="1.5.0", + org.osgi.service.cm;version="1.2.0";resolution:=optional, org.osgi.service.log;version="1.3", org.osgi.util.tracker;version="1.3.1" Bundle-ActivationPolicy: lazy diff --git a/providers/bundles/org.eclipse.ecf.provider.dnssd/src/org/eclipse/ecf/provider/dnssd/Activator.java b/providers/bundles/org.eclipse.ecf.provider.dnssd/src/org/eclipse/ecf/provider/dnssd/Activator.java index 8e0b2713c..9542d18bf 100644 --- a/providers/bundles/org.eclipse.ecf.provider.dnssd/src/org/eclipse/ecf/provider/dnssd/Activator.java +++ b/providers/bundles/org.eclipse.ecf.provider.dnssd/src/org/eclipse/ecf/provider/dnssd/Activator.java @@ -10,6 +10,10 @@ ******************************************************************************/ package org.eclipse.ecf.provider.dnssd; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import java.util.Properties; import org.eclipse.ecf.core.ContainerConnectException; @@ -22,10 +26,13 @@ import org.osgi.framework.Constants; import org.osgi.framework.ServiceFactory; import org.osgi.framework.ServiceReference; import org.osgi.framework.ServiceRegistration; +import org.osgi.service.cm.ConfigurationException; +import org.osgi.service.cm.ManagedServiceFactory; -public class Activator implements BundleActivator { +public class Activator implements BundleActivator, ManagedServiceFactory { - private ServiceRegistration serviceRegistration; + private Map serviceRegistrations = new HashMap(); + private BundleContext context; private static final String NAME = "ecf.discovery.dnssd"; /* @@ -33,10 +40,18 @@ public class Activator implements BundleActivator { * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext context) throws Exception { + this.context = context; + + // register a managed factory for this service + final Properties cmProps = new Properties(); + cmProps.put(Constants.SERVICE_PID, NAME); + context.registerService(ManagedServiceFactory.class.getName(), this, cmProps); + + // register the service final Properties props = new Properties(); props.put("org.eclipse.ecf.discovery.containerName", NAME); props.put(Constants.SERVICE_RANKING, new Integer(750)); - serviceRegistration = context.registerService(IDiscoveryLocator.class.getName(), new ServiceFactory() { + serviceRegistrations.put(null, context.registerService(IDiscoveryLocator.class.getName(), new ServiceFactory() { private volatile DnsSdDisocoveryLocator locator; /* (non-Javadoc) @@ -59,7 +74,7 @@ public class Activator implements BundleActivator { */ public void ungetService(final Bundle bundle, final ServiceRegistration registration, final Object service) { } - }, props); + }, props)); } /* @@ -67,17 +82,75 @@ public class Activator implements BundleActivator { * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { - if (serviceRegistration != null) { - ServiceReference reference = serviceRegistration.getReference(); - IDiscoveryLocator aLocator = (IDiscoveryLocator) context.getService(reference); + if (serviceRegistrations != null) { + for (Iterator itr = serviceRegistrations.values().iterator(); itr.hasNext();) { + ServiceRegistration serviceRegistration = (ServiceRegistration) itr.next(); + ServiceReference reference = serviceRegistration.getReference(); + IDiscoveryLocator aLocator = (IDiscoveryLocator) context.getService(reference); + + serviceRegistration.unregister(); + IContainer container = (IContainer) aLocator.getAdapter(IContainer.class); + container.dispose(); + container.disconnect(); + } - serviceRegistration.unregister(); + serviceRegistrations = null; + } + this.context = null; + } - IContainer container = (IContainer) aLocator.getAdapter(IContainer.class); - container.disconnect(); - container.dispose(); + /* (non-Javadoc) + * @see org.osgi.service.cm.ManagedServiceFactory#getName() + */ + public String getName() { + return this.getClass().getName(); + } - serviceRegistration = null; + /* (non-Javadoc) + * @see org.osgi.service.cm.ManagedServiceFactory#updated(java.lang.String, java.util.Dictionary) + */ + public void updated(String pid, Dictionary properties) + throws ConfigurationException { + if(properties != null) { + Properties props = new Properties(); + props.put(Constants.SERVICE_PID, pid); + + DnsSdDisocoveryLocator locator = new DnsSdDisocoveryLocator(); + DnsSdServiceTypeID targetID = new DnsSdServiceTypeID(); + try { + //TODO use properties and define consts + final String[] searchPaths = (String[]) properties.get("searchPath"); + if(searchPaths != null) { + targetID.setSearchPath(searchPaths); + } + + final String resolver = (String) properties.get("resolver"); + if(resolver != null) { + locator.setResolver(resolver); + } + + locator.connect(targetID, null); + serviceRegistrations.put(pid, context.registerService(IDiscoveryLocator.class.getName(), locator, props)); + } catch (ContainerConnectException e) { + throw new ConfigurationException("", "", e); + } catch (ClassCastException cce) { + throw new ConfigurationException("", "", cce); + } } } + + /* (non-Javadoc) + * @see org.osgi.service.cm.ManagedServiceFactory#deleted(java.lang.String) + */ + public void deleted(String pid) { + ServiceRegistration serviceRegistration = (ServiceRegistration) serviceRegistrations.get(pid); + ServiceReference reference = serviceRegistration.getReference(); + IDiscoveryLocator aLocator = (IDiscoveryLocator) context.getService(reference); + + serviceRegistration.unregister(); + IContainer container = (IContainer) aLocator.getAdapter(IContainer.class); + container.dispose(); + container.disconnect(); + return; + } } |
