You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ps...@apache.org on 2005/04/11 08:01:53 UTC
svn commit: r160838 - in directory/naming/trunk/naming-config/src:
java/org/apache/naming/config/Config.java
test/org/apache/naming/config/XmlConfiguratorTest.java test/test-jndi3.xml
Author: psteitz
Date: Sun Apr 10 23:01:52 2005
New Revision: 160838
URL: http://svn.apache.org/viewcvs?view=rev&rev=160838
Log:
Added support and tests for ldap links.
Modified:
directory/naming/trunk/naming-config/src/java/org/apache/naming/config/Config.java
directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java
directory/naming/trunk/naming-config/src/test/test-jndi3.xml
Modified: directory/naming/trunk/naming-config/src/java/org/apache/naming/config/Config.java
URL: http://svn.apache.org/viewcvs/directory/naming/trunk/naming-config/src/java/org/apache/naming/config/Config.java?view=diff&r1=160837&r2=160838
==============================================================================
--- directory/naming/trunk/naming-config/src/java/org/apache/naming/config/Config.java (original)
+++ directory/naming/trunk/naming-config/src/java/org/apache/naming/config/Config.java Sun Apr 10 23:01:52 2005
@@ -557,14 +557,19 @@
}
/**
- * Creates a LinkRef with a jndi URL as content, based on the external
+ * Creates a LinkRef with a jndi or ldap URL as content, based on the external
* context name and the name of the entry in the external context.
- * Link content is of the form "jndi:context/rname".
+ * Link content is of the form "jndi:context/rname" if context is not null;
+ * otherwise just rname is passed to LinkRef constructor.
*
* @return object instance
*/
public Object createValue() {
- return new LinkRef("jndi:"+ context + "/" + rname);
+ String linkRef = rname;
+ if (context != null && context.length() > 0) {
+ linkRef="jndi:"+ context + "/" + rname;
+ }
+ return new LinkRef(linkRef);
}
/**
Modified: directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java
URL: http://svn.apache.org/viewcvs/directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java?view=diff&r1=160837&r2=160838
==============================================================================
--- directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java (original)
+++ directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java Sun Apr 10 23:01:52 2005
@@ -26,12 +26,16 @@
import javax.naming.Context;
import javax.naming.InitialContext;
-import javax.mail.Session;
+import javax.naming.directory.Attributes;
+import javax.naming.ldap.LdapContext;
+
import javax.mail.Message;
-import javax.mail.internet.MimePartDataSource;
+import javax.mail.Session;
+import javax.mail.Transport;
+
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
-import javax.mail.Transport;
+import javax.mail.internet.MimePartDataSource;
import javax.sql.DataSource;
@@ -202,6 +206,66 @@
checkDs((DataSource) ctx.lookup("java:comp/env/datasource"));
// use jndi url explicitly
checkDs((DataSource) ctx.lookup("jndi:global/java:comp/env/jdbc/pool"));
+ }
+
+ /**
+ * Tests ldap links. To activate, change the name to "testLdapLinks" and
+ *
+ * 1. Change the rname in the following line from /test-jndi3.xml to a valid
+ * ldap url pointing to a live server which will accept the connection
+ *
+ * <link name="fooCo" rname="ldap://ldap.fooco.com" />
+ *
+ * 2. Change the value of lookupString so that rname + lookupString names
+ * an entry in the directory
+ *
+ * 3. Change the value of mail to match the value of the mail attribute of
+ * the entry named by rname + lookupString.
+ */
+ public void tstLdapLinks() throws Exception {
+ XmlConfigurator.loadConfiguration(getClass().getResourceAsStream("/test-jndi3.xml"));
+ Hashtable env = new Hashtable();
+ env.put(Context.INITIAL_CONTEXT_FACTORY,
+ "org.apache.naming.NamingContextFactory");
+ env.put(NamingContextFactory.NAME, "app1");
+ Context ctx = new InitialContext(env);
+ Context envCtx = (Context) ctx.lookup("java:comp/env");
+
+ // local (link) name of the ldap context. This is the name attribute
+ // of the link in the config
+ String name = "fooCo";
+
+ // name of the ldap entry to look up, relative to the ldap url provided
+ // in the rname of the config
+ String lookupString = "uid=jbgoode,ou=People,dc=fooco,dc=com";
+
+ // Value of mail attribute of target entry.
+ String mail = "jbgoode@fooco.com";
+
+ // follow link to ldap URL
+ Context ldapContext = (Context) envCtx.lookup(name);
+ LdapContext entryCtx = (LdapContext)
+ ldapContext.lookup(lookupString);
+ Attributes attributes = (Attributes) entryCtx.getAttributes("");
+
+ /* Remove comment to dump entry
+ NamingEnumeration enum = attributes.getAll();
+ while (enum.hasMore()) {
+ System.out.println(enum.next());
+ } */
+
+ assertTrue(attributes.get("mail").contains(mail));
+ ldapContext.close();
+
+ // Now try through a jndi url
+ ldapContext = (Context) new InitialContext().lookup
+ ("jndi:app1/java:comp/env/" + name);
+ entryCtx = (LdapContext)
+ ldapContext.lookup(lookupString);
+ attributes = (Attributes) entryCtx.getAttributes("");
+ assertTrue(attributes.get("mail").contains(mail));
+ ldapContext.close();
+
}
protected void checkDs(DataSource ds) throws Exception {
Modified: directory/naming/trunk/naming-config/src/test/test-jndi3.xml
URL: http://svn.apache.org/viewcvs/directory/naming/trunk/naming-config/src/test/test-jndi3.xml?view=diff&r1=160837&r2=160838
==============================================================================
--- directory/naming/trunk/naming-config/src/test/test-jndi3.xml (original)
+++ directory/naming/trunk/naming-config/src/test/test-jndi3.xml Sun Apr 10 23:01:52 2005
@@ -22,5 +22,11 @@
<context name="app1" base="java:comp/env">
<environment name="port" value="5555" type="java.lang.Integer" />
<link name="datasource" context="global" rname="java:comp/env/jdbc/pool" />
+ <!--
+ If the ldap link test is activated, rname below needs to be a valid
+ ldap url. Lookups from this context, starting with name will link to
+ the namespace rooted at the value of the ldap url supplied in rname
+ -->
+ <link name="fooCo" rname="ldap://ldap.fooco.com" />
</context>
</naming>