Home > Code > Simple Configuration Manager in Java

Simple Configuration Manager in Java

October 27th, 2009

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.

package com.karlockhart.util.config.configurator;

import java.io.*;
import java.io.Serializable;
import java.util.Hashtable;

public class Configurator implements Serializable{

 /**
  * This class is a simple configuration manager designed and built for
  * the management of the configurations of simple projects. This class
  * only allows for one configuration file and is implemented as a singleton
  * in order to share this file for the the entire project.
  *
  * @author Karl Lockhart – karlockhart.com
  *
  *
  */
 private static final long serialVersionUID = -3699362344368245416L;
 private static Configurator instance;
 
 //Hashtablse to store the configuration values
 private Hashtable<String, Boolean> booleanMap;
 private Hashtable<String, Integer> integerMap;
 private Hashtable<String, String> stringMap;
 
 //Dummy private constructor for Singleton pattern
 private Configurator(){
  
  booleanMap = new Hashtable<String, Boolean>();
  integerMap = new Hashtable<String, Integer>();
  stringMap = new Hashtable<String, String>();  
 }
 
 //Constructor for the singleton pattern
 private Configurator(String filename){
  
  booleanMap = new Hashtable<String, Boolean>();
  integerMap = new Hashtable<String, Integer>();
  stringMap = new Hashtable<String, String>();
  
  File test = new File(filename);
  if (test.exists())
  {
   FileInputStream fis = null;
   ObjectInputStream ois = null;
   Configurator temp = new Configurator();
  
   try{
    fis = new FileInputStream(filename);
    ois = new ObjectInputStream(fis);
    temp = (Configurator) ois.readObject();
    ois.close();
   } catch (Exception ex){
    /*
     * This error handling is almost certainly not the best
     * for your project
     */ 
    ex.printStackTrace();
   }
   this.booleanMap = temp.booleanMap;
   this.integerMap = temp.integerMap;
   this.stringMap = temp.stringMap;
  }
 }
 
 /**
  * This is the getInstance method of the Singleton Configurator.
  * The Configurator does not allow the user to instantiate the base
  * object. This is the only way to access the global instance of the
  * Configurator. 
  */
 
 public static Configurator getInstance(){
  if (instance == null){
   instance = new Configurator(“.//configurator.cfg”);
  }
  return instance;
  
 }

 //Boolean Properties 
 
 /**
  * Returns the value of the property referred to by this key.
  * This method is for boolean properties.
  *
  *@param key The text key that refers to the property you wish to retrieve
  *@return the value of the property referred to by the key
  */
 public boolean getPropertyBool(String key){
  return booleanMap.get(key);
 }
 
 /* Sets the value for the provided key. This method is
  * for boolean properties.
  * @param key The text key that the value is to be set for.
  * @param value The value to set in the property.
  */
 public void setPropertyBool (String key, boolean value){
  if (booleanMap.containsKey(key)){
   booleanMap.remove(key);
  }
  booleanMap.put(key, value);
 }
 
 /**
  * Returns true if this key exists and false if it does not.
  * This method is for boolean properties.
  * @param key The key to check for existence.
  */
 public boolean isBoolSet(String key){
  return booleanMap.containsKey(key);
 }
 
 /**
  * Deletes the property related to this key.
  * This method is for boolean properties.
  * @param key The key whose property will be deleted.
  */
 public void removeBoolKey(String key){
  if (booleanMap.containsKey(key)){
   booleanMap.remove(key);
  }  
 }

 /**
  * If the value contains the key return it and if not set the
  * passed value.
  * This method is for boolean properties.
  * @param key The key to check for existence.
  * @param value The value to set if this key does not exist
  * @return the value of the key
  */
 public boolean getExistOrDefaultBool(String key, boolean value){
  if(!booleanMap.containsKey(key))
   booleanMap.put(key, value);
  return booleanMap.get(key);
   
 }  
 
 //Integer Properties
 
 /**
  * Returns the value of the property referred to by this key.
  * This method is for integer properties.
  *
  *@param key The text key that refers to the property you wish to retrieve
  *@return the value of the property referred to by the key
  */
 public int getPropertyInt(String key){
  return integerMap.get(key);
 }

 /**
  * Sets the value for the provided key. This method is
  * for integer properties.
  * @param key The text key that the value is to be set for.
  * @param value The value to set in the property.
  */ 
 public void setPropertyInt (String key, int value){
  if (integerMap.containsKey(key)){
   integerMap.remove(key);
  }
  integerMap.put(key, value);
 }

 /**
  * Returns true if this key exists and false if it does not.
  * This method is for integer properties.
  * @param key The key to check for existence.
  */
 public boolean isIntSet(String key){
  return integerMap.containsKey(key);
 }

 /**
  * Deletes the property related to this key.
  * This method is for integer properties.
  * @param key The key whose property will be deleted.
  */
 public void removeIntKey(String key){
  if (integerMap.containsKey(key)){
   integerMap.remove(key);
  }  
 }
 
 /**
  * If the value contains the key return it and if not set the
  * passed value.
  * This method is for integer properties.
  * @param key The key to check for existence.
  * @param value The value to set if this key does not exist
  * @return the value of the key
  */
 public int getExistOrDefaultInt(String key, int value){
  if(!integerMap.containsKey(key))
   integerMap.put(key, value);
  return integerMap.get(key);
   
 } 
 
 //String Properties
 
 /**
  * Returns the value of the property referred to by this key.
  * This method is for string properties.
  *
  *@param key The text key that refers to the property you wish to retrieve
  *@return the value of the property referred to by the key
  */ 
 public String getPropertyStr(String key){
  return stringMap.get(key);
 }

 /**
  * Sets the value for the provided key. This method is
  * for string properties.
  * @param key The text key that the value is to be set for.
  * @param value The value to set in the property.
  */ 
 public void setPropertyStr (String key, String value){
  if (stringMap.containsKey(key)){
   stringMap.remove(key);
  }
  stringMap.put(key, value);
 }

 /**
  * Returns true if this key exists and false if it does not.
  * This method is for string properties.
  * @param key The key to check for existence.
  */
 public boolean isStrSet(String key){
  return stringMap.containsKey(key);
 }

 /**
  * If the value contains the key return it and if not set the
  * passed value.
  * This method is for string properties.
  * @param key The key to check for existence.
  * @param value The value to set if this key does not exist
  * @return the value of the key
  */
 public String getExistOrDefaultStr(String key, String value){
  if(!stringMap.containsKey(key))
   stringMap.put(key, value);
  return stringMap.get(key);
   
 } 
 
 /**
  * Deletes the property related to this key.
  * This method is for string properties.
  * @param key The key whose property will be deleted.
  */ 
 public void removeStrKey(String key){
  if (stringMap.containsKey(key)){
   stringMap.remove(key);
  }  
 }
 
 /**
  * Writes the configuration file to disk using serialization of
  * the Configurator object.
  *
  */
 public void writeConfiguration(){
  FileOutputStream fos = null;
  ObjectOutputStream oos = null;
  
  try{
   fos = new FileOutputStream(“.//configurator.cfg”);
   oos = new ObjectOutputStream(fos);
   oos.writeObject(this);
   oos.close();
  }catch (IOException ex){
   /*
    * This error handling is almost certainly not the best
    * for your project
    */    
   ex.printStackTrace();
  }
  
 }
}

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Code , , , , , ,

  1. No comments yet.
  1. No trackbacks yet.