Fix potential deadlock when framework is shutdown and a url is created on the outside at the same time (FELIX-1565).

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@812916 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/URLHandlers.java b/framework/src/main/java/org/apache/felix/framework/URLHandlers.java
index 9b49a59..d6b444d 100644
--- a/framework/src/main/java/org/apache/felix/framework/URLHandlers.java
+++ b/framework/src/main/java/org/apache/felix/framework/URLHandlers.java
@@ -521,6 +521,7 @@
     **/
     public static void unregisterFrameworkInstance(Object framework)
     {
+        boolean unregister = false;
         synchronized (m_frameworks)
         {
             m_counter--;
@@ -528,24 +529,28 @@
             {    
                 if (m_frameworks.isEmpty())
                 {
-                    try
-                    {
-                        m_secureAction.invoke(m_secureAction.getDeclaredMethod(
-                            m_rootURLHandlers.getClass(), 
-                            "unregisterFrameworkListsForContextSearch", 
-                            new Class[]{ ClassLoader.class}), 
-                            m_rootURLHandlers,
-                            new Object[] {URLHANDLERS_CLASS.getClassLoader()});
-                    }
-                    catch (Exception e)
-                    {
-                        // TODO: this should not happen
-                        e.printStackTrace();
-                    }
+                    unregister = true;
                     m_handler = null;
                 }
             }
         }
+        if (unregister)
+        {
+             try
+             {
+                 m_secureAction.invoke(m_secureAction.getDeclaredMethod(
+                     m_rootURLHandlers.getClass(), 
+                     "unregisterFrameworkListsForContextSearch", 
+                     new Class[]{ ClassLoader.class}), 
+                     m_rootURLHandlers,
+                     new Object[] {URLHANDLERS_CLASS.getClassLoader()});
+             }
+             catch (Exception e)
+             {
+                 // TODO: this should not happen
+                 e.printStackTrace();
+             }
+        }
     }
 
     /**