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 2010/02/28 14:00:20 UTC

svn commit: r917173 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/ framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/

Author: aadamchik
Date: Sun Feb 28 13:00:19 2010
New Revision: 917173

URL: http://svn.apache.org/viewvc?rev=917173&view=rev
Log:
CAY-1394 DI-based replacement for JNDI hack

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/PropertyDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/PropertyDataSourceFactoryTest.java
Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneServerModule.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoader.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoaderTest.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=917173&r1=917172&r2=917173&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Sun Feb 28 13:00:19 2010
@@ -31,6 +31,7 @@
 CAY-1374 Add parent dataMap property to Query interface
 CAY-1380 Support for Escaped LIKE Clauses in Expressions
 CAY-1393 Update velocity to 1.6.3
+CAY-1394 DI-based replacement for JNDI hack
 
 Bug Fixes Since 3.0:
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneServerModule.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneServerModule.java?rev=917173&r1=917172&r2=917173&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneServerModule.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/CayenneServerModule.java Sun Feb 28 13:00:19 2010
@@ -68,7 +68,7 @@
                 runtimeName);
 
         // configure known DbAdapter detectors in reverse order of popularity. Users can
-        // add their own for their own to install custom adapters automatically
+        // add their own to install custom adapters automatically
         binder
                 .bindList(DbAdapterFactory.class)
                 .add(new OpenBaseSniffer())
@@ -87,8 +87,8 @@
 
         binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class).in(
                 Scopes.SINGLETON);
