commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r385190 - in /jakarta/commons/proper/dbcp/trunk: ./ src/java/org/apache/commons/dbcp/datasources/ src/test/org/apache/commons/dbcp/
Date Sat, 11 Mar 2006 23:10:17 GMT
Author: psteitz
Date: Sat Mar 11 15:10:15 2006
New Revision: 385190

URL: http://svn.apache.org/viewcvs?rev=385190&view=rev
Log:
Changed getReference method in InstanceKeyDataSource to return a concrete
factory and added implementations of getReference in concrete subclasses.
Addresses BZ #38073
Fix contributed by Sandy McArthur.
Bug reported and test case contributed by Thomas Fischer.


Added:
    jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestJndi.java   (with
props)
Modified:
    jakarta/commons/proper/dbcp/trunk/project.xml
    jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java
    jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/PerUserPoolDataSource.java
    jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/SharedPoolDataSource.java

Modified: jakarta/commons/proper/dbcp/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/dbcp/trunk/project.xml?rev=385190&r1=385189&r2=385190&view=diff
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/project.xml (original)
+++ jakarta/commons/proper/dbcp/trunk/project.xml Sat Mar 11 15:10:15 2006
@@ -196,6 +196,7 @@
       <artifactId>jdbc-stdext</artifactId>
       <version>2.0</version>
     </dependency>
+   
 
     <dependency>
       <groupId>junit</groupId>
@@ -219,6 +220,39 @@
       <version>2.0.2</version>
     </dependency>
     <!-- /these two are required by maven -->
+    
+    <!-- tomcat naming jars for jndi reference tests -->
+    <dependency>
+      <groupId>tomcat</groupId>
+      <artifactId>naming-common</artifactId>
+      <version>5.0.28</version>
+      <properties>
+        <scope>test</scope>
+        <comment>Only required for jndi reference tests</comment>
+      </properties>
+    </dependency>
+    
+    <dependency>
+      <groupId>tomcat</groupId>
+      <artifactId>naming-java</artifactId>
+      <version>5.0.28</version>
+      <properties>
+        <scope>test</scope>
+        <comment>Only required for jndi reference tests</comment>
+      </properties>
+    </dependency>
+    
+    <!-- dragged in by tomcat-naming jars - only for jndi tests -->
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+       <version>1.0.3</version>
+       <url>http://jakarta.apache.org/commons/logging</url>
+       <properties>
+         <scope>test</scope>
+         <comment>Only required for jndi reference tests</comment>
+      </properties>
+    </dependency>
   
   </dependencies>
 
@@ -241,7 +275,8 @@
         <include>org/apache/commons/dbcp/TestBasicDataSource.java</include>
         <include>org/apache/commons/dbcp/TestAbandonedBasicDataSource.java</include>
         <include>org/apache/commons/dbcp/TestPStmtPoolingBasicDataSource.java</include>
-
+        <include>org/apache/commons/dbcp/TestJndi.java</include>
+        
         <include>org/apache/commons/dbcp/datasources/TestFactory.java</include>
         <include>org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java</include>
         <include>org/apache/commons/dbcp/datasources/TestSharedPoolDataSource.java</include>

Modified: jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java?rev=385190&r1=385189&r2=385190&view=diff
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java
(original)
+++ jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java
Sat Mar 11 15:10:15 2006
@@ -742,12 +742,23 @@
     // Referenceable implementation 
 
     /**
-     * <CODE>Referenceable</CODE> implementation prepares object for
-     * binding in jndi.
+     * Retrieves the Reference of this object.
+     * <strong>Note:</strong> <code>InstanceKeyDataSource</code>
subclasses
+     * should override this method. The implementaion included below
+     * is not robust and will be removed at the next major version DBCP
+     * release.
+     *
+     * @return The non-null Reference of this object.
+     * @exception NamingException If a naming exception was encountered
+     *		while retrieving the reference.
      */
+    // TODO: Remove the implementation of this method at next major
+    // version release.
+    
     public Reference getReference() throws NamingException {
-        Reference ref = new Reference(getClass().getName(), 
-            InstanceKeyObjectFactory.class.getName(), null);
+        final String className = getClass().getName();
+        final String factoryName = className + "Factory"; // XXX: not robust 
+        Reference ref = new Reference(className, factoryName, null);
         ref.add(new StringRefAddr("instanceKey", instanceKey));
         return ref;
     }

Modified: jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/PerUserPoolDataSource.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/PerUserPoolDataSource.java?rev=385190&r1=385189&r2=385190&view=diff
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/PerUserPoolDataSource.java
(original)
+++ jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/PerUserPoolDataSource.java
Sat Mar 11 15:10:15 2006
@@ -25,6 +25,8 @@
 import java.util.Map;
 
 import javax.naming.NamingException;
+import javax.naming.Reference;
+import javax.naming.StringRefAddr;
 import javax.sql.ConnectionPoolDataSource;
 
 import org.apache.commons.pool.ObjectPool;
@@ -417,6 +419,13 @@
             con.setTransactionIsolation(defaultTransactionIsolation);
         }
         con.setReadOnly(defaultReadOnly);
