79c19de070cce83b772e7b1c151f9fb193f0b70c
[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 java.io.File;
30 import java.io.IOException;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.Map;
34
35 import javax.servlet.ServletContext;
36
37 import multex.Exc;
38 import multex.Failure;
39
40 import org.apache.commons.collections.ExtendedProperties;
41
42 /**
43  * @author idefix
44  */
45 public class MirPropertiesConfiguration extends ExtendedProperties {
46   private static MirPropertiesConfiguration instance;
47   private static ServletContext context;
48
49   private File home;
50
51   /**
52    * Constructor for MirPropertiesConfiguration.
53    */
54   private MirPropertiesConfiguration(ServletContext ctx) throws IOException {
55     //loading the defaults-config
56     super(ctx.getRealPath("/WEB-INF/") + "/default.properties");
57
58     //loading the user-config
59     ExtendedProperties userConfig =
60       new ExtendedProperties(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties");
61
62     //merging them to one config while overriding the defaults
63     this.combine(userConfig);
64
65     home = new File(ctx.getRealPath("/WEB-INF/")+"/");
66   }
67
68   public static synchronized MirPropertiesConfiguration instance() {
69     if (instance == null) {
70       if (context == null) {
71         throw new Error("Context was not set");
72       }
73
74       try {
75         instance = new MirPropertiesConfiguration(context);
76       }
77       catch (IOException e) {
78         throw new Error("cannot load configuration: " + e.toString());
79       }
80     }
81
82     return instance;
83   }
84
85   /**
86    * Sets the context.
87    *
88    * @param context The context to set
89    */
90   public static void setContext(ServletContext context) {
91     MirPropertiesConfiguration.context = context;
92   }
93
94   /**
95    * Returns the context.
96    *
97    * @return ServletContext
98    */
99   public static ServletContext getContext() {
100     return context;
101   }
102
103   /**
104    * Returns all properties in a Map
105    * @return Map
106    */
107   public Map allSettings() {
108     Iterator iterator = this.getKeys();
109     Map returnMap = new HashMap();
110     while (iterator.hasNext()) {
111       String key = (String) iterator.next();
112       Object o = this.getProperty(key);
113
114       returnMap.put(key, o);
115     }
116
117     // ML: hack for now
118     if (!returnMap.containsKey("Producer.DocRoot")) {
119       returnMap.put("Producer.DocRoot", "");
120     }
121
122     return returnMap;
123   }
124
125   /**
126    * Return mir's home directory.
127    * Normally this is the <code>WEB-INF</code> dir of the
128    * deployed mir servlets.
129    */
130   public File getHome() {
131     return home;
132   }
133
134   /**
135    * Returns a file based on a configuration setting.
136    *
137    * The file may be configured with an absolute path, or
138    * it may be a relative path.
139    *
140    * Relative paths work relative to {@link #home} :
141    * normally this is the <code>WEB-INF</code> dir in a
142    * deployed java servlet.
143    */
144   public File getFile(String aKey) {
145     String path = getString(aKey);
146
147     File result = new File(path);
148     if (result.isAbsolute()) {
149       return result;
150     }
151                 return new File(home, path);
152   }
153
154   /**
155    * @return the vlaue of this property as String
156    * @param key the key of this property
157    */
158   public String getString(String key) {
159     return getString(key, "");
160   }
161
162
163   /**
164    * @return the value of this property as String
165    * @param aKey the key of the property
166    * @param aDefaultValue the default value of this property if it is null
167    * @see org.apache.commons.collections.ExtendedProperties#getString(java.lang.String, java.lang.String)
168    */
169   public String getString(String aKey, String aDefaultValue) {
170     if (aDefaultValue == null) {
171       aDefaultValue = "";
172     }
173     Object result = getProperty(aKey);
174
175     if (result == null){
176       return aDefaultValue;
177     }
178
179     if (result instanceof String) {
180       return (String) result;
181     }
182
183     return result.toString();
184   }
185
186   public boolean getBoolean(String aKey, boolean aDefaultValue) {
187     try {
188       return getBoolean(aKey);
189     }
190     catch (Throwable t) {
191       return aDefaultValue;
192     }
193   }
194
195   public boolean getBoolean(String aKey) {
196     String value = getString(aKey).trim();
197
198     return "1".equals(value) || "y".equalsIgnoreCase(value) ||
199            "yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value);
200   }
201
202   /**
203    * Returns a property according to the given key
204    * @param key the key of the property
205    * @return the value of the property as Object, if no property available it returns a empty String
206    */
207   public Object getProperty(String key) {
208     if (super.getProperty(key) == null) {
209       return new String();
210     }
211
212     return super.getProperty(key);
213   }
214
215   /**
216    * @author idefix
217    */
218   public static class PropertiesConfigExc extends Exc {
219     /**
220      * Constructor for PropertiesConfigExc.
221      */
222     public PropertiesConfigExc(String msg) {
223       super(msg);
224     }
225   }
226
227   /**
228    * @author idefix
229    */
230   public static class PropertiesConfigFailure extends Failure {
231     /**
232      * Constructor for PropertiesConfigExc.
233      */
234     public PropertiesConfigFailure(String msg, Throwable cause) {
235       super(msg, cause);
236     }
237   }
238 }