directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r159351 - in directory/naming/trunk/naming-config/src: java/org/apache/naming/config/Config.java java/org/apache/naming/config/XmlConfigurator.java test/org/apache/naming/config/XmlConfiguratorTest.java test/test-jndi1.xml test/test-jndi3.xml
Date Tue, 29 Mar 2005 05:52:29 GMT
Author: psteitz
Date: Mon Mar 28 21:52:27 2005
New Revision: 159351

URL: http://svn.apache.org/viewcvs?view=rev&rev=159351
Log:
Added support for links in XmlConfigurator, per DIRNAMING-16.
Links include local names, names of external contexts and names relative
to external context. XmlConfigurator binds links using local names to LinkRefs
that are handled correctly by NamingContext (no change required).
Initial tests verify links to external in-memory contexts work.

Added:
    directory/naming/trunk/naming-config/src/test/test-jndi3.xml
Modified:
    directory/naming/trunk/naming-config/src/java/org/apache/naming/config/Config.java
    directory/naming/trunk/naming-config/src/java/org/apache/naming/config/XmlConfigurator.java
    directory/naming/trunk/naming-config/src/test/org/apache/naming/config/XmlConfiguratorTest.java
    directory/naming/trunk/naming-config/src/test/test-jndi1.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=159350&r2=159351
==============================================================================
--- 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 Mon
Mar 28 21:52:27 2005
@@ -26,7 +26,8 @@
 import java.util.TreeSet;
 
 import javax.naming.CompositeName;
-import javax.naming.InvalidNameException;
+import javax.naming.InvalidNameException;
+import javax.naming.LinkRef;
 import javax.naming.StringRefAddr;
 
 import org.apache.commons.collections.map.UnmodifiableMap;
@@ -119,7 +120,8 @@
         private String base;
         
         private final Collection environmentList = new LinkedList();
-        private final Collection resourceList = new LinkedList();
+        private final Collection resourceList = new LinkedList();
+        private final Collection linkList = new LinkedList();
         
         /**
          * Adds an Environment configuration to the environment list.
@@ -175,6 +177,16 @@
             resourceList.add(resource);
         }
         
+        /**
+         * Adds a Link configuration to the link list.
+         * 
+         * @param link link configuration to add.
+         */
+        public void addLink(Link link)
+        {
+            linkList.add(link);
+        }
+        
         
         /**
          * Returns the environment list.
@@ -214,6 +226,16 @@
         public Collection getResourceList()
         {
             return Collections.unmodifiableCollection(resourceList);
+        }
+        
+        /**
+         * Returns the link list.
+         * 
+         * @return list of Link configurations in the Context.
+         */
+        public Collection getLinkList()
+        {
+            return Collections.unmodifiableCollection(linkList);
         }
         
         /**
@@ -493,6 +515,72 @@
             .append("parameters", parameters)
             .toString();
         }
+    }
+    
+    /**
+     * Configuration for a link.
+     * <code>name</code> property is the (local) jndi name of the link.
+     * <code>rname</code> property is the link content, a jndi URL referencing
+     * an entry in an external jndi context.
+     */
+    public static final class Link {
+        private String name;
+        private String rname;
+        private String context;
+        /**
+         * @return Returns the (local) name for the link.
+         */
+        public String getName() {
+            return name;
+        }
+
+        /**
+         * @param name The name to set.
+         */
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        /**
+         * @return Returns the link content, a jndi URL pointing to an
+         * entry in an external naming context
+         */
+        public String getRname() {
+            return rname;
+        }
+
+        /**
+         * @param rname The rname to set.
+         */
+        public void setRname(String rname) {
+            this.rname = rname;
+        }
+        
+        /**
+         * Creates a LinkRef with a jndi 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".
+         * 
+         * @return object instance
+         */
+        public Object createValue() {
+            return new LinkRef("jndi:"+ context + "/" + rname);
+        }
+
+        /**
+         * @return Returns the name of the external context.
+         */
+        public String getContext() {
+            return context;
+        }
+
+        /**
+         * @param context sets the external context name.
+         */
+        public void setContext(String context) {
+            this.context = context;
+        }
+
     }
 }
 

Modified: directory/naming/trunk/naming-config/src/java/org/apache/naming/config/XmlConfigurator.java
URL: http://svn.apache.org/viewcvs/directory/naming/trunk/naming-config/src/java/org/apache/naming/config/XmlConfigurator.java?view=diff&r1=159350&r2=159351
==============================================================================
--- directory/naming/trunk/naming-config/src/java/org/apache/naming/config/XmlConfigurator.java
(original)
+++ directory/naming/trunk/naming-config/src/java/org/apache/naming/config/XmlConfigurator.java
Mon Mar 28 21:52:27 2005
@@ -63,6 +63,7 @@
     private static final String CONTEXT_ELEMENT = ROOT_ELEMENT + "/context";
     private static final String ENV_ELEMENT = CONTEXT_ELEMENT + "/environment";
     private static final String RES_ELEMENT = CONTEXT_ELEMENT + "/resource";
