protectedvoidinit(Hashtable<?,?>environment)throwsNamingException{myProps=ResourceManager.getInitialEnvironment(environment);if(myProps.get(Context.INITIAL_CONTEXT_FACTORY)!=null){// user has specified initial context factory; try to get itgetDefaultInitCtx();}}
publicstaticHashtablegetInitialEnvironment(Hashtableenv)throwsNamingException{String[]props=VersionHelper.PROPS;// system/applet propertiesif(env==null){env=newHashtable(11);}Objectapplet=env.get(Context.APPLET);// Merge property values from env param, applet params, and system// properties. The first value wins: there's no concatenation of// colon-separated lists.// Read system properties by first trying System.getProperties(),// and then trying System.getProperty() if that fails. The former// is more efficient due to fewer permission checks.String[]jndiSysProps=helper.getJndiProperties();for(inti=0;i<props.length;i++){Objectval=env.get(props[i]);if(val==null){if(applet!=null){val=AppletParameter.get(applet,props[i]);}if(val==null){// Read system property.val=(jndiSysProps!=null)?jndiSysProps[i]:helper.getJndiProperty(i);}if(val!=null){env.put(props[i],val);}}}// Merge the above with the values read from all application// resource files. Colon-separated lists are concatenated.mergeTables(env,getApplicationResources());returnenv;}
finalstaticString[]PROPS=newString[]{javax.naming.Context.INITIAL_CONTEXT_FACTORY,javax.naming.Context.OBJECT_FACTORIES,javax.naming.Context.URL_PKG_PREFIXES,javax.naming.Context.STATE_FACTORIES,javax.naming.Context.PROVIDER_URL,javax.naming.Context.DNS_URL,// The following shouldn't create a runtime dependence on ldap package.javax.naming.ldap.LdapContext.CONTROL_FACTORIES};
privatestaticHashtablegetApplicationResources()throwsNamingException{ClassLoadercl=helper.getContextClassLoader();synchronized(propertiesCache){Hashtableresult=(Hashtable)propertiesCache.get(cl);if(result!=null){returnresult;}try{NamingEnumerationresources=helper.getResources(cl,APP_RESOURCE_FILE_NAME);while(resources.hasMore()){Propertiesprops=newProperties();props.load((InputStream)resources.next());if(result==null){result=props;}else{mergeTables(result,props);}}// Merge in properties from file in <java.home>/lib.InputStreamistream=helper.getJavaHomeLibStream(JRELIB_PROPERTY_FILE_NAME);if(istream!=null){Propertiesprops=newProperties();props.load(istream);if(result==null){result=props;}else{mergeTables(result,props);}}}catch(IOExceptione){NamingExceptionne=newConfigurationException("Error reading application resource file");ne.setRootCause(e);throwne;}if(result==null){result=newHashtable(11);}propertiesCache.put(cl,result);returnresult;}}
protectedvoidinit(Hashtable<?,?>environment)throwsNamingException{myProps=ResourceManager.getInitialEnvironment(environment);if(myProps.get(Context.INITIAL_CONTEXT_FACTORY)!=null){// user has specified initial context factory; try to get itgetDefaultInitCtx();}}
publicstaticContextgetInitialContext(Hashtable<?,?>env)throwsNamingException{InitialContextFactoryfactory;InitialContextFactoryBuilderbuilder=getInitialContextFactoryBuilder();if(builder==null){// No factory installed, use property// Get initial context factory class nameStringclassName=env!=null?(String)env.get(Context.INITIAL_CONTEXT_FACTORY):null;if(className==null){NoInitialContextExceptionne=newNoInitialContextException("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);throwne;}try{factory=(InitialContextFactory)helper.loadClass(className).newInstance();}catch(Exceptione){NoInitialContextExceptionne=newNoInitialContextException("Cannot instantiate class: "+className);ne.setRootCause(e);throwne;}}else{factory=builder.createInitialContextFactory(env);}returnfactory.getInitialContext(env);}