Added ConfigWrapper to unify the logic for locating configuration data.
This commit is contained in:
parent
e57a63b3aa
commit
a2c9348a99
@ -0,0 +1,81 @@
|
||||
package com.jdbernard.wdiwtlt
|
||||
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
|
||||
public class ConfigWrapper {
|
||||
|
||||
Properties configProps
|
||||
Map<String, String> env = System.getenv()
|
||||
|
||||
public ConfigWrapper() {
|
||||
if (env.WDIWTLT_CONFIG_FILE) {
|
||||
if (tryLoadConfigFile(new File(env.WDIWTLT_CONFIG_FILE))) {
|
||||
return } }
|
||||
|
||||
if (tryLoadConfigFile(new File(env.HOME, ".wdiwtlt.properties"))) return
|
||||
|
||||
try {
|
||||
ConfigWrapper.getResourceAsStream("/wdiwtlt.default.properties")
|
||||
.withStream { tryLoadConfig(is) } }
|
||||
catch (Exception e) {} }
|
||||
|
||||
public ConfigWrapper(File propertiesFile) {
|
||||
this.configPropertiesFile = configPropertiesFile }
|
||||
|
||||
public String getLibraryRootPath() {
|
||||
// 1. Check config properties
|
||||
if (configProps && configProps["library.dir"])
|
||||
return configProps["library.dir"]
|
||||
|
||||
// 2. Check environment variable
|
||||
if (env.WDIWTLT_LIBRARY_DIR)
|
||||
return env.WDIWTLT_LIBRARY_DIR
|
||||
|
||||
return null }
|
||||
|
||||
public String getHikariConfig() {
|
||||
Properties props = new Properties()
|
||||
|
||||
// 1. Check config properties for database config file
|
||||
if (configProps) {
|
||||
|
||||
// 1.1 Look for a reference to a dedicated config file
|
||||
if (configProps["database.config.file"]) {
|
||||
File cf = new File(configProps["database.config.file"])
|
||||
if (cf.exists() && cf.isFile())
|
||||
cf.withInputStream { props.load(it) } }
|
||||
|
||||
// 1.2 Look for prefixed properties in this config file
|
||||
else if (configProps["database.config.dataSourceClassName"]) {
|
||||
props.putAll(configProps
|
||||
.findAll { it.key.startsWith("database.config.") }
|
||||
.collectEntries { [it.key[16..-1], it.value] } ) } }
|
||||
|
||||
// 2. Look for environment variables
|
||||
if (!props) {
|
||||
if (env.WDIWTLT_DATASOURCE_CLASSNAME)
|
||||
props["dataSourceClassName"] = env.WDIWTLT_DATABASE_DATASOURCECLASSNAME
|
||||
|
||||
if (env.WDIWTLT_DATASOURCE_PROPERTIES) {
|
||||
props.putAll(env.WDIWTLT_DATASOURCE_PROPERTIES.split(":")
|
||||
.collect { it.split("=") }
|
||||
.collectEntries { ["dataSource.${it[0]}", it[1]] }) } }
|
||||
|
||||
// If properties exist, create and return HikariConfig
|
||||
if (props) return new HikariConfig(props)
|
||||
else return null }
|
||||
|
||||
private boolean tryLoadConfigFile(File configFile) {
|
||||
if (!configFile.exists() || !configFile.isFile()) return false
|
||||
|
||||
return configFile.withInputStream { tryLoadConfig(is) } }
|
||||
|
||||
private boolean tryLoadConfig(InputStream is) {
|
||||
try {
|
||||
Properties props = new Properties()
|
||||
props.load(is)
|
||||
this.configProps = props }
|
||||
catch (Exception e) { return false }
|
||||
|
||||
return true }
|
||||
}
|
Loading…
Reference in New Issue
Block a user