ServletModule exception cleanup + different error templates for admin + open postings...
[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 java.io.File;
34 import java.io.FileNotFoundException;
35 import java.io.IOException;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.Map;
39
40 import javax.servlet.ServletContext;
41
42 import multex.Exc;
43 import multex.Failure;
44
45 import org.apache.commons.configuration.PropertiesConfiguration;
46
47
48 /**
49  * @author idefix
50  */
51 public class MirPropertiesConfiguration extends PropertiesConfiguration {
52   private static MirPropertiesConfiguration instance;
53   private static ServletContext context;
54   private static String contextPath;
55
56   //if one of these properties is not present a new
57   //property is added with its default value;
58   private  static NeededProperty[] neededWithValue =
59   {
60         new NeededProperty("Producer.DocRoot",""),
61                 new NeededProperty("Producer.ImageRoot",""),
62                 new NeededProperty("Producer.Image.Path",""),
63                 new NeededProperty("Producer.Media.Path",""),
64                 new NeededProperty("Producer.RealMedia.Path",""),
65                 new NeededProperty("Producer.Image.IconPath","")
66   };
67
68   /**
69    * Constructor for MirPropertiesConfiguration.
70    */
71   private MirPropertiesConfiguration(ServletContext ctx, String ctxPath)
72     throws IOException {
73     super(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties",
74       ctx.getRealPath("/WEB-INF/") + "/default.properties");
75     addProperty("Home", ctx.getRealPath("/WEB-INF/") + "/");
76     checkMissing();
77   }
78
79   public static synchronized MirPropertiesConfiguration instance()
80     throws PropertiesConfigExc {
81     if (instance == null) {
82       if (context == null) {
83         throw new MirPropertiesConfiguration.PropertiesConfigExc(
84           "Context was not set");
85       }
86
87       try {
88         instance = new MirPropertiesConfiguration(context, contextPath);
89       } catch (IOException e) {
90         e.printStackTrace();
91       }
92     }
93
94     return instance;
95   }
96
97   /**
98    * Sets the context.
99    * @param context The context to set
100    */
101   public static void setContext(ServletContext context) {
102     MirPropertiesConfiguration.context = context;
103   }
104
105   /**
106    * Returns the context.
107    * @return ServletContext
108    */
109   public static ServletContext getContext() {
110     return context;
111   }
112
113         /**
114          * Returns all properties in a Map
115          * @return Map
116          */
117   public Map allSettings() {
118     Iterator iterator = this.getKeys();
119     Map returnMap = new HashMap();
120
121     while (iterator.hasNext()) {
122       String key = (String) iterator.next();
123       Object o = this.getString(key);
124
125       if (o == null) {
126         o = new Object();
127       }
128
129       returnMap.put(key, o);
130     }
131
132     return returnMap;
133   }
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    * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
176    */
177   public String getString(String key) {
178     if (super.getString(key) == null) {
179       return new String();
180     }
181
182     return super.getString(key);
183   }
184
185   /**
186    * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
187    */
188   public Object getProperty(String key) {
189     if (super.getProperty(key) == null) {
190       return new String();
191     }
192
193     return super.getProperty(key);
194   }
195
196   /**
197    * @author idefix
198    */
199   public static class PropertiesConfigExc extends Exc {
200     /**
201      * Constructor for PropertiesConfigExc.
202      * @param arg0
203      */
204     public PropertiesConfigExc(String msg) {
205       super(msg);
206     }
207   }
208
209   /**
210    * @author idefix
211    */
212   public static class PropertiesConfigFailure extends Failure {
213     /**
214      * Constructor for PropertiesConfigExc.
215      * @param arg0
216      */
217     public PropertiesConfigFailure(String msg, Throwable cause) {
218       super(msg, cause);
219     }
220
221   }
222
223         /**
224          * A Class for properties to be checked
225          * @author idefix
226          */
227         private static class NeededProperty {
228                 private String _key;
229                 private String _value;
230
231                 public NeededProperty(String key, String value) {
232                         _key = key;
233                         _value = value;
234                 }
235
236                 public String getKey() {
237                         return _key;
238                 }
239
240                 public String getValue() {
241                         return _value;
242                 }
243         }
244 }