/** * Use for testing a LDAP Directory Server via JNDI. */ import java.io.*; import java.util.*; import javax.naming.*; import javax.naming.directory.*; import javax.naming.event.*; public class JNDI_Simple implements NamespaceChangeListener, ObjectChangeListener { public void namingExceptionThrown(NamingExceptionEvent evt) { final String methodName = "namingExceptionThrown"; JNDI_Simple.logDebug(methodName, "ENTERING"); JNDI_Simple.logDebug(methodName, evt.getException().toString()); JNDI_Simple.logDebug(methodName, "EXITING"); } public void objectAdded(NamingEvent evt) { final String methodName = "objectAdded"; JNDI_Simple.logDebug(methodName, "ENTERING"); String oldBindingName = ""; String newBindingName = ""; if (evt.getOldBinding() != null) { oldBindingName = evt.getOldBinding().getName(); } if (evt.getNewBinding() != null) { newBindingName = evt.getNewBinding().getName(); } String msg = String.format( "[OLD_BINDING's NAME: '%s']\t[NEW_BINDING's NAME: '%s']", oldBindingName, newBindingName); JNDI_Simple.logDebug(methodName, msg); JNDI_Simple.logDebug(methodName, "EXITING"); } public void objectChanged(NamingEvent evt) { final String methodName = "objectChanged"; JNDI_Simple.logDebug(methodName, "ENTERING"); String oldBindingName = ""; String newBindingName = ""; if (evt.getOldBinding() != null) { oldBindingName = evt.getOldBinding().getName(); } if (evt.getNewBinding() != null) { newBindingName = evt.getNewBinding().getName(); } String msg = String.format( "[OLD_BINDING's NAME: '%s']\t[NEW_BINDING's NAME: '%s']", oldBindingName, newBindingName); JNDI_Simple.logDebug(methodName, msg); JNDI_Simple.logDebug(methodName, "EXITING"); } public void objectRemoved(NamingEvent evt) { final String methodName = "objectRemoved"; JNDI_Simple.logDebug(methodName, "ENTERING"); String oldBindingName = ""; String newBindingName = ""; if (evt.getOldBinding() != null) { oldBindingName = evt.getOldBinding().getName(); } if (evt.getNewBinding() != null) { newBindingName = evt.getNewBinding().getName(); } String msg = String.format( "[OLD_BINDING's NAME: '%s']\t[NEW_BINDING's NAME: '%s']", oldBindingName, newBindingName); JNDI_Simple.logDebug(methodName, msg); JNDI_Simple.logDebug(methodName, "EXITING"); } public void objectRenamed(NamingEvent evt) { final String methodName = "objectRenamed"; JNDI_Simple.logDebug(methodName, "ENTERING"); String oldBindingName = ""; String newBindingName = ""; if (evt.getOldBinding() != null) { oldBindingName = evt.getOldBinding().getName(); } if (evt.getNewBinding() != null) { newBindingName = evt.getNewBinding().getName(); } String msg = String.format( "[OLD_BINDING's NAME: '%s']\t[NEW_BINDING's NAME: '%s']", oldBindingName, newBindingName); JNDI_Simple.logDebug(methodName, msg); JNDI_Simple.logDebug(methodName, "EXITING"); } private static EventDirContext LDAP_CONTEXT = null; // MODIFY ME private static String CONTEXT = "dc=example"; private static String SANDBOX_CONTEXT = "cn=sandbox"; private static BufferedReader STDIN = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) { final String methodName = "main"; try { JNDI_Simple myListener = new JNDI_Simple(); EventDirContext LDAP_CONTEXT = null; Hashtable ldapEnvironmentHashtable = new Hashtable(); ldapEnvironmentHashtable.put( javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // MODIFY ME ldapEnvironmentHashtable.put( javax.naming.Context.PROVIDER_URL, "ldap://" + "localhost" + ":389"); ldapEnvironmentHashtable.put( javax.naming.Context.SECURITY_AUTHENTICATION, "simple"); // MODIFY ME ldapEnvironmentHashtable.put( javax.naming.Context.SECURITY_PRINCIPAL, "cn=Directory Manager"); // MODIFY ME ldapEnvironmentHashtable.put( javax.naming.Context.SECURITY_CREDENTIALS, "password"); // Establish a context to LDAP. LDAP_CONTEXT = (EventDirContext) new InitialDirContext( ldapEnvironmentHashtable).lookup(CONTEXT); // MODIFY ME, COMMENT ME OUT TO TRIGGER CLOSE_WAIT on 389 DS. Thread.sleep(6000); logDebug(methodName, String.format("%s: '%s'", "javax.naming.Context.PROVIDER_URL", ldapEnvironmentHashtable.get( javax.naming.Context.PROVIDER_URL))); // Add a listener logDebug(methodName, "About to add a listener"); //JNDI_Simple.pause(); LDAP_CONTEXT.addNamingListener( SANDBOX_CONTEXT, EventContext.SUBTREE_SCOPE, myListener); // Create sandbox context logDebug(methodName, "About to create sandbox context"); //JNDI_Simple.pause(); LDAP_CONTEXT.createSubcontext(SANDBOX_CONTEXT); // Create some new entries in the sandbox. logDebug(methodName, "About to add new entries to sandbox context"); //JNDI_Simple.pause(); LDAP_CONTEXT.createSubcontext("cn=test1," + SANDBOX_CONTEXT); LDAP_CONTEXT.createSubcontext("cn=test2," + SANDBOX_CONTEXT); LDAP_CONTEXT.createSubcontext("cn=test3," + SANDBOX_CONTEXT); LDAP_CONTEXT.createSubcontext("cn=test4," + SANDBOX_CONTEXT); // We destroy new entries from the sandbox logDebug(methodName, "About to remove all new entries from sandbox context"); //JNDI_Simple.pause(); LDAP_CONTEXT.destroySubcontext("cn=test1," + SANDBOX_CONTEXT); LDAP_CONTEXT.destroySubcontext("cn=test2," + SANDBOX_CONTEXT); LDAP_CONTEXT.destroySubcontext("cn=test3," + SANDBOX_CONTEXT); LDAP_CONTEXT.destroySubcontext("cn=test4," + SANDBOX_CONTEXT); // destroy sandbox context logDebug(methodName, "About to destroy sandbox context"); //JNDI_Simple.pause(); LDAP_CONTEXT.destroySubcontext(SANDBOX_CONTEXT); // Remove listener logDebug(methodName, "About to remove listener"); //JNDI_Simple.pause(); LDAP_CONTEXT.removeNamingListener(myListener); logDebug(methodName, "About to close connection"); //JNDI_Simple.pause(); LDAP_CONTEXT.close(); logDebug(methodName, "Closed connection"); //JNDI_Simple.pause(); } catch (NamingException ne) { logDebug(methodName, ne.toString()); ne.printStackTrace(); } catch (Throwable th) { logDebug(methodName, th.toString()); th.printStackTrace(); } } private static void pause() throws Throwable { logDebug("pause", "Press any key to continue..."); STDIN.readLine(); } public static void logDebug(String methodName, String msg) { System.out.println(String.format( "%s - %s - %s", "JNDI_SIMPLE_TEST", methodName, msg)); } }