/** * Creates an initial context using the specified environment properties. * * <p>If an InitialContextFactoryBuilder has been installed, it is used to create the factory for * creating the initial context. Otherwise, the class specified in the * <tt>Context.INITIAL_CONTEXT_FACTORY</tt> 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. * * @param env The possibly null environment properties used when creating the context. * @return A non-null initial context. * @exception NoInitialContextException If the <tt>Context.INITIAL_CONTEXT_FACTORY</tt> property * is not found or names a nonexistent class or a class that cannot be instantiated, or if the * initial context could not be created for some other reason. * @exception NamingException If some other naming exception was encountered. * @see javax.naming.InitialContext * @see javax.naming.directory.InitialDirContext */ public static Context getInitialContext(Hashtable<?, ?> env) throws NamingException { InitialContextFactory factory; InitialContextFactoryBuilder builder = getInitialContextFactoryBuilder(); if (builder == null) { // No factory installed, use property // Get initial context factory class name String className = env != null ? (String) env.get(Context.INITIAL_CONTEXT_FACTORY) : null; if (className == null) { NoInitialContextException ne = new NoInitialContextException( "Need to specify class name in environment or system " + "property, or as an applet parameter, or in an " + "application resource file: " + Context.INITIAL_CONTEXT_FACTORY); throw ne; } 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); } return factory.getInitialContext(env); }