Provide access to URL fields when jdbcUrl is provided in Cloud Foundry connector.

This commit is contained in:
Scott Frederick
2015-08-20 18:43:48 -05:00
parent 9c3da5b7ff
commit 6f1b17d12d
25 changed files with 222 additions and 154 deletions

View File

@@ -4,25 +4,20 @@ import org.springframework.cloud.service.ServiceInfo;
@ServiceInfo.ServiceLabel("db2")
public class DB2ServiceInfo extends RelationalServiceInfo {
public static final String DB2_SCHEME = "db2";
private static final String JDBC_URL_TYPE = "db2";
public static final String DB2_SCHEME = JDBC_URL_TYPE;
public DB2ServiceInfo(String id, String url) {
super(id, url, JDBC_URL_TYPE);
public DB2ServiceInfo(String id, String url) {
this(id, url, null);
}
public DB2ServiceInfo(String id, String url, String jdbcUrl) {
super(id, url, jdbcUrl, DB2_SCHEME);
}
@ServiceProperty(category = "connection")
@Override
public String getJdbcUrl() {
if (getUriInfo().getUriString().startsWith(JDBC_PREFIX)) {
return getUriInfo().getUriString();
}
protected String buildJdbcUrl() {
return String.format("jdbc:%s://%s:%d/%s:user=%s;password=%s;",
jdbcUrlDatabaseType,
jdbcUrlDatabaseType,
getHost(), getPort(), getPath(), getUserName(), getPassword());
}
}

View File

@@ -9,12 +9,13 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
*/
@ServiceLabel("mysql")
public class MysqlServiceInfo extends RelationalServiceInfo {
private static final String JDBC_URL_TYPE = "mysql";
public static final String MYSQL_SCHEME = JDBC_URL_TYPE;
public static final String MYSQL_SCHEME = "mysql";
public MysqlServiceInfo(String id, String url) {
super(id, url, MYSQL_SCHEME);
this(id, url, null);
}
public MysqlServiceInfo(String id, String url, String jdbcUrl) {
super(id, url, jdbcUrl, MYSQL_SCHEME);
}
}

View File

@@ -4,24 +4,20 @@ import org.springframework.cloud.service.ServiceInfo;
@ServiceInfo.ServiceLabel("oracle")
public class OracleServiceInfo extends RelationalServiceInfo {
private static final String JDBC_URL_TYPE = "oracle";
public static final String ORACLE_SCHEME = JDBC_URL_TYPE;
public static final String ORACLE_SCHEME = "oracle";
public OracleServiceInfo(String id, String url) {
super(id, url, JDBC_URL_TYPE);
this(id, url, null);
}
public OracleServiceInfo(String id, String url, String jdbcUrl) {
super(id, url, jdbcUrl, ORACLE_SCHEME);
}
@Override
public String getJdbcUrl() {
if (getUriInfo().getUriString().startsWith(JDBC_PREFIX)) {
return getUriInfo().getUriString();
}
protected String buildJdbcUrl() {
return String.format("jdbc:%s:thin:%s/%s@%s:%d/%s",
jdbcUrlDatabaseType, getUserName(), getPassword(),
getHost(), getPort(), getPath());
}
}

View File

@@ -2,7 +2,6 @@ package org.springframework.cloud.service.common;
import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
/**
*
* @author Ramnivas Laddad
@@ -10,12 +9,14 @@ import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
*/
@ServiceLabel("postgresql")
public class PostgresqlServiceInfo extends RelationalServiceInfo {
private static final String JDBC_URL_TYPE = "postgresql";
public static final String POSTGRES_SCHEME = "postgres";
public static final String POSTGRES_JDBC_SCHEME = "postgresql";
public PostgresqlServiceInfo(String id, String url) {
super(id, url, JDBC_URL_TYPE);
this(id, url, null);
}
public PostgresqlServiceInfo(String id, String url, String jdbcUrl) {
super(id, url, jdbcUrl, POSTGRES_JDBC_SCHEME);
}
}

View File

@@ -10,19 +10,21 @@ public abstract class RelationalServiceInfo extends UriBasedServiceInfo {
public static final String JDBC_PREFIX = "jdbc:";
protected final String jdbcUrl;
protected final String jdbcUrlDatabaseType;
public RelationalServiceInfo(String id, String uriString, String jdbcUrlDatabaseType) {
public RelationalServiceInfo(String id, String uriString, String jdbcUrl, String jdbcUrlDatabaseType) {
super(id, uriString);
this.jdbcUrl = jdbcUrl;
this.jdbcUrlDatabaseType = jdbcUrlDatabaseType;
}
@ServiceProperty(category = "connection")
public String getJdbcUrl() {
if (getUriInfo().getUriString().startsWith(JDBC_PREFIX)) {
return getUriInfo().getUriString();
}
return jdbcUrl == null ? buildJdbcUrl() : jdbcUrl;
}
protected String buildJdbcUrl() {
return String.format("%s%s://%s%s/%s%s%s", JDBC_PREFIX, jdbcUrlDatabaseType, getHost(), formatPort(),
getPath(), formatUserinfo(), formatQuery());
}

View File

@@ -4,20 +4,18 @@ import org.springframework.cloud.service.ServiceInfo;
@ServiceInfo.ServiceLabel("sqlserver")
public class SqlServerServiceInfo extends RelationalServiceInfo {
private static final String JDBC_URL_TYPE = "sqlserver";
public static final String SQLSERVER_SCHEME = JDBC_URL_TYPE;
public static final String SQLSERVER_SCHEME = "sqlserver";
public SqlServerServiceInfo(String id, String url) {
super(id, url, JDBC_URL_TYPE);
this(id, url, null);
}
public SqlServerServiceInfo(String id, String url, String jdbcUrl) {
super(id, url, jdbcUrl, SQLSERVER_SCHEME);
}
@Override
public String getJdbcUrl() {
if (getUriInfo().getUriString().startsWith(JDBC_PREFIX)) {
return getUriInfo().getUriString();
}
protected String buildJdbcUrl() {
return String.format("jdbc:%s://%s:%d;database=%s;user=%s;password=%s",
jdbcUrlDatabaseType,
getHost(), getPort(), getPath(), getUserName(), getPassword());

View File

@@ -6,6 +6,14 @@ import static org.junit.Assert.assertEquals;
public class RelationalServiceInfoTest {
@Test
public void jdbcUrl() {
RelationalServiceInfo serviceInfo = createServiceInfoWithJdbcUrl("bad://bad:bad@bad:1234/bad",
"jdbc:jdbcdbtype://hostname:1234/database?user=username&password=password");
assertEquals("jdbc:jdbcdbtype://hostname:1234/database?user=username&password=password", serviceInfo.getJdbcUrl());
}
@Test
public void jdbcFullUrl() {
RelationalServiceInfo serviceInfo = createServiceInfo("dbtype://username:password@hostname:1234/database");
@@ -47,7 +55,12 @@ public class RelationalServiceInfoTest {
}
private RelationalServiceInfo createServiceInfo(final String uri) {
return new RelationalServiceInfo("test", uri, "jdbcdbtype") {
return new RelationalServiceInfo("test", uri, null, "jdbcdbtype") {
};
}
private RelationalServiceInfo createServiceInfoWithJdbcUrl(final String uri, final String jdbcUrl) {
return new RelationalServiceInfo("test", uri, jdbcUrl, "jdbcdbtype") {
};
}