FELIX-4031 complete timeout wait on InterruptedException

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1471172 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ComponentActorThread.java b/scr/src/main/java/org/apache/felix/scr/impl/ComponentActorThread.java
index 1a1638b..8083ee7 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ComponentActorThread.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ComponentActorThread.java
@@ -77,6 +77,7 @@
                     }
                     catch ( InterruptedException ie )
                     {
+                        Thread.currentThread().interrupt();
                         // don't care
                     }
                 }
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
index 41069a8..f428c90 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
@@ -186,10 +186,21 @@
         }
         catch ( InterruptedException e )
         {
+            try
+            {
+                if (!m_stateLock.tryLock( getLockTimeout(), TimeUnit.MILLISECONDS ) )
+                {
+                    dumpThreads();
+                    throw new IllegalStateException( "Could not obtain lock" );
+                }
+            }
+            catch ( InterruptedException e1 )
+            {
+                Thread.currentThread().interrupt();
+                //TODO is there a better exception to throw?
+                throw new IllegalStateException( "Interrupted twice: Could not obtain lock" );
+            }
             Thread.currentThread().interrupt();
-            dumpThreads();
-            //TODO this is so wrong
-            throw new IllegalStateException( "Could not obtain lock (Reason: " + e + ")" );
         }
     }
 
@@ -268,20 +279,26 @@
                         new Object[] {trackingCount, ceiling, missing}, null );
                 try
                 {
-                    if ( !missingCondition.await( getLockTimeout(), TimeUnit.MILLISECONDS ))
+                    if ( !doMissingWait())
                     {
-                        log( LogService.LOG_ERROR, "waitForTracked timed out: {0} ceiling: {1} missing: {2},  Expect further errors",
-                                new Object[] {trackingCount, ceiling, missing}, null );
-                        dumpThreads();
-                        missing.clear();
-                        return;
-                        
+                        return;                        
                     }
                 }
                 catch ( InterruptedException e )
                 {
+                    try
+                    {
+                        if ( !doMissingWait())
+                        {
+                            return;                        
+                        }
+                    }
+                    catch ( InterruptedException e1 )
+                    {
+                        log( LogService.LOG_ERROR, "waitForTracked interrupted twice: {0} ceiling: {1} missing: {2},  Expect further errors",
+                                new Object[] {trackingCount, ceiling, missing}, e1 );
+                    }
                     Thread.currentThread().interrupt();
-                    dumpThreads();
                 }
             }
         }
@@ -290,6 +307,19 @@
             missingLock.unlock();
         }
     }
+    
+    private boolean doMissingWait() throws InterruptedException
+    {
+        if ( !missingCondition.await( getLockTimeout(), TimeUnit.MILLISECONDS ))
+        {
+            log( LogService.LOG_ERROR, "waitForTracked timed out: {0} ceiling: {1} missing: {2},  Expect further errors",
+                    new Object[] {trackingCount, ceiling, missing}, null );
+            dumpThreads();
+            missing.clear();
+            return false;
+        }
+        return true;
+    }
 
 //---------- Component ID management
 
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java
index 9475e70..2203fed 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java
@@ -31,6 +31,7 @@
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentInstance;
+import org.osgi.service.log.LogService;
 
 
 /**
@@ -187,8 +188,18 @@
         }
         catch ( InterruptedException e )
         {
+            try
+            {
+                if (accessibleLatch.await( m_componentManager.getLockTimeout(), TimeUnit.MILLISECONDS ) && m_implementationAccessible)
+                {
+                    return m_implementationObject;
+                }
+            }
+            catch ( InterruptedException e1 )
+            {
+                m_componentManager.log( LogService.LOG_INFO, "Interrupted twice waiting for implementation object to become accessible", e1 );
+            }
             Thread.currentThread().interrupt();
-            getComponentManager().dumpThreads();
             return null;
         }
         return null;
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
index 60492d5..d6b319a 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
@@ -1645,9 +1645,22 @@
             }
             catch ( InterruptedException e )
             {
+                try
+                {
+                    if (!info.getOpenLatch().await( getLockTimeout(), TimeUnit.MILLISECONDS ))
+                    {
+                        m_componentManager.log( LogService.LOG_ERROR,
+                                "DependencyManager : invokeUpdatedMethod : timeout on open latch {0}",  new Object[] {getName()}, null );
+                        m_componentManager.dumpThreads();
+                    }
+                }
+                catch ( InterruptedException e1 )
+                {
+                    m_componentManager.log( LogService.LOG_ERROR,
+                            "DependencyManager : invokeUpdatedMethod : Interrupted twice on open latch {0}",  new Object[] {getName()}, null );
+                    Thread.currentThread().interrupt();
+                }
                 Thread.currentThread().interrupt();
-                m_componentManager.dumpThreads();
-                //ignore
             }
             if ( !getServiceObject( m_bindMethods.getUpdated(), refPair ))
             {
@@ -1708,9 +1721,21 @@
                     }
                     catch ( InterruptedException e )
                     {
+                        try
+                        {
+                            if (!info.getCloseLatch().await( getLockTimeout(), TimeUnit.MILLISECONDS ) )
+                            {
+                                m_componentManager.log( LogService.LOG_ERROR,
+                                        "DependencyManager : invokeUnbindMethod : timeout on close latch {0}",  new Object[] {getName()}, null );
+                                m_componentManager.dumpThreads();
+                            }
+                        }
+                        catch ( InterruptedException e1 )
+                        {
+                            m_componentManager.log( LogService.LOG_ERROR,
+                                    "DependencyManager : invokeUnbindMethod : Interrupted twice on close latch {0}",  new Object[] {getName()}, null );
+                        }
                         Thread.currentThread().interrupt();
-                        m_componentManager.dumpThreads();
-                        //ignore
                     }
                 }
                 //ignore events after close started or we will have duplicate unbinds.
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/RegistrationManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/RegistrationManager.java
index be5c514..adabae5 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/RegistrationManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/RegistrationManager.java
@@ -178,10 +178,21 @@
                 }
                 catch ( InterruptedException e )
                 {
+                    try
+                    {
+                        if ( !rsw.getLatch().await( getTimeout(), TimeUnit.MILLISECONDS ))
+                        {
+                            log( LogService.LOG_ERROR, "Timeout waiting for reg change to complete {0}", new Object[]
+                                    {rsw.getRegState()}, null);
+                            reportTimeout();
+                        }
+                    }
+                    catch ( InterruptedException e1 )
+                    {
+                        log( LogService.LOG_ERROR, "Interrupted twice waiting for reg change to complete {0}", new Object[]
+                                {rsw.getRegState()}, null);
+                    }
                     Thread.currentThread().interrupt();
-                    log( LogService.LOG_ERROR, "Interrupted exception waiting for reg change to complete {0}", new Object[]
-                            {rsw.getRegState()}, null);
-                    reportTimeout();
                 }
             }
         }