From e1fcd4c9cd712ddeb5292697e895cc1da3f611c4 Mon Sep 17 00:00:00 2001 From: Markus Alexander Kuppe Date: Fri, 11 Jun 2010 14:12:44 +0200 Subject: WIP - bug 314998: [Discovery][DNS-SD] Make provider configurable with Configuration Admin https://bugs.eclipse.org/bugs/show_bug.cgi?id=314998 --- .../META-INF/MANIFEST.MF | 4 + .../ecf/tests/provider/dnssd/Activator.java | 88 ++++++++++++++++++++++ .../DnsSdDiscoveryServiceTestWithParamsSet.java | 7 +- 3 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/Activator.java (limited to 'tests/bundles/org.eclipse.ecf.tests.provider.dnssd') diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/META-INF/MANIFEST.MF b/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/META-INF/MANIFEST.MF index 1aabfc917..7e82b5d28 100644 --- a/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/META-INF/MANIFEST.MF +++ b/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/META-INF/MANIFEST.MF @@ -11,3 +11,7 @@ Require-Bundle: org.eclipse.equinox.common;bundle-version="3.4.0", org.eclipse.ecf.provider.dnssd;bundle-version="1.0.0", org.eclipse.ecf.tests.discovery;bundle-version="2.0.0", org.junit;bundle-version="3.8.2" +Bundle-ActivationPolicy: lazy +Import-Package: org.osgi.framework;version="1.3.0", + org.osgi.service.cm;version="1.2.0" +Bundle-Activator: org.eclipse.ecf.tests.provider.dnssd.Activator 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 new file mode 100644 index 000000000..c90eac5fb --- /dev/null +++ b/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/Activator.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (c) 2010 Markus Alexander Kuppe. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Markus Alexander Kuppe (ecf-dev_eclipse.org lemmster de) - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.tests.provider.dnssd; + +import java.util.Dictionary; +import java.util.Hashtable; + +import org.eclipse.ecf.discovery.IDiscoveryLocator; +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.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; + + public Activator() { + instance = this; + } + + /* (non-Javadoc) + * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + this.context = context; + + final ServiceReference configAdminServiceRef = context + .getServiceReference(ConfigurationAdmin.class.getName()); + + if (configAdminServiceRef != null) { + ConfigurationAdmin configAdmin = (ConfigurationAdmin) context + .getService(configAdminServiceRef); + + 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"); + config.update(properties); + + filter = context.createFilter("(" + Constants.SERVICE_PID + "=" + config.getPid() + ")"); + } + } + + /* (non-Javadoc) + * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + context = null; + } + + public static Activator getDefault() { + return instance; + } + + 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); + } + return null; + } +} diff --git a/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/DnsSdDiscoveryServiceTestWithParamsSet.java b/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/DnsSdDiscoveryServiceTestWithParamsSet.java index 985e19722..66a0905c9 100644 --- a/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/DnsSdDiscoveryServiceTestWithParamsSet.java +++ b/tests/bundles/org.eclipse.ecf.tests.provider.dnssd/src/org/eclipse/ecf/tests/provider/dnssd/DnsSdDiscoveryServiceTestWithParamsSet.java @@ -11,7 +11,6 @@ package org.eclipse.ecf.tests.provider.dnssd; import org.eclipse.ecf.discovery.IDiscoveryLocator; -import org.eclipse.ecf.provider.dnssd.DnsSdDisocoveryLocator; public class DnsSdDiscoveryServiceTestWithParamsSet extends DnsSdDiscoveryServiceTest { @@ -26,10 +25,6 @@ public class DnsSdDiscoveryServiceTestWithParamsSet extends * @see org.eclipse.ecf.tests.discovery.DiscoveryServiceTest#getDiscoveryLocator() */ protected IDiscoveryLocator getDiscoveryLocator() { - DnsSdDisocoveryLocator locator = (DnsSdDisocoveryLocator) super.getDiscoveryLocator(); - locator.setSearchPath(new String[]{DOMAIN}); - locator.setResolver(RESOLVER); - return locator; + return Activator.getDefault().getDiscoveryLocator(); } - } -- cgit v1.2.3