+    private static final String LINK_ELEMENT = CONTEXT_ELEMENT + "/link";
     private static final String RES_PARAM_ELEMENT = RES_ELEMENT + "/parameter";
     private static Context envContext = null;
 
@@ -130,6 +131,9 @@
         digester.addObjectCreate(ENV_ELEMENT, Config.Environment.class);
         digester.addSetProperties(ENV_ELEMENT);
         digester.addSetNext(ENV_ELEMENT, "addEnvironment");
+        digester.addObjectCreate(LINK_ELEMENT, Config.Link.class);
+        digester.addSetProperties(LINK_ELEMENT);
+        digester.addSetNext(LINK_ELEMENT, "addLink");
         digester.addObjectCreate(RES_ELEMENT, Config.Resource.class);
         digester.addSetProperties(RES_ELEMENT);
         digester.addSetNext(RES_ELEMENT, "addResource");
@@ -225,6 +229,11 @@
             for (Iterator j = ctx.getResourceList().iterator(); j.hasNext();) {
                 Config.Resource r = (Config.Resource) j.next();
                 jndiCtx.rebind(r.getName(), r.createValue());
+            }
+            
+            for (Iterator j = ctx.getLinkList().iterator(); j.hasNext();) {
+                Config.Link l = (Config.Link) j.next();
+                jndiCtx.rebind(l.getName(), l.createValue());
             }
         }
     }

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=159350&r2=159351
==============================================================================
--- 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
Mon Mar 28 21:52:27 2005
@@ -171,6 +171,32 @@
     protected void checkJdbc(String root, Context ctx) throws Exception {
         Context env = (Context) ctx.lookup(root);
         DataSource ds = (DataSource) env.lookup("jdbc/pool");
+        checkDs(ds);
+    }
+    
+    public void testLocalLinks() 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(Context.URL_PKG_PREFIXES, "org.apache.naming");
+        env.put(NamingContextFactory.NAME, "global"); //name attribute 
+        checkJdbc(DEFAULT_ROOT, new InitialContext(env));
+        
+        env.put(NamingContextFactory.NAME, "app1");
+        Context ctx = new InitialContext(env);
+        Context envCtx = (Context) ctx.lookup("java:comp/env");
+        Integer port = (Integer) envCtx.lookup("port");
+        // follow link
+        DataSource ds = (DataSource) envCtx.lookup("datasource"); 
+        checkDs(ds);
+        // resolve across link to other context
+        checkDs((DataSource) ctx.lookup("java:comp/env/datasource")); 
+        // use jndi url explicitly
+        checkDs((DataSource) ctx.lookup("jndi:global/java:comp/env/jdbc/pool")); 
+    }
+    
+    protected void checkDs(DataSource ds) throws Exception {
         Connection con = null;
         Statement stat = null; 
         ResultSet rs = null;
@@ -182,7 +208,7 @@
             stat.executeUpdate("INSERT INTO DUAL VALUES(1)");
             rs = stat.executeQuery("SELECT * FROM DUAL");
             while (rs.next()) {
-               assertEquals("Check you get back what you put into the DB", 1, rs.getInt(1));
+                assertEquals("Check you get back what you put into the DB", 1, rs.getInt(1));
             }
         }
         finally {
@@ -195,7 +221,7 @@
             if (con != null) { 
                 con.close(); 
             }
-        }
+        }      
     }
 }
 

Modified: directory/naming/trunk/naming-config/src/test/test-jndi1.xml
URL: http://svn.apache.org/viewcvs/directory/naming/trunk/naming-config/src/test/test-jndi1.xml?view=diff&r1=159350&r2=159351
==============================================================================
--- directory/naming/trunk/naming-config/src/test/test-jndi1.xml (original)
+++ directory/naming/trunk/naming-config/src/test/test-jndi1.xml Mon Mar 28 21:52:27 2005
@@ -1,7 +1,7 @@
 <naming>
   <!-- 
        Anonymous context with base specified, different environment values
-       from test-jndi2.  Loading after jnti2 should overwrite environment entries.
+       from test-jndi2.  Loading after jndi2 should overwrite environment entries.
    -->
   <context base="alt/root/context">
     <environment name="config/host" value="www.jakarta.org" type="java.lang.String" />

Added: 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=auto&rev=159351
==============================================================================
--- directory/naming/trunk/naming-config/src/test/test-jndi3.xml (added)
+++ directory/naming/trunk/naming-config/src/test/test-jndi3.xml Mon Mar 28 21:52:27 2005
@@ -0,0 +1,26 @@
+<naming>
+  <context name="global">
+    <resource name="jdbc/pool" type="javax.sql.DataSource">
+      <parameter>
+        <name>driverClassName</name>
+        <value>org.hsqldb.jdbcDriver</value>
+      </parameter>
+      <parameter>
+        <name>url</name>
+        <value>jdbc:hsqldb:target/hsqldb</value>
+      </parameter>
+      <parameter>
+        <name>username</name>
+        <value>sa</value>
+      </parameter>
+      <parameter>
+        <name>password</name>
+        <value></value>
+      </parameter>
+    </resource>
+  </context>
+  <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" />
+  </context>
+</naming>



Mime
View raw message