View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003-2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111AssertLevel.NO_FUNCTION07 USA
18  */
19  package net.sf.jameleon.util;
20  
21  import java.io.FileInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.Properties;
27  import java.util.Set;
28  import java.util.TreeSet;
29  
30  public class Configurator {
31  
32      private static Configurator config;
33      public static final String DEFAULT_CONFIG_NAME = "jameleon.conf";
34      protected String configName = DEFAULT_CONFIG_NAME;
35      //Static because the Configurator instance gets cleared everytime between
36      //test case executions.
37      protected static boolean warningMsgGiven = false;
38  
39      protected Properties props;
40  
41      /***
42       * This is a singleton, use {@link #getInstance} instead
43       */
44      private Configurator(){
45      }
46  
47      /***
48       * Gets an instance Configurator if one has already been created or creates a new one if not.
49       * @return An instance Configurator if one has already been created or creates a new one if not.
50       */
51      public synchronized static Configurator getInstance(){
52          if (config == null) {
53              config = new Configurator();
54          }
55          return config;
56      }
57  
58      public Set getKeys(){
59          configure();
60          Set keys = null;
61          if (props != null) {
62              keys = props.keySet();
63          }
64          return keys;
65      }
66  
67      public Properties getProperties(){
68          configure();
69          return props;
70      }
71  
72      public Set getKeysStartingWith(String prefix){
73          configure();
74          Set matchingKeys = Collections.synchronizedSortedSet(new TreeSet());
75          if (props != null) {
76              Set keys = props.keySet();
77              Iterator it = keys.iterator();
78              String key;
79              while(it.hasNext()){
80                  key = (String)it.next();
81                  if (key.startsWith(prefix)) {
82                       matchingKeys.add(key);
83                  }
84              }
85          }
86          return matchingKeys;
87      }
88  
89      /***
90       * Gets a value from the provided configuration file or if it's not set, then set it's default value
91       * @param key - the name of the variable in the configuration file.
92       * @param defaultValue - the default value to set if the variable is not set in the configuration file.
93       * @return A value from the provided configuration file or a default value
94       */
95      public String getValue(String key, String defaultValue){
96          configure();
97          String value = defaultValue;
98          if (props != null) {
99              value = props.getProperty(key, defaultValue);
100         }
101         return value;
102     }
103 
104 
105     /***
106      * Gets a value from the provided configuration file.
107      * @param key - the name of the variable in the configuration file.
108      * @return A value from the provided configuration file.
109      */
110     public String getValue(String key){
111         configure();
112         String value = null;
113         if (props != null) {
114             value = props.getProperty(key);
115         }
116         return value;
117     }
118 
119     /***
120      * Gets a value from the provided configuration file as an array.
121      * This is used for space-separated variables that have values which represent arrays
122      * @return A value from the provided configuration file.
123      */
124     public String[] getValueAsArray(String key){
125         return getValueAsArray(key, " ");
126     }
127 
128     /***
129      * Gets a value from the provided configuration file as an array.
130      * This is used for space-separated variables that have values which represent arrays
131      * @return A value from the provided configuration file.
132      */
133     public String[] getValueAsArray(String key, String separator){
134         configure();
135         String[] values = null;
136         if (props != null) {
137             String value = props.getProperty(key);
138             if (value != null) {
139                 values = value.split(separator);
140             }
141         }
142         return values;
143     }
144 
145     /***
146      * Sets a value of a given property
147      * @param key - the name of the variable to set
148      * @param value - the value to the variable to.
149      */
150     public void setValue(String key, String value){
151         configure();
152         if (props != null) {
153             if (value != null) {
154                 props.setProperty(key, value);
155             }else{
156                 props.remove(key);
157             }
158         }
159     }
160 
161     /***
162      * Set everything up by reading the configuration file. This method is called
163      * by {@link #getValue} and is only executed if things aren't already set up.
164      * In other words, once a file has been read in, this method will no longer do 
165      * anything. To read in a new file, call {@link #clearInstance} first.
166      */
167     protected void configure(){
168         if (props == null) {
169             props = new Properties();
170             InputStream in = null;
171             try{
172                 in = getInputStream(configName);
173                 props.load(in);
174             }catch(IOException e){
175                 if (!warningMsgGiven) {
176                     System.err.println(configName+" not found. Using default settings.");
177                     warningMsgGiven = true;
178                 }
179             }catch(NullPointerException npe){
180                 //this is just an annoying message
181             }finally{
182                 if (in != null) {
183                     try{
184                         in.close();
185                     }catch(IOException ioe){
186                         ioe.printStackTrace();
187                     }
188                 }
189             }
190         }
191     }
192 
193     protected InputStream getInputStream(String fileName) throws IOException{
194         return new FileInputStream(fileName);
195     }
196 
197     /***
198      * Sets the name of the configuration file.
199      * @param configName - the name and path of the config file. The default location is from where the JVM was started from.
200      */
201     public void setConfigName(String configName){
202         this.configName = configName;
203     }
204 
205     /***
206      * Gets the name of the configuration file.
207      * @return the name and path of the config file.
208      */
209     public String getConfigName(){
210         return configName;
211     }
212 
213     /***
214      * Cleans everything up and sets the current instance to null
215      */
216     public synchronized static void clearInstance(){
217         if (config != null) {
218             if (config.props != null) {
219                 config.props.clear();
220                 config.props = null;
221             }
222             config.configName = null;
223             config = null;
224         }
225     }
226 }