commons-configuration is gone to commons-collections
[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 import org.apache.commons.configuration.PropertiesConfiguration;
38
39 import java.io.File;
40 import java.io.FileNotFoundException;
41 import java.io.IOException;
42
43 import java.util.HashMap;
44 import java.util.Iterator;
45 import java.util.Map;
46
47 import javax.servlet.ServletContext;
48
49
50 /**
51  * @author idefix
52  */
53 public class MirPropertiesConfiguration extends ExtendedProperties {
54   private static MirPropertiesConfiguration instance;
55   private static ServletContext context;
56   private static String contextPath;
57
58   //if one of these properties is not present a new
59   //property is added with its default value;
60   private static NeededProperty[] neededWithValue =
61   {
62     new NeededProperty("Producer.DocRoot", ""),
63     new NeededProperty("Producer.ImageRoot", ""),
64     new NeededProperty("Producer.Image.Path", ""),
65     new NeededProperty("Producer.Media.Path", ""),
66     new NeededProperty("Producer.RealMedia.Path", ""),
67     new NeededProperty("Producer.Image.IconPath", "")
68   };
69
70   /**
71    * Constructor for MirPropertiesConfiguration.
72    */
73   private MirPropertiesConfiguration(ServletContext ctx, String ctxPath)
74     throws IOException {
75     super(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties",
76       ctx.getRealPath("/WEB-INF/") + "/default.properties");
77     addProperty("Home", ctx.getRealPath("/WEB-INF/") + "/");
78     checkMissing();
79   }
80
81   public static synchronized MirPropertiesConfiguration instance()
82     throws PropertiesConfigExc {
83     if (instance == null) {
84       if (context == null) {
85         throw new MirPropertiesConfiguration.PropertiesConfigExc(
86           "Context was not set");
87       }
88
89       try {
90         instance = new MirPropertiesConfiguration(context, contextPath);
91       } catch (IOException e) {
92         e.printStackTrace();
93       }
94     }
95
96     return instance;
97   }
98
99   /**
100    * Sets the context.
101    * @param context The context to set
102    */
103   public static void setContext(ServletContext context) {
104     MirPropertiesConfiguration.context = context;
105   }
106
107   /**
108    * Returns the context.
109    * @return ServletContext
110    */
111   public static ServletContext getContext() {
112     return context;
113   }
114
115   /**
116    * Returns all properties in a Map
117    * @return Map
118    */
119   public Map allSettings() {
120     Iterator iterator = this.getKeys();
121     Map returnMap = new HashMap();
122
123     while (iterator.hasNext()) {
124       String key = (String) iterator.next();
125       Object o = this.getProperty(key);
126
127       if (o == null) {
128         o = new Object();
129       }
130
131       returnMap.put(key, o);
132     }
133
134     return returnMap;
135   }
136
137   /**
138    * Returns a String-property concatenated with the home-dir of the
139    * installation
140    * @param key
141    * @return String
142    */
143   public String getStringWithHome(String key) {
144     String returnString = getString(key);
145
146     if (returnString == null) {
147       returnString = new String();
148     }
149
150     return getString("Home") + returnString;
151   }
152
153   /**
154    * Checks if one property is missing and adds a default value
155    */
156   private void checkMissing() {
157     for (int i = 0; i < neededWithValue.length; i++) {
158       if (super.getProperty(neededWithValue[i].getKey()) == null) {
159         addProperty(neededWithValue[i].getKey(), neededWithValue[i].getValue());
160       }
161     }
162   }
163
164   public File getFile(String key) throws FileNotFoundException {
165     String path = getStringWithHome(key);
166     File returnFile = new File(path);
167
168     if (returnFile.exists()) {
169       return returnFile;
170     } else {
171       throw new FileNotFoundException();
172     }
173   }
174
175   /**
176    * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
177    */
178   public String getString(String key) {
179         Object object = getProperty(key);       
180         if(object != null){
181                 if (object instanceof String) {
182                         return (String)object;
183                 } else {
184                         return object.toString();
185                 }
186         } else {
187       return new String();
188     }
189   }
190
191   /**
192    * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
193    */
194   public Object getProperty(String key) {
195     if (super.getProperty(key) == null) {
196       return new String();
197     }
198
199     return super.getProperty(key);
200   }
201
202   /**
203    * @author idefix
204    */
205   public static class PropertiesConfigExc extends Exc {
206     /**
207      * Constructor for PropertiesConfigExc.
208      * @param arg0
209      */
210     public PropertiesConfigExc(String msg) {
211       super(msg);
212     }
213   }
214
215   /**
216    * @author idefix
217    */
218   public static class PropertiesConfigFailure extends Failure {
219     /**
220      * Constructor for PropertiesConfigExc.
221      * @param arg0
222      */
223     public PropertiesConfigFailure(String msg, Throwable cause) {
224       super(msg, cause);
225     }
226   }
227
228   /**
229    * A Class for properties to be checked
230    * @author idefix
231    */
232   private static class NeededProperty {
233     private String _key;
234     private String _value;
235
236     public NeededProperty(String key, String value) {
237       _key = key;
238       _value = value;
239     }
240
241     public String getKey() {
242       return _key;
243     }
244
245     public String getValue() {
246       return _value;
247     }
248   }
249 }