cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [1/3] cayenne git commit: CAY-2010 DataSourceBuilder to help users to create Cayenne connection pool
Date Tue, 19 May 2015 09:19:24 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 3aa0af9a0 -> 79a66d803


CAY-2010 DataSourceBuilder to help users to create Cayenne connection pool

* undoing c2ce2dfa6c3424093205f726ddbb59fb285ed2b3 - it wasn't well thought out


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/e3eb315a
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/e3eb315a
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/e3eb315a

Branch: refs/heads/master
Commit: e3eb315a451abcb74557cfb140300c2b5e91bdf3
Parents: 3aa0af9
Author: aadamchik <aadamchik@apache.org>
Authored: Tue May 19 11:42:20 2015 +0300
Committer: aadamchik <aadamchik@apache.org>
Committed: Tue May 19 11:49:05 2015 +0300

----------------------------------------------------------------------
 .../server/ServerRuntimeBuilder.java            | 95 ++++++++++++++++++++
 .../server/ServerRuntimeBuilderIT.java          | 27 ++++++
 docs/doc/src/main/resources/UPGRADE.txt         |  6 --
 3 files changed, 122 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/e3eb315a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java
b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java
index 58f76a9..522d8b0 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilder.java
@@ -30,6 +30,7 @@ import org.apache.cayenne.access.DataDomain;
 import org.apache.cayenne.configuration.Constants;
 import org.apache.cayenne.datasource.DataSourceBuilder;
 import org.apache.cayenne.di.Binder;
+import org.apache.cayenne.di.MapBuilder;
 import org.apache.cayenne.di.Module;
 
 /**
@@ -47,6 +48,13 @@ public class ServerRuntimeBuilder {
 	private Collection<String> configs;
 	private List<Module> modules;
 	private DataSourceFactory dataSourceFactory;
+	private String jdbcUrl;
+	private String jdbcDriver;
+	private String jdbcUser;
+	private String jdbcPassword;
+	private int jdbcMinConnections;
+	private int jdbcMaxConnections;
+	private String validationQuery;
 
 	public static ServerRuntimeBuilder builder() {
 		return new ServerRuntimeBuilder();
@@ -96,6 +104,61 @@ public class ServerRuntimeBuilder {
 		return this;
 	}
 
+	/**
+	 * Sets a database URL for the default DataSource.
+	 */
+	public ServerRuntimeBuilder url(String url) {
+		this.jdbcUrl = url;
+		return this;
+	}
+
+	/**
+	 * Sets a driver Java class for the default DataSource.
+	 */
+	public ServerRuntimeBuilder jdbcDriver(String driver) {
+		// TODO: guess the driver from URL
+		this.jdbcDriver = driver;
+		return this;
+	}
+
+	/**
+	 * Sets a validation query for the default DataSource.
+	 * 
+	 * @param validationQuery
+	 *            a SQL string that returns some result. It will be used to
+	 *            validate connections in the pool.
+	 */
+	public ServerRuntimeBuilder validationQuery(String validationQuery) {
+		this.validationQuery = validationQuery;
+		return this;
+	}
+
+	/**
+	 * Sets a user name for the default DataSource.
+	 */
+	public ServerRuntimeBuilder user(String user) {
+		this.jdbcUser = user;
+		return this;
+	}
+
+	/**
+	 * Sets a password for the default DataSource.
+	 */
+	public ServerRuntimeBuilder password(String password) {
+		this.jdbcPassword = password;
+		return this;
+	}
+
+	public ServerRuntimeBuilder minConnections(int minConnections) {
+		this.jdbcMinConnections = minConnections;
+		return this;
+	}
+
+	public ServerRuntimeBuilder maxConnections(int maxConnections) {
+		this.jdbcMaxConnections = maxConnections;
+		return this;
+	}
+
 	public ServerRuntimeBuilder addConfig(String configurationLocation) {
 		configs.add(configurationLocation);
 		return this;
@@ -167,6 +230,38 @@ public class ServerRuntimeBuilder {
 			});
 
 		}
