Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Modify cloud:data-source to work with PostgreSQL relational database …

…service

Change-Id: Ie90ebb2bbec951bc20afd3d0ec4b90c61e1933c7
  • Loading branch information...
commit 284cf15ce341194fb050a136f89f6b539390b23b 1 parent 82477f2
Thomas Risberg authored
Showing with 148 additions and 85 deletions.
  1. +1 −0  cloudfoundry-runtime/.gitignore
  2. +1 −0  cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/env/AbstractDataSourceServiceInfo.java
  3. +6 −0 cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/env/AbstractServiceInfo.java
  4. +4 −2 cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/env/CloudEnvironment.java
  5. +32 −0 cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/env/RdbmsServiceInfo.java
  6. +5 −3 cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/service/config/xml/CloudNamespaceHandler.java
  7. +30 −0 cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/service/relational/CloudDataSourceFactory.java
  8. +0 −28 ...dfoundry-runtime/src/main/java/org/cloudfoundry/runtime/service/relational/CloudMySqlDataSourceFactory.java
  9. +0 −28 ...dry-runtime/src/main/java/org/cloudfoundry/runtime/service/relational/CloudPostgresqlDataSourceFactory.java
  10. +47 −0 cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/service/relational/RdbmsServiceCreator.java
  11. +2 −2 cloudfoundry-runtime/src/test/java/org/cloudfoundry/runtime/env/CloudEnvironmentTest.java
  12. +20 −22 cloudfoundry-runtime/src/test/java/org/cloudfoundry/runtime/service/relational/CloudDataSourceFactoryTest.java
View
1  cloudfoundry-runtime/.gitignore
@@ -1,3 +1,4 @@
.springBeans
.settings/
*.log
+/*.iml
View
1  ...dry-runtime/src/main/java/org/cloudfoundry/runtime/env/AbstractDataSourceServiceInfo.java
@@ -6,6 +6,7 @@
* Base information for all database service types
*
* @author Xin Li
+ * @author Thomas Risberg
*
**/
View
6 cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/env/AbstractServiceInfo.java
@@ -11,10 +11,12 @@
public abstract class AbstractServiceInfo {
private String serviceName;
private String plan;
+ private String label;
public AbstractServiceInfo(Map<String, Object> serviceInfo) {
serviceName = (String) serviceInfo.get("name");
plan = (String) serviceInfo.get("plan");
+ label = (String) serviceInfo.get("label");
}
public String getServiceName() {
@@ -25,6 +27,10 @@ public String getPlan() {
return plan;
}
+ public String getLabel() {
+ return label;
+ }
+
public abstract String getHost();
public abstract int getPort();
public abstract String getPassword();
View
6 cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/env/CloudEnvironment.java
@@ -21,6 +21,8 @@
*
* @author Ramnivas Laddad
* @author Scott Andrews
+ * @author Thomas Risberg
+ *
*/
public class CloudEnvironment {
@@ -42,10 +44,10 @@ private static void labelledServiceType(Class<? extends AbstractServiceInfo> ser
}
static {
- labelledServiceType(MysqlServiceInfo.class, "mysql-5.1");
+ labelledServiceType(RdbmsServiceInfo.class, "mysql-5.1");
+ labelledServiceType(RdbmsServiceInfo.class, "postgresql-9.0");
labelledServiceType(RedisServiceInfo.class, "redis-2.2");
labelledServiceType(MongoServiceInfo.class, "mongodb-1.8");
- labelledServiceType(PostgresqlServiceInfo.class, "postgresql-9.0");
// the old rabbitmq service
labelledServiceType(RabbitServiceInfo.class, "rabbitmq-2.4");
View
32 cloudfoundry-runtime/src/main/java/org/cloudfoundry/runtime/env/RdbmsServiceInfo.java
@@ -0,0 +1,32 @@
+package org.cloudfoundry.runtime.env;
+
+import java.util.Map;
+
+
+/**
+ * Information to access RDBMS service.
+ *
+ * @author Thomas Risberg
+ *
+ */
+
+public class RdbmsServiceInfo extends AbstractDataSourceServiceInfo {
+
+ private AbstractDataSourceServiceInfo delegate;
+
+ public RdbmsServiceInfo(Map<String, Object> serviceInfo) {
+ super(serviceInfo);
+ if (getLabel() != null && getLabel().startsWith("postgres")) {
+ this.delegate = new PostgresqlServiceInfo(serviceInfo);
+ }
+ else {
+ this.delegate = new MysqlServiceInfo(serviceInfo);
+ }
+
+ }
+
+ @Override
+ public String getUrl() {
+ return delegate.getUrl();
+ }
+}
View
8 ...time/src/main/java/org/cloudfoundry/runtime/service/config/xml/CloudNamespaceHandler.java
@@ -2,14 +2,14 @@
import org.cloudfoundry.runtime.env.CloudPropertiesFactoryBean;
import org.cloudfoundry.runtime.env.MongoServiceInfo;
-import org.cloudfoundry.runtime.env.MysqlServiceInfo;
+import org.cloudfoundry.runtime.env.RdbmsServiceInfo;
import org.cloudfoundry.runtime.env.RabbitServiceInfo;
import org.cloudfoundry.runtime.env.RedisServiceInfo;
import org.cloudfoundry.runtime.service.CloudServicesScanner;
import org.cloudfoundry.runtime.service.document.CloudMongoDbFactoryBean;
import org.cloudfoundry.runtime.service.keyvalue.CloudRedisConnectionFactoryBean;
import org.cloudfoundry.runtime.service.messaging.CloudRabbitConnectionFactoryBean;
-import org.cloudfoundry.runtime.service.relational.CloudMySqlDataSourceFactory;
+import org.cloudfoundry.runtime.service.relational.CloudDataSourceFactory;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.w3c.dom.Element;
@@ -21,6 +21,8 @@
* @author Costin Leau
* @author Ramnivas Laddad
* @author Scott Andrews
+ * @author Thomas Risberg
+ *
*/
public class CloudNamespaceHandler extends NamespaceHandlerSupport {
@@ -33,7 +35,7 @@ public void init() {
this.registerBeanDefinitionParser("mongo-db-factory",
new CloudServiceFactoryParser(CloudMongoDbFactoryBean.class, MongoServiceInfo.class));
this.registerBeanDefinitionParser("data-source",
- new CloudServiceFactoryParser(CloudMySqlDataSourceFactory.class, MysqlServiceInfo.class));
+ new CloudServiceFactoryParser(CloudDataSourceFactory.class, RdbmsServiceInfo.class));
this.registerBeanDefinitionParser("properties", new AbstractSimpleBeanDefinitionParser() {
@Override
View
30 ...ime/src/main/java/org/cloudfoundry/runtime/service/relational/CloudDataSourceFactory.java
@@ -0,0 +1,30 @@
+package org.cloudfoundry.runtime.service.relational;
+
+import javax.sql.DataSource;
+
+import org.cloudfoundry.runtime.env.CloudEnvironment;
+import org.cloudfoundry.runtime.env.RdbmsServiceInfo;
+import org.cloudfoundry.runtime.service.AbstractCloudServiceFactory;
+
+/**
+ * Spring factory bean for datasource service.
+ *
+ * @author Ramnivas Laddad
+ * @author Thomas Risberg
+ *
+ */
+public class CloudDataSourceFactory extends AbstractCloudServiceFactory<DataSource, RdbmsServiceInfo> {
+
+ public CloudDataSourceFactory(CloudEnvironment cloudEnvironment) {
+ super(new RdbmsServiceCreator(cloudEnvironment));
+ }
+
+ public CloudDataSourceFactory() {
+ super(new RdbmsServiceCreator(new CloudEnvironment()));
+ }
+
+ @Override
+ public Class<? extends DataSource> getObjectType() {
+ return DataSource.class;
+ }
+}
View
28 ...rc/main/java/org/cloudfoundry/runtime/service/relational/CloudMySqlDataSourceFactory.java
@@ -1,28 +0,0 @@
-package org.cloudfoundry.runtime.service.relational;
-
-import javax.sql.DataSource;
-
-import org.cloudfoundry.runtime.env.CloudEnvironment;
-import org.cloudfoundry.runtime.env.MysqlServiceInfo;
-import org.cloudfoundry.runtime.service.AbstractCloudServiceFactory;
-
-/**
- * Spring factory bean for datasource service.
- *
- * @author Ramnivas Laddad
- *
- */
-public class CloudMySqlDataSourceFactory extends AbstractCloudServiceFactory<DataSource, MysqlServiceInfo> {
- public CloudMySqlDataSourceFactory(CloudEnvironment cloudEnvironment) {
- super(new MysqlServiceCreator(cloudEnvironment));
- }
-
- public CloudMySqlDataSourceFactory() {
- super(new MysqlServiceCreator(new CloudEnvironment()));
- }
-
- @Override
- public Class<? extends DataSource> getObjectType() {
- return DataSource.class;
- }
-}
View
28 ...in/java/org/cloudfoundry/runtime/service/relational/CloudPostgresqlDataSourceFactory.java
@@ -1,28 +0,0 @@
-package org.cloudfoundry.runtime.service.relational;
-
-import javax.sql.DataSource;
-
-import org.cloudfoundry.runtime.env.CloudEnvironment;
-import org.cloudfoundry.runtime.env.PostgresqlServiceInfo;
-import org.cloudfoundry.runtime.service.AbstractCloudServiceFactory;
-
-/**
- * Spring factory bean for PostgreSQL datasource service.
- *
- * @author Jiajun Wang
- *
- */
-public class CloudPostgresqlDataSourceFactory extends AbstractCloudServiceFactory<DataSource, PostgresqlServiceInfo> {
- public CloudPostgresqlDataSourceFactory(CloudEnvironment cloudEnvironment) {
- super(new PostgresqlServiceCreator(cloudEnvironment));
- }
-
- public CloudPostgresqlDataSourceFactory() {
- super(new PostgresqlServiceCreator(new CloudEnvironment()));
- }
-
- @Override
- public Class<? extends DataSource> getObjectType() {
- return DataSource.class;
- }
-}
View
47 ...untime/src/main/java/org/cloudfoundry/runtime/service/relational/RdbmsServiceCreator.java
@@ -0,0 +1,47 @@
+package org.cloudfoundry.runtime.service.relational;
+
+import org.cloudfoundry.runtime.env.*;
+import org.cloudfoundry.runtime.service.AbstractDataSourceCreator;
+import org.springframework.util.Assert;
+
+import javax.sql.DataSource;
+
+/**
+ * Simplified access to RDBMS service.
+ *
+ * @author Thomas Risberg
+ *
+ */
+public class RdbmsServiceCreator extends AbstractDataSourceCreator<RdbmsServiceInfo> {
+
+ private AbstractDataSourceCreator delegate;
+ private CloudEnvironment cloudEnvironment;
+
+ public RdbmsServiceCreator(CloudEnvironment cloudEnvironment) {
+ super(cloudEnvironment, RdbmsServiceInfo.class);
+ this.cloudEnvironment = cloudEnvironment;
+ }
+
+ @Override
+ public DataSource createService(AbstractDataSourceServiceInfo serviceInfo) {
+ if (serviceInfo.getLabel() != null && serviceInfo.getLabel().startsWith("postgres")) {
+ this.delegate = new PostgresqlServiceCreator(cloudEnvironment);
+ }
+ else {
+ this.delegate = new MysqlServiceCreator(cloudEnvironment);
+ }
+ return super.createService(serviceInfo);
+ }
+
+ @Override
+ public String getDriverClassName() {
+ Assert.notNull(delegate, "DataSourceCreator delegate was not populated");
+ return delegate.getDriverClassName();
+ }
+
+ @Override
+ public String getValidationQuery() {
+ Assert.notNull(delegate, "DataSourceCreator delegate was not populated");
+ return delegate.getValidationQuery();
+ }
+}
View
4 cloudfoundry-runtime/src/test/java/org/cloudfoundry/runtime/env/CloudEnvironmentTest.java
@@ -95,7 +95,7 @@ public void getServiceInfoMysql() {
null,
null,
null));
- MysqlServiceInfo info = testRuntime.getServiceInfo("mysql-1", MysqlServiceInfo.class);
+ RdbmsServiceInfo info = testRuntime.getServiceInfo("mysql-1", RdbmsServiceInfo.class);
assertEquals("mysql-1", info.getServiceName());
assertEquals("jdbc:mysql://"+ hostname + ":" + port + "/database-123", info.getUrl());
assertEquals(username, info.getUserName());
@@ -109,7 +109,7 @@ public void getServiceInfoPostgresql() {
null,
null,
null));
- PostgresqlServiceInfo info = testRuntime.getServiceInfo("postgresql-1", PostgresqlServiceInfo.class);
+ RdbmsServiceInfo info = testRuntime.getServiceInfo("postgresql-1", RdbmsServiceInfo.class);
assertEquals("postgresql-1", info.getServiceName());
assertEquals("jdbc:postgresql://" + hostname + ":" + port + "/db-123", info.getUrl());
assertEquals(username, info.getUserName());
View
42 ...src/test/java/org/cloudfoundry/runtime/service/relational/CloudDataSourceFactoryTest.java
@@ -10,6 +10,7 @@
import org.cloudfoundry.runtime.env.CloudEnvironment;
import org.cloudfoundry.runtime.env.MysqlServiceInfo;
import org.cloudfoundry.runtime.env.PostgresqlServiceInfo;
+import org.cloudfoundry.runtime.env.RdbmsServiceInfo;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
@@ -18,30 +19,27 @@
public class CloudDataSourceFactoryTest {
@Mock private CloudEnvironment mockRuntime;
- @Mock private MysqlServiceInfo mockMysqlServiceInfo;
- @Mock private PostgresqlServiceInfo mockPostgresqlServiceInfo;
- private CloudMySqlDataSourceFactory mysqlFactory;
- private CloudPostgresqlDataSourceFactory postgresqlFactory;
+ @Mock private RdbmsServiceInfo mockRdbmsServiceInfo;
+ private CloudDataSourceFactory rdbmsFactory;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
- mysqlFactory = new CloudMySqlDataSourceFactory(mockRuntime);
- postgresqlFactory = new CloudPostgresqlDataSourceFactory(mockRuntime);
+ rdbmsFactory = new CloudDataSourceFactory(mockRuntime);
}
@Test
public void cloudMySQLDataSourceCreation() throws Exception {
- when(mockRuntime.getServiceInfo("mysql-1", MysqlServiceInfo.class))
- .thenReturn(mockMysqlServiceInfo);
- when(mockMysqlServiceInfo.getUrl()).thenReturn("jdbc:mysql://10.20.30.40:3306/database-123");
- when(mockMysqlServiceInfo.getUserName()).thenReturn("myuser");
- when(mockMysqlServiceInfo.getPassword()).thenReturn("mypass");
+ when(mockRuntime.getServiceInfo("mysql-1", RdbmsServiceInfo.class))
+ .thenReturn(mockRdbmsServiceInfo);
+ when(mockRdbmsServiceInfo.getUrl()).thenReturn("jdbc:mysql://10.20.30.40:3306/database-123");
+ when(mockRdbmsServiceInfo.getUserName()).thenReturn("myuser");
+ when(mockRdbmsServiceInfo.getPassword()).thenReturn("mypass");
- mysqlFactory.setServiceName("mysql-1");
- mysqlFactory.afterPropertiesSet();
+ rdbmsFactory.setServiceName("mysql-1");
+ rdbmsFactory.afterPropertiesSet();
- DataSource dataSource = mysqlFactory.getObject();
+ DataSource dataSource = rdbmsFactory.getObject();
assertNotNull(dataSource);
assertEquals("jdbc:mysql://10.20.30.40:3306/database-123", ReflectionTestUtils.getField(dataSource, "url"));
@@ -53,16 +51,16 @@ public void cloudMySQLDataSourceCreation() throws Exception {
@Test
public void cloudPostgreSQLDataSourceCreation() throws Exception {
- when(mockRuntime.getServiceInfo("postgres-1", PostgresqlServiceInfo.class))
- .thenReturn(mockPostgresqlServiceInfo);
- when(mockPostgresqlServiceInfo.getUrl()).thenReturn("jdbc:postgresql://10.20.30.40:5432/database-123");
- when(mockPostgresqlServiceInfo.getUserName()).thenReturn("pguser");
- when(mockPostgresqlServiceInfo.getPassword()).thenReturn("pgpass");
+ when(mockRuntime.getServiceInfo("postgres-1", RdbmsServiceInfo.class))
+ .thenReturn(mockRdbmsServiceInfo);
+ when(mockRdbmsServiceInfo.getUrl()).thenReturn("jdbc:postgresql://10.20.30.40:5432/database-123");
+ when(mockRdbmsServiceInfo.getUserName()).thenReturn("pguser");
+ when(mockRdbmsServiceInfo.getPassword()).thenReturn("pgpass");
- postgresqlFactory.setServiceName("postgres-1");
- postgresqlFactory.afterPropertiesSet();
+ rdbmsFactory.setServiceName("postgres-1");
+ rdbmsFactory.afterPropertiesSet();
- DataSource dataSource = postgresqlFactory.getObject();
+ DataSource dataSource = rdbmsFactory.getObject();
assertNotNull(dataSource);
assertEquals("jdbc:postgresql://10.20.30.40:5432/database-123", ReflectionTestUtils.getField(dataSource, "url"));
Please sign in to comment.
Something went wrong with that request. Please try again.