2 * Copyright (C) 2001, 2002 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 * 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.
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.Iterator;
39 import javax.servlet.ServletContext;
42 import multex.Failure;
44 import org.apache.commons.collections.ExtendedProperties;
50 public class MirPropertiesConfiguration extends ExtendedProperties {
51 private static MirPropertiesConfiguration instance;
52 private static ServletContext context;
53 private static String contextPath;
55 //if one of these properties is not present a new
56 //property is added with its default value;
57 private static NeededProperty[] neededWithValue =
59 new NeededProperty("Producer.DocRoot", ""),
60 new NeededProperty("Producer.ImageRoot", ""),
61 new NeededProperty("Producer.Image.Path", ""),
62 new NeededProperty("Producer.Media.Path", ""),
63 new NeededProperty("Producer.RealMedia.Path", ""),
64 new NeededProperty("Producer.Image.IconPath", "")
68 * Constructor for MirPropertiesConfiguration.
70 private MirPropertiesConfiguration(ServletContext ctx, String ctxPath)
72 //loading the defaults-config
73 super(ctx.getRealPath("/WEB-INF/") + "/default.properties");
74 //loading the user-config
75 ExtendedProperties userConfig =
76 new ExtendedProperties(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties");
77 //merging them to one config while overriding the defaults
78 this.combine(userConfig);
79 addProperty("Home", ctx.getRealPath("/WEB-INF/") + "/");
83 public static synchronized MirPropertiesConfiguration instance()
84 throws PropertiesConfigExc {
85 if (instance == null) {
86 if (context == null) {
87 throw new MirPropertiesConfiguration.PropertiesConfigExc(
88 "Context was not set");
92 instance = new MirPropertiesConfiguration(context, contextPath);
94 catch (IOException e) {
95 throw new RuntimeException(e.toString());
104 * @param context The context to set
106 public static void setContext(ServletContext context) {
107 MirPropertiesConfiguration.context = context;
111 * Returns the context.
112 * @return ServletContext
114 public static ServletContext getContext() {
119 * Returns all properties in a Map
122 public Map allSettings() {
123 Iterator iterator = this.getKeys();
124 Map returnMap = new HashMap();
125 while (iterator.hasNext()) {
126 String key = (String) iterator.next();
127 Object o = this.getProperty(key);
133 returnMap.put(key, o);
140 * Returns a String-property concatenated with the home-dir of the
145 public String getStringWithHome(String key) {
146 String returnString = getString(key);
148 if (returnString == null) {
149 returnString = new String();
152 return getString("Home") + returnString;
156 * Checks if one property is missing and adds a default value
158 private void checkMissing() {
159 for (int i = 0; i < neededWithValue.length; i++) {
160 if (super.getProperty(neededWithValue[i].getKey()) == null) {
161 addProperty(neededWithValue[i].getKey(), neededWithValue[i].getValue());
166 public File getFile(String key) throws FileNotFoundException {
167 String path = getStringWithHome(key);
168 File returnFile = new File(path);
170 if (returnFile.exists()) {
173 throw new FileNotFoundException();
178 * @return the vlaue of this property as String
179 * @param key the key of this property
180 * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
182 public String getString(String key) {
183 return getString(key, "");
188 * @return the value of this property as String
189 * @param key the key of the property
190 * @param defaultValue the default value of this property if it is null
191 * @see org.apache.commons.collections.ExtendedProperties#getString(java.lang.String, java.lang.String)
193 public String getString(String key, String defaultValue) {
194 Object object = getProperty(key);
196 if (defaultValue == null) {
201 if (object instanceof String) {
202 return (String)object;
204 return object.toString();
208 * Returns a property according to the given key
209 * @param key the key of the property
210 * @return the value of the property as Object, if no property available it returns a empty String
211 * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
213 public Object getProperty(String key) {
214 if (super.getProperty(key) == null) {
218 return super.getProperty(key);
224 public static class PropertiesConfigExc extends Exc {
226 * Constructor for PropertiesConfigExc.
229 public PropertiesConfigExc(String msg) {
237 public static class PropertiesConfigFailure extends Failure {
239 * Constructor for PropertiesConfigExc.
242 public PropertiesConfigFailure(String msg, Throwable cause) {
248 * A Class for properties to be checked
251 private static class NeededProperty {
253 private String _value;
255 public NeededProperty(String key, String value) {
260 public String getKey() {
264 public String getValue() {