2 * Copyright (C) 2005 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
30 import multex.Failure;
31 import org.apache.commons.collections.ExtendedProperties;
33 import javax.servlet.ServletContext;
35 import java.io.IOException;
36 import java.util.HashMap;
37 import java.util.Iterator;
43 public class MirPropertiesConfiguration extends ExtendedProperties {
44 private static MirPropertiesConfiguration instance;
45 private static ServletContext context;
50 * Constructor for MirPropertiesConfiguration.
52 private MirPropertiesConfiguration(ServletContext ctx) throws IOException {
53 //loading the defaults-config
54 super(ctx.getRealPath("/WEB-INF/") + "/default.properties");
56 //loading the user-config
57 ExtendedProperties userConfig =
58 new ExtendedProperties(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties");
60 //merging them to one config while overriding the defaults
61 this.combine(userConfig);
63 home = new File(ctx.getRealPath("/WEB-INF/")+"/");
66 public static synchronized MirPropertiesConfiguration instance() {
67 if (instance == null) {
68 if (context == null) {
69 throw new Error("Context was not set");
73 instance = new MirPropertiesConfiguration(context);
75 catch (IOException e) {
76 throw new Error("cannot load configuration: " + e.toString());
86 * @param context The context to set
88 public static void setContext(ServletContext context) {
89 MirPropertiesConfiguration.context = context;
93 * Returns the context.
95 * @return ServletContext
97 public static ServletContext getContext() {
102 * Returns all properties in a Map
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);
112 returnMap.put(key, o);
116 if (!returnMap.containsKey("Producer.DocRoot")) {
117 returnMap.put("Producer.DocRoot", "");
124 * Return mir's home directory.
125 * Normally this is the <code>WEB-INF</code> dir of the
126 * deployed mir servlets.
128 public File getHome() {
133 * Returns a file based on a configuration setting.
135 * The file may be configured with an absolute path, or
136 * it may be a relative path.
138 * Relative paths work relative to {@link #home} :
139 * normally this is the <code>WEB-INF</code> dir in a
140 * deployed java servlet.
142 public File getFile(String aKey) {
143 String path = getString(aKey);
145 File result = new File(path);
146 if (result.isAbsolute()) {
149 return new File(home, path);
153 * @return the vlaue of this property as String
154 * @param key the key of this property
156 public String getString(String key) {
157 return getString(key, "");
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)
167 public String getString(String aKey, String aDefaultValue) {
168 if (aDefaultValue == null) {
171 Object result = getProperty(aKey);
174 return aDefaultValue;
177 if (result instanceof String) {
178 return (String) result;
181 return result.toString();
184 public boolean getBoolean(String aKey, boolean aDefaultValue) {
186 return getBoolean(aKey);
188 catch (Throwable t) {
189 return aDefaultValue;
193 public boolean getBoolean(String aKey) {
194 String value = getString(aKey).trim();
196 return "1".equals(value) || "y".equalsIgnoreCase(value) ||
197 "yes".equalsIgnoreCase(value) || "true".equalsIgnoreCase(value);
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
205 public Object getProperty(String key) {
206 if (super.getProperty(key) == null) {
210 return super.getProperty(key);
216 public static class PropertiesConfigExc extends Exc {
218 * Constructor for PropertiesConfigExc.
220 public PropertiesConfigExc(String msg) {
228 public static class PropertiesConfigFailure extends Failure {
230 * Constructor for PropertiesConfigExc.
232 public PropertiesConfigFailure(String msg, Throwable cause) {