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