some code cleanup. removed unnecessary semikolons, unused vars, etc.
[mir.git] / source / mir / config / MirPropertiesConfiguration.java
1 /*
2  * Copyright (C) 2001, 2002 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  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mir.config;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.Map;
37
38 import javax.servlet.ServletContext;
39
40 import multex.Exc;
41 import multex.Failure;
42
43 import org.apache.commons.collections.ExtendedProperties;
44
45 /**
46  * @author idefix
47  */
48 public class MirPropertiesConfiguration extends ExtendedProperties {
49   private static MirPropertiesConfiguration instance;
50   private static ServletContext context;
51
52   private File home;
53
54   /**
55    * Constructor for MirPropertiesConfiguration.
56    */
57   private MirPropertiesConfiguration(ServletContext ctx) throws IOException {
58     //loading the defaults-config
59     super(ctx.getRealPath("/WEB-INF/") + "/default.properties");
60
61     //loading the user-config
62     ExtendedProperties userConfig =
63       new ExtendedProperties(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties");
64
65     //merging them to one config while overriding the defaults
66     this.combine(userConfig);
67
68     home = new File(ctx.getRealPath("/WEB-INF/")+"/");
69   }
70
71   public static synchronized MirPropertiesConfiguration instance() {
72     if (instance == null) {
73       if (context == null) {
74         throw new Error("Context was not set");
75       }
76
77       try {
78         instance = new MirPropertiesConfiguration(context);
79       }
80       catch (IOException e) {
81         throw new Error("cannot load configuration: " + e.toString());
82       }
83     }
84
85     return instance;
86   }
87
88   /**
89    * Sets the context.
90    *
91    * @param context The context to set
92    */
93   public static void setContext(ServletContext context) {
94     MirPropertiesConfiguration.context = context;
95   }
96
97   /**
98    * Returns the context.
99    *
100    * @return ServletContext
101    */
102   public static ServletContext getContext() {
103     return context;
104   }
105
106   /**
107    * Returns all properties in a Map
108    * @return Map
109    */
110   public Map allSettings() {
111     Iterator iterator = this.getKeys();
112     Map returnMap = new HashMap();
113     while (iterator.hasNext()) {
114       String key = (String) iterator.next();
115       Object o = this.getProperty(key);
116
117       returnMap.put(key, o);
118     }
119
120     // ML: hack for now
121     if (!returnMap.containsKey("Producer.DocRoot")) {
122       returnMap.put("Producer.DocRoot", "");
123     }
124
125     return returnMap;
126   }
127
128   /**
129    * Return mir's home directory.
130    * Normally this is the <code>WEB-INF</code> dir of the
131    * deployed mir servlets.
132    */
133   public File getHome() {
134     return home;
135   }
136
137   /**
138    * Returns a file based on a configuration setting.
139    *
140    * The file may be configured with an absolute path, or
141    * it may be a relative path.
142    *
143    * Relative paths work relative to {@link #home} :
144    * normally this is the <code>WEB-INF</code> dir in a
145    * deployed java servlet.
146    */
147   public File getFile(String aKey) {
148     String path = getString(aKey);
149
150     File result = new File(path);
151     if (result.isAbsolute()) {
152       return result;
153     }
154                 return new File(home, path);
155   }
156
157   /**
158    * @return the vlaue of this property as String
159    * @param key the key of this property
160    */
161   public String getString(String key) {
162     return getString(key, "");
163   }
164
165
166   /**
167    * @return the value of this property as String
168    * @param key the key of the property
169    * @param defaultValue the default value of this property if it is null
170    * @see org.apache.commons.collections.ExtendedProperties#getString(java.lang.String, java.lang.String)
171    */
172   public String getString(String key, String defaultValue) {
173     Object object = getProperty(key);
174     if(object == null){
175       if (defaultValue == null) {
176         return new String();
177       }
178       return defaultValue;
179     }
180     if (object instanceof String) {
181       return (String)object;
182     }
183     return object.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 }