If a jdbcUrl field is present in the credentials field in relational service connectors, prefer to use it unchanged over building a jdbcUrl from other credentials fields.

This commit is contained in:
Scott Frederick
2015-05-11 18:56:17 -05:00
parent 06b9fd1bb4
commit b6882df6ae
14 changed files with 196 additions and 60 deletions

View File

@@ -15,6 +15,10 @@ public class OracleServiceInfo extends RelationalServiceInfo {
@Override
public String getJdbcUrl() {
if (getUriInfo().getRawUriString().startsWith(JDBC_PREFIX)) {
return getUriInfo().getRawUriString();
}
return String.format("jdbc:%s:thin:%s/%s@%s:%d/%s",
jdbcUrlDatabaseType, getUserName(), getPassword(),
getHost(), getPort(), getPath());

View File

@@ -1,12 +1,19 @@
package org.springframework.cloud.service.common;
import org.springframework.cloud.service.UriBasedServiceInfo;
import org.springframework.cloud.util.StandardUriInfoFactory;
import org.springframework.cloud.util.UriInfo;
import org.springframework.cloud.util.UriInfoFactory;
/**
* @author Ramnivas Laddad
* @author Scott Frederick
*/
public abstract class RelationalServiceInfo extends UriBasedServiceInfo {
public static final String JDBC_PREFIX = "jdbc:";
private static JdbcUriInfoFactory jdbcUriInfoFactory = new JdbcUriInfoFactory();
protected final String jdbcUrlDatabaseType;
public RelationalServiceInfo(String id, String uriString, String jdbcUrlDatabaseType) {
@@ -14,9 +21,18 @@ public abstract class RelationalServiceInfo extends UriBasedServiceInfo {
this.jdbcUrlDatabaseType = jdbcUrlDatabaseType;
}
@Override
public UriInfoFactory getUriInfoFactory() {
return jdbcUriInfoFactory;
}
@ServiceProperty(category = "connection")
public String getJdbcUrl() {
return String.format("jdbc:%s://%s%s/%s%s%s", jdbcUrlDatabaseType, getHost(), formatPort(),
if (getUriInfo().getRawUriString().startsWith(JDBC_PREFIX)) {
return getUriInfo().getRawUriString();
}
return String.format("%s%s://%s%s/%s%s%s", JDBC_PREFIX, jdbcUrlDatabaseType, getHost(), formatPort(),
getPath(), formatUserinfo(), formatQuery());
}
@@ -47,4 +63,20 @@ public abstract class RelationalServiceInfo extends UriBasedServiceInfo {
}
return "";
}
public static class JdbcUriInfoFactory extends StandardUriInfoFactory {
@Override
public UriInfo createUri(String uriString) {
if (uriString.startsWith(JDBC_PREFIX)) {
return new JdbcUriInfo(uriString);
}
return super.createUri(uriString);
}
}
public static class JdbcUriInfo extends UriInfo {
public JdbcUriInfo(String rawUriString) {
super(rawUriString);
}
}
}

View File

@@ -7,12 +7,10 @@ import java.net.URLDecoder;
/**
* Factory for standard Cloud Foundry URIs, which all conform to the format:
* <p>
* {@code [jdbc:]scheme://[user:pass]@authority[:port]/path}
* {@code scheme://[user:pass]@authority[:port]/path}
*/
public class StandardUriInfoFactory implements UriInfoFactory {
public static final String JDBC_PREFIX = "jdbc:";
@Override
public UriInfo createUri(String scheme, String host, int port, String username, String password, String path) {
return new UriInfo(scheme, host, port, username, password, path);
@@ -20,10 +18,7 @@ public class StandardUriInfoFactory implements UriInfoFactory {
@Override
public UriInfo createUri(String uriString) {
uriString = trimJdbcScheme(uriString);
URI tmpUri = createTmpUri(uriString);
URI tmpUri = uriFromString(uriString);
String[] userInfo = parseUserinfo(tmpUri);
String userName = uriDecode(userInfo[0]);
@@ -33,21 +28,14 @@ public class StandardUriInfoFactory implements UriInfoFactory {
userName, password, parsePath(tmpUri), tmpUri.getRawQuery(), uriString);
}
private URI createTmpUri(String uriString) {
private URI uriFromString(String uriString) {
try {
return new URI(uriString);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
throw new IllegalArgumentException("Invalid URI " + uriString, e);
}
}
private String trimJdbcScheme(String uriString) {
if (uriString.startsWith(JDBC_PREFIX)) {
uriString = uriString.substring(JDBC_PREFIX.length());
}
return uriString;
}
private String[] parseUserinfo(URI uri) {
String userInfo = uri.getRawUserInfo();

View File

@@ -44,6 +44,10 @@ public class UriInfo {
this.uri = buildUri();
}
public UriInfo(String rawUriString) {
this.rawUriString = rawUriString;
}
public String getScheme() {
return scheme;
}

View File

@@ -12,31 +12,24 @@ package org.springframework.cloud.util;
*/
public interface UriInfoFactory {
/**
* Create a {@link UriInfo} based on a URI string
*
* @param uriString
* the URI string to parse
* @return a {@link UriInfo}
*/
public UriInfo createUri(String uriString);
/**
* Create a {@link UriInfo} based on a URI string
*
* @param uriString the URI string to parse
* @return a {@link UriInfo}
*/
UriInfo createUri(String uriString);
/**
* Create a {@link UriInfo} based on explicit components of the URI
*
* @param scheme
* the URI scheme for this service
* @param host
* the host for this service
* @param port
* the port for this service
* @param username
* the authentication username for this service
* @param password
* the authentication password for this service
* @param path
* the path to this service resource on the server
* @return a {@link UriInfo}
*/
public UriInfo createUri(String scheme, String host, int port, String username, String password, String path);
/**
* Create a {@link UriInfo} based on explicit components of the URI
*
* @param scheme the URI scheme for this service
* @param host the host for this service
* @param port the port for this service
* @param username the authentication username for this service
* @param password the authentication password for this service
* @param path the path to this service resource on the server
* @return a {@link UriInfo}
*/
UriInfo createUri(String scheme, String host, int port, String username, String password, String path);
}

View File

@@ -31,15 +31,6 @@ public class StandardUriInfoFactoryTest {
assertEquals(uri, result.getUri().toString());
}
@Test
public void createWithJdbcUri() {
String uri = "mysql://joe:joes_password@localhost:1527/big_db";
UriInfo result = factory.createUri("jdbc:" + uri);
assertUriInfoEquals(result, "localhost", 1527, "joe", "joes_password", "big_db", null);
assertEquals(uri, result.getUri().toString());
}
@Test
public void createNoUsernamePassword() {
String uri = "mysql://localhost:1527/big_db";