You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2009/12/06 23:33:34 UTC

svn commit: r887787 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/conf/ main/java/org/apache/cayenne/configuration/ test/java/org/apache/cayenne/configuration/ test/resources/org/apache/cayenne/configur...

Author: aadamchik
Date: Sun Dec  6 22:33:33 2009
New Revision: 887787

URL: http://svn.apache.org/viewvc?rev=887787&view=rev
Log:
CAY-1319 Switch Cayenne configuration loading to cayenne-di container

* DBCPDataSourceFactory

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DBCPDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DBCPDataSourceFactoryTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP.properties
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP_legacy.properties
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/DBCPDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderAction.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/DBCPDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/DBCPDataSourceFactory.java?rev=887787&r1=887786&r2=887787&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/DBCPDataSourceFactory.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/DBCPDataSourceFactory.java Sun Dec  6 22:33:33 2009
@@ -36,6 +36,7 @@
  * list of supported properties.
  * 
  * @since 1.2
+ * @deprecated since 3.1 replaced with {@link org.apache.cayenne.configuration.DBCPDataSourceFactory}
  */
 public class DBCPDataSourceFactory implements DataSourceFactory {
 

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DBCPDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DBCPDataSourceFactory.java?rev=887787&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DBCPDataSourceFactory.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DBCPDataSourceFactory.java Sun Dec  6 22:33:33 2009
@@ -0,0 +1,108 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.cayenne.configuration;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+import java.util.Map.Entry;
+
+import javax.sql.DataSource;
+
+import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.di.Inject;
+import org.apache.cayenne.resource.Resource;
+import org.apache.cayenne.resource.ResourceLocator;
+import org.apache.commons.dbcp.BasicDataSourceFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * A {@link DataSourceFactory} based on DBCP connection pool library.
+ * 
+ * @since 3.1
+ */
+public class DBCPDataSourceFactory implements DataSourceFactory {
+
+    private static final Log logger = LogFactory.getLog(DBCPDataSourceFactory.class);
+
+    @Inject
+    protected ResourceLocator resourceLocator;
+
+    public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
+
+        Resource dbcpConfiguration = nodeDescriptor.getConfigurationResource();
+        if (dbcpConfiguration == null) {
+            throw new CayenneRuntimeException(
+                    "Null 'configurationResource' for nodeDescriptor '%s'",
+                    nodeDescriptor.getName());
+        }
+
+        Properties properties = getProperties(dbcpConfiguration);
+        if (logger.isDebugEnabled()) {
+            logger.debug("DBCP Properties: " + properties);
+        }
+
+        properties = filteredDeprecatedProperties(properties);
+        return BasicDataSourceFactory.createDataSource(properties);
+    }
+
+    /**
+     * Converts old-style cayene.dbcp.xyz properties to just cayenne.dbcp.
+     */
+    private Properties filteredDeprecatedProperties(Properties unfiltered) {
+        Properties properties = new Properties();
+
+        final String deprecatedPrefix = "cayenne.dbcp.";
+
+        for (Entry<Object, Object> entry : unfiltered.entrySet()) {
+            Object key = entry.getKey();
+            if (key instanceof String && key.toString().startsWith(deprecatedPrefix)) {
+
+                String oldKey = key.toString();
+                key = oldKey.substring(deprecatedPrefix.length());
+                logger.info("Deprecated use of 'cayenne.dbcp.' prefix in '"
+                        + oldKey
+                        + "', converting to "
+                        + key);
+            }
+
+            properties.put(key, entry.getValue());
+        }
+
+        return properties;
+    }
+
+    private Properties getProperties(Resource dbcpConfiguration) throws IOException {
+        Properties properties = new Properties();
+        InputStream in = dbcpConfiguration.getURL().openStream();
+        try {
+            properties.load(in);
+        }
+        finally {
+            try {
+                in.close();
+            }
+            catch (IOException e) {
+            }
+        }
+
+        return properties;
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java?rev=887787&r1=887786&r2=887787&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/JNDIDataSourceFactory.java Sun Dec  6 22:33:33 2009
@@ -47,7 +47,7 @@
         }
 
         try {
-            return loadViaJNDI(location);
+            return lookupViaJNDI(location);
         }
         catch (Exception ex) {
             logger.info("failed JNDI lookup of DataSource location '" + location + "'");
@@ -56,7 +56,7 @@
         }
     }
 
-    DataSource loadViaJNDI(String location) throws NamingException {
+    DataSource lookupViaJNDI(String location) throws NamingException {
         QueryLogger.logConnect(location);
 
         Context context = new InitialContext();

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderAction.java?rev=887787&r1=887786&r2=887787&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderAction.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/XMLDataChannelDescriptorLoaderAction.java Sun Dec  6 22:33:33 2009
@@ -177,7 +177,7 @@
                         "",
                         "schema-update-strategy"));
 