+		// URL and driver are the minimal requirement for
+		// DelegatingDataSourceFactory to work
+		else if (jdbcUrl != null && jdbcDriver != null) {
+			prepend(new Module() {
+				@Override
+				public void configure(Binder binder) {
+					binder.bind(DataDomain.class).toProvider(SyntheticNodeDataDomainProvider.class);
+					MapBuilder<Object> props = binder.bindMap(Constants.PROPERTIES_MAP)
+							.put(Constants.JDBC_DRIVER_PROPERTY, jdbcDriver).put(Constants.JDBC_URL_PROPERTY,
jdbcUrl);
+
+					if (jdbcUser != null) {
+						props.put(Constants.JDBC_USERNAME_PROPERTY, jdbcUser);
+					}
+
+					if (jdbcPassword != null) {
+						props.put(Constants.JDBC_PASSWORD_PROPERTY, jdbcPassword);
+					}
+
+					if (jdbcMinConnections > 0) {
+						props.put(Constants.JDBC_MIN_CONNECTIONS_PROPERTY, Integer.toString(jdbcMinConnections));
+					}
+
+					if (jdbcMaxConnections > 0) {
+						props.put(Constants.JDBC_MAX_CONNECTIONS_PROPERTY, Integer.toString(jdbcMaxConnections));
+					}
+
+					if (validationQuery != null) {
+						props.put(Constants.JDBC_VALIDATION_QUERY_PROPERTY, validationQuery);
+					}
+				}
+			});
+		}
 	}
 
 	private void prepend(Module module) {

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e3eb315a/cayenne-server/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilderIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilderIT.java
b/cayenne-server/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilderIT.java
index b220bc2..c2c7ee8 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilderIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/configuration/server/ServerRuntimeBuilderIT.java
@@ -78,6 +78,33 @@ public class ServerRuntimeBuilderIT extends ServerCase {
 	}
 
 	@Test
+	public void testConfigFree_WithDBParams() {
+
+		localRuntime = new ServerRuntimeBuilder().jdbcDriver(dsi.getJdbcDriver()).url(dsi.getDataSourceUrl())
+				.password(dsi.getPassword()).user(dsi.getUserName()).minConnections(1).maxConnections(2).build();
+
+		List<DataRow> result = SQLSelect.dataRowQuery("SELECT * FROM ARTIST").select(localRuntime.newContext());
+		assertEquals(2, result.size());
+	}
+
+	@Test
+	public void tesConfigFree_WithDBParams() {
+
+		localRuntime = new ServerRuntimeBuilder().addConfig(CayenneProjects.TESTMAP_PROJECT)
+				.jdbcDriver(dsi.getJdbcDriver()).url(dsi.getDataSourceUrl()).password(dsi.getPassword())
+				.user(dsi.getUserName()).minConnections(1).maxConnections(2).build();
+
+		DataMap map = localRuntime.getDataDomain().getDataMap("testmap");
+		assertNotNull(map);
+
+		DataNode node = localRuntime.getDataDomain().getDefaultNode();
+		assertNotNull(node);
+		assertEquals(1, node.getDataMaps().size());
+
+		assertSame(map, node.getDataMap("testmap"));
+	}
+
+	@Test
 	public void testConfigFree_WithDataSource() {
 
 		localRuntime = new ServerRuntimeBuilder().dataSource(dataSource).build();

http://git-wip-us.apache.org/repos/asf/cayenne/blob/e3eb315a/docs/doc/src/main/resources/UPGRADE.txt
----------------------------------------------------------------------
diff --git a/docs/doc/src/main/resources/UPGRADE.txt b/docs/doc/src/main/resources/UPGRADE.txt
index c980f1d..06f77cc 100644
--- a/docs/doc/src/main/resources/UPGRADE.txt
+++ b/docs/doc/src/main/resources/UPGRADE.txt
@@ -12,12 +12,6 @@ UPGRADING TO 4.0.M3
   Note if you have references to @Deprecated String properties and you run cgen without "createPropertyNames"
flag,
   there will be errors. Reference Jira: CAY-1991
 
-* Per CAY-2010 DataSource building methods were refactored out of ServerRuntimeBuilder and
into a standalone 
-  org.apache.cayenne.datasource.DataSourceBuilder. So creating a DataSource in runtime may
look like this:
-
-  DataSource ds = DataSourceBuilder.url(dbUrl).driver(dbDriver).build();
-  ServerRuntime runtime = ServerRuntimeBuilder.builder().dataSource(ds).build();
-
 * Per CAY-2008, CAY-2009 we got rid of org.apache.cayenne.conn.PoolManager and associated
classes that made up a 
   pooling DataSource. A replacement is non-blocking DataSource under org.apache.cayenne.datasource
(PoolingDataSource, ManagedPoolingDataSource),
   best assembled using org.apache.cayenne.datasource.DataSourceBuilder.


Mime
View raw message