From 7b3be90fe1a107762d710309fa1ed2a402c316aa Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 21 Jan 2011 14:03:05 -0600 Subject: [PATCH] Updated SmartConfig Added support for file/directory property types. Removed some extraneous logging checks. --- project.properties | 6 +-- .../com/jdbernard/util/SmartConfig.groovy | 41 +++++++++++++++---- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/project.properties b/project.properties index 368104d..38a9df8 100644 --- a/project.properties +++ b/project.properties @@ -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 diff --git a/src/main/com/jdbernard/util/SmartConfig.groovy b/src/main/com/jdbernard/util/SmartConfig.groovy index f80e315..dadeda2 100644 --- a/src/main/com/jdbernard/util/SmartConfig.groovy +++ b/src/main/com/jdbernard/util/SmartConfig.groovy @@ -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)) - public save() { + 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() } }