This is an automated email from the ASF dual-hosted git repository.

pauls pushed a commit to branch framework-6.0.5
in repository https://gitbox.apache.org/repos/asf/felix-dev.git

commit d56ed62f4c0162c584e6db96bd89b56a89ae26d6
Author: Karl Pauls <[email protected]>
AuthorDate: Tue Jun 15 09:33:56 2021 +0200

    FELIX-6433: cherry-pick FELIX-6430: fallback to builtin url handlers if we 
can't reflect on unsave
---
 framework/pom.xml                                  |  3 +
 .../org/apache/felix/framework/URLHandlers.java    | 30 ++++++++-
 .../framework/URLHandlersStreamHandlerProxy.java   | 77 ++++++++++------------
 main/pom.xml                                       |  3 +
 4 files changed, 66 insertions(+), 47 deletions(-)

diff --git a/framework/pom.xml b/framework/pom.xml
index df06b99..bcc5785 100644
--- a/framework/pom.xml
+++ b/framework/pom.xml
@@ -69,6 +69,9 @@
                 
osgi.service;objectClass:List&lt;String&gt;="org.osgi.service.packageadmin.PackageAdmin",
                 
osgi.service;objectClass:List&lt;String&gt;="org.osgi.service.startlevel.StartLevel"
             </Provide-Capability>
+              <Add-opens>
+                  java.base/java.net java.base/sun.net.www.protocol.file 
java.base/sun.net.www.protocol.ftp java.base/sun.net.www.protocol.http 
java.base/sun.net.www.protocol.https java.base/sun.net.www.protocol.jar 
java.base/sun.net.www.protocol.jmod java.base/sun.net.www.protocol.mailto 
java.base/sun.net.www.protocol.jrt java.base/jdk.internal.loader 
java.base/java.security
+              </Add-opens>
           </instructions>
         </configuration>
       </plugin>
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 79b13ce..d87a338 100644
--- a/framework/src/main/java/org/apache/felix/framework/URLHandlers.java
+++ b/framework/src/main/java/org/apache/felix/framework/URLHandlers.java
@@ -116,8 +116,16 @@ class URLHandlers implements URLStreamHandlerFactory, 
ContentHandlerFactory
         m_streamPkgs = (pkgs.equals(""))
             ? DEFAULT_STREAM_HANDLER_PACKAGE
             : pkgs + "|" + DEFAULT_STREAM_HANDLER_PACKAGE;
-        m_loaded = (null != URLHandlersStreamHandlerProxy.class) &&
-            (null != URLHandlersContentHandlerProxy.class) && (null != 
URLStreamHandlerService.class);
+        boolean loaded;
+        try
+        {
+            loaded = (null != URLHandlersStreamHandlerProxy.class) &&
+                    (null != URLHandlersContentHandlerProxy.class) && (null != 
URLStreamHandlerService.class) && new URLHandlersStreamHandlerProxy(null, null) 
!= null;
+        }
+        catch (Throwable e) {
+            loaded = false;
+        }
+        m_loaded = loaded;
     }
 
     private void init(String protocol, URLStreamHandlerFactory factory)
@@ -509,12 +517,28 @@ class URLHandlers implements URLStreamHandlerFactory, 
ContentHandlerFactory
         handler = getBuiltInStreamHandler(protocol,
             (m_streamHandlerFactory != this) ? m_streamHandlerFactory : null);
 
+        if (handler == null && isJVM(protocol))
+        {
+            return null;
+        }
         // If built-in content handler, then create a proxy handler.
         return addToCache(m_streamHandlerCache, protocol,
-            new URLHandlersStreamHandlerProxy(protocol, m_secureAction,
+            URLHandlersStreamHandlerProxy.wrap(protocol, m_secureAction,
                 handler, getFromCache(m_protocolToURL, protocol)));
     }
 
+    private boolean isJVM(String protocol)
+    {
+        return protocol.equals("file") ||
+                protocol.equals("ftp") ||
+                protocol.equals("http") ||
+                protocol.equals("https") ||
+                protocol.equals("jar") ||
+                protocol.equals("jmod") ||
+                protocol.equals("mailto") ||
+                protocol.equals("jrt");
+    }
+
     /**
      * <p>
      * This is a method implementation for the <tt>ContentHandlerFactory</tt>
diff --git 
a/framework/src/main/java/org/apache/felix/framework/URLHandlersStreamHandlerProxy.java
 
b/framework/src/main/java/org/apache/felix/framework/URLHandlersStreamHandlerProxy.java
index ef577c6..b6bc451 100644
--- 
a/framework/src/main/java/org/apache/felix/framework/URLHandlersStreamHandlerProxy.java
+++ 
b/framework/src/main/java/org/apache/felix/framework/URLHandlersStreamHandlerProxy.java
@@ -72,54 +72,35 @@ public class URLHandlersStreamHandlerProxy extends 
URLStreamHandler
 
     static {
         SecureAction action = new SecureAction();
-        try
-        {
-            EQUALS = URLStreamHandler.class.getDeclaredMethod("equals",
-                new Class[]{URL.class, URL.class});
-            action.setAccesssible(EQUALS);
-            GET_DEFAULT_PORT = 
URLStreamHandler.class.getDeclaredMethod("getDefaultPort",
-                (Class[]) null);
-            action.setAccesssible(GET_DEFAULT_PORT);
-            GET_HOST_ADDRESS = URLStreamHandler.class.getDeclaredMethod(
-                    "getHostAddress", new Class[]{URL.class});
-            action.setAccesssible(GET_HOST_ADDRESS);
-            HASH_CODE = URLStreamHandler.class.getDeclaredMethod(
-                    "hashCode", new Class[]{URL.class});
-            action.setAccesssible(HASH_CODE);
-            HOSTS_EQUAL = URLStreamHandler.class.getDeclaredMethod(
-                    "hostsEqual", new Class[]{URL.class, URL.class});
-            action.setAccesssible(HOSTS_EQUAL);
-            OPEN_CONNECTION = URLStreamHandler.class.getDeclaredMethod(
-                    "openConnection", new Class[]{URL.class});
-            action.setAccesssible(OPEN_CONNECTION);
-            SAME_FILE = URLStreamHandler.class.getDeclaredMethod(
-                    "sameFile", new Class[]{URL.class, URL.class});
-            action.setAccesssible(SAME_FILE);
-            TO_EXTERNAL_FORM = URLStreamHandler.class.getDeclaredMethod(
-                   "toExternalForm", new Class[]{URL.class});
-            action.setAccesssible(TO_EXTERNAL_FORM);
-        }
-        catch (Exception ex)
-        {
-            throw new RuntimeException(ex.getMessage(), ex);
-        }
 
-        Method open_connection_proxy = null;
-        Class[] url_proxy_class = null;
+        EQUALS = reflect(action, "equals",
+            new Class[]{URL.class, URL.class});
+        GET_DEFAULT_PORT = reflect(action, "getDefaultPort",
+            (Class[]) null);
+        GET_HOST_ADDRESS = reflect(action, "getHostAddress", new 
Class[]{URL.class});
+        HASH_CODE = reflect(action, "hashCode", new Class[]{URL.class});
+        HOSTS_EQUAL = reflect(action, "hostsEqual", new Class[]{URL.class, 
URL.class});
+        OPEN_CONNECTION = reflect(action, "openConnection", new 
Class[]{URL.class});
+        SAME_FILE = reflect(action, "sameFile", new Class[]{URL.class, 
URL.class});
+        TO_EXTERNAL_FORM =reflect(action, "toExternalForm", new 
Class[]{URL.class});
+
+        URL_PROXY_CLASS = new Class[]{URL.class, java.net.Proxy.class};
+        OPEN_CONNECTION_PROXY = reflect(action, "openConnection", 
URL_PROXY_CLASS);
+    }
+
+    private static Method reflect(SecureAction action, String name, Class... 
classes)
+    {
         try
         {
-               url_proxy_class = new Class[]{URL.class, java.net.Proxy.class};
-            open_connection_proxy = URLStreamHandler.class.getDeclaredMethod(
-                "openConnection", url_proxy_class);
-            action.setAccesssible(open_connection_proxy);
+            Method method = URLStreamHandler.class.getDeclaredMethod(name,
+                    classes);
+            action.setAccesssible(method);
+            return method;
         }
-        catch (Throwable ex)
+        catch (Throwable t)
         {
-           open_connection_proxy = null;
-           url_proxy_class = null;
+            return null;
         }
-        OPEN_CONNECTION_PROXY = open_connection_proxy;
-        URL_PROXY_CLASS = url_proxy_class;
     }
 
     private final Object m_service;
@@ -147,6 +128,14 @@ public class URLHandlersStreamHandlerProxy extends 
URLStreamHandler
         m_builtInURL = null;
     }
 
+    static URLStreamHandler wrap(String protocol, SecureAction action, 
URLStreamHandler builtIn, URL builtInURL)
+    {
+        return ((EQUALS == null) || (GET_DEFAULT_PORT == null) ||
+                (GET_HOST_ADDRESS == null) || HASH_CODE == null || HOSTS_EQUAL 
== null ||
+                (OPEN_CONNECTION == null) || (OPEN_CONNECTION_PROXY == null) 
|| (SAME_FILE == null) ||
+                (TO_EXTERNAL_FORM == null)) && builtIn != null ? builtIn : new 
URLHandlersStreamHandlerProxy(protocol, action, builtIn, builtInURL);
+    }
+
     //
     // URLStreamHandler interface methods.
     //
@@ -390,7 +379,7 @@ public class URLHandlersStreamHandlerProxy extends 
URLStreamHandler
                 if (m_builtInURL != null)
                 {
                     // However, if we are on gnu/classpath we have to pass the 
handler directly
-                    // because the hidden feature is not there. Funnily, the 
workaround to pass
+                    // because the hidden feature is not there. Funnily, the 
workaround to
                     // pass the handler directly doesn't work on sun as their 
handler detects
                     // that it is not the same as the one inside the url and 
throws an exception
                     // Luckily it doesn't do that on gnu/classpath. We detect 
that we need to
@@ -418,7 +407,7 @@ public class URLHandlersStreamHandlerProxy extends 
URLStreamHandler
                 else
                 {
                     // We don't have a url with a built-in handler for this 
but still want to create
-                    // the url with the buil-in handler as we could find one 
now. This might not
+                    // the url with the built-in handler as we could find one 
now. This might not
                     // work for all handlers on sun but it is better then 
doing nothing.
                     test = m_action.createURL(url, spec, (URLStreamHandler) 
svc);
                 }
diff --git a/main/pom.xml b/main/pom.xml
index bfd47a6..4029381 100644
--- a/main/pom.xml
+++ b/main/pom.xml
@@ -87,6 +87,9 @@
             <Private-Package>org.apache.felix.main.*</Private-Package>
             <Export-Package>!org.osgi.service.log, !org.osgi.service.obr, 
org.osgi.*</Export-Package>
             <Import-Package>!*</Import-Package>
+            <Add-opens>
+                  java.base/java.net java.base/sun.net.www.protocol.file 
java.base/sun.net.www.protocol.ftp java.base/sun.net.www.protocol.http 
java.base/sun.net.www.protocol.https java.base/sun.net.www.protocol.jar 
java.base/sun.net.www.protocol.jmod java.base/sun.net.www.protocol.mailto 
java.base/sun.net.www.protocol.jrt java.base/jdk.internal.loader 
java.base/java.security
+            </Add-opens>
           </instructions>
         </configuration>
       </plugin>

Reply via email to