<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Life and Code &#187; configurator</title>
	<atom:link href="http://www.karlockhart.com/wordpress/tag/configurator/feed" rel="self" type="application/rss+xml" />
	<link>http://www.karlockhart.com/wordpress</link>
	<description>Pixels bleed just like us...</description>
	<lastBuildDate>Wed, 14 Jul 2010 17:13:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Simple Configuration Manager in Java</title>
		<link>http://www.karlockhart.com/wordpress/2009/10/27/simple-configuration-manager-in-java</link>
		<comments>http://www.karlockhart.com/wordpress/2009/10/27/simple-configuration-manager-in-java#comments</comments>
		<pubDate>Tue, 27 Oct 2009 06:18:13 +0000</pubDate>
		<dc:creator>karl.lockhart</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[configurator]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[manager]]></category>
		<category><![CDATA[serializable]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.karlockhart.com/wordpress/?p=109</guid>
		<description><![CDATA[I have decided to share a configuration manager I wrote. I use this to maintain configuration files for my projects. Remember, as usual this code is as is, and written for my uses, I only share as it may be useful to others. See the software links on the right for download.]]></description>
			<content:encoded><![CDATA[<p>I have decided to share a configuration manager I wrote. I use this to maintain configuration files for my projects. Remember, as usual this code is as is, and written for my uses, I only share as it may be useful to others. See the software links on the right for download.</p>
<p><span id="more-109"></span></p>
<blockquote><p>package com.karlockhart.util.config.configurator;</p>
<p>import java.io.*;<br />
import java.io.Serializable;<br />
import java.util.Hashtable;</p>
<p>public class Configurator implements Serializable{</p>
<p> /**<br />
  * This class is a simple configuration manager designed and built for<br />
  * the management of the configurations of simple projects. This class<br />
  * only allows for one configuration file and is implemented as a singleton<br />
  * in order to share this file for the the entire project.<br />
  *<br />
  * @author Karl Lockhart &#8211; karlockhart.com<br />
  *<br />
  *<br />
  */<br />
 private static final long serialVersionUID = -3699362344368245416L;<br />
 private static Configurator instance;<br />
 <br />
 //Hashtablse to store the configuration values<br />
 private Hashtable&lt;String, Boolean&gt; booleanMap;<br />
 private Hashtable&lt;String, Integer&gt; integerMap;<br />
 private Hashtable&lt;String, String&gt; stringMap;<br />
 <br />
 //Dummy private constructor for Singleton pattern<br />
 private Configurator(){<br />
  <br />
  booleanMap = new Hashtable&lt;String, Boolean&gt;();<br />
  integerMap = new Hashtable&lt;String, Integer&gt;();<br />
  stringMap = new Hashtable&lt;String, String&gt;();  <br />
 }<br />
 <br />
 //Constructor for the singleton pattern<br />
 private Configurator(String filename){<br />
  <br />
  booleanMap = new Hashtable&lt;String, Boolean&gt;();<br />
  integerMap = new Hashtable&lt;String, Integer&gt;();<br />
  stringMap = new Hashtable&lt;String, String&gt;();<br />
  <br />
  File test = new File(filename);<br />
  if (test.exists())<br />
  {<br />
   FileInputStream fis = null;<br />
   ObjectInputStream ois = null;<br />
   Configurator temp = new Configurator();<br />
  <br />
   try{<br />
    fis = new FileInputStream(filename);<br />
    ois = new ObjectInputStream(fis);<br />
    temp = (Configurator) ois.readObject();<br />
    ois.close();<br />
   } catch (Exception ex){<br />
    /*<br />
     * This error handling is almost certainly not the best<br />
     * for your project<br />
     */ <br />
    ex.printStackTrace();<br />
   }<br />
   this.booleanMap = temp.booleanMap;<br />
   this.integerMap = temp.integerMap;<br />
   this.stringMap = temp.stringMap;<br />
  }<br />
 }<br />
 <br />
 /**<br />
  * This is the getInstance method of the Singleton Configurator.<br />
  * The Configurator does not allow the user to instantiate the base<br />
  * object. This is the only way to access the global instance of the<br />
  * Configurator. <br />
  */<br />
 <br />
 public static Configurator getInstance(){<br />
  if (instance == null){<br />
   instance = new Configurator(&#8220;.//configurator.cfg&#8221;);<br />
  }<br />
  return instance;<br />
  <br />
 }</p>
<p> //Boolean Properties <br />
 <br />
 /**<br />
  * Returns the value of the property referred to by this key.<br />
  * This method is for boolean properties.<br />
  *<br />
  <a href="mailto:*@param">*@param</a> key The text key that refers to the property you wish to retrieve<br />
  <a href="mailto:*@return">*@return</a> the value of the property referred to by the key<br />
  */<br />
 public boolean getPropertyBool(String key){<br />
  return booleanMap.get(key);<br />
 }<br />
 <br />
 /* Sets the value for the provided key. This method is<br />
  * for boolean properties.<br />
  * @param key The text key that the value is to be set for.<br />
  * @param value The value to set in the property.<br />
  */<br />
 public void setPropertyBool (String key, boolean value){<br />
  if (booleanMap.containsKey(key)){<br />
   booleanMap.remove(key);<br />
  }<br />
  booleanMap.put(key, value);<br />
 }<br />
 <br />
 /**<br />
  * Returns true if this key exists and false if it does not.<br />
  * This method is for boolean properties.<br />
  * @param key The key to check for existence.<br />
  */<br />
 public boolean isBoolSet(String key){<br />
  return booleanMap.containsKey(key);<br />
 }<br />
 <br />
 /**<br />
  * Deletes the property related to this key.<br />
  * This method is for boolean properties.<br />
  * @param key The key whose property will be deleted.<br />
  */<br />
 public void removeBoolKey(String key){<br />
  if (booleanMap.containsKey(key)){<br />
   booleanMap.remove(key);<br />
  }  <br />
 }</p>
<p> /**<br />
  * If the value contains the key return it and if not set the<br />
  * passed value.<br />
  * This method is for boolean properties.<br />
  * @param key The key to check for existence.<br />
  * @param value The value to set if this key does not exist<br />
  * @return the value of the key<br />
  */<br />
 public boolean getExistOrDefaultBool(String key, boolean value){<br />
  if(!booleanMap.containsKey(key))<br />
   booleanMap.put(key, value);<br />
  return booleanMap.get(key);<br />
   <br />
 }  <br />
 <br />
 //Integer Properties<br />
 <br />
 /**<br />
  * Returns the value of the property referred to by this key.<br />
  * This method is for integer properties.<br />
  *<br />
  <a href="mailto:*@param">*@param</a> key The text key that refers to the property you wish to retrieve<br />
  <a href="mailto:*@return">*@return</a> the value of the property referred to by the key<br />
  */<br />
 public int getPropertyInt(String key){<br />
  return integerMap.get(key);<br />
 }</p>
<p> /**<br />
  * Sets the value for the provided key. This method is<br />
  * for integer properties.<br />
  * @param key The text key that the value is to be set for.<br />
  * @param value The value to set in the property.<br />
  */ <br />
 public void setPropertyInt (String key, int value){<br />
  if (integerMap.containsKey(key)){<br />
   integerMap.remove(key);<br />
  }<br />
  integerMap.put(key, value);<br />
 }</p>
<p> /**<br />
  * Returns true if this key exists and false if it does not.<br />
  * This method is for integer properties.<br />
  * @param key The key to check for existence.<br />
  */<br />
 public boolean isIntSet(String key){<br />
  return integerMap.containsKey(key);<br />
 }</p>
<p> /**<br />
  * Deletes the property related to this key.<br />
  * This method is for integer properties.<br />
  * @param key The key whose property will be deleted.<br />
  */<br />
 public void removeIntKey(String key){<br />
  if (integerMap.containsKey(key)){<br />
   integerMap.remove(key);<br />
  }  <br />
 }<br />
 <br />
 /**<br />
  * If the value contains the key return it and if not set the<br />
  * passed value.<br />
  * This method is for integer properties.<br />
  * @param key The key to check for existence.<br />
  * @param value The value to set if this key does not exist<br />
  * @return the value of the key<br />
  */<br />
 public int getExistOrDefaultInt(String key, int value){<br />
  if(!integerMap.containsKey(key))<br />
   integerMap.put(key, value);<br />
  return integerMap.get(key);<br />
   <br />
 } <br />
 <br />
 //String Properties<br />
 <br />
 /**<br />
  * Returns the value of the property referred to by this key.<br />
  * This method is for string properties.<br />
  *<br />
  <a href="mailto:*@param">*@param</a> key The text key that refers to the property you wish to retrieve<br />
  <a href="mailto:*@return">*@return</a> the value of the property referred to by the key<br />
  */ <br />
 public String getPropertyStr(String key){<br />
  return stringMap.get(key);<br />
 }</p>
<p> /**<br />
  * Sets the value for the provided key. This method is<br />
  * for string properties.<br />
  * @param key The text key that the value is to be set for.<br />
  * @param value The value to set in the property.<br />
  */ <br />
 public void setPropertyStr (String key, String value){<br />
  if (stringMap.containsKey(key)){<br />
   stringMap.remove(key);<br />
  }<br />
  stringMap.put(key, value);<br />
 }</p>
<p> /**<br />
  * Returns true if this key exists and false if it does not.<br />
  * This method is for string properties.<br />
  * @param key The key to check for existence.<br />
  */<br />
 public boolean isStrSet(String key){<br />
  return stringMap.containsKey(key);<br />
 }</p>
<p> /**<br />
  * If the value contains the key return it and if not set the<br />
  * passed value.<br />
  * This method is for string properties.<br />
  * @param key The key to check for existence.<br />
  * @param value The value to set if this key does not exist<br />
  * @return the value of the key<br />
  */<br />
 public String getExistOrDefaultStr(String key, String value){<br />
  if(!stringMap.containsKey(key))<br />
   stringMap.put(key, value);<br />
  return stringMap.get(key);<br />
   <br />
 } <br />
 <br />
 /**<br />
  * Deletes the property related to this key.<br />
  * This method is for string properties.<br />
  * @param key The key whose property will be deleted.<br />
  */ <br />
 public void removeStrKey(String key){<br />
  if (stringMap.containsKey(key)){<br />
   stringMap.remove(key);<br />
  }  <br />
 }<br />
 <br />
 /**<br />
  * Writes the configuration file to disk using serialization of<br />
  * the Configurator object.<br />
  *<br />
  */<br />
 public void writeConfiguration(){<br />
  FileOutputStream fos = null;<br />
  ObjectOutputStream oos = null;<br />
  <br />
  try{<br />
   fos = new FileOutputStream(&#8220;.//configurator.cfg&#8221;);<br />
   oos = new ObjectOutputStream(fos);<br />
   oos.writeObject(this);<br />
   oos.close();<br />
  }catch (IOException ex){<br />
   /*<br />
    * This error handling is almost certainly not the best<br />
    * for your project<br />
    */    <br />
   ex.printStackTrace();<br />
  }<br />
  <br />
 }<br />
}</p></blockquote>
<div class="lightsocial_container"><a class="lightsocial_a" href="http://digg.com/submit?url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java&amp;title=Simple+Configuration+Manager+in+Java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/digg.png" alt="Digg This" title="Digg This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.reddit.com/submit?url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java&amp;title=Simple+Configuration+Manager+in+Java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/reddit.png" alt="Reddit This" title="Reddit This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java&amp;title=Simple+Configuration+Manager+in+Java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/stumbleupon.png" alt="Stumble Now!" title="Stumble Now!" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://buzz.yahoo.com/buzz?targetUrl=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java&amp;headline=Simple+Configuration+Manager+in+Java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/yahoo_buzz.png" alt="Buzz This" title="Buzz This" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dzone.com/links/add.html?title=Simple+Configuration+Manager+in+Java&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/dzone.png" alt="Vote on DZone" title="Vote on DZone" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.facebook.com/sharer.php?t=Simple+Configuration+Manager+in+Java&amp;u=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/facebook.png" alt="Share on Facebook" title="Share on Facebook" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://delicious.com/save?title=Simple+Configuration+Manager+in+Java&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/delicious.png" alt="Bookmark this on Delicious" title="Bookmark this on Delicious" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.dotnetkicks.com/kick/?title=Simple+Configuration+Manager+in+Java&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/dotnetkicks.png" alt="Kick It on DotNetKicks.com" title="Kick It on DotNetKicks.com" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://dotnetshoutout.com/Submit?title=Simple+Configuration+Manager+in+Java&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/dotnetshoutout.png" alt="Shout it" title="Shout it" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java&amp;title=Simple+Configuration+Manager+in+Java&amp;summary=&amp;source=" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/linkedin.png" alt="Share on LinkedIn" title="Share on LinkedIn" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/technorati.png" alt="Bookmark this on Technorati" title="Bookmark this on Technorati" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://twitter.com/home?status=Reading+http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/twitter.png" alt="Post on Twitter" title="Post on Twitter" /></a>&nbsp;&nbsp;<a class="lightsocial_a" href="http://www.google.com/buzz/post?url=http%3A%2F%2Fwww.karlockhart.com%2Fwordpress%2F2009%2F10%2F27%2Fsimple-configuration-manager-in-java" ><img class="lightsocial_img" src="http://www.karlockhart.com/wordpress/wp-content/plugins/light-social/google_buzz.png" alt="Google Buzz (aka. Google Reader)" title="Google Buzz (aka. Google Reader)" /></a>&nbsp;&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://www.karlockhart.com/wordpress/2009/10/27/simple-configuration-manager-in-java/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
