Organizing import, refresh the license (zapata deleted cos.jar)
[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       } catch (IOException e) {
94         e.printStackTrace();
95       }
96     }
97
98     return instance;
99   }
100
101   /**
102    * Sets the context.
103    * @param context The context to set
104    */
105   public static void setContext(ServletContext context) {
106     MirPropertiesConfiguration.context = context;
107   }
108
109   /**
110    * Returns the context.
111    * @return ServletContext
112    */
113   public static ServletContext getContext() {
114     return context;
115   }
116
117   /**
118    * Returns all properties in a Map
119    * @return Map
120    */
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);
127
128       if (o == null) {
129         o = new Object();
130       }
131
132       returnMap.put(key, o);
133     }
134
135     return returnMap;
136   }
137
138   /**
139    * Returns a String-property concatenated with the home-dir of the
140    * installation
141    * @param key
142    * @return String
143    */
144   public String getStringWithHome(String key) {
145     String returnString = getString(key);
146
147     if (returnString == null) {
148       returnString = new String();
149     }
150
151     return getString("Home") + returnString;
152   }
153
154   /**
155    * Checks if one property is missing and adds a default value
156    */
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());
161       }
162     }
163   }
164
165   public File getFile(String key) throws FileNotFoundException {
166     String path = getStringWithHome(key);
167     File returnFile = new File(path);
168
169     if (returnFile.exists()) {
170       return returnFile;
171     } else {
172       throw new FileNotFoundException();
173     }
174   }
175
176   /**
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)
180    */
181   public String getString(String key) {
182         return getString(key, "");
183   }
184   
185   
186   /** 
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)
191    */
192   public String getString(String key, String defaultValue) {
193                 Object object = getProperty(key);       
194                 if(object == null){
195                         if (defaultValue == null) {
196                                 return new String();
197                         }
198                   return defaultValue;
199                 }                       
200                 if (object instanceof String) {
201                         return (String)object;
202                 }
203                 return object.toString();
204   }
205
206   /**
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)
211    */
212   public Object getProperty(String key) {
213     if (super.getProperty(key) == null) {
214       return new String();
215     }
216
217     return super.getProperty(key);
218   }
219   
220   /**
221    * @author idefix
222    */
223   public static class PropertiesConfigExc extends Exc {
224     /**
225      * Constructor for PropertiesConfigExc.
226      * @param arg0
227      */
228     public PropertiesConfigExc(String msg) {
229       super(msg);
230     }
231   }
232
233   /**
234    * @author idefix
235    */
236   public static class PropertiesConfigFailure extends Failure {
237     /**
238      * Constructor for PropertiesConfigExc.
239      * @param arg0
240      */
241     public PropertiesConfigFailure(String msg, Throwable cause) {
242       super(msg, cause);
243     }
244   }
245
246   /**
247    * A Class for properties to be checked
248    * @author idefix
249    */
250   private static class NeededProperty {
251     private String _key;
252     private String _value;
253
254     public NeededProperty(String key, String value) {
255       _key = key;
256       _value = value;
257     }
258
259     public String getKey() {
260       return _key;
261     }
262
263     public String getValue() {
264       return _value;
265     }
266   }
267 }