FELIX-599 Mutliple changes to service registration locking:
- only log once (at info level) when waiting for the lock release
- wait at most 10 * 1 seconds
- unlock only allowed for the lock owner
- prevent calling the unlock method if lock acquisition failed

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@663930 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/AbstractComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/AbstractComponentManager.java
index 2526fdd..fa9e043 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/AbstractComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/AbstractComponentManager.java
@@ -680,10 +680,11 @@
 
     protected void unregisterComponentService()
     {
+        // outside of try-finally to not trigger inadvertend unlock
+        lockServiceRegistration();
+
         try
         {
-            lockServiceRegistration();
-
             if ( m_serviceRegistration != null )
             {
                 m_serviceRegistration.unregister();
@@ -736,28 +737,33 @@
     {
         synchronized ( serviceRegistrationLock )
         {
-            int waitGuard = 10;
-            while ( serviceRegistrationLockOwner != null && waitGuard > 0 )
+            if ( serviceRegistrationLockOwner != null )
             {
-                log( LogService.LOG_DEBUG, "Service Registration already locked by " + serviceRegistrationLockOwner
-                    + ", waiting for release", m_componentMetadata, null );
+                log( LogService.LOG_INFO, "Waiting for Service Registration owned " + serviceRegistrationLockOwner,
+                    m_componentMetadata, null );
 
-                // wait at most 10 seconds
-                try
+                int waitGuard = 10;
+                while ( serviceRegistrationLockOwner != null && waitGuard > 0 )
                 {
-                    serviceRegistrationLock.wait( 10L * 1000L );
+                    // wait at most one second
+                    try
+                    {
+                        serviceRegistrationLock.wait( 1000L );
+                    }
+                    catch ( InterruptedException ie )
+                    {
+                        // don't care
+                    }
+                    waitGuard--;
                 }
-                catch ( InterruptedException ie )
-                {
-                    // don't care
-                }
-                waitGuard--;
-            }
 
-            // timedout waiting for the service registration lock
-            if ( waitGuard <= 0 )
-            {
-                throw new IllegalStateException( "Cannot get the service registration lock !!" );
+                // timedout waiting for the service registration lock
+                if ( waitGuard <= 0 )
+                {
+                    throw new IllegalStateException( "Cannot get Service Registration, owned by "
+                        + serviceRegistrationLockOwner );
+                }
+
             }
 
             serviceRegistrationLockOwner = Thread.currentThread();
@@ -774,13 +780,22 @@
     {
         synchronized ( serviceRegistrationLock )
         {
-            log( LogService.LOG_DEBUG, "Service Registration released by " + serviceRegistrationLockOwner,
-                m_componentMetadata, null );
+            Thread current = Thread.currentThread();
+            if ( serviceRegistrationLockOwner == current )
+            {
+                log( LogService.LOG_DEBUG, "Service Registration released by " + serviceRegistrationLockOwner,
+                    m_componentMetadata, null );
 
-            serviceRegistrationLockOwner = null;
+                serviceRegistrationLockOwner = null;
 
-            // notify threads waiting to lock service registration
-            serviceRegistrationLock.notifyAll();
+                // notify threads waiting to lock service registration
+                serviceRegistrationLock.notifyAll();
+            }
+            else
+            {
+                log( LogService.LOG_DEBUG, "Not releasing Service Registration by " + current + ", owner is "
+                    + serviceRegistrationLockOwner, m_componentMetadata, null );
+            }
         }
     }
 
@@ -885,10 +900,11 @@
 
     ServiceReference getServiceReference()
     {
+        // outside of try-finally to not trigger inadvertend unlock
+        lockServiceRegistration();
+
         try
         {
-            lockServiceRegistration();
-            
             return ( m_serviceRegistration != null ) ? m_serviceRegistration.getReference() : null;
         }
         finally