DBVIEW
|
00001 /* 00002 DbView - Graph Visualization 00003 Copyright (C) 2012 Denis BEURIVE 00004 00005 This program is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 /** 00020 * @author Denis Beurive 00021 */ 00022 00023 package org.dbview.conf; 00024 00025 import java.util.Properties; 00026 import java.io.FileReader; 00027 import java.io.File; 00028 import java.net.URL; 00029 import java.util.Enumeration; 00030 import java.util.Hashtable; 00031 00032 /** 00033 * This class contains the software's configuration. 00034 * @remark Please note that the configuration is provided by an external files that contains "properties". 00035 * @see http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Properties.html 00036 * @author Denis BEURIVE 00037 */ 00038 public class Conf 00039 { 00040 /** 00041 * This array contains the list of configuration <i>external</i> files to load. 00042 */ 00043 private static final String[] CONF_FILES = { "resources.properties" }; 00044 00045 /** 00046 * This hash table contains the loaded configuration. 00047 */ 00048 private static Hashtable<String, String> __conf = new Hashtable<String, String>(); 00049 00050 /** 00051 * This flag indicates whether the configuration has been loaded or not. 00052 */ 00053 private static Boolean ininialized = Boolean.FALSE; 00054 00055 /** 00056 * This method initializes the configuration. 00057 * This method loads all external configuration files and build the global configuration. 00058 * @throws Exception 00059 */ 00060 public static void init() throws Exception 00061 { 00062 if (Conf.ininialized) { return; } 00063 00064 for (String conf_file : Conf.CONF_FILES) 00065 { 00066 // Search for the configuration file 00067 URL conf_url = Conf.class.getClassLoader().getResource(conf_file); 00068 if (null == conf_url) { throw new Exception("Could not find my configuration file " + conf_file + "!"); } 00069 00070 // Load configuration file 00071 Properties p = new Properties(); 00072 p.load(new FileReader(new File(conf_url.toURI()))); 00073 00074 // Trim spaces 00075 Enumeration<Object> e = p.keys(); 00076 while (Boolean.TRUE) 00077 { 00078 if (! e.hasMoreElements()) { break; } 00079 String key = ((String)e.nextElement()).trim(); 00080 00081 if (Conf.__conf.containsKey(key)) 00082 { throw new Exception("The configuration's entry \"" + key + "\" is defined twice!"); } 00083 00084 String value = ((String)p.get(key)).trim(); 00085 Conf.__conf.put(key, value); 00086 } 00087 } 00088 00089 Conf.ininialized = Boolean.TRUE; 00090 } 00091 00092 /** 00093 * This method returns the value of a given property. 00094 * @param in_key Name of the property to get. 00095 * @return The method returns the value associated with the given property's name. 00096 * @throws Exception 00097 */ 00098 public static String get(String in_key) throws Exception 00099 { 00100 if (! Conf.ininialized) { Conf.init(); } 00101 return Conf.__conf.get(in_key); 00102 } 00103 }