tiny fixes here and there
[mir.git] / source / mir / config / MirPropertiesConfiguration.java
1 /*
2  * Copyright (C) 2005 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License.
22  * You must obey the GNU General Public License in all respects for all of the code used
23  * other than the above mentioned libraries.  If you modify this file, you may extend this
24  * exception to your version of the file, but you are not obligated to do so.
25  * If you do not wish to do so, delete this exception statement from your version.
26  */
27 package mir.config;
28
29 import multex.Exc;
30 import multex.Failure;
31 import org.apache.commons.collections.ExtendedProperties;
32
33 import javax.servlet.ServletContext;
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.Map;
39
40 /**
41  * @author idefix
42  */
43 public class MirPropertiesConfiguration extends ExtendedProperties {
44   private static MirPropertiesConfiguration instance;
45   private static ServletContext context;
46
47   private File home;
48
49   /**
50    * Constructor for MirPropertiesConfiguration.
51    */
52   private MirPropertiesConfiguration(ServletContext ctx) throws IOException {
53     //loading the defaults-config
54     super(ctx.getRealPath("/WEB-INF/") + "/default.properties");
55
56     //loading the user-config
57     ExtendedProperties userConfig =
58       new ExtendedProperties(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties");
59
60     //merging them to one config while overriding the defaults
61     this.combine(userConfig);
62
63     home = new File(ctx.getRealPath("/WEB-INF/")+"/");
64   }
65
66   public static synchronized MirPropertiesConfiguration instance() {
67     if (instance == null) {
68       if (context == null) {
69         throw new Error("Context was not set");
70       }
71
72       try {
73         instance = new MirPropertiesConfiguration(context);
74       }
75       catch (IOException e) {
76         throw new Error("cannot load configuration: " + e.toString());
77       }
78     }
79
80     return instance;
81   }
82
83   /**
84    * Sets the context.
85    *
86    * @param context The context to set
87    */
88   public static void setContext(ServletContext context) {
89     MirPropertiesConfiguration.context = context;
90   }
91
92   /**
93    * Returns the context.
94    *
95    * @return ServletContext
96    */
97   public static ServletContext getContext() {
98     return context;
99   }
100
101   /**
102    * Returns all properties in a Map
103    * @return Map
104    */
105   public Map allSettings() {
106     Iterator iterator = this.getKeys();
107     Map returnMap = new HashMap();
108     while (iterator.hasNext()) {
109       String key = (String) iterator.next();
110       Object o = this.getProperty(key);
111
112       returnMap.put(key, o);
113     }
114
115     // ML: hack for now
116     if (!returnMap.containsKey("Producer.DocRoot")) {
117       returnMap.put("Producer.DocRoot", "");
118     }
119
120     return returnMap;
121   }
122
123   /**
124    * Return mir's home directory.
125    * Normally this is the <code>WEB-INF</code> dir of the
126    * deployed mir servlets.
127    */
128   public File getHome() {
129     return home;
130   }
131
132   /**
133    * Returns a file based on a configuration setting.
134    *
135    * The file may be configured with an absolute path, or
136    * it may be a relative path.
137    *
138    * Relative paths work relative to {@link #home} :
139    * normally this is the <code>WEB-INF</code> dir in a
140    * deployed java servlet.
141    */
142   public File getFile(String aKey) {
143     String path = getString(aKey);
144
145     File result = new File(path);
146     if (result.isAbsolute()) {
147       return result;
148     }
149                 return new File(home, path);
150   }
151
152   /**
153    * @return the vlaue of this property as String
154    * @param key the key of this property
155    */
156   public String getString(String key) {
157     return getString(key, "");
158   }
159
160
161   /**
162    * @return the value of this property as String
163    * @param aKey the key of the property
164    * @param aDefaultValue the default value of this property if it is null
165    * @see org.apache.commons.collections.ExtendedProperties#getString(java.lang.String, java.lang.String)
166    */
167   public String getString(String aKey, String aDefaultValue) {
168     if (aDefaultValue == null) {
169       aDefaultValue = "";
170     }
171     Object result = getProperty(aKey);
172
173     if (result == null){
174       return aDefaultValue;
175     }
176
177     if (result instanceof String) {
178       return (String) result;
179     }
180
181     return result.toString();
182   }
183
184   public boolean getBoolean(String aKey, boolean aDefaultValue) {
185     try {
186       return getBoolean(aKey);
187     }
188     catch (Throwable t) {
189       return aDefaultValue;
190     }
191   }
192
193   public boolean getBoolean(String aKey) {
194     String value = getString(aKey).trim();
195
196     return "1".equals(value) || "y".equalsIgnoreCase(value) ||
197            "yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value);
198   }
199
200   /**
201    * Returns a property according to the given key
202    * @param key the key of the property
203    * @return the value of the property as Object, if no property available it returns a empty String
204    */
205   public Object getProperty(String key) {
206     if (super.getProperty(key) == null) {
207       return new String();
208     }
209
210     return super.getProperty(key);
211   }
212
213   /**
214    * @author idefix
215    */
216   public static class PropertiesConfigExc extends Exc {
217     /**
218      * Constructor for PropertiesConfigExc.
219      */
220     public PropertiesConfigExc(String msg) {
221       super(msg);
222     }
223   }
224
225   /**
226    * @author idefix
227    */
228   public static class PropertiesConfigFailure extends Failure {
229     /**
230      * Constructor for PropertiesConfigExc.
231      */
232     public PropertiesConfigFailure(String msg, Throwable cause) {
233       super(msg, cause);
234     }
235   }
236 }