Task:
=====
I want to use
      LdapContext lctx = new InitialLdapContext();
that is InitialLdapContext without environment parameters.

My understanding of J2EE standards is that the servlet container tries
to find the file jndi.properties on the application classpath and if found
initializes the context environment with properties from there.

Problem:
========
Ldap operations on lctx fail with exception:
      javax.naming.NotContextException: Not an instance of DirContext

Cause:
======
The property in jndi.properties:
      java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory
gets overwritten by Tomcat and is aquivalent to
      java.naming.factory.initial=
org.apache.naming.java.javaURLContextFactory
I've written a small web application to prove it and I read a property from
that file as follows:
      com.example.test.key=42
This value is available in the web-application as
      String testValue = (String) lctx.getEnvironment().get(
"com.example.test.key");
which is "42".
So jndi.properties are found by the classloader(?) and evaluated.

Workaround:
===========
I read my LDAP connection environment properties from my own
properties file. But I want to use the JNDI standard way!

Bugfix:
======
Find and change the lines where java.naming.factory.initial gets
overwritten
with Tomcats own value org.apache.naming.java.javaURLContextFactory

If this is a real bug I would take it to the developers list or bugzilla.

Here is my Testclass:
package com.example;

import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;

/**
 * Test the JNDI Implementation of Tomcat (5.0.x).
 * Connect to LDAP and search person objects
 * Demonstrate failure to read INITIAL_CONTEXT_FACTORY property from file
jndi.properties
 *
 * @author Frerk.Meyer(at)edeka.de
 *
 */
public class JNDITest {
      static public void testInitialLdapContext() throws NamingException {
            // get initial ldap context with environment from
jndi.properties
            LdapContext lctx = new InitialLdapContext();
            // check if key INITIAL_CONTEXT_FACTORY has the value from
jndi.properties
            String contextFactory = (String) lctx.getEnvironment
().get(Context.INITIAL_CONTEXT_FACTORY);
            // if not print out the false value
            if (!contextFactory.equals("com.sun.jndi.ldap.LdapCtxFactory"))
{
                  System.out.println("INITIAL_CONTEXT_FACTORY="
+contextFactory);
                  System.err.println("INITIAL_CONTEXT_FACTORY="
+contextFactory);
            }
            // check if my jndi.properties was read with arbitrary
key-value-pair
            String testValue = (String) lctx.getEnvironment().get(
"com.example.test.key");
            if (testValue!= null) {
                        System.out.println("com.example.test.key="
+testValue);
                        System.err.println("com.example.test.key="
+testValue);
            }
            // do some typical ldap search for person objects
            SearchControls sc = new SearchControls();
            sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
            sc.setTimeLimit(1000);
            sc.setCountLimit(100);
            sc.setReturningAttributes(null);
            NamingEnumeration results = lctx.search("",
"(objectclass=inetorgperson)",sc);
            while (results.hasMoreElements()) {
                  SearchResult result = (SearchResult) results.nextElement
();
                  String name = (String) result.getName();
                  System.out.println(name+"\n<br>\n");
            }
            results.close();
            lctx.close();
      }
}

Here it is called from within a JSP:
<%@ page import="com.example.JNDITest,javax.naming.NamingException" %>
<html>
  <head>
    <title>JNDITest for Tomcat</title>
  </head>
<body>
<h1>JNDITest for Tomcat</h1>
<p>
<%
      try {
            JNDITest.testInitialLdapContext();
      } catch (NamingException ne) {
            out.println("NamingException in testInitialLdapContext: <br />"
+ne.toString());
      }
%>
</p>
</body>
</html>

And here is my jndi.properties (change to apply to your LDAP server):
# jndi.properties for LDAP
java.naming.provider.url=ldap://webas01.zentrale.edekanet.de:389/o=Prod,dc=edekanet,dc=de
java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory
java.naming.factory.object=com.sun.jndi.ldap.obj.LdapGroupFactory
java.naming.factory.state=com.sun.jndi.ldap.obj.LdapGroupFactory
java.naming.security.authentication=none
java.naming.ldap.version=3
#java.naming.factory.url.pkgs=com.sun.jndi.url.ldap
com.example.test.key=42


Frerk Meyer

EDEKA Aktiengesellschaft
GB Datenverarbeitung
Frerk Meyer
CC Web Technologien
New-York-Ring 6
22297 Hamburg
Tel: 040/6377 - 3272
Fax: 040/6377 - 41268
mailto:[EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to