--- old/src/java.naming/share/classes/javax/naming/spi/NamingManager.java 2014-09-16 12:04:27.000000000 +0100
+++ new/src/java.naming/share/classes/javax/naming/spi/NamingManager.java 2014-09-16 12:04:27.000000000 +0100
@@ -25,9 +25,7 @@
package javax.naming.spi;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.StringTokenizer;
+import java.util.*;
import java.net.MalformedURLException;
import javax.naming.*;
@@ -625,15 +623,28 @@
/**
* Creates an initial context using the specified environment
* properties.
- *
- * If an InitialContextFactoryBuilder has been installed,
- * it is used to create the factory for creating the initial context.
- * Otherwise, the class specified in the
- * Context.INITIAL_CONTEXT_FACTORY environment property is used.
- * Note that an initial context factory (an object that implements the
- * InitialContextFactory interface) must be public and must have a
- * public constructor that accepts no arguments.
- *
+ *
+ * This is done as follows:
+ *
+ * - If an InitialContextFactoryBuilder has been installed,
+ * it is used to create the factory for creating the initial
+ * context
+ * - Otherwise, the class specified in the
+ * Context.INITIAL_CONTEXT_FACTORY environment property
+ * is used
+ *
+ * - First, the {@linkplain java.util.ServiceLoader ServiceLoader}
+ * mechanism tries to locate an {@code InitialContextFactory}
+ * provider with system classloader
+ * - Failing that, this implementation tries to locate a suitable
+ * {@code InitialContextFactory} using a built-in mechanism
+ *
+ * (Note that an initial context factory (an object that implements
+ * the InitialContextFactory interface) must be public and must have
+ * a public constructor that accepts no arguments)
+ *
+ *
+ *
* @param env The possibly null environment properties used when
* creating the context.
* @return A non-null initial context.
@@ -649,11 +660,11 @@
*/
public static Context getInitialContext(Hashtable,?> env)
throws NamingException {
- InitialContextFactory factory;
+ InitialContextFactory factory = null;
InitialContextFactoryBuilder builder = getInitialContextFactoryBuilder();
if (builder == null) {
- // No factory installed, use property
+ // No builder installed, use property
// Get initial context factory class name
String className = env != null ?
@@ -666,16 +677,40 @@
throw ne;
}
+ ServiceLoader loader =
+ ServiceLoader.load(InitialContextFactory.class,
+ ClassLoader.getSystemClassLoader());
+
+ Iterator iterator = loader.iterator();
try {
- factory = (InitialContextFactory)
- helper.loadClass(className).newInstance();
- } catch(Exception e) {
+ while (iterator.hasNext()) {
+ InitialContextFactory f = iterator.next();
+ if (f.getClass().getName().equals(className)) {
+ factory = f;
+ break;
+ }
+ }
+ } catch (ServiceConfigurationError e) {
NoInitialContextException ne =
- new NoInitialContextException(
- "Cannot instantiate class: " + className);
+ new NoInitialContextException(
+ "Cannot load initial context factory "
+ + "'" + className + "'");
ne.setRootCause(e);
throw ne;
}
+
+ if (factory == null) {
+ try {
+ factory = (InitialContextFactory)
+ helper.loadClass(className).newInstance();
+ } catch (Exception e) {
+ NoInitialContextException ne =
+ new NoInitialContextException(
+ "Cannot instantiate class: " + className);
+ ne.setRootCause(e);
+ throw ne;
+ }
+ }
} else {
factory = builder.createInitialContextFactory(env);
}