impl: Fix JNDI to work on JPMS for loading system InitialContextFactory
package org.osgi.impl.service.jndi;
import java.util.Hashtable;
+import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -51,14 +52,25 @@ class DefaultRuntimeInitialContextFactoryBuilder implements
final String initialContextFactoryName =
(String) environment.get(Context.INITIAL_CONTEXT_FACTORY);
+ try {
// attempt to load this provider from the system classpath
try {
- Class< ? > clazz =
- getClass().getClassLoader().loadClass(initialContextFactoryName);
+ Class< ? > clazz = getClass().getClassLoader()
+ .loadClass(initialContextFactoryName);
return (InitialContextFactory) clazz.getConstructor()
.newInstance();
+ } catch (IllegalAccessException e) {
+ // Under JPMS, the class constructor is not accessible
+ for (InitialContextFactory factory : ServiceLoader
+ .loadInstalled(InitialContextFactory.class)) {
+ if (factory.getClass()
+ .getName()
+ .equals(initialContextFactoryName)) {
+ return factory;
+ }
+ }
}
- catch (Exception e) {
+ } catch (Exception e) {
logger.log(Level.FINEST,
"Error while trying to load system-level JNDI provider",
e);