+    }
+
+    public Reference getReference() throws NamingException {
+        Reference ref = new Reference(getClass().getName(),
+                PerUserPoolDataSourceFactory.class.getName(), null);
+        ref.add(new StringRefAddr("instanceKey", instanceKey));
+        return ref;
     }
 
     private PoolKey getPoolKey(String username) {

Modified: jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/SharedPoolDataSource.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/SharedPoolDataSource.java?rev=385190&r1=385189&r2=385190&view=diff
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/SharedPoolDataSource.java
(original)
+++ jakarta/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/datasources/SharedPoolDataSource.java
Sat Mar 11 15:10:15 2006
@@ -23,6 +23,8 @@
 import java.util.Map;
 
 import javax.naming.NamingException;
+import javax.naming.Reference;
+import javax.naming.StringRefAddr;
 import javax.sql.ConnectionPoolDataSource;
 
 import org.apache.commons.collections.LRUMap;
@@ -172,6 +174,13 @@
         return info;
     }
 
+    public Reference getReference() throws NamingException {
+        Reference ref = new Reference(getClass().getName(),
+            SharedPoolDataSourceFactory.class.getName(), null);
+        ref.add(new StringRefAddr("instanceKey", instanceKey));
+        return ref;
+    }
+    
     private UserPassKey getUserPassKey(String username, String password) {
         UserPassKey key = (UserPassKey) userKeys.get(username);
         if (key == null) {

Added: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestJndi.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestJndi.java?rev=385190&view=auto
==============================================================================
--- jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestJndi.java (added)
+++ jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestJndi.java Sat Mar
11 15:10:15 2006
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.dbcp;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
+import org.apache.commons.dbcp.datasources.PerUserPoolDataSource;
+
+/**
+ * Tests JNID bind and lookup for DataSource implementations.
+ * Demonstrates problem indicated in BZ #38073.
+ *
+ */
+
+public class TestJndi extends TestCase {
+    /**
+     * The subcontext where the data source is bound.
+     */
+    protected static final String JNDI_SUBCONTEXT = "jdbc";
+
+    /**
+     * the full jndi path to the data source.
+     */
+    protected static final String JNDI_PATH = JNDI_SUBCONTEXT + "/"
+            + "jndiTestDataSource";
+    
+    /** jndi context to use in tests **/
+    protected Context context = null;
+
+    /**
+     * Creates a new instance.
+     */
+    public TestJndi(String name) {
+        super(name);
+    }
+
+    /**
+     * Test BasicDatsource bind and lookup
+     * 
+     * @throws Exception
+     */
+    public void testBasicDataSourceBind() throws Exception {
+        BasicDataSource dataSource = new BasicDataSource();
+        checkBind(dataSource);      
+    }
+    
+    /**
+     * Test SharedPoolDataSource bind and lookup
+     * 
+     * @throws Exception
+     */
+    public void testSharedPoolDataSourceBind() throws Exception {
+        SharedPoolDataSource dataSource = new SharedPoolDataSource();
+        checkBind(dataSource);      
+    }
+    
+    /**
+     * Test PerUserPoolDataSource bind and lookup
+     * 
+     * @throws Exception
+     */
+    public void testPerUserPoolDataSourceBind() throws Exception {
+        PerUserPoolDataSource dataSource = new PerUserPoolDataSource();
+        checkBind(dataSource);      
+    }
+    
+    public void setUp() throws Exception {
+        context = getInitialContext();
+        context.createSubcontext(JNDI_SUBCONTEXT);  
+    }
+    
+    public void tearDown() throws Exception {
+        context.unbind(JNDI_PATH);
+        context.destroySubcontext(JNDI_SUBCONTEXT);    
+    }
+    
+    /**
+     * Binds a DataSource to the jndi and checks that we have successfully 
+     * bound it by looking it up again.
+     * 
+     * @throws Exception if the bind, lookup or connect fails
+     */
+    protected void checkBind(DataSource dataSource) throws Exception {
+        bindDataSource(dataSource);
+        retrieveDataSource();
+    }
+
+    /**
+     * Binds a DataSource into jndi.
+     * 
+     * @throws Exception if creation or binding fails.
+     */
+    protected void bindDataSource(DataSource dataSource) throws Exception {
+        context.bind(JNDI_PATH, dataSource);
+    }
+
+    /**
+     * Retrieves a DataSource from jndi.
+     * 
+     * @throws Exception if the jndi lookup fails or no DataSource is bound.
+     */
+    protected DataSource retrieveDataSource() throws Exception {
+        Context context = getInitialContext();
+        DataSource dataSource = (DataSource) context.lookup(JNDI_PATH);
+
+        if (dataSource == null) {
+            fail("DataSource should not be null");
+        }
+        return dataSource;
+    }
+
+    /**
+     * Retrieves (or creates if it does not exist) an InitialContext.
+     * 
+     * @return the InitialContext.
+     * @throws NamingException if the InitialContext cannot be retrieved 
+     *         or created.
+     */
+    protected InitialContext getInitialContext() throws NamingException {
+        Hashtable environment = new Hashtable();
+        environment.put(Context.INITIAL_CONTEXT_FACTORY,
+                org.apache.naming.java.javaURLContextFactory.class.getName());
+        InitialContext context = new InitialContext(environment);
+        return context;
+    }
+}

Propchange: jakarta/commons/proper/dbcp/trunk/src/test/org/apache/commons/dbcp/TestJndi.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message