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 2011/02/20 11:39:54 UTC

svn commit: r1072549 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/configuration/server/ test/java/org/apache/cayenne/configuration/server/

Author: aadamchik
Date: Sun Feb 20 10:39:54 2011
New Revision: 1072549

URL: http://svn.apache.org/viewvc?rev=1072549&view=rev
Log:
CAY-1542 DI container is not shutting down Cayenne-managed connection pool

make sure DataSourceFactories read properties from a DI service

Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactoryTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DefaultDataSourceFactoryLoaderTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java?rev=1072549&r1=1072548&r2=1072549&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/DelegatingDataSourceFactory.java Sun Feb 20 10:39:54 2011
@@ -23,6 +23,7 @@ import javax.sql.DataSource;
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.configuration.AdhocObjectFactory;
 import org.apache.cayenne.configuration.DataNodeDescriptor;
+import org.apache.cayenne.configuration.RuntimeProperties;
 import org.apache.cayenne.di.Inject;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -48,6 +49,9 @@ public class DelegatingDataSourceFactory
     @Inject
     protected AdhocObjectFactory objectFactory;
 
+    @Inject
+    protected RuntimeProperties properties;
+
     public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
         return getDataSourceFactory(nodeDescriptor).getDataSource(nodeDescriptor);
     }
@@ -82,10 +86,10 @@ public class DelegatingDataSourceFactory
                 ? nodeDescriptor.getDataChannelDescriptor().getName()
                 : null;
 
