Skip to content

Commit

Permalink
GERONIMO-6624 Apply service loader mediator to key modules useful for…
Browse files Browse the repository at this point in the history
… microprofile: EL 2.2

Signed-off-by: Raymond Auge <[email protected]>

git-svn-id: https://svn.apache.org/repos/asf/geronimo/specs/trunk@1838551 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
rotty3000 committed Aug 21, 2018
1 parent 7fa658a commit 74d2470
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
18 changes: 5 additions & 13 deletions geronimo-el_2.2_spec/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand Down
72 changes: 47 additions & 25 deletions geronimo-el_2.2_spec/src/main/java/javax/el/ExpressionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand Down Expand Up @@ -57,12 +58,12 @@ public static ExpressionFactory newInstance() {
}

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) {
Expand All @@ -77,7 +78,7 @@ public static ExpressionFactory newInstance(Properties properties) {
} catch (SecurityException e) {
throw new ELException("Fail to get constuctor from ExpressionFactory implementation class", e);
} catch (NoSuchMethodException e) {
return newInstance0(implClass);
return factory;
}
}

Expand All @@ -91,7 +92,7 @@ private static ExpressionFactory newInstance0(Class<?> implClass) {
}
}

private static String lookupExpressionFactoryImplClass() {
private static ExpressionFactory lookupExpressionFactoryImpl(ClassLoader cl) throws ClassNotFoundException {

String implClassName = lookupByJREPropertyFile();
if (implClassName == null) {
Expand All @@ -100,48 +101,69 @@ private static String lookupExpressionFactoryImplClass() {
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);
}
}
}

0 comments on commit 74d2470

Please sign in to comment.