-                // this may be bogus for nodes other than driver nodes, but here we can't
+                // this may be bogus for some nodes, such as JNDI, but here we can't
                 // tell for sure
                 if (location != null) {
                     nodeDescriptor.setConfigurationResource(descriptor

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DBCPDataSourceFactoryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DBCPDataSourceFactoryTest.java?rev=887787&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DBCPDataSourceFactoryTest.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DBCPDataSourceFactoryTest.java Sun Dec  6 22:33:33 2009
@@ -0,0 +1,110 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.cayenne.configuration;
+
+import java.io.IOException;
+import java.net.URL;
+
+import javax.sql.DataSource;
+
+import junit.framework.TestCase;
+
+import org.apache.cayenne.resource.URLResource;
+import org.apache.commons.dbcp.BasicDataSource;
+
+public class DBCPDataSourceFactoryTest extends TestCase {
+
+    public void testGetDataSource() throws Exception {
+
+        String baseUrl = getClass().getPackage().getName().replace('.', '/');
+        URL url = getClass().getClassLoader().getResource(
+                baseUrl + "/testDBCP.properties");
+        assertNotNull(url);
+
+        DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
+        nodeDescriptor.setConfigurationResource(new URLResource(url));
+
+        DBCPDataSourceFactory factory = new DBCPDataSourceFactory();
+        DataSource dataSource = factory.getDataSource(nodeDescriptor);
+        assertNotNull(dataSource);
+
+        assertTrue(dataSource instanceof BasicDataSource);
+        BasicDataSource basicDataSource = (BasicDataSource) dataSource;
+        assertEquals("com.example.jdbc.Driver", basicDataSource.getDriverClassName());
+        assertEquals("jdbc:somedb://localhost/cayenne", basicDataSource.getUrl());
+        assertEquals("john", basicDataSource.getUsername());
+        assertEquals("secret", basicDataSource.getPassword());
+        assertEquals(20, basicDataSource.getMaxActive());
+        assertEquals(5, basicDataSource.getMinIdle());
+        assertEquals(8, basicDataSource.getMaxIdle());
+        assertEquals(10000, basicDataSource.getMaxWait());
+        assertEquals("select 1 from xyz;", basicDataSource.getValidationQuery());
+    }
+    
+    public void testGetDataSource_LegacyConfig() throws Exception {
+
+        String baseUrl = getClass().getPackage().getName().replace('.', '/');
+        URL url = getClass().getClassLoader().getResource(
+                baseUrl + "/testDBCP_legacy.properties");
+        assertNotNull(url);
+
+        DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
+        nodeDescriptor.setConfigurationResource(new URLResource(url));
+
+        DBCPDataSourceFactory factory = new DBCPDataSourceFactory();
+        DataSource dataSource = factory.getDataSource(nodeDescriptor);
+        assertNotNull(dataSource);
+
+        assertTrue(dataSource instanceof BasicDataSource);
+        BasicDataSource basicDataSource = (BasicDataSource) dataSource;
+        assertEquals("com.example.jdbc.Driver", basicDataSource.getDriverClassName());
+        assertEquals("jdbc:somedb://localhost/cayenne", basicDataSource.getUrl());
+        assertEquals("john", basicDataSource.getUsername());
+        assertEquals("secret", basicDataSource.getPassword());
+        assertEquals(20, basicDataSource.getMaxActive());
+        assertEquals(5, basicDataSource.getMinIdle());
+        assertEquals(8, basicDataSource.getMaxIdle());
+        assertEquals(10000, basicDataSource.getMaxWait());
+        assertEquals("select 1 from xyz;", basicDataSource.getValidationQuery());
+    }
+
+    public void testGetDataSource_InvalidLocation() throws Exception {
+
+        String baseUrl = getClass().getPackage().getName().replace('.', '/');
+        URL url = getClass().getClassLoader().getResource(
+                baseUrl + "/testDBCP.properties");
+        assertNotNull(url);
+        url = new URL(url, ".nosuchfile");
+
+        DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
+        nodeDescriptor.setConfigurationResource(new URLResource(url));
+
+        DBCPDataSourceFactory factory = new DBCPDataSourceFactory();
+
+        try {
+            factory.getDataSource(nodeDescriptor);
+            fail("didn't throw on abscent config file");
+        }
+        catch (IOException ex) {
+            // expected
+        }
+
+    }
+
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP.properties
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP.properties?rev=887787&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP.properties (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP.properties Sun Dec  6 22:33:33 2009
@@ -0,0 +1,9 @@
+driverClassName=com.example.jdbc.Driver
+url=jdbc:somedb://localhost/cayenne
+username=john
+password=secret
+maxActive=20
+minIdle=5
+maxIdle=8
+maxWait=10000
+validationQuery=select 1 from xyz;
\ No newline at end of file

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP_legacy.properties
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP_legacy.properties?rev=887787&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP_legacy.properties (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/resources/org/apache/cayenne/configuration/testDBCP_legacy.properties Sun Dec  6 22:33:33 2009
@@ -0,0 +1,10 @@
+# DBCPDataSourceFactory properties for Cayenne < 3.1
+cayenne.dbcp.driverClassName=com.example.jdbc.Driver
+cayenne.dbcp.url=jdbc:somedb://localhost/cayenne
+cayenne.dbcp.username=john
+cayenne.dbcp.password=secret
+cayenne.dbcp.maxActive=20
+cayenne.dbcp.minIdle=5
+cayenne.dbcp.maxIdle=8
+cayenne.dbcp.maxWait=10000
+cayenne.dbcp.validationQuery=select 1 from xyz;
\ No newline at end of file