diff options
author | Markus Alexander Kuppe | 2010-10-19 07:18:27 +0000 |
---|---|---|
committer | Markus Alexander Kuppe | 2010-10-19 07:19:22 +0000 |
commit | 93fcd868b6991f47e9e580941873afaaf2fc5912 (patch) | |
tree | 97363b63bcfca2c2074cb67fe764fabc122168a8 /framework/bundles/org.eclipse.ecf.discovery | |
parent | d9924bb877286d2eb369b95b8e5ea4e8e87dd116 (diff) | |
parent | 18ca08e7f88d9ae670a487318839fcf7deb32c7d (diff) | |
download | org.eclipse.ecf-93fcd868b6991f47e9e580941873afaaf2fc5912.tar.gz org.eclipse.ecf-93fcd868b6991f47e9e580941873afaaf2fc5912.tar.xz org.eclipse.ecf-93fcd868b6991f47e9e580941873afaaf2fc5912.zip |
RESOLVED - bug 328011: Use OSGi whiteboard pattern for IServiceListener|IServiceTypeListener registration
https://bugs.eclipse.org/bugs/show_bug.cgi?id=328011
Diffstat (limited to 'framework/bundles/org.eclipse.ecf.discovery')
6 files changed, 171 insertions, 55 deletions
diff --git a/framework/bundles/org.eclipse.ecf.discovery/build.properties b/framework/bundles/org.eclipse.ecf.discovery/build.properties index 4bd958e44..d930590b8 100644 --- a/framework/bundles/org.eclipse.ecf.discovery/build.properties +++ b/framework/bundles/org.eclipse.ecf.discovery/build.properties @@ -2,7 +2,8 @@ bin.includes = .,\ META-INF/,\ about.html,\ plugin.properties,\ - .options + .options,\ + plugin.xml src.includes = about.html jars.compile.order = . source.. = src/ diff --git a/framework/bundles/org.eclipse.ecf.discovery/plugin.xml b/framework/bundles/org.eclipse.ecf.discovery/plugin.xml new file mode 100644 index 000000000..a482710d8 --- /dev/null +++ b/framework/bundles/org.eclipse.ecf.discovery/plugin.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<?eclipse version="3.4"?> +<plugin> + <extension + point="org.eclipse.ecf.identity.namespace"> + <namespace + class="org.eclipse.ecf.internal.discovery.DiscoveryNamespace" + name="ecf.namespace.discovery"> + </namespace> + </extension> +</plugin> diff --git a/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/discovery/AbstractDiscoveryContainerAdapter.java b/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/discovery/AbstractDiscoveryContainerAdapter.java index 96e664f34..3520055ac 100644 --- a/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/discovery/AbstractDiscoveryContainerAdapter.java +++ b/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/discovery/AbstractDiscoveryContainerAdapter.java @@ -261,11 +261,11 @@ public abstract class AbstractDiscoveryContainerAdapter extends Collection listeners = new HashSet(); synchronized (serviceListeners) { for (Iterator itr = serviceListeners.keySet().iterator(); itr.hasNext();) { - IServiceTypeID typeID = (IServiceTypeID) itr.next(); + final IServiceTypeID typeID = (IServiceTypeID) itr.next(); int compare = discoveryServiceListenerComparator.compare(aServiceType, typeID); if(compare == 0) { Collection collection = (Collection) serviceListeners - .get(aServiceType); + .get(typeID); if (collection != null) { listeners.addAll(collection); } diff --git a/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/DiscoveryNamespace.java b/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/DiscoveryNamespace.java new file mode 100644 index 000000000..bf8f0d3b0 --- /dev/null +++ b/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/DiscoveryNamespace.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * 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 <at> lemmster <dot> de) - initial API and implementation + ******************************************************************************/ +package org.eclipse.ecf.internal.discovery; + +import org.eclipse.ecf.core.identity.*; +import org.eclipse.ecf.discovery.identity.IServiceTypeID; + +public class DiscoveryNamespace extends Namespace { + + private static final long serialVersionUID = 6474091408790223505L; + public static final String NAME = "ecf.namespace.discovery"; //$NON-NLS-1$ + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ecf.core.identity.Namespace#createInstance(java.lang.Object + * []) + */ + public ID createInstance(Object[] parameters) throws IDCreateException { + if (parameters != null && parameters.length == 1 + && parameters[0] instanceof IServiceTypeID) { + return (ID) parameters[0]; + } + throw new IDCreateException("Parameters must be of type IServiceTypeID"); //$NON-NLS-1$ + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.core.identity.Namespace#getScheme() + */ + public String getScheme() { + return "discovery"; //$NON-NLS-1$ + } +} diff --git a/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/DiscoveryServiceListener.java b/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/DiscoveryServiceListener.java index 617f8ea68..530117984 100644 --- a/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/DiscoveryServiceListener.java +++ b/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/DiscoveryServiceListener.java @@ -1,31 +1,43 @@ -/** - * - */ +/******************************************************************************* + * 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 <at> lemmster <dot> de) - initial API and implementation + ******************************************************************************/ package org.eclipse.ecf.internal.discovery; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; -import org.eclipse.ecf.core.identity.IDFactory; -import org.eclipse.ecf.core.identity.Namespace; +import org.eclipse.ecf.core.identity.*; +import org.eclipse.ecf.core.util.StringUtils; import org.eclipse.ecf.discovery.*; -import org.eclipse.ecf.discovery.identity.IServiceTypeID; +import org.eclipse.ecf.discovery.identity.*; import org.osgi.framework.*; public class DiscoveryServiceListener implements ServiceListener { - private AbstractDiscoveryContainerAdapter discoveryContainer; - private BundleContext context; - private Class listenerClass; + private final AbstractDiscoveryContainerAdapter discoveryContainer; + private final Class listenerClass; + private final BundleContext context; + private final IServiceIDFactory idFactory; + private final Namespace discoveryNamespace; public DiscoveryServiceListener( AbstractDiscoveryContainerAdapter anAbstractDiscoveryContainerAdapter, Class clazz) { discoveryContainer = anAbstractDiscoveryContainerAdapter; listenerClass = clazz; + discoveryNamespace = IDFactory.getDefault().getNamespaceByName( + DiscoveryNamespace.NAME); + idFactory = ServiceIDFactory.getDefault(); context = DiscoveryPlugin.getDefault().getBundleContext(); try { // get existing listener - ServiceReference[] references = context.getServiceReferences( + final ServiceReference[] references = context.getServiceReferences( listenerClass.getName(), null); addServiceListener(references); @@ -43,16 +55,22 @@ public class DiscoveryServiceListener implements ServiceListener { } private void addServiceListener(ServiceReference[] references) { + if (references == null) { + return; + } for (int i = 0; i < references.length; i++) { - ServiceReference serviceReference = references[i]; + final ServiceReference serviceReference = references[i]; if (listenerClass.getName() .equals(IServiceListener.class.getName())) { - IServiceTypeID aType = getIServiceTypeID(serviceReference); - IServiceListener aListener = (IServiceListener) context + final IServiceTypeID aType = getIServiceTypeID(serviceReference); + if (aType == null) { + continue; + } + final IServiceListener aListener = (IServiceListener) context .getService(serviceReference); discoveryContainer.addServiceListener(aType, aListener); } else { - IServiceTypeListener aListener = (IServiceTypeListener) context + final IServiceTypeListener aListener = (IServiceTypeListener) context .getService(serviceReference); discoveryContainer.addServiceTypeListener(aListener); } @@ -64,16 +82,22 @@ public class DiscoveryServiceListener implements ServiceListener { } private void removeServiceListener(ServiceReference[] references) { + if (references == null) { + return; + } for (int i = 0; i < references.length; i++) { - ServiceReference serviceReference = references[i]; + final ServiceReference serviceReference = references[i]; if (listenerClass.getName() .equals(IServiceListener.class.getName())) { - IServiceTypeID aType = getIServiceTypeID(serviceReference); - IServiceListener aListener = (IServiceListener) context + final IServiceTypeID aType = getIServiceTypeID(serviceReference); + if (aType == null) { + continue; + } + final IServiceListener aListener = (IServiceListener) context .getService(serviceReference); discoveryContainer.removeServiceListener(aType, aListener); } else { - IServiceTypeListener aListener = (IServiceTypeListener) context + final IServiceTypeListener aListener = (IServiceTypeListener) context .getService(serviceReference); discoveryContainer.removeServiceTypeListener(aListener); } @@ -85,19 +109,37 @@ public class DiscoveryServiceListener implements ServiceListener { } private IServiceTypeID getIServiceTypeID(ServiceReference serviceReference) { - Namespace namespace = discoveryContainer.getServicesNamespace(); - String services = (String) serviceReference - .getProperty("org.eclipse.ecf.discovery.services"); - String scopes = (String) serviceReference - .getProperty("org.eclipse.ecf.discovery.scopes"); - String protocols = (String) serviceReference - .getProperty("org.eclipse.ecf.discovery.protocols"); String namingAuthority = (String) serviceReference - .getProperty("org.eclipse.ecf.discovery.namingauthority"); - return (IServiceTypeID) IDFactory.getDefault().createID( - namespace, - "_" + services + "" + protocols + "" + scopes + "" - + namingAuthority); + .getProperty("org.eclipse.ecf.discovery.namingauthority"); //$NON-NLS-1$ + if (namingAuthority == null) { + namingAuthority = "*"; //$NON-NLS-1$ + } + try { + final IServiceTypeID createServiceTypeID = idFactory + .createServiceTypeID( + discoveryNamespace, + convert(serviceReference, + "org.eclipse.ecf.discovery.services"), //$NON-NLS-1$ + convert(serviceReference, + "org.eclipse.ecf.discovery.scopes"), //$NON-NLS-1$ + convert(serviceReference, + "org.eclipse.ecf.discovery.protocols"), //$NON-NLS-1$ + namingAuthority); + return createServiceTypeID; + } catch (final IDCreateException e) { + return null; + } + } + + private String[] convert(ServiceReference serviceReference, String key) { + final Object value = serviceReference.getProperty(key); + // default to wildcard for non-set values + if (value == null) { + return new String[] { "*" }; //$NON-NLS-1$ + } else if (value instanceof String[]) { + return (String[]) value; + } + return StringUtils.split((String) value, "._"); //$NON-NLS-1$ } /* @@ -121,7 +163,7 @@ public class DiscoveryServiceListener implements ServiceListener { } private String getFilter() { - return "(" + Constants.OBJECTCLASS + "=" + listenerClass.getName() - + ")"; + return "(" + Constants.OBJECTCLASS + "=" + listenerClass.getName() //$NON-NLS-1$ //$NON-NLS-2$ + + ")"; //$NON-NLS-1$ } } diff --git a/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/ServiceTypeComparator.java b/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/ServiceTypeComparator.java index e357a8f99..8201270fe 100644 --- a/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/ServiceTypeComparator.java +++ b/framework/bundles/org.eclipse.ecf.discovery/src/org/eclipse/ecf/internal/discovery/ServiceTypeComparator.java @@ -1,7 +1,18 @@ +/******************************************************************************* + * Copyright (c) 2007 Versant Corp. + * 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 Kuppe (mkuppe <at> versant <dot> com) - initial API and implementation + ******************************************************************************/ package org.eclipse.ecf.internal.discovery; import java.util.Arrays; import java.util.Comparator; +import org.eclipse.core.runtime.Assert; import org.eclipse.ecf.discovery.identity.IServiceTypeID; public class ServiceTypeComparator implements Comparator { @@ -11,42 +22,49 @@ public class ServiceTypeComparator implements Comparator { * * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ - public int compare(Object object1, Object object2) { + public int compare(final Object object1, final Object object2) { + Assert.isNotNull(object1); + Assert.isNotNull(object2); + if (object1 == object2) { + return 0; + } + if (object1.equals(object2)) { + return 0; + } if (object1 instanceof IServiceTypeID && object2 instanceof IServiceTypeID) { - IServiceTypeID type1 = (IServiceTypeID) object1; - IServiceTypeID type2 = (IServiceTypeID) object2; + final IServiceTypeID type1 = (IServiceTypeID) object1; + final IServiceTypeID type2 = (IServiceTypeID) object2; - String name1 = type1.getNamingAuthority(); - String name2 = type2.getNamingAuthority(); - if (!name1.equals("*") || !name2.equals("*") - || !name1.equals(name2)) { + final String name1 = type1.getNamingAuthority(); + final String name2 = type2.getNamingAuthority(); + if (!name1.equals("*") && !name2.equals("*") //$NON-NLS-1$ //$NON-NLS-2$ + && !name1.equals(name2)) { return -1; } - String[] services1 = type1.getServices(); - String[] services2 = type2.getServices(); - if (!services1[0].equals("*") || !services2[0].equals("*") - || !Arrays.equals(services1, services2)) { + final String[] services1 = type1.getServices(); + final String[] services2 = type2.getServices(); + if (!services1[0].equals("*") && !services2[0].equals("*") //$NON-NLS-1$ //$NON-NLS-2$ + && !Arrays.equals(services1, services2)) { return -1; } - String[] protocols1 = type1.getProtocols(); - String[] protocols2 = type2.getProtocols(); - if (!protocols1[0].equals("*") || !protocols2[0].equals("*") - || !Arrays.equals(protocols1, protocols2)) { + final String[] protocols1 = type1.getProtocols(); + final String[] protocols2 = type2.getProtocols(); + if (!protocols1[0].equals("*") && !protocols2[0].equals("*") //$NON-NLS-1$ //$NON-NLS-2$ + && !Arrays.equals(protocols1, protocols2)) { return -1; } - String[] scopes1 = type1.getScopes(); - String[] scopes2 = type2.getScopes(); - if (!scopes1[0].equals("*") || !scopes2[0].equals("*") - || !Arrays.equals(scopes1, scopes2)) { + final String[] scopes1 = type1.getScopes(); + final String[] scopes2 = type2.getScopes(); + if (!scopes1[0].equals("*") && !scopes2[0].equals("*") //$NON-NLS-1$ //$NON-NLS-2$ + && !Arrays.equals(scopes1, scopes2)) { return -1; } return 0; } return -1; } - } |