geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rotty3...@apache.org
Subject svn commit: r1834162 - in /geronimo/specs/trunk: geronimo-json_1.0_spec/pom.xml geronimo-json_1.0_spec/src/main/java/javax/json/spi/JsonProvider.java geronimo-json_1.1_spec/pom.xml geronimo-json_1.1_spec/src/main/java/javax/json/spi/JsonProvider.java
Date Fri, 22 Jun 2018 21:00:36 GMT
Author: rotty3000
Date: Fri Jun 22 21:00:36 2018
New Revision: 1834162

URL: http://svn.apache.org/viewvc?rev=1834162&view=rev
Log:
GERONIMO-6560 JSON spec jar is not able to load provider in an OSGi environment

Based on https://github.com/apache/geronimo-specs/pull/6 by @sseifert

Signed-off-by: Raymond Auge <rotty3000@apache.org>

Modified:
    geronimo/specs/trunk/geronimo-json_1.0_spec/pom.xml
    geronimo/specs/trunk/geronimo-json_1.0_spec/src/main/java/javax/json/spi/JsonProvider.java
    geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml
    geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/spi/JsonProvider.java

Modified: geronimo/specs/trunk/geronimo-json_1.0_spec/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-json_1.0_spec/pom.xml?rev=1834162&r1=1834161&r2=1834162&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-json_1.0_spec/pom.xml (original)
+++ geronimo/specs/trunk/geronimo-json_1.0_spec/pom.xml Fri Jun 22 21:00:36 2018
@@ -100,6 +100,10 @@
             <Provide-Capability><![CDATA[
                osgi.contract;osgi.contract=JavaJSONP;uses:="${packages;NAMED;javax.*}";version:List<Version>='1.0'
             ]]></Provide-Capability>
+            <Require-Capability><![CDATA[
+              osgi.serviceloader;filter:="(osgi.serviceloader=javax.json.spi.JsonProvider)";cardinality:=multiple,
+              osgi.extender;filter:="(osgi.extender=osgi.serviceloader.processor)"
+            ]]></Require-Capability>
           </instructions>
         </configuration>
       </plugin>

Modified: geronimo/specs/trunk/geronimo-json_1.0_spec/src/main/java/javax/json/spi/JsonProvider.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-json_1.0_spec/src/main/java/javax/json/spi/JsonProvider.java?rev=1834162&r1=1834161&r2=1834162&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-json_1.0_spec/src/main/java/javax/json/spi/JsonProvider.java
(original)
+++ geronimo/specs/trunk/geronimo-json_1.0_spec/src/main/java/javax/json/spi/JsonProvider.java
Fri Jun 22 21:00:36 2018
@@ -28,21 +28,16 @@ import javax.json.stream.JsonGenerator;
 import javax.json.stream.JsonGeneratorFactory;
 import javax.json.stream.JsonParser;
 import javax.json.stream.JsonParserFactory;
-import java.io.BufferedReader;
-import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.Reader;
 import java.io.Writer;
 import java.lang.ref.WeakReference;
-import java.lang.reflect.Method;
-import java.net.URL;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
-import java.util.Enumeration;
-import java.util.List;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.ServiceLoader;
 import java.util.WeakHashMap;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
@@ -155,17 +150,8 @@ public abstract class JsonProvider {
             if (tccl == null) {
                 tccl = ClassLoader.getSystemClassLoader();
             }
-            try {
-                final Class<?> clazz = Class.forName("org.apache.geronimo.osgi.locator.ProviderLocator");
-                final Method getServices = clazz.getDeclaredMethod("getServices", String.class,
Class.class, ClassLoader.class);
-                final List<JsonProvider> osgiProviders = (List<JsonProvider>)
getServices.invoke(null, JsonProvider.class.getName(), JsonProvider.class, tccl);
-                if (osgiProviders != null && !osgiProviders.isEmpty()) {
-                    return osgiProviders.iterator().next();
-                }
-            } catch (final Throwable e) {
-                // locator not available, try normal mode
-            }
 
+            // try to load provider specified via system property
             final String className = System.getProperty(JsonProvider.class.getName());
             if (className != null) {
                 try {
@@ -175,47 +161,13 @@ public abstract class JsonProvider {
                 }
             }
 
-            // don't use Class.forName() to avoid to bind class to tccl if thats a classloader
facade
-            // so implementing a simple SPI when ProviderLocator is not here
-            final String name = "META-INF/services/" + JsonProvider.class.getName();
-            try {
-                final Enumeration<URL> configs = tccl.getResources(name);
-                if (configs.hasMoreElements()) {
-                    InputStream in = null;
-                    BufferedReader r = null;
-                    try {
-                        in = configs.nextElement().openStream();
-                        r = new BufferedReader(new InputStreamReader(in, "utf-8"));
-                        String l;
-                        while ((l = r.readLine()) != null) {
-                            if (l.startsWith("#")) {
-                                continue;
-                            }
-                            return JsonProvider.class.cast(tccl.loadClass(l).newInstance());
-                        }
-                    } catch (final IOException x) {
-                        // no-op
-                    } finally {
-                        try {
-                            if (r != null) {
-                                r.close();
-                            }
-                        } catch (final IOException y) {
-                            // no-op
-                        }
-                        try {
-                            if (in != null) {
-                                in.close();
-                            }
-                        } catch (final IOException y) {
-                            // no-op
-                        }
-                    }
-                }
-            } catch (final Exception ex) {
-                // no-op
+            // try to load via ServiceLoader (as registered in META-INF/services)
+            Iterator<JsonProvider> providers = ServiceLoader.load(JsonProvider.class).iterator();
+            if (providers.hasNext()) {
+                return providers.next();
             }
 
+            // try to load to default provider
             try {
                 return JsonProvider.class.cast(tccl.loadClass(DEFAULT_PROVIDER).newInstance());
             } catch (final Throwable cnfe) {
@@ -224,4 +176,3 @@ public abstract class JsonProvider {
         }
     }
 }
-

Modified: geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml?rev=1834162&r1=1834161&r2=1834162&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml (original)
+++ geronimo/specs/trunk/geronimo-json_1.1_spec/pom.xml Fri Jun 22 21:00:36 2018
@@ -86,6 +86,10 @@
             <Provide-Capability><![CDATA[
                osgi.contract;osgi.contract=JavaJSONP;uses:="${packages;NAMED;javax.*}";version:List<Version>='1.1,1.0'
             ]]></Provide-Capability>
+            <Require-Capability><![CDATA[
+              osgi.serviceloader;filter:="(osgi.serviceloader=javax.json.spi.JsonProvider)";cardinality:=multiple,
+              osgi.extender;filter:="(osgi.extender=osgi.serviceloader.processor)"
+            ]]></Require-Capability>
           </instructions>
         </configuration>
       </plugin>

Modified: geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/spi/JsonProvider.java
URL: http://svn.apache.org/viewvc/geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/spi/JsonProvider.java?rev=1834162&r1=1834161&r2=1834162&view=diff
==============================================================================
--- geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/spi/JsonProvider.java
(original)
+++ geronimo/specs/trunk/geronimo-json_1.1_spec/src/main/java/javax/json/spi/JsonProvider.java
Fri Jun 22 21:00:36 2018
@@ -55,8 +55,10 @@ import java.security.PrivilegedAction;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Enumeration;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.ServiceLoader;
 
 /**
  * JsonProvider is the actual implementation of all the Json logic.
@@ -88,69 +90,30 @@ public abstract class JsonProvider {
     }
 
     private static JsonProvider doLoadProvider() throws JsonException {
-        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-        try {
-            final Class<?> clazz = Class.forName("org.apache.geronimo.osgi.locator.ProviderLocator");
-            final Method getServices = clazz.getDeclaredMethod("getServices", String.class,
Class.class, ClassLoader.class);
-            final List<JsonProvider> osgiProviders = (List<JsonProvider>) getServices.invoke(null,
JsonProvider.class.getName(), JsonProvider.class, tccl);
-            if (osgiProviders != null && !osgiProviders.isEmpty()) {
-                return osgiProviders.iterator().next();
-            }
-        } catch (final Throwable e) {
-            // locator not available, try normal mode
+        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+        if (tccl == null) {
+            tccl = ClassLoader.getSystemClassLoader();
         }
 
-        // don't use Class.forName() to avoid to bind class to tccl if thats a classloader
facade
-        // so implementing a simple SPI when ProviderLocator is not here
-        final String name = "META-INF/services/" + JsonProvider.class.getName();
-        try {
-            Enumeration<URL> configs;
-            if (tccl == null) {
-                configs = ClassLoader.getSystemResources(name);
-            } else {
-                configs = tccl.getResources(name);
+        // try to load provider specified via system property
+        final String className = System.getProperty(JsonProvider.class.getName());
+        if (className != null) {
+            try {
+                return JsonProvider.class.cast(tccl.loadClass(className.trim()).newInstance());
+            } catch (final Exception e) {
+                throw new JsonException("Specified provider as system property can't be loaded:
" + className, e);
             }
+        }
 
-            if (configs.hasMoreElements()) {
-                InputStream in = null;
-                BufferedReader r = null;
-                final List<String> names = new ArrayList<String>();
-                try {
-                    in = configs.nextElement().openStream();
-                    r = new BufferedReader(new InputStreamReader(in, "utf-8"));
-                    String l;
-                    while ((l = r.readLine()) != null) {
-                        if (l.startsWith("#")) {
-                            continue;
-                        }
-                        return JsonProvider.class.cast(tccl.loadClass(l).newInstance());
-                    }
-                } catch (final IOException x) {
-                    // no-op
-                } finally {
-                    try {
-                        if (r != null) {
-                            r.close();
-                        }
-                    } catch (final IOException y) {
-                        // no-op
-                    }
-                    try {
-                        if (in != null) {
-                            in.close();
-                        }
-                    } catch (final IOException y) {
-                        // no-op
-                    }
-                }
-            }
-        } catch (final Exception ex) {
-            // no-op
+        // try to load via ServiceLoader (as registered in META-INF/services)
+        Iterator<JsonProvider> providers = ServiceLoader.load(JsonProvider.class).iterator();
+        if (providers.hasNext()) {
+            return providers.next();
         }
 
+        // try to load to default provider
         try {
-            final Class<?> clazz = tccl.loadClass(DEFAULT_PROVIDER);
-            return JsonProvider.class.cast(clazz.newInstance());
+            return JsonProvider.class.cast(tccl.loadClass(DEFAULT_PROVIDER).newInstance());
         } catch (final Throwable cnfe) {
             throw new JsonException(DEFAULT_PROVIDER + " not found", cnfe);
         }



Mime
View raw message