-        String driver = getProperty(PropertyDataSourceFactory.JDBC_DRIVER_PROPERTY);
+        String driver = properties.get(PropertyDataSourceFactory.JDBC_DRIVER_PROPERTY);
 
         if (driver == null && channelName != null) {
-            driver = getProperty(PropertyDataSourceFactory.JDBC_DRIVER_PROPERTY
+            driver = properties.get(PropertyDataSourceFactory.JDBC_DRIVER_PROPERTY
                     + "."
                     + nodeDescriptor.getDataChannelDescriptor().getName()
                     + "."
@@ -96,10 +100,10 @@ public class DelegatingDataSourceFactory
             return false;
         }
 
-        String url = getProperty(PropertyDataSourceFactory.JDBC_URL_PROPERTY);
+        String url = properties.get(PropertyDataSourceFactory.JDBC_URL_PROPERTY);
 
         if (url == null && channelName != null) {
-            url = getProperty(PropertyDataSourceFactory.JDBC_URL_PROPERTY
+            url = properties.get(PropertyDataSourceFactory.JDBC_URL_PROPERTY
                     + "."
                     + nodeDescriptor.getDataChannelDescriptor().getName()
                     + "."
@@ -120,12 +124,4 @@ public class DelegatingDataSourceFactory
         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);
-    }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java?rev=1072549&r1=1072548&r2=1072549&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/configuration/server/PropertyDataSourceFactory.java Sun Feb 20 10:39:54 2011
@@ -24,7 +24,9 @@ import org.apache.cayenne.ConfigurationE
 import org.apache.cayenne.access.ConnectionLogger;
 import org.apache.cayenne.access.QueryLogger;
 import org.apache.cayenne.configuration.DataNodeDescriptor;
+import org.apache.cayenne.configuration.RuntimeProperties;
 import org.apache.cayenne.conn.PoolManager;
+import org.apache.cayenne.di.Inject;
 
 /**
  * A DataSourceFactrory that creates a DataSource based on system properties. Properties
@@ -53,6 +55,9 @@ public class PropertyDataSourceFactory i
     static final String JDBC_MIN_CONNECTIONS_PROPERTY = "cayenne.jdbc.min.connections";
     static final String JDBC_MAX_CONNECTIONS_PROPERTY = "cayenne.jdbc.max.conections";
 
+    @Inject
+    protected RuntimeProperties properties;
+
     public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
 
         String suffix = "."
@@ -103,16 +108,7 @@ public class PropertyDataSourceFactory i
     }
 
     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);
+        String value = properties.get(propertyName + suffix);
+        return value != null ? value : properties.get(propertyName);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DefaultDataSourceFactoryLoaderTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DefaultDataSourceFactoryLoaderTest.java?rev=1072549&r1=1072548&r2=1072549&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DefaultDataSourceFactoryLoaderTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/configuration/server/DefaultDataSourceFactoryLoaderTest.java Sun Feb 20 10:39:54 2011
@@ -18,20 +18,16 @@
  ****************************************************************/
 package org.apache.cayenne.configuration.server;
 
-import java.util.HashMap;
-import java.util.Map;
-
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 import junit.framework.TestCase;
 
 import org.apache.cayenne.configuration.AdhocObjectFactory;
 import org.apache.cayenne.configuration.DataChannelDescriptor;
 import org.apache.cayenne.configuration.DataNodeDescriptor;
 import org.apache.cayenne.configuration.DefaultAdhocObjectFactory;
+import org.apache.cayenne.configuration.RuntimeProperties;
 import org.apache.cayenne.configuration.mock.MockDataSourceFactory1;
-import org.apache.cayenne.configuration.server.DataSourceFactory;
-import org.apache.cayenne.configuration.server.DelegatingDataSourceFactory;
-import org.apache.cayenne.configuration.server.PropertyDataSourceFactory;
-import org.apache.cayenne.configuration.server.XMLPoolingDataSourceFactory;
 import org.apache.cayenne.conn.DataSourceInfo;
 import org.apache.cayenne.di.Binder;
 import org.apache.cayenne.di.DIBootstrap;
@@ -53,6 +49,8 @@ public class DefaultDataSourceFactoryLoa
             public void configure(Binder binder) {
                 binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
                 binder.bind(ResourceLocator.class).to(MockResourceLocator.class);
+                binder.bind(RuntimeProperties.class).toInstance(
+                        mock(RuntimeProperties.class));
             }
         };
 
@@ -77,6 +75,8 @@ public class DefaultDataSourceFactoryLoa
             public void configure(Binder binder) {
                 binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
                 binder.bind(ResourceLocator.class).to(MockResourceLocator.class);
+                binder.bind(RuntimeProperties.class).toInstance(
+                        mock(RuntimeProperties.class));
             }
         };
 
@@ -96,6 +96,11 @@ public class DefaultDataSourceFactoryLoa
 
     public void testGetDataSourceFactory_Property() throws Exception {
 
+        final RuntimeProperties properties = mock(RuntimeProperties.class);
+        when(properties.get(PropertyDataSourceFactory.JDBC_DRIVER_PROPERTY)).thenReturn(
+                "x");
+        when(properties.get(PropertyDataSourceFactory.JDBC_URL_PROPERTY)).thenReturn("y");
+
         DataChannelDescriptor channelDescriptor = new DataChannelDescriptor();
         channelDescriptor.setName("X");
         DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
@@ -108,39 +113,33 @@ public class DefaultDataSourceFactoryLoa
             public void configure(Binder binder) {
                 binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
                 binder.bind(ResourceLocator.class).to(MockResourceLocator.class);
+                binder.bind(RuntimeProperties.class).toInstance(properties);
             }
         };
 
         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");
-        DelegatingDataSourceFactory factoryLoader = new DelegatingDataSourceFactory() {
-
-            @Override
-            protected String getProperty(String key) {
-                return properties.get(key);
-            }
-        };
+        DelegatingDataSourceFactory factoryLoader = new DelegatingDataSourceFactory();
         injector.injectMembers(factoryLoader);
 
         DataSourceFactory factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
         assertNotNull(factory);
         assertTrue(factory instanceof PropertyDataSourceFactory);
 
-        properties.remove(PropertyDataSourceFactory.JDBC_URL_PROPERTY);
+        when(properties.get(PropertyDataSourceFactory.JDBC_URL_PROPERTY))
+                .thenReturn(null);
         factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
         assertNotNull(factory);
         assertFalse(factory instanceof PropertyDataSourceFactory);
 
-        properties.put(PropertyDataSourceFactory.JDBC_URL_PROPERTY + ".X.node2", "y");
+        when(properties.get(PropertyDataSourceFactory.JDBC_URL_PROPERTY + ".X.node2"))
+                .thenReturn("y");
         factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
         assertNotNull(factory);
         assertFalse(factory instanceof PropertyDataSourceFactory);
-        
-        properties.put(PropertyDataSourceFactory.JDBC_URL_PROPERTY + ".X.node1", "y");
+
+        when(properties.get(PropertyDataSourceFactory.JDBC_URL_PROPERTY + ".X.node1"))
+                .thenReturn("y");
         factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
         assertNotNull(factory);
         assertTrue(factory instanceof PropertyDataSourceFactory);