2d5c038a8807458c55e5033e81cd2e1955277a63
[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.FileNotFoundException;
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.Map;
38
39 import javax.servlet.ServletContext;
40
41 import multex.Exc;
42 import multex.Failure;
43
44 import org.apache.commons.collections.ExtendedProperties;
45
46
47 /**
48  * @author idefix
49  */
50 public class MirPropertiesConfiguration extends ExtendedProperties {
51   private static MirPropertiesConfiguration instance;
52   private static ServletContext context;
53   private static String contextPath;
54
55   //if one of these properties is not present a new
56   //property is added with its default value;
57   private static NeededProperty[] neededWithValue =
58   {
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", "")
65   };
66
67   /**
68    * Constructor for MirPropertiesConfiguration.
69    */
70   private MirPropertiesConfiguration(ServletContext ctx, String ctxPath)
71     throws IOException {
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/") + "/");
80     checkMissing();
81   }
82
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");
89       }
90
91       try {
92         instance = new MirPropertiesConfiguration(context, contextPath);
93       }
94       catch (IOException e) {
95         throw new RuntimeException(e.toString());
96       }
97     }
98
99     return instance;
100   }
101
102   /**
103    * Sets the context.
104    * @param context The context to set
105    */
106   public static void setContext(ServletContext context) {
107     MirPropertiesConfiguration.context = context;
108   }
109
110   /**
111    * Returns the context.
112    * @return ServletContext
113    */
114   public static ServletContext getContext() {
115     return context;
116   }
117
118   /**
119    * Returns all properties in a Map
120    * @return Map
121    */
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);
128
129       if (o == null) {
130         o = new Object();
131       }
132
133       returnMap.put(key, o);
134     }
135
136     return returnMap;
137   }
138
139   /**
140    * Returns a String-property concatenated with the home-dir of the
141    * installation
142    * @param key
143    * @return String
144    */
145   public String getStringWithHome(String key) {
146     String returnString = getString(key);
147
148     if (returnString == null) {
149       returnString = new String();
150     }
151
152     return getString("Home") + returnString;
153   }
154
155   /**
156    * Checks if one property is missing and adds a default value
157    */
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());
162       }
163     }
164   }
165
166   public File getFile(String key) throws FileNotFoundException {
167     String path = getStringWithHome(key);
168     File returnFile = new File(path);
169
170     if (returnFile.exists()) {
171       return returnFile;
172     } else {
173       throw new FileNotFoundException();
174     }
175   }
176
177   /**
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)
181    */
182   public String getString(String key) {
183     return getString(key, "");
184   }
185
186
187   /**
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)
192    */
193   public String getString(String key, String defaultValue) {
194     Object object = getProperty(key);
195     if(object == null){
196       if (defaultValue == null) {
197         return new String();
198       }
199       return defaultValue;
200     }
201     if (object instanceof String) {
202       return (String)object;
203     }
204     return object.toString();
205   }
206
207   /**
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)
212    */
213   public Object getProperty(String key) {
214     if (super.getProperty(key) == null) {
215       return new String();
216     }
217
218     return super.getProperty(key);
219   }
220
221   /**
222    * @author idefix
223    */
224   public static class PropertiesConfigExc extends Exc {
225     /**
226      * Constructor for PropertiesConfigExc.
227      * @param arg0
228      */
229     public PropertiesConfigExc(String msg) {
230       super(msg);
231     }
232   }
233
234   /**
235    * @author idefix
236    */
237   public static class PropertiesConfigFailure extends Failure {
238     /**
239      * Constructor for PropertiesConfigExc.
240      * @param arg0
241      */
242     public PropertiesConfigFailure(String msg, Throwable cause) {
243       super(msg, cause);
244     }
245   }
246
247   /**
248    * A Class for properties to be checked
249    * @author idefix
250    */
251   private static class NeededProperty {
252     private String _key;
253     private String _value;
254
255     public NeededProperty(String key, String value) {
256       _key = key;
257       _value = value;
258     }
259
260     public String getKey() {
261       return _key;
262     }
263
264     public String getValue() {
265       return _value;
266     }
267   }
268 }