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) {
		super(id, uriString);
		this.jdbcUrlDatabaseType = jdbcUrlDatabaseType;
	}

	@Override
	public UriInfoFactory getUriInfoFactory() {
		return jdbcUriInfoFactory;
	}

	@ServiceProperty(category = "connection")
	public String getJdbcUrl() {
		if (getUriInfo().getUriString().startsWith(JDBC_PREFIX)) {
			return getUriInfo().getUriString();
		}

		return String.format("%s%s://%s%s/%s%s%s", JDBC_PREFIX, jdbcUrlDatabaseType, getHost(), formatPort(),
				getPath(), formatUserinfo(), formatQuery());
	}

	private String formatPort() {
		if (getPort() != -1) {
			return String.format(":%d", getPort());
		}
		return "";
	}

	private String formatUserinfo() {
		if (getUserName() != null && getPassword() != null) {
			return String.format("?user=%s&password=%s", getUserName(), getPassword());
		}
		if (getUserName() != null) {
			return String.format("?user=%s", getUserName());
		}
		return "";
	}

	private String formatQuery() {
		if (getQuery() != null) {
			if (getUserName() == null && getPassword() == null) {
				return String.format("?%s", getQuery());
			} else {
				return String.format("&%s", getQuery());
			}
		}
		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);
		}
	}
}

@@ -109,171 +109,8 @@
 nfo;
-%0Aimport org.springframework.cloud.util.StandardUriInfoFactory;%0Aimport org.springframework.cloud.util.UriInfo;%0Aimport org.springframework.cloud.util.UriInfoFactory;
 %0A%0A/*
@@ -298,90 +298,8 @@
 c:%22;
-%0A%09private static JdbcUriInfoFactory jdbcUriInfoFactory = new JdbcUriInfoFactory();
 %0A%0A%09p
@@ -513,97 +513,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public UriInfoFactory getUriInfoFactory() %7B%0A%09%09return jdbcUriInfoFactory;%0A%09%7D%0A%0A
 %09@Se
@@ -1466,24 +1466,24 @@
 );%0A%09%09%09%7D%0A%09%09%7D%0A
+
 %09%09return %22%22;
@@ -1489,400 +1489,7 @@
 ;%0A%09%7D
-%0A%0A%09public static class JdbcUriInfoFactory extends StandardUriInfoFactory %7B%0A%09%09@Override%0A%09%09public UriInfo createUri(String uriString) %7B%0A%09%09%09if (uriString.startsWith(JDBC_PREFIX)) %7B%0A%09%09%09%09return new JdbcUriInfo(uriString);%0A%09%09%09%7D%0A%09%09%09return super.createUri(uriString);%0A%09%09%7D%0A%09%7D%0A%0A%09public static class JdbcUriInfo extends UriInfo %7B%0A%09%09public JdbcUriInfo(String rawUriString) %7B%0A%09%09%09super(rawUriString);%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Remove JDBC-specific extension to StandardUriInfoFactory, which was made obsolete by the previous commit.