-        binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class).in(
-                Scopes.SINGLETON);
+        binder.bind(ConfigurationNameMapper.class).to(
+                DefaultConfigurationNameMapper.class).in(Scopes.SINGLETON);
 
         // a service to provide the main stack DataDomain
         binder.bind(DataDomain.class).toProvider(DataDomainProvider.class).in(

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoader.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoader.java?rev=917173&r1=917172&r2=917173&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoader.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoader.java Sun Feb 28 13:00:19 2010
@@ -20,36 +20,104 @@
 
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.di.Inject;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
  * A {@link DataSourceFactoryLoader} that loads factories explicitly configured in the
  * {@link DataNodeDescriptor}. If the factory class is not explicitly configured, and the
  * descriptor has a configuration resource attached to it,
  * {@link XMLPoolingDataSourceFactory} is returned.
+ * <p>
+ * If the environment contains properties <em>cayenne.jdbc.url.domain_name.node_name</em>
+ * (or <em>cayenne.jdbc.url</em>) and <em>cayenne.jdbc.driver.domain_name.node_name</em>
+ * (or <em>cayenne.jdbc.driver</em>), any DataSourceFactory configured in the project is
+ * ignored, and the {@link PropertyDataSourceFactory} is returned.
  * 
  * @since 3.1
  */
 public class DefaultDataSourceFactoryLoader implements DataSourceFactoryLoader {
 
+    private static final Log logger = LogFactory
+            .getLog(DefaultDataSourceFactoryLoader.class);
+
     @Inject
     protected AdhocObjectFactory objectFactory;
 
     public DataSourceFactory getDataSourceFactory(DataNodeDescriptor nodeDescriptor) {
-        String typeName = nodeDescriptor.getDataSourceFactoryType();
+        String typeName = null;
+
+        if (shouldConfigureDataSourceFromProperties(nodeDescriptor)) {
+            typeName = PropertyDataSourceFactory.class.getName();
+        }
+        else {
+            typeName = nodeDescriptor.getDataSourceFactoryType();
+        }
 
         if (typeName == null) {
-            if (nodeDescriptor.getDataSourceDescriptor() != null) {
-                return objectFactory.newInstance(
-                        DataSourceFactory.class,
-                        XMLPoolingDataSourceFactory.class.getName());
-            }
-            else {
+            if (nodeDescriptor.getDataSourceDescriptor() == null) {
                 throw new CayenneRuntimeException(
                         "DataNodeDescriptor '%s' has null 'dataSourceFactoryType' and 'dataSourceDescriptor' properties",
                         nodeDescriptor.getName());
             }
+
+            typeName = XMLPoolingDataSourceFactory.class.getName();
         }
 
         return objectFactory.newInstance(DataSourceFactory.class, typeName);
     }
+
+    protected boolean shouldConfigureDataSourceFromProperties(
+            DataNodeDescriptor nodeDescriptor) {
+
+        String channelName = nodeDescriptor.getDataChannelDescriptor() != null
+                ? nodeDescriptor.getDataChannelDescriptor().getName()
+                : null;
+
+        String driver = getProperty(PropertyDataSourceFactory.JDBC_DRIVER_PROPERTY);
+
+        if (driver == null && channelName != null) {
+            driver = getProperty(PropertyDataSourceFactory.JDBC_DRIVER_PROPERTY
+                    + "."
+                    + nodeDescriptor.getDataChannelDescriptor().getName()
+                    + "."
+                    + nodeDescriptor.getName());
+        }
+
+        if (driver == null) {
+            return false;
+        }
+
+        String url = getProperty(PropertyDataSourceFactory.JDBC_URL_PROPERTY);
+
+        if (url == null && channelName != null) {
+            url = getProperty(PropertyDataSourceFactory.JDBC_URL_PROPERTY
+                    + "."
+                    + nodeDescriptor.getDataChannelDescriptor().getName()
+                    + "."
+                    + nodeDescriptor.getName());
+        }
+
+        if (url == null) {
+            return false;
+        }
+
+        logger
+                .info(String
+                        .format(
+                                "Found DataSourceFactory system property overrides for URL and Driver "
+                                        + "of '%s.%s' node. Will ignore project DataSource configuration.",
+                                channelName,
+                                nodeDescriptor.getName()));
+        return true;
+    }
+
+    /**
+     * Returns a property value for a given full key. This implementation returns a System
+     * property. Subclasses may lookup properties elsewhere. E.g. overriding this method
+     * can help with unit testing this class.
+     */
+    protected String getProperty(String key) {
+        return System.getProperty(key);
+    }
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/PropertyDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/PropertyDataSourceFactory.java?rev=917173&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/PropertyDataSourceFactory.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/PropertyDataSourceFactory.java Sun Feb 28 13:00:19 2010
@@ -0,0 +1,117 @@
+/*****************************************************************
+ *   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 javax.sql.DataSource;
+
+import org.apache.cayenne.ConfigurationException;
+import org.apache.cayenne.access.ConnectionLogger;
+import org.apache.cayenne.access.QueryLogger;
+import org.apache.cayenne.conn.PoolManager;
+
+/**
+ * A DataSourceFactrory that creates a DataSource based on system properties. Properties
+ * can be set per domain/node name or globally, aplying to all nodes without explicit
+ * property set. The following properties are supported:
+ * <ul>
+ * <li>cayenne.jdbc.driver[.domain_name.node_name]
+ * <li>cayenne.jdbc.url[.domain_name.node_name]
+ * <li>cayenne.adapter[.domain_name.node_name]
+ * <li>cayenne.jdbc.username[.domain_name.node_name]
+ * <li>cayenne.jdbc.password[.domain_name.node_name]
+ * <li>cayenne.jdbc.min.connections[.domain_name.node_name]
+ * <li>cayenne.jdbc.max.conections[.domain_name.node_name]
+ * </ul>
+ * At least url and driver properties must be specified for this factory to return a valid
+ * DataSource.
+ * 
+ * @since 3.1
+ */
+public class PropertyDataSourceFactory implements DataSourceFactory {
+
+    static final String JDBC_DRIVER_PROPERTY = "cayenne.jdbc.driver";
+    static final String JDBC_URL_PROPERTY = "cayenne.jdbc.url";
+    static final String JDBC_USERNAME_PROPERTY = "cayenne.jdbc.username";
+    static final String JDBC_PASSWORD_PROPERTY = "cayenne.jdbc.password";
+    static final String JDBC_MIN_CONNECTIONS_PROPERTY = "cayenne.jdbc.min.connections";
+    static final String JDBC_MAX_CONNECTIONS_PROPERTY = "cayenne.jdbc.max.conections";
+
+    public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
+
+        String suffix = "."
+                + nodeDescriptor.getDataChannelDescriptor().getName()
+                + "."
+                + nodeDescriptor.getName();
+
+        String driver = getProperty(JDBC_DRIVER_PROPERTY, suffix);
+        String url = getProperty(JDBC_URL_PROPERTY, suffix);
+        String username = getProperty(JDBC_USERNAME_PROPERTY, suffix);
+        String password = getProperty(JDBC_PASSWORD_PROPERTY, suffix);
+        int minConnections = getIntProperty(JDBC_MIN_CONNECTIONS_PROPERTY, suffix, 1);
+        int maxConnections = getIntProperty(JDBC_MAX_CONNECTIONS_PROPERTY, suffix, 1);
+
+        ConnectionLogger logger = new ConnectionLogger();
+        try {
+            return new PoolManager(
+                    driver,
+                    url,
+                    minConnections,
+                    maxConnections,
+                    username,
+                    password,
+                    logger);
+        }
+        catch (Exception e) {
+            QueryLogger.logConnectFailure(e);
+            throw e;
+        }
+    }
+
+    protected int getIntProperty(String propertyName, String suffix, int defaultValue) {
+        String string = getProperty(propertyName, suffix);
+
+        if (string == null) {
+            return defaultValue;
+        }
+
+        try {
+            return Integer.parseInt(string);
+        }
+        catch (NumberFormatException e) {
+            throw new ConfigurationException(
+                    "Invalid int property '%s': '%s'",
+                    propertyName,
+                    string);
+        }
+    }
+
+    protected String getProperty(String propertyName, String suffix) {
+        String value = getProperty(propertyName + suffix);
+        return value != null ? value : getProperty(propertyName);
+    }
+
+    /**
+     * Returns a property value for a given full key. This implementation returns a System
+     * property. Subclasses may lookup properties elsewhere. E.g. overriding this method
+     * can help with unit testing this class.
+     */
+    protected String getProperty(String key) {
+        return System.getProperty(key);
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoaderTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoaderTest.java?rev=917173&r1=917172&r2=917173&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoaderTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/DefaultDataSourceFactoryLoaderTest.java Sun Feb 28 13:00:19 2010
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.configuration;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import junit.framework.TestCase;
 
 import org.apache.cayenne.configuration.mock.MockDataSourceFactory1;
@@ -82,4 +85,56 @@
                 injector,
                 ((MockDataSourceFactory1) factory).getInjector());
     }
+
+    public void testGetDataSourceFactory_Property() throws Exception {
+
+        DataChannelDescriptor channelDescriptor = new DataChannelDescriptor();
+        channelDescriptor.setName("X");
+        DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
+        nodeDescriptor.setName("node1");
+        nodeDescriptor.setDataSourceFactoryType(MockDataSourceFactory1.class.getName());
+        nodeDescriptor.setDataChannelDescriptor(channelDescriptor);
+
+        Module testModule = new Module() {
+
+            public void configure(Binder binder) {
+                binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
+                binder.bind(ResourceLocator.class).to(MockResourceLocator.class);
+            }
+        };
+
+        Injector injector = DIBootstrap.createInjector(testModule);
+
+        final Map<String, String> properties = new HashMap<String, String>();
+
+        properties.put(PropertyDataSourceFactory.JDBC_DRIVER_PROPERTY, "x");
+        properties.put(PropertyDataSourceFactory.JDBC_URL_PROPERTY, "y");
+        DefaultDataSourceFactoryLoader factoryLoader = new DefaultDataSourceFactoryLoader() {
+
+            @Override
+            protected String getProperty(String key) {
+                return properties.get(key);
+            }
+        };
+        injector.injectMembers(factoryLoader);
+
+        DataSourceFactory factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
+        assertNotNull(factory);
+        assertTrue(factory instanceof PropertyDataSourceFactory);
+
+        properties.remove(PropertyDataSourceFactory.JDBC_URL_PROPERTY);
+        factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
+        assertNotNull(factory);
+        assertFalse(factory instanceof PropertyDataSourceFactory);
+
+        properties.put(PropertyDataSourceFactory.JDBC_URL_PROPERTY + ".X.node2", "y");
+        factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
+        assertNotNull(factory);
+        assertFalse(factory instanceof PropertyDataSourceFactory);
+        
+        properties.put(PropertyDataSourceFactory.JDBC_URL_PROPERTY + ".X.node1", "y");
+        factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
+        assertNotNull(factory);
+        assertTrue(factory instanceof PropertyDataSourceFactory);
+    }
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/PropertyDataSourceFactoryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/PropertyDataSourceFactoryTest.java?rev=917173&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/PropertyDataSourceFactoryTest.java (added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/PropertyDataSourceFactoryTest.java Sun Feb 28 13:00:19 2010
@@ -0,0 +1,49 @@
+/*****************************************************************
+ *   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.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+public class PropertyDataSourceFactoryTest extends TestCase {
+
+    public void testGetProperty() {
+
+        final Map<String, String> properties = new HashMap<String, String>();
+
+        properties.put("x", "1");
+        properties.put("x.s", "2");
+        properties.put("y", "3");
+
+        PropertyDataSourceFactory factory = new PropertyDataSourceFactory() {
+
+            @Override
+            protected String getProperty(String key) {
+                return properties.get(key);
+            }
+        };
+
+        assertEquals("2", factory.getProperty("x", ".s"));
+        assertEquals("1", factory.getProperty("x", ".t"));
+        assertEquals("3", factory.getProperty("y", ".s"));
+        assertNull(factory.getProperty("z", ".u"));
+    }
+}