This is a Spring Framework-based datasource instantiator which reads its properties from the Spring Framework context, i.e. the values are initially all String
from properties files and numbers have to be tested for presence and validity.
Normally it would be totally superfluous in Spring Framework because the purpose of Spring Framework is to allow Java developers to by-pass it all. However sometimes as in this case, something is different and it has to be done manually.
My main concern with this method is the repeated use of the couldBe
variable which is an attempt to make it more readable.
couldBe
is the possibly hopefully valid number retrieved from the properties file, which could in fact be invalid.
This is all that is in the class:
private final Pattern testForInt =
Pattern.compile(GeneralUtils.REGEX_TEST_FOR_INT);
@Bean
@ConfigurationProperties(prefix = "tardis.datasource")
public Map<String, String> dataSourceProperties() {
// Spring populates the map with properties prefixed as above
return new HashMap<>();
}
@Bean
public DataSource dataSource() {
Map<String, String> props = dataSourceProperties();
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setDriverClassName(props.get("driver"));
dataSourceConfig.setUsername(props.get("username"));
dataSourceConfig.setPassword(props.get("password"));
dataSourceConfig.setJdbcUrl(props.get("url"));
String couldBe = props.get("maximumPoolSize");
if (couldBe != null && testForInt.matcher(couldBe).find()) {
dataSourceConfig.setMaximumPoolSize(Integer.valueOf(couldBe));
}
couldBe = props.get("maxLifeTime");
if (couldBe != null && testForInt.matcher(couldBe).find()) {
dataSourceConfig.setMaxLifetime(Long.valueOf(couldBe));
}
couldBe = props.get("leakDetectionThreshold");
if (couldBe != null && testForInt.matcher(couldBe).find()) {
dataSourceConfig.setLeakDetectionThreshold(Long.valueOf(couldBe));
}
couldBe = props.get("connectionTimeout");
if (couldBe != null && testForInt.matcher(couldBe).find()) {
dataSourceConfig.setConnectionTimeout(Long.valueOf(couldBe));
}
return new HikariDataSource(dataSourceConfig);
}
DatasourceConfiguration
interface withint getMaxLifeTime()
and do the magic within the implementation (e.g. a privategetInt(String property, Integer default)
method). \$\endgroup\$ – slowy May 17 '17 at 16:53HikariConfig
- so I need to let Spring initialise that, instead of the map, by changing all my property names - as long as that bit of magic with the property prefix annotation works. \$\endgroup\$ – Adam May 18 '17 at 9:08HikariConfig
I suspect it will silently fail to initialiseint
values when the property value is invalid. \$\endgroup\$ – Adam May 19 '17 at 8:35