Author: pauls
Date: Tue Mar 13 07:50:24 2018
New Revision: 1826608

URL: http://svn.apache.org/viewvc?rev=1826608&view=rev
Log:
FELIX-5804: Improve framework lookup for bundle protocol urls

Modified:
    
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlers.java
    
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleStreamHandler.java
    
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleURLConnection.java
    
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/util/Util.java

Modified: 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlers.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlers.java?rev=1826608&r1=1826607&r2=1826608&view=diff
==============================================================================
--- 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlers.java
 (original)
+++ 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlers.java
 Tue Mar 13 07:50:24 2018
@@ -34,6 +34,7 @@ import static org.apache.felix.framework
 import org.apache.felix.framework.util.FelixConstants;
 import org.apache.felix.framework.util.SecureAction;
 import org.apache.felix.framework.util.SecurityManagerEx;
+import org.osgi.framework.Constants;
 import org.osgi.service.url.URLStreamHandlerService;
 
 /**
@@ -91,7 +92,7 @@ class URLHandlers implements URLStreamHa
     private final static ConcurrentHashMap<ClassLoader, List<Object>> 
m_classloaderToFrameworkLists = new ConcurrentHashMap<ClassLoader, 
List<Object>>();
 
     // The list to hold all enabled frameworks registered with this handlers
-    private static final CopyOnWriteArrayList m_frameworks = new 
CopyOnWriteArrayList();
+    private static final CopyOnWriteArrayList<Felix> m_frameworks = new 
CopyOnWriteArrayList<Felix>();
     private static volatile int m_counter = 0;
 
     private static final ConcurrentHashMap<String, ContentHandler> 
m_contentHandlerCache = new ConcurrentHashMap<String, ContentHandler>();
@@ -540,7 +541,7 @@ class URLHandlers implements URLStreamHa
      * @param enable a flag indicating whether or not the framework wants to
      *        enable the URL Handlers service.
     **/
-    public static void registerFrameworkInstance(Object framework, boolean 
enable)
+    public static void registerFrameworkInstance(Felix framework, boolean 
enable)
     {
         boolean register = false;
         synchronized (m_frameworks)
@@ -740,4 +741,42 @@ class URLHandlers implements URLStreamHa
         }
         return null;
     }
+
+    public static Object getFrameworkFromContext(String uuid)
+    {
+        if (uuid != null)
+        {
+            for (Felix framework : m_frameworks)
+            {
+                if 
(uuid.equals(framework._getProperty(Constants.FRAMEWORK_UUID)))
+                {
+                    return framework;
+                }
+            }
+            for (List frameworks : m_classloaderToFrameworkLists.values())
+            {
+                for (Object framework : frameworks)
+                {
+                    try
+                    {
+                        if (uuid.equals(
+                                m_secureAction.invoke(
+                                        
m_secureAction.getDeclaredMethod(framework.getClass(),"getProperty", new 
Class[]{String.class}),
+                                        framework, new 
Object[]{Constants.FRAMEWORK_UUID})))
+                        {
+                            return framework;
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        // This should not happen but if it does there is
+                        // not much we can do other then ignore it.
+                        // Maybe log this or something.
+                        ex.printStackTrace();
+                    }
+                }
+            }
+        }
+        return getFrameworkFromContext();
+    }
 }

Modified: 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleStreamHandler.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleStreamHandler.java?rev=1826608&r1=1826607&r2=1826608&view=diff
==============================================================================
--- 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleStreamHandler.java
 (original)
+++ 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleStreamHandler.java
 Tue Mar 13 07:50:24 2018
@@ -55,7 +55,7 @@ class URLHandlersBundleStreamHandler ext
             return new URLHandlersBundleURLConnection(url, m_framework);
         }
 
-        Object framework = URLHandlers.getFrameworkFromContext();
+        Object framework = 
URLHandlers.getFrameworkFromContext(Util.getFrameworkUUIDFromURL(url.getHost()));
 
         if (framework != null)
         {
@@ -130,14 +130,14 @@ class URLHandlersBundleStreamHandler ext
             Object framework = m_framework;
             if (framework == null)
             {
-                framework = URLHandlers.getFrameworkFromContext();
+                framework = 
URLHandlers.getFrameworkFromContext(Util.getFrameworkUUIDFromURL(u.getHost()));
                 if (!(framework instanceof Felix))
                 {
                     return false;
                 }
             }
             Felix felix = (Felix) framework;
-            long bundleId = Util.getBundleIdFromRevisionId(u.getHost());
+            long bundleId = 
Util.getBundleIdFromRevisionId(Util.getRevisionIdFromURL(u.getHost()));
             Bundle bundle = felix.getBundle(bundleId);
             if (bundle != null)
             {

Modified: 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleURLConnection.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleURLConnection.java?rev=1826608&r1=1826607&r2=1826608&view=diff
==============================================================================
--- 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleURLConnection.java
 (original)
+++ 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/URLHandlersBundleURLConnection.java
 Tue Mar 13 07:50:24 2018
@@ -40,11 +40,6 @@ class URLHandlersBundleURLConnection ext
     private String m_contentType;
     private InputStream m_is;
 
-    public URLHandlersBundleURLConnection(URL url)
-    {
-        super(url);
-    }
-
     public URLHandlersBundleURLConnection(URL url, Felix framework)
         throws IOException
     {
@@ -66,7 +61,7 @@ class URLHandlersBundleURLConnection ext
         // one from the call context.
         if (m_framework == null)
         {
-            Object tmp = URLHandlers.getFrameworkFromContext();
+            Object tmp = 
URLHandlers.getFrameworkFromContext(Util.getFrameworkUUIDFromURL(url.getHost()));
             if (tmp instanceof Felix)
             {
                 m_framework = (Felix) tmp;
@@ -82,7 +77,7 @@ class URLHandlersBundleURLConnection ext
         // The URL is constructed like this:
         //     bundle://<revision-id>:<bundle-classpath-index>/<resource-path>
         // Where <revision-id> = <bundle-id>.<revision>
-        long bundleId = Util.getBundleIdFromRevisionId(url.getHost());
+        long bundleId = 
Util.getBundleIdFromRevisionId(Util.getRevisionIdFromURL(url.getHost()));
         Bundle bundle = m_framework.getBundle(bundleId);
         if (bundle == null)
         {

Modified: 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/util/Util.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/util/Util.java?rev=1826608&r1=1826607&r2=1826608&view=diff
==============================================================================
--- 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/util/Util.java
 (original)
+++ 
felix/trunk/osgi-r7/framework/src/main/java/org/apache/felix/framework/util/Util.java
 Tue Mar 13 07:50:24 2018
@@ -910,4 +910,34 @@ public class Util
         V result = map.putIfAbsent(key, value);
         return result != null ? result : value;
     }
+
+    public static String getFrameworkUUIDFromURL(String host)
+    {
+        if (host != null)
+        {
+            int idx = host.indexOf('_');
+            if (idx > 0)
+            {
+                return host.substring(0, idx);
+            }
+        }
+        return null;
+    }
+
+    public static String getRevisionIdFromURL(String host)
+    {
+        if (host != null)
+        {
+            int idx = host.indexOf('_');
+            if (idx > 0 && idx < host.length())
+            {
+                return host.substring(idx + 1);
+            }
+            else
+            {
+                return host;
+            }
+        }
+        return null;
+    }
 }
\ No newline at end of file


Reply via email to