KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > data > config > ConfigFileReader


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.data.config;
11
12 import java.util.*;
13 import java.io.*;
14
15 /**
16  * This is a Singleton Helper class to read a JGAP config file and provide a
17  * simple interface to the config properties.
18  *
19  * @author Siddhartha Azad
20  * @since 2.3
21  */

22 public class ConfigFileReader {
23   private final static String JavaDoc CVS_REVISION = "$Revision: 1.5 $";
24
25   // Name of the config file to read
26
private String JavaDoc m_fileName;
27
28   // Properties read from the config file
29
private Properties m_props;
30
31   // namespace of the property
32
private String JavaDoc m_ns;
33
34   /**
35    * Singleton Instance of ConfigFileReader
36    */

37   private static ConfigFileReader m_cfReader;
38
39   /**
40    * Method to create and access the Singleton ConfigFileReader instance.
41    * @return instance of the ConfigFileReader
42    *
43    * @author Siddhartha Azad
44    * @since 2.3
45    */

46   public static ConfigFileReader instance() {
47     if (m_cfReader == null) {
48       m_cfReader = new ConfigFileReader();
49     }
50     return m_cfReader;
51   }
52
53   /**
54    * Private Constructor.
55    *
56    * @author Siddhartha Azad
57    * @since 2.3
58    */

59   private ConfigFileReader() {
60     m_props = new Properties();
61   }
62
63   /**
64    * Retrieve the value for the property with the name as in param name.
65    * @param a_name name of the property of which the value is required
66    * @return value for the property with the name as in param name, null if
67    * property not found
68    *
69    * @author Siddhartha Azad
70    * @since 2.3
71    */

72   public String JavaDoc getValue(final String JavaDoc a_name) {
73     String JavaDoc tmpName = m_ns + "." + a_name;
74     String JavaDoc val = m_props.getProperty(tmpName);
75     return val;
76   }
77
78   /**
79    * Retrieve the values for the property with the name as in param name.
80    *
81    * @param a_name the name of the property of which the value is required
82    * @return ArrayList of Strings with values for the property with the name as
83    * in param name, null if property not found
84    *
85    * @author Siddhartha Azad
86    * */

87   public List getValues(final String JavaDoc a_name) {
88     String JavaDoc val = "";
89     boolean done = false;
90     String JavaDoc tmpName = "";
91     int idx = 0;
92     List values = Collections.synchronizedList(new ArrayList());
93     while (!done) {
94       tmpName = m_ns + "." + a_name + "[" + idx + "]";
95       val = m_props.getProperty(tmpName);
96       if (val == null) {
97         done = true;
98       }
99       else {
100         values.add(val);
101         idx++;
102       }
103     }
104     if (idx == 0) {
105       return null;
106     }
107     else {
108       return values;
109     }
110   }
111
112   /**
113    * Set the namespace for the properties that are being read from the
114    * config file at this point.
115    *
116    * @param a_ns namespace for the properties being read
117    *
118    * @author Siddhartha Azad
119    */

120   public void setNS(final String JavaDoc a_ns) {
121     m_ns = a_ns;
122   }
123
124   /**
125    * Set the config file to load from. Everytime this method is called,
126    * properties are reloaded from the config file.
127    *
128    * @param a_fileName Name of the config file.
129    * @throws ConfigException
130    *
131    * @author Siddhartha Azad
132    * @since 2.3
133    */

134   public void setFileName(final String JavaDoc a_fileName)
135       throws ConfigException {
136     m_fileName = a_fileName;
137     load();
138   }
139
140   /**
141    * Load the config properties file into a Properties instance.
142    * @throws ConfigException
143    *
144    * @author Siddhartha Azad
145    * @since 2.3
146    */

147   private void load()
148       throws ConfigException {
149     try {
150       m_props.load(new FileInputStream(m_fileName));
151     }
152     catch (Exception JavaDoc ex) {
153       String JavaDoc dir = new File(".").getAbsolutePath();
154       throw new ConfigException("Error reading Config file " + m_fileName
155                                 + " in directory " + dir);
156     }
157   }
158 }
159
Popular Tags