From 9a179af663fea9bae575da944516aa9883bbc0c9 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 21 Jan 2011 12:58:35 -0600 Subject: [PATCH] Implementation of SmartConfig --- .../com/jdbernard/util/SmartConfig.groovy | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/main/com/jdbernard/util/SmartConfig.groovy b/src/main/com/jdbernard/util/SmartConfig.groovy index e69de29..f80e315 100644 --- a/src/main/com/jdbernard/util/SmartConfig.groovy +++ b/src/main/com/jdbernard/util/SmartConfig.groovy @@ -0,0 +1,62 @@ +package com.jdbernard.util + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SmartConfig { + + private File file + private Properties props + private Logger log = LoggerFactory.getLogger(getClass()) + + public SmartConfig(File file) { + this.@file = file + this.@props = new Properties() + + try { file.withInputStream { is -> this.@props.load(is) } } + catch (FileNotFoundException fnfe) {} + catch (Exception e) { log.warn("Cannot open config file.", e) } + } + + public SmartConfig(String filename) { this(new File(filename)) } + + public save() { + try {file.withOutputStream { os -> this.@props.store(os, "") } } + catch (Exception e) { log.warn("Cannot save config file.", e) } + } + + def getProperty(String name) { getProperty(name, "") } + + def getProperty(String name, Object defVal) { + + if (log.isTraceEnabled()) 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") + + val = defVal + props.setProperty(name, defVal.toString()) + + } else { + + // boolean + if (name ==~ /.*\?/) { + if (log.isTraceEnabled()) log.trace("Interpreting as a boolean") + + if (val.toLowerCase() =~ /(false|no|off|f|n)/) + val = false + else val = (Boolean) val + } + } + + return val.asType(defVal.class) + } + + void setProperty(String name, def value) { + props."$name" = value.toString() + save() + } +}