a bit nicer
[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 the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31 package mir.config;
32
33 import multex.Exc;
34 import multex.Failure;
35
36 import org.apache.commons.collections.ExtendedProperties;
37
38 import java.io.File;
39 import java.io.FileNotFoundException;
40 import java.io.IOException;
41
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.Map;
45
46 import javax.servlet.ServletContext;
47
48
49 /**
50  * @author idefix
51  */
52 public class MirPropertiesConfiguration extends ExtendedProperties {
53   private static MirPropertiesConfiguration instance;
54   private static ServletContext context;
55   private static String contextPath;
56
57   //if one of these properties is not present a new
58   //property is added with its default value;
59   private static NeededProperty[] neededWithValue =
60   {
61     new NeededProperty("Producer.DocRoot", ""),
62     new NeededProperty("Producer.ImageRoot", ""),
63     new NeededProperty("Producer.Image.Path", ""),
64     new NeededProperty("Producer.Media.Path", ""),
65     new NeededProperty("Producer.RealMedia.Path", ""),
66     new NeededProperty("Producer.Image.IconPath", "")
67   };
68
69   /**
70    * Constructor for MirPropertiesConfiguration.
71    */
72   private MirPropertiesConfiguration(ServletContext ctx, String ctxPath)
73     throws IOException {
74     super(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties",
75       ctx.getRealPath("/WEB-INF/") + "/default.properties");
76     addProperty("Home", ctx.getRealPath("/WEB-INF/") + "/");
77     checkMissing();
78   }
79
80   public static synchronized MirPropertiesConfiguration instance()
81     throws PropertiesConfigExc {
82     if (instance == null) {
83       if (context == null) {
84         throw new MirPropertiesConfiguration.PropertiesConfigExc(
85           "Context was not set");
86       }
87
88       try {
89         instance = new MirPropertiesConfiguration(context, contextPath);
90       } catch (IOException e) {
91         e.printStackTrace();
92       }
93     }
94
95     return instance;
96   }
97
98   /**
99    * Sets the context.
100    * @param context The context to set
101    */
102   public static void setContext(ServletContext context) {
103     MirPropertiesConfiguration.context = context;
104   }
105
106   /**
107    * Returns the context.
108    * @return ServletContext
109    */
110   public static ServletContext getContext() {
111     return context;
112   }
113
114   /**
115    * Returns all properties in a Map
116    * @return Map
117    */
118   public Map allSettings() {
119     Iterator iterator = this.getKeys();
120     Map returnMap = new HashMap();
121
122     while (iterator.hasNext()) {
123       String key = (String) iterator.next();
124       Object o = this.getProperty(key);
125
126       if (o == null) {
127         o = new Object();
128       }
129
130       returnMap.put(key, o);
131     }
132
133     return returnMap;
134   }
135
136   /**
137    * Returns a String-property concatenated with the home-dir of the
138    * installation
139    * @param key
140    * @return String
141    */
142   public String getStringWithHome(String key) {
143     String returnString = getString(key);
144
145     if (returnString == null) {
146       returnString = new String();
147     }
148
149     return getString("Home") + returnString;
150   }
151
152   /**
153    * Checks if one property is missing and adds a default value
154    */
155   private void checkMissing() {
156     for (int i = 0; i < neededWithValue.length; i++) {
157       if (super.getProperty(neededWithValue[i].getKey()) == null) {
158         addProperty(neededWithValue[i].getKey(), neededWithValue[i].getValue());
159       }
160     }
161   }
162
163   public File getFile(String key) throws FileNotFoundException {
164     String path = getStringWithHome(key);
165     File returnFile = new File(path);
166
167     if (returnFile.exists()) {
168       return returnFile;
169     } else {
170       throw new FileNotFoundException();
171     }
172   }
173
174   /**
175    * @return the vlaue of this property as String
176    * @param key the key of this property
177    * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
178    */
179   public String getString(String key) {
180         return getString(key, "");
181   }
182   
183   
184   /** 
185    * @return the value of this property as String
186    * @param key the key of the property
187    * @param defaultValue the default value of this property if it is null
188    * @see org.apache.commons.collections.ExtendedProperties#getString(java.lang.String, java.lang.String)
189    */
190   public String getString(String key, String defaultValue) {
191                 Object object = getProperty(key);       
192                 if(object == null){
193                         if (defaultValue == null) {
194                                 return new String();
195                         }
196                   return defaultValue;
197                 }                       
198                 if (object instanceof String) {
199                         return (String)object;
200                 }
201                 return object.toString();
202   }
203
204   /**
205    * Returns a property according to the given key
206    * @param key the key of the property
207    * @return the value of the property as Object, if no property available it returns a empty String
208    * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
209    */
210   public Object getProperty(String key) {
211     if (super.getProperty(key) == null) {
212       return new String();
213     }
214
215     return super.getProperty(key);
216   }
217   
218   /**
219    * @author idefix
220    */
221   public static class PropertiesConfigExc extends Exc {
222     /**
223      * Constructor for PropertiesConfigExc.
224      * @param arg0
225      */
226     public PropertiesConfigExc(String msg) {
227       super(msg);
228     }
229   }
230
231   /**
232    * @author idefix
233    */
234   public static class PropertiesConfigFailure extends Failure {
235     /**
236      * Constructor for PropertiesConfigExc.
237      * @param arg0
238      */
239     public PropertiesConfigFailure(String msg, Throwable cause) {
240       super(msg, cause);
241     }
242   }
243
244   /**
245    * A Class for properties to be checked
246    * @author idefix
247    */
248   private static class NeededProperty {
249     private String _key;
250     private String _value;
251
252     public NeededProperty(String key, String value) {
253       _key = key;
254       _value = value;
255     }
256
257     public String getKey() {
258       return _key;
259     }
260
261     public String getValue() {
262       return _value;
263     }
264   }
265 }