Updated SmartConfig

Added support for file/directory property types.
Removed some extraneous logging checks.
This commit is contained in:
Jonathan Bernard 2011-01-21 14:03:05 -06:00
parent 2d7e8e67ca
commit 7b3be90fe1
2 changed files with 35 additions and 12 deletions

View File

@ -1,6 +1,6 @@
#Fri, 21 Jan 2011 13:00:05 -0600
#Fri, 21 Jan 2011 13:58:02 -0600
name=jdb-util
version=1.0
version=1.1
lib.local=true
build.number=4
build.number=1

View File

@ -18,9 +18,14 @@ public class SmartConfig {
catch (Exception e) { log.warn("Cannot open config file.", e) }
}
public SmartConfig(String filename) { this(new File(filename)) }
public SmartConfig(String filename) {
this(new File(filename))
log.trace("Loading configuration from {}",
new File(filename).canonicalPath)
}
public save() {
log.trace("Saving changes.")
try {file.withOutputStream { os -> this.@props.store(os, "") } }
catch (Exception e) { log.warn("Cannot save config file.", e) }
}
@ -29,34 +34,52 @@ public class SmartConfig {
def getProperty(String name, Object defVal) {
if (log.isTraceEnabled()) log.trace("Looking up $name")
log.trace("Looking up {}", name)
def val = props.getProperty(name)
if (val == null) {
if (log.isTraceEnabled())
log.trace("Doesn't exists, setting with given default")
log.trace("Doesn't exists, setting with given default")
val = defVal
props.setProperty(name, defVal.toString())
this.setProperty(name, defVal)
} else {
// boolean
if (name ==~ /.*\?/) {
if (log.isTraceEnabled()) log.trace("Interpreting as a boolean")
log.trace("Interpreting as a boolean")
if (val.toLowerCase() =~ /(false|no|off|f|n)/)
val = false
else val = (Boolean) val
}
// directory or file
else if (name ==~ /.*([dD]irectory|[dD]ir|[fF]ile)/) {
log.trace("Interpreting as a directory.")
val = new File(val)
} else {
log.trace("No known interpretation, casting to type of " +
"default argument.")
val = val.asType(defVal.class)
}
}
return val.asType(defVal.class)
return val
}
void setProperty(String name, def value) {
props."$name" = value.toString()
log.trace("Setting property: {}", name)
if (name ==~ /.*([dD]irectory|[dD]ir|[fF]ile)/) {
log.trace("Interpreting as a file/directory.")
props."$name" = value.canonicalPath
} else {
props."$name" = value.toString()
}
save()
}
}