From 6e0e76e459266a4210de21f6b002e806adbc7d05 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Thu, 9 Oct 2008 17:36:59 +0000 Subject: [PATCH] [250287] Support multiple registration of a service --- .../dd/dsf/service/AbstractDsfService.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/plugins/org.eclipse.dd.dsf/src/org/eclipse/dd/dsf/service/AbstractDsfService.java b/plugins/org.eclipse.dd.dsf/src/org/eclipse/dd/dsf/service/AbstractDsfService.java index bd879d4169c..8ce3c178878 100644 --- a/plugins/org.eclipse.dd.dsf/src/org/eclipse/dd/dsf/service/AbstractDsfService.java +++ b/plugins/org.eclipse.dd.dsf/src/org/eclipse/dd/dsf/service/AbstractDsfService.java @@ -13,6 +13,8 @@ package org.eclipse.dd.dsf.service; import java.util.Arrays; import java.util.Dictionary; import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; import org.eclipse.dd.dsf.concurrent.DsfExecutor; import org.eclipse.dd.dsf.concurrent.IDsfStatusConstants; @@ -96,6 +98,36 @@ abstract public class AbstractDsfService */ @SuppressWarnings("unchecked") protected void register(String[] classes, Dictionary properties) { + + /* + * If this service has already been registered, make sure we + * keep the names it has been registered with. However, we + * must trigger a new registration or else OSGI will keep the two + * registration separate. + */ + if (fRegistration != null) { + String[] previousClasses = (String[])fRegistration.getReference().getProperty(Constants.OBJECTCLASS); + + // Use a HashSet to avoid duplicates + Set newClasses = new HashSet(); + newClasses.addAll(Arrays.asList(previousClasses)); + newClasses.addAll(Arrays.asList(classes)); + classes = newClasses.toArray(new String[0]); + + /* + * Also keep all previous properties. + */ + if (fProperties != null) { + for (Enumeration e = fProperties.keys() ; e.hasMoreElements();) { + Object key = e.nextElement(); + Object value = fProperties.get(key); + properties.put(key, value); + } + } + + // Now, cancel the previous registration + unregister(); + } /* * Ensure that the list of classes contains the base DSF service * interface, as well as the actual class type of this object. @@ -179,7 +211,10 @@ abstract public class AbstractDsfService * */ protected void unregister() { - fRegistration.unregister(); + if (fRegistration != null) { + fRegistration.unregister(); + } + fRegistration = null; } /** Returns the registration object that was obtained when this service was registered */