diff options
author | Markus Alexander Kuppe | 2010-06-14 11:13:57 +0000 |
---|---|---|
committer | Markus Alexander Kuppe | 2010-06-14 11:13:57 +0000 |
commit | 5c4826a9c7655af49f61c470c88a05253e8d0232 (patch) | |
tree | f16fc625d1e57b97cc2672ef9ce988f652b923d0 /tests/bundles/org.eclipse.ecf.tests.provider.dnssd | |
parent | 7651eb1713217d5465f98285ebeafac788b6c49b (diff) | |
download | org.eclipse.ecf-5c4826a9c7655af49f61c470c88a05253e8d0232.tar.gz org.eclipse.ecf-5c4826a9c7655af49f61c470c88a05253e8d0232.tar.xz org.eclipse.ecf-5c4826a9c7655af49f61c470c88a05253e8d0232.zip |
WIP - bug 314998: [Discovery][DNS-SD] Make provider configurable with Configuration Admin https://bugs.eclipse.org/bugs/show_bug.cgi?id=314998
Diffstat (limited to 'tests/bundles/org.eclipse.ecf.tests.provider.dnssd')
-rw-r--r-- | tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/Activator.java | 82 |
1 files changed, 61 insertions, 21 deletions
diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/Activator.java b/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/Activator.java index c90eac5fb..73a6a4fe3 100644 --- a/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/Activator.java +++ b/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/Activator.java @@ -14,22 +14,27 @@ import java.util.Dictionary; import java.util.Hashtable; import org.eclipse.ecf.discovery.IDiscoveryLocator; +import org.eclipse.ecf.provider.dnssd.IDnsSdDiscoveryConstants; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; import org.osgi.framework.Filter; import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; import org.osgi.service.cm.Configuration; import org.osgi.service.cm.ConfigurationAdmin; public class Activator implements BundleActivator { private static Activator instance; - - private Filter filter; - private BundleContext context; + private IDiscoveryLocator discoveryLocator; + + private ServiceListener listener; + private final Object lock = new Object(); + public Activator() { instance = this; } @@ -37,8 +42,8 @@ public class Activator implements BundleActivator { /* (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ - public void start(BundleContext context) throws Exception { - this.context = context; + public void start(final BundleContext context) throws Exception { + Filter filter = null; final ServiceReference configAdminServiceRef = context .getServiceReference(ConfigurationAdmin.class.getName()); @@ -50,19 +55,56 @@ public class Activator implements BundleActivator { Configuration config = configAdmin.createFactoryConfiguration( DnsSdDiscoveryServiceTest.ECF_DISCOVERY_DNSSD, null); Dictionary properties = new Hashtable(); - properties.put("searchPath", new String[]{DnsSdDiscoveryServiceTest.DOMAIN}); - properties.put("resolver", "8.8.8.8"); + properties.put(IDnsSdDiscoveryConstants.CA_SEARCH_PATH, new String[]{DnsSdDiscoveryServiceTest.DOMAIN}); + properties.put(IDnsSdDiscoveryConstants.CA_RESOLVER, "8.8.8.8"); config.update(properties); filter = context.createFilter("(" + Constants.SERVICE_PID + "=" + config.getPid() + ")"); } + + // add the service listener + listener = new ServiceListener() { + public void serviceChanged(ServiceEvent event) { + switch (event.getType()) { + case ServiceEvent.REGISTERED: + ServiceReference serviceReference = event.getServiceReference(); + discoveryLocator = (IDiscoveryLocator) context.getService(serviceReference); + synchronized (lock) { + lock.notifyAll(); + } + } + } + }; + context.addServiceListener(listener, filter.toString()); + + // try to get the service initially + ServiceReference[] references = null; + try { + references = context.getServiceReferences(IDiscoveryLocator.class.getName(), filter.toString()); + } catch (InvalidSyntaxException e) { + // may never happen + e.printStackTrace(); + } + if(references == null) { + return; + } + for (int i = 0; i < references.length;) { + ServiceReference serviceReference = references[i]; + discoveryLocator = (IDiscoveryLocator) context.getService(serviceReference); + synchronized (lock) { + lock.notifyAll(); + } + } } /* (non-Javadoc) * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ public void stop(BundleContext context) throws Exception { - context = null; + if(listener != null) { + context.removeServiceListener(listener); + listener = null; + } } public static Activator getDefault() { @@ -70,19 +112,17 @@ public class Activator implements BundleActivator { } public IDiscoveryLocator getDiscoveryLocator() { - //TODO need to block until the service comes available - ServiceReference[] references = null; - try { - references = context.getServiceReferences(IDiscoveryLocator.class.getName(), filter.toString()); - } catch (InvalidSyntaxException e) { - // may never happen - e.printStackTrace(); - return null; - } - for (int i = 0; i < references.length;) { - ServiceReference serviceReference = references[i]; - return (IDiscoveryLocator) context.getService(serviceReference); + if (discoveryLocator == null) { + try { + synchronized (lock) { + lock.wait(); + } + } catch (InterruptedException e) { + // may never happen + e.printStackTrace(); + return null; + } } - return null; + return discoveryLocator; } } |