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);
93 } catch (IOException e) {
103 * @param context The context to set
105 public static void setContext(ServletContext context) {
106 MirPropertiesConfiguration.context = context;
110 * Returns the context.
111 * @return ServletContext
113 public static ServletContext getContext() {
118 * Returns all properties in a Map
121 public Map allSettings() {
122 Iterator iterator = this.getKeys();
123 Map returnMap = new HashMap();
124 while (iterator.hasNext()) {
125 String key = (String) iterator.next();
126 Object o = this.getProperty(key);
132 returnMap.put(key, o);
139 * Returns a String-property concatenated with the home-dir of the
144 public String getStringWithHome(String key) {
145 String returnString = getString(key);
147 if (returnString == null) {
148 returnString = new String();
151 return getString("Home") + returnString;
155 * Checks if one property is missing and adds a default value
157 private void checkMissing() {
158 for (int i = 0; i < neededWithValue.length; i++) {
159 if (super.getProperty(neededWithValue[i].getKey()) == null) {
160 addProperty(neededWithValue[i].getKey(), neededWithValue[i].getValue());
165 public File getFile(String key) throws FileNotFoundException {
166 String path = getStringWithHome(key);
167 File returnFile = new File(path);
169 if (returnFile.exists()) {
172 throw new FileNotFoundException();
177 * @return the vlaue of this property as String
178 * @param key the key of this property
179 * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
181 public String getString(String key) {
182 return getString(key, "");
187 * @return the value of this property as String
188 * @param key the key of the property
189 * @param defaultValue the default value of this property if it is null
190 * @see org.apache.commons.collections.ExtendedProperties#getString(java.lang.String, java.lang.String)
192 public String getString(String key, String defaultValue) {
193 Object object = getProperty(key);
195 if (defaultValue == null) {
200 if (object instanceof String) {
201 return (String)object;
203 return object.toString();
207 * Returns a property according to the given key
208 * @param key the key of the property
209 * @return the value of the property as Object, if no property available it returns a empty String
210 * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
212 public Object getProperty(String key) {
213 if (super.getProperty(key) == null) {
217 return super.getProperty(key);
223 public static class PropertiesConfigExc extends Exc {
225 * Constructor for PropertiesConfigExc.
228 public PropertiesConfigExc(String msg) {
236 public static class PropertiesConfigFailure extends Failure {
238 * Constructor for PropertiesConfigExc.
241 public PropertiesConfigFailure(String msg, Throwable cause) {
247 * A Class for properties to be checked
250 private static class NeededProperty {
252 private String _value;
254 public NeededProperty(String key, String value) {
259 public String getKey() {
263 public String getValue() {