Skip to content

Commit

Permalink
Improve DataSourceProperties exception messages
Browse files Browse the repository at this point in the history
Update DataSourceProperties exceptions to include a less misleading
message. Errors message now note that you may need to add an embedded
database to the classpath or active a profile to pickup specific
settings.

Fixes spring-projectsgh-4012
  • Loading branch information
philwebb committed Sep 24, 2015
1 parent 44c1223 commit 06f3202
Showing 1 changed file with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
Expand All @@ -39,10 +42,15 @@
* @since 1.1.0
*/
@ConfigurationProperties(prefix = DataSourceProperties.PREFIX)
public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean {
public class DataSourceProperties implements BeanClassLoaderAware, EnvironmentAware,
InitializingBean {

public static final String PREFIX = "spring.datasource";

private ClassLoader classLoader;

private Environment environment;

/**
* Name of the datasource.
*/
Expand Down Expand Up @@ -74,8 +82,6 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
*/
private String password;

private ClassLoader classLoader;

/**
* JNDI location of the datasource. Class, url, username & password are ignored when
* set.
Expand Down Expand Up @@ -126,6 +132,11 @@ public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}

@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}

@Override
public void afterPropertiesSet() throws Exception {
this.embeddedDatabaseConnection = EmbeddedDatabaseConnection
Expand Down Expand Up @@ -165,11 +176,8 @@ public String getDriverClassName() {
}

if (!StringUtils.hasText(driverClassName)) {
throw new BeanCreationException(
"Cannot determine embedded database driver class for database type "
+ this.embeddedDatabaseConnection
+ ". If you want an embedded "
+ "database please put a supported one on the classpath.");
throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection,
this.environment, "driver class");
}
return driverClassName;
}
Expand All @@ -184,11 +192,8 @@ public String getUrl() {
}
String url = this.embeddedDatabaseConnection.getUrl();
if (!StringUtils.hasText(url)) {
throw new BeanCreationException(
"Cannot determine embedded database url for database type "
+ this.embeddedDatabaseConnection
+ ". If you want an embedded "
+ "database please put a supported one on the classpath.");
throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection,
this.environment, "url");
}
return url;
}
Expand Down Expand Up @@ -340,4 +345,38 @@ public void setProperties(Map<String, String> properties) {

}

private static class DataSourceBeanCreationException extends BeanCreationException {

public DataSourceBeanCreationException(EmbeddedDatabaseConnection connection,
Environment environment, String property) {
super(getMessage(connection, environment, property));
}

private static String getMessage(EmbeddedDatabaseConnection connection,
Environment environment, String property) {
StringBuilder message = new StringBuilder();
message.append("Cannot determine embedded database " + property
+ " for database type " + connection + ". ");
message.append("If you want an embedded database please put a supported "
+ "one on the classpath. ");
message.append("If you have database settings to be loaded from a "
+ "particular profile you may need to active it");
if (environment != null) {
String[] profiles = environment.getActiveProfiles();
if (ObjectUtils.isEmpty(profiles)) {
message.append(" (no profiles are currently active)");
}
else {
message.append(" (the profiles \""
+ StringUtils.arrayToCommaDelimitedString(environment
.getActiveProfiles()) + "\" are currently active)");

}
}
message.append(".");
return message.toString();
}

}

}

0 comments on commit 06f3202

Please sign in to comment.