GERONIMO-6624 Apply service loader mediator to key modules useful for microprofile: EL 2.2

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

git-svn-id: https://svn.apache.org/repos/asf/geronimo/specs/trunk@1838551 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/geronimo-el_2.2_spec/pom.xml b/geronimo-el_2.2_spec/pom.xml
index 5f3965d..563a35d 100644
--- a/geronimo-el_2.2_spec/pom.xml
+++ b/geronimo-el_2.2_spec/pom.xml
@@ -34,7 +34,7 @@
     <artifactId>geronimo-el_2.2_spec</artifactId>
     <packaging>bundle</packaging>
     <name>Apache Geronimo Expression Language Spec 2.2</name>
-    <version>1.0.5-SNAPSHOT</version>
+    <version>1.1-SNAPSHOT</version>
     <description>Expression Languague API 2.2</description>
 
     <url>http://geronimo.apache.org/maven/${siteId}/${project.version}</url>
@@ -55,15 +55,6 @@
         <url>http://svn.apache.org/viewcvs.cgi/geronimo/specs/trunk/geronimo-el_2.2_spec/</url>
     </scm>
 
-    <dependencies>
-        <dependency>
-            <groupId>org.apache.geronimo.specs</groupId>
-            <artifactId>geronimo-osgi-locator</artifactId>
-            <version>1.0</version>
-            <scope>provided</scope>
-        </dependency>
-    </dependencies>
-
     <build>
         <plugins>
             <plugin>
@@ -77,12 +68,13 @@
                         <Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
                         <Specification-Version>2.2</Specification-Version>
                         <Export-Package>javax.el*;version=2.2</Export-Package>
-                        <Import-Package>org.apache.geronimo.osgi.registry.api;resolution:=optional,*</Import-Package>
-                        <Private-Package>org.apache.geronimo.osgi.locator</Private-Package>
-                        <Bundle-Activator>org.apache.geronimo.osgi.locator.Activator</Bundle-Activator>
                         <Provide-Capability><![CDATA[
                             osgi.contract;osgi.contract=JavaEL;uses:="${packages;NAMED;javax.*}";version:List<Version>='2.2,2.1,2.0,1.0'
                         ]]></Provide-Capability>
+                        <Require-Capability><![CDATA[
+                          osgi.serviceloader;filter:="(osgi.serviceloader=javax.el.ExpressionFactory)";cardinality:=multiple;resolution:=optional,
+                          osgi.extender;filter:="(osgi.extender=osgi.serviceloader.processor)"
+                        ]]></Require-Capability>
                     </instructions>
                 </configuration>
             </plugin>
diff --git a/geronimo-el_2.2_spec/src/main/java/javax/el/ExpressionFactory.java b/geronimo-el_2.2_spec/src/main/java/javax/el/ExpressionFactory.java
index 4063f8d..eb5e54a 100644
--- a/geronimo-el_2.2_spec/src/main/java/javax/el/ExpressionFactory.java
+++ b/geronimo-el_2.2_spec/src/main/java/javax/el/ExpressionFactory.java
@@ -18,12 +18,13 @@
 package javax.el;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.Properties;
-
-import org.apache.geronimo.osgi.locator.ProviderLocator;
+import java.util.ServiceLoader;
 
 /**
  *
@@ -57,12 +58,12 @@
     }
 
     public static ExpressionFactory newInstance(Properties properties) {
-        Class<?> implClass = loadExpressionFactoryImplClass();
+        ExpressionFactory factory = loadExpressionFactoryImpl();
         if (properties == null) {
-            return newInstance0(implClass);
+            return factory;
         }
         try {
-            Constructor<?> constructor = implClass.getConstructor(Properties.class);
+            Constructor<?> constructor = factory.getClass().getConstructor(Properties.class);
             try {
                 return (ExpressionFactory) constructor.newInstance(properties);
             } catch (IllegalArgumentException e) {
@@ -77,7 +78,7 @@
         } catch (SecurityException e) {
             throw new ELException("Fail to get constuctor from ExpressionFactory implementation class", e);
         } catch (NoSuchMethodException e) {
-            return newInstance0(implClass);
+            return factory;
         }
     }
 
@@ -91,7 +92,7 @@
         }
     }
 
-    private static String lookupExpressionFactoryImplClass() {
+    private static ExpressionFactory lookupExpressionFactoryImpl(ClassLoader cl) throws ClassNotFoundException {
 
         String implClassName = lookupByJREPropertyFile();
         if (implClassName == null) {
@@ -100,48 +101,69 @@
                 implClassName = PLATFORM_DEFAULT_FACTORY_CLASS;
             }
         }
-        return implClassName;
+        return newInstance0(cl.loadClass(implClassName));
     }
 
-    private static Class<?> lookupByServiceEntryURL(ClassLoader cl) throws ClassNotFoundException {
-        // use the common lookup/parsing logic for the service files.
-        return ProviderLocator.getServiceClass(ExpressionFactory.class.getName(), ExpressionFactory.class, cl);
+    private static ExpressionFactory lookupByServiceEntryURL(ClassLoader cl) throws ClassNotFoundException {
+        Thread thread = Thread.currentThread();
+        ClassLoader original = thread.getContextClassLoader();
+        try {
+            thread.setContextClassLoader(cl);
+            for (ExpressionFactory factory : ServiceLoader.load(ExpressionFactory.class)) {
+                return factory;
+            }
+        }
+        finally {
+            thread.setContextClassLoader(original);
+        }
+        return null;
     }
 
     private static String lookupByJREPropertyFile() {
         try {
-            return ProviderLocator.lookupByJREPropertyFile(JAVA_RUNTIME_PROPERTY_FILE_LOCATION, SYSTEM_PROPERTY_NAME);
+            String jreDirectory = System.getProperty("java.home");
+            File configurationFile = new File(jreDirectory + File.separator + JAVA_RUNTIME_PROPERTY_FILE_LOCATION);
+            if (configurationFile.exists() && configurationFile.canRead()) {
+                Properties properties = new Properties();
+                InputStream in = null;
+                try {
+                    in = new FileInputStream(configurationFile);
+                    properties.load(in);
+                    return properties.getProperty(SYSTEM_PROPERTY_NAME);
+                } finally {
+                    if (in != null) {
+                        try {
+                            in.close();
+                        } catch (Exception e) {
+                        }
+                    }
+                }
+            }
+            return null;
         } catch (IOException e) {
             throw new ELException("Fail to read configuration file", e);
         }
     }
 
-    private static Class<?> loadExpressionFactoryImplClass() {
+    private static ExpressionFactory loadExpressionFactoryImpl() {
 
-        String implClassName = null;
         try {
             ClassLoader cl = Thread.currentThread().getContextClassLoader();
             if (cl == null) {
                 cl = ClassLoader.getSystemClassLoader();
             }
             // check the META-INF/services defintions first
-            Class<?> cls = lookupByServiceEntryURL(cl);
-            if (cls != null) {
-                return cls;
+            ExpressionFactory factory = lookupByServiceEntryURL(cl);
+            if (factory != null) {
+                return factory;
             }
             // try resolving using the alternate property lookups (always returns
             // something, since there is a default
-            implClassName = lookupExpressionFactoryImplClass();
-            return ProviderLocator.loadClass(implClassName, ExpressionFactory.class, cl);
+            return lookupExpressionFactoryImpl(cl);
         } catch (ClassNotFoundException e) {
             // can be thrown either as a result of a classloading failure in the service
             // lookup or a failure to directly load the class
-            if (implClassName != null) {
-                throw new ELException("Fail to load implementation class " + implClassName, e);
-            }
-            else {
-                throw new ELException("Fail to load implementation class", e);
-            }
+            throw new ELException("Fail to load implementation class", e);
         }
     }
 }