If you use a service implementation that does not implement all interfaces it registers, a deadlock would occur if you would access its ServiceRegistration. Now it will throw an IllegalStateException instead.

git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@424959 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceImpl.java b/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceImpl.java
index 0ba7404..9fcd567 100644
--- a/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceImpl.java
+++ b/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceImpl.java
@@ -380,13 +380,20 @@
             configureImplementation(ServiceRegistration.class, wrapper);
             // service name can either be a string or an array of strings
             ServiceRegistration registration;
-            if (m_serviceName instanceof String) {
-                registration = m_context.registerService((String) m_serviceName, m_serviceInstance, m_serviceProperties);
+            try {
+                if (m_serviceName instanceof String) {
+                    registration = m_context.registerService((String) m_serviceName, m_serviceInstance, m_serviceProperties);
+                }
+                else {
+                    registration = m_context.registerService((String[]) m_serviceName, m_serviceInstance, m_serviceProperties);
+                }
+                wrapper.setServiceRegistration(registration);
             }
-            else {
-                registration = m_context.registerService((String[]) m_serviceName, m_serviceInstance, m_serviceProperties);
+            catch (IllegalArgumentException iae) {
+                // set the registration to an illegal state object, which will make all invocations on this
+                // wrapper fail with an ISE (which also occurs when the SR becomes invalid)
+                wrapper.setServiceRegistration(ServiceRegistrationImpl.ILLEGAL_STATE);
             }
-            wrapper.setServiceRegistration(registration);
         }
     }
     
diff --git a/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceRegistrationImpl.java b/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceRegistrationImpl.java
index e469b19..dc15d5f 100644
--- a/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceRegistrationImpl.java
+++ b/org.apache.felix.dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceRegistrationImpl.java
@@ -28,6 +28,7 @@
  * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
  */
 public class ServiceRegistrationImpl implements ServiceRegistration {
+    public static final ServiceRegistrationImpl ILLEGAL_STATE = new ServiceRegistrationImpl();
     private ServiceRegistration m_registration;
 
     public ServiceRegistrationImpl() {
@@ -74,6 +75,9 @@
                 // service registration ready; if not we wait again
             }
         }
+        if (ILLEGAL_STATE.equals(m_registration)) {
+            throw new IllegalStateException("Service is not registered.");
+        }
     }
 
     void setServiceRegistration(ServiceRegistration registration) {