aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1422910 - /aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ServiceRecipe.java
Date Mon, 17 Dec 2012 12:48:00 GMT
Author: gnodet
Date: Mon Dec 17 12:47:59 2012
New Revision: 1422910

URL: http://svn.apache.org/viewvc?rev=1422910&view=rev
Log:
[ARIES-991] Concurrency issue which can cause a service to be kept registered

Modified:
    aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ServiceRecipe.java

Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ServiceRecipe.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ServiceRecipe.java?rev=1422910&r1=1422909&r2=1422910&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ServiceRecipe.java
(original)
+++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/ServiceRecipe.java
Mon Dec 17 12:47:59 2012
@@ -82,7 +82,6 @@ public class ServiceRecipe extends Abstr
     private final List<Recipe> explicitDependencies;
 
     private Map properties;
-    private final AtomicBoolean registered = new AtomicBoolean();
     private final AtomicReference<ServiceRegistration> registration = new AtomicReference<ServiceRegistration>();
     private Map registrationProperties;
     private List<ServiceListener> listeners;
@@ -149,7 +148,7 @@ public class ServiceRecipe extends Abstr
     }
 
     public boolean isRegistered() {
-        return registered.get();
+        return registration.get() != null;
     }
 
     public void register() {
@@ -157,47 +156,50 @@ public class ServiceRecipe extends Abstr
         if (state != Bundle.ACTIVE && state != Bundle.STARTING) {
             return;
         }
-        if (registered.compareAndSet(false, true)) {
-            createExplicitDependencies();
-            
-            Hashtable props = new Hashtable();
-            if (properties == null) {
-                properties = (Map) createRecipe(propertiesRecipe);
-            }
-            props.putAll(properties);
-            if (metadata.getRanking() == 0) {
-                props.remove(Constants.SERVICE_RANKING);
-            } else {
-                props.put(Constants.SERVICE_RANKING, metadata.getRanking());
-            }
-            String componentName = getComponentName();
-            if (componentName != null) {
-                props.put(BlueprintConstants.COMPONENT_NAME_PROPERTY, componentName);
-            } else {
-                props.remove(BlueprintConstants.COMPONENT_NAME_PROPERTY);
-            }
-            for (ServiceProcessor processor : blueprintContainer.getProcessors(ServiceProcessor.class))
{
-                processor.updateProperties(new PropertiesUpdater(), props);
-            }
+        createExplicitDependencies();
 
-            registrationProperties = props;
+        Hashtable props = new Hashtable();
+        if (properties == null) {
+            properties = (Map) createRecipe(propertiesRecipe);
+        }
+        props.putAll(properties);
+        if (metadata.getRanking() == 0) {
+            props.remove(Constants.SERVICE_RANKING);
+        } else {
+            props.put(Constants.SERVICE_RANKING, metadata.getRanking());
+        }
+        String componentName = getComponentName();
+        if (componentName != null) {
+            props.put(BlueprintConstants.COMPONENT_NAME_PROPERTY, componentName);
+        } else {
+            props.remove(BlueprintConstants.COMPONENT_NAME_PROPERTY);
+        }
+        for (ServiceProcessor processor : blueprintContainer.getProcessors(ServiceProcessor.class))
{
+            processor.updateProperties(new PropertiesUpdater(), props);
+        }
 
-            Set<String> classes = getClasses();
-            String[] classArray = classes.toArray(new String[classes.size()]);
+        registrationProperties = props;
+
+        Set<String> classes = getClasses();
+        String[] classArray = classes.toArray(new String[classes.size()]);
 
-            LOGGER.debug("Registering service {} with interfaces {} and properties {}",
-                         new Object[] { name, classes, props });
+        LOGGER.debug("Registering service {} with interfaces {} and properties {}",
+                     new Object[] { name, classes, props });
 
-            registration.set(blueprintContainer.registerService(classArray, new TriggerServiceFactory(this,
metadata), props));            
+        if (registration.get() == null) {
+            ServiceRegistration reg = blueprintContainer.registerService(classArray, new
TriggerServiceFactory(this, metadata), props);
+            if (!registration.compareAndSet(null, reg) && registration.get() != reg)
{
+                reg.unregister();
+            }
         }
     }
 
     public void unregister() {
-        if (registered.compareAndSet(true, false)) {
+        ServiceRegistration reg = registration.get();
+        if (reg != null) {
             LOGGER.debug("Unregistering service {}", name);
             // This method needs to allow reentrance, so if we need to make sure the registration
is
             // set to null before actually unregistering the service
-            ServiceRegistration reg = registration.get();
             if (listeners != null) {
                 LOGGER.debug("Calling listeners for service unregistration");
                 for (ServiceListener listener : listeners) {
@@ -307,7 +309,7 @@ public class ServiceRecipe extends Abstr
                     listeners = Collections.emptyList();
                 }
                 LOGGER.debug("Listeners created: {}", listeners);
-                if (registered.get()) {
+                if (registration.get() != null) {
                     LOGGER.debug("Calling listeners for initial service registration");
                     for (ServiceListener listener : listeners) {
                         listener.register(service, registrationProperties);



Mime
View raw message