[ Back | Previous | Next ]

How to avoid NoInitalContextExceptoins?

Package:
javax.naming.*
Product:
JNDI
Release:
1.2
Related Links:
General
InitialDirContext
Comment:
	final Hashtable env = new Hashtable();
	env.put(Context.INITIAL_CONTEXT_FACTORY, Env.INITIAL_CONTEXT_FACTORY);
	env.put(Context.PROVIDER_URL, Env.PROVIDER_URL);
	//env.put(Context.OBJECT_FACTORIES, Env.HAVENS_OBJECT_FACTORIES);
	env.put(Context.SECURITY_AUTHENTICATION, Env.SECURITY_AUTHENTICATION);
	env.put(Context.SECURITY_PRINCIPAL, Env.SECURITY_PRINCIPAL); // specify the username
	env.put(Context.SECURITY_CREDENTIALS, Env.SECURITY_CREDENTIALS); // specify the password
	DirContext ctx = null;
	// save current class loader
	ClassLoader prevcl = Thread.currentThread().getContextClassLoader();
	// get class loader of servlet
	ClassLoader classcl = (new Env()).getClass().getClassLoader();
	try {
		// Use servlet's classloader so that providers are visible
		Thread.currentThread().setContextClassLoader(classcl);

		/* get a handle to an Initial DirContext */
		ctx = new InitialDirContext(env);

		/* specify search constraints to search subtree */
		final SearchControls constraints = new SearchControls();
		constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);

		/* Environment Settings */
		/*for (Enumeration e = ctx.getEnvironment().keys(); e.hasMoreElements();) {
		Object key = e.nextElement();
		dbg("key: " + key + ", " + ctx.getEnvironment().get(key));
		}*/
		return ctx.search(searchbase, searchString, constraints);
	} catch (NamingException e) {
		throw e;
	} finally {
		// Restore
		Thread.currentThread().setContextClassLoader(prevcl);
	}
1