Bug 285464. [DS] dependency injection does not work when starting more than one instance of a service
diff --git a/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/SCRmessages.properties b/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/SCRmessages.properties
index 9399c8b..16ddd7f 100644
--- a/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/SCRmessages.properties
+++ b/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/SCRmessages.properties
@@ -166,7 +166,6 @@
SERVICE_EVENT_TYPE=Service event type: {0}
SERVICE_NO_LONGER_USED=ServiceReg.ungetService(): service ''{0}'' no longer used, disposing object = {1}
SERVICE_REFERENCE_ALREADY_BOUND=[SCR] ComponentReference.bind(): service reference {0} is already bound to instance {1}
-SERVICE_REFERENCE_BOUND=[SCR] ComponentReference.bind(): service reference {0} is already bound to another instance: {1}
SERVICE_UNREGISTERED_BECAUSE_COMP_DISPOSED=The service of component {0} was unregistered because the component is already disposed\!
SERVICE_USAGE_COUNT=service ''{0}'' is used {1} time(s)
SPECIFIED_ACTIVATE_METHOD_NOT_FOUND=[SCR] Cannot activate instance {0} of component {1}! The specified activate method was not found!
diff --git a/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/model/ComponentReference.java b/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/model/ComponentReference.java
index c6144d1..09a0c41 100644
--- a/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/model/ComponentReference.java
+++ b/bundles/org.eclipse.equinox.ds/src/org/eclipse/equinox/internal/ds/model/ComponentReference.java
@@ -294,34 +294,19 @@
final void bind(Reference reference, ComponentInstance instance, ServiceReference serviceReference) throws Exception {
if (bind != null) {
// DON'T rebind the same object again
- boolean isComponentFactory = component.factory != null;
synchronized (serviceReferences) {
- if (isComponentFactory) {
- Vector instances = (Vector) serviceReferences.get(serviceReference);
- if (instances == null) {
- instances = new Vector(2);
- instances.addElement(instance);
- serviceReferences.put(serviceReference, instances);
- } else if (instances.contains(instance)) {
- if (reference.isUnary()) {
- logWarning(NLS.bind(Messages.SERVICE_REFERENCE_ALREADY_BOUND, serviceReference, instance), null, reference);
- }
- return;
- } else {
- instances.addElement(instance);
+ Vector instances = (Vector) serviceReferences.get(serviceReference);
+ if (instances == null) {
+ instances = new Vector(1);
+ instances.addElement(instance);
+ serviceReferences.put(serviceReference, instances);
+ } else if (instances.contains(instance)) {
+ if (reference.isUnary()) {
+ logWarning(NLS.bind(Messages.SERVICE_REFERENCE_ALREADY_BOUND, serviceReference, instance), null, reference);
}
+ return;
} else {
- Object compInstance = serviceReferences.get(serviceReference);
- if (compInstance == instance) {
- if (reference.isUnary()) {
- logWarning(NLS.bind(Messages.SERVICE_REFERENCE_ALREADY_BOUND, serviceReference, instance), null, reference);
- }
- return;
- } else if (compInstance != null) {
- logWarning(NLS.bind(Messages.SERVICE_REFERENCE_BOUND, serviceReference, compInstance), null, reference);
- return;
- }
- serviceReferences.put(serviceReference, instance);
+ instances.addElement(instance);
}
}
// retrieve the method from cache
@@ -391,14 +376,9 @@
}
private void removeServiceReference(ServiceReference serviceReference, ComponentInstance instance) {
- boolean isComponentFactory = component.factory != null;
- if (isComponentFactory) {
- Vector instances = (Vector) serviceReferences.get(serviceReference);
- instances.removeElement(instance);
- if (instances.isEmpty()) {
- serviceReferences.remove(serviceReference);
- }
- } else {
+ Vector instances = (Vector) serviceReferences.get(serviceReference);
+ instances.removeElement(instance);
+ if (instances.isEmpty()) {
serviceReferences.remove(serviceReference);
}
}
@@ -406,48 +386,27 @@
public final void unbind(Reference reference, ComponentInstance instance, ServiceReference serviceReference) {
// don't unbind an object that wasn't bound
boolean referenceExists = true;
- boolean componentFactory = (component.factory != null);
synchronized (serviceReferences) {
- if (componentFactory) {
- Vector instances = (Vector) serviceReferences.get(serviceReference);
- if (instances == null) {
- referenceExists = false;
- } else {
- if (!instances.contains(instance)) {
- logWarning(NLS.bind(Messages.INSTANCE_NOT_BOUND, instance), null, reference);
- return;
- }
- }
+ Vector instances = (Vector) serviceReferences.get(serviceReference);
+ if (instances == null) {
+ referenceExists = false;
} else {
- Object compInstance = serviceReferences.get(serviceReference);
- if (compInstance == null) {
- referenceExists = false;
- } else {
- if (compInstance != instance) {
- logWarning(NLS.bind(Messages.INSTANCE_NOT_BOUND, instance), null, reference);
- return;
- }
+ if (!instances.contains(instance)) {
+ logWarning(NLS.bind(Messages.INSTANCE_NOT_BOUND, instance), null, reference);
+ return;
}
}
if (referenceExists) {
- if (componentFactory) {
- Vector instances = (Vector) serviceReferencesToUnbind.get(serviceReference);
- if (instances != null && instances.contains(instance)) {
- //the service reference is already in process of unbinding
- return;
- }
- if (instances == null) {
- instances = new Vector(2);
- serviceReferencesToUnbind.put(serviceReference, instances);
- }
- instances.addElement(instance);
- } else {
- if (serviceReferencesToUnbind.get(serviceReference) == instance) {
- //the service reference is already in process of unbinding
- return;
- }
- serviceReferencesToUnbind.put(serviceReference, instance);
+ Vector instancesToUnbind = (Vector) serviceReferencesToUnbind.get(serviceReference);
+ if (instancesToUnbind != null && instancesToUnbind.contains(instance)) {
+ //the service reference is already in process of unbinding
+ return;
}
+ if (instancesToUnbind == null) {
+ instancesToUnbind = new Vector(1);
+ serviceReferencesToUnbind.put(serviceReference, instancesToUnbind);
+ }
+ instancesToUnbind.addElement(instance);
}
}
if (!referenceExists) {
@@ -506,20 +465,15 @@
}
} finally {
synchronized (serviceReferences) {
- if (componentFactory) {
- Vector instances = (Vector) serviceReferences.get(serviceReference);
- instances.removeElement(instance);
- if (instances.isEmpty()) {
- serviceReferences.remove(serviceReference);
- }
-
- instances = (Vector) serviceReferencesToUnbind.get(serviceReference);
- instances.removeElement(instance);
- if (instances.isEmpty()) {
- serviceReferencesToUnbind.remove(serviceReference);
- }
- } else {
+ Vector instances = (Vector) serviceReferences.get(serviceReference);
+ instances.removeElement(instance);
+ if (instances.isEmpty()) {
serviceReferences.remove(serviceReference);
+ }
+
+ instances = (Vector) serviceReferencesToUnbind.get(serviceReference);
+ instances.removeElement(instance);
+ if (instances.isEmpty()) {
serviceReferencesToUnbind.remove(serviceReference);
}
}