Runtime properties

This commit is contained in:
Claude Brisson
2023-06-05 17:58:17 +02:00
parent 121e1a22b7
commit eb1f0c4040
7 changed files with 44 additions and 45 deletions

View File

@@ -55,32 +55,14 @@ class WebappManager : ServletContextListener, ServletContextAttributeListener, H
context.setAttribute("manager", this)
webappRoot = context.getRealPath("/")
try {
properties.load(context.getResourceAsStream("/WEB-INF/pairgoth.properties"))
val submaps: MutableMap<String, MutableMap<String, String>> = HashMap()
for (prop in properties.stringPropertyNames()) {
val value = properties.getProperty(prop)
// filter out missing values and passwords
if (value.startsWith("\${") || prop.contains("password")) continue
context.setAttribute(prop, value)
// also support one level of submaps (TODO - more)
val dot = prop.indexOf('.')
if (dot != -1) {
val topKey = prop.substring(0, dot)
val subKey = prop.substring(dot + 1)
if ("password" == subKey) continue
var submap = submaps[topKey]
if (submap == null) {
submap = HashMap()
submaps[topKey] = submap
}
submap[subKey] = value
}
}
for ((key, value) in submaps) {
context.setAttribute(key, value)
// load default properties
properties.load(context.getResourceAsStream("/WEB-INF/pairgoth.default.properties"))
// override with system properties after stripping off the 'pairgoth.' prefix
System.getProperties().filter { (key, value) -> key is String && key.startsWith(PAIRGOTH_PROPERTIES_PREFIX)
}.forEach { (key, value) ->
properties[(key as String).removePrefix(PAIRGOTH_PROPERTIES_PREFIX)] = value
}
logger.info("Using profile {}", properties.getProperty("webapp.env"))
// set system user agent string to empty string
@@ -119,6 +101,7 @@ class WebappManager : ServletContextListener, ServletContextAttributeListener, H
override fun sessionDestroyed(se: HttpSessionEvent) {}
companion object {
const val PAIRGOTH_PROPERTIES_PREFIX = "pairgoth."
lateinit var webappRoot: String
lateinit var context: ServletContext
private val webServices: MutableMap<String?, Pair<Runnable, Thread?>> = TreeMap()

View File

@@ -1,2 +0,0 @@
format = [%level] %ip [%logger] %message (@%file:%line:%column)
level = trace

View File

@@ -0,0 +1,14 @@
# webapp
webapp.env = dev
webapp.url = http://localhost:8080
# smtp
smtp.sender =
smtp.host =
smtp.port = 587
smtp.user =
smtp.password =
# logging
logger.level = trace
logger.format = [%level] %ip [%logger] %message

View File

@@ -53,17 +53,4 @@
<param-name>webapp-slf4j-logger.format</param-name>
<param-value>%logger [%level] [%ip] %message @%file:%line:%column</param-value>
</context-param>
<context-param>
<param-name>webapp-slf4j-logger.level</param-name>
<param-value>trace</param-value>
<!-- CB TODO parametrize from webapp properties file
<param-value>${logger.level}</param-value>
-->
</context-param>
<!--
<context-param>
<param-name>webapp-slf4j-logger.notification</param-name>
<param-value>${logger.notification}</param-value>
</context-param>
-->
</web-app>