some codeformatting, license for the new classes
[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.configuration.PropertiesConfiguration;
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 PropertiesConfiguration {
53   private static MirPropertiesConfiguration instance;
54   private static ServletContext context;
55   private static String contextPath;
56
57   /**
58    * Constructor for MirPropertiesConfiguration.
59    */
60   private MirPropertiesConfiguration(ServletContext ctx, String ctxPath)
61     throws IOException {
62     super(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties",
63       ctx.getRealPath("/WEB-INF/etc/") + "/default.properties");
64     this.addProperty("Home", ctx.getRealPath("/WEB-INF/") + "/");
65     this.addProperty("RootUri", ctxPath);
66   }
67
68   public static synchronized MirPropertiesConfiguration instance()
69     throws PropertiesConfigExc {
70     if (instance == null) {
71       if ((context == null) || (contextPath == null) ||
72           (contextPath.length() == 0)) {
73         throw new MirPropertiesConfiguration.PropertiesConfigExc(
74           "Context was not set");
75       }
76
77       try {
78         instance = new MirPropertiesConfiguration(context, contextPath);
79       } catch (IOException e) {
80         e.printStackTrace();
81       }
82     }
83
84     return instance;
85   }
86
87   /**
88    * Sets the context.
89    * @param context The context to set
90    */
91   public static void setContext(ServletContext context) {
92     MirPropertiesConfiguration.context = context;
93   }
94
95   /**
96    * Sets the contextPath.
97    * @param contextPath The contextPath to set
98    */
99   public static void setContextPath(String contextPath) {
100     MirPropertiesConfiguration.contextPath = contextPath;
101   }
102
103   public Map allSettings() {
104     Iterator iterator = this.getKeys();
105     Map returnMap = new HashMap();
106
107     while (iterator.hasNext()) {
108       String key = (String) iterator.next();
109       returnMap.put(key, this.getProperty(key));
110     }
111
112     return returnMap;
113   }
114
115   /**
116    * Returns the context.
117    * @return ServletContext
118    */
119   public static ServletContext getContext() {
120     return context;
121   }
122
123   public String getStringWithHome(String key) {
124     String returnString = getString(key);
125
126     if (returnString == null) {
127       returnString = new String();
128     }
129
130     return getString("Home") + returnString;
131   }
132
133   public File getFile(String key) throws FileNotFoundException {
134     String path = getStringWithHome(key);
135     File returnFile = new File(path);
136
137     if (returnFile.exists()) {
138       return returnFile;
139     } else {
140       throw new FileNotFoundException();
141     }
142   }
143
144   /**
145    * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
146    */
147   public String getString(String key) {
148     if (super.getString(key) == null) {
149       return new String();
150     }
151
152     return super.getString(key);
153   }
154
155   /**
156    * @author idefix
157    */
158   public static class PropertiesConfigExc extends Exc {
159     /**
160      * Constructor for PropertiesConfigExc.
161      * @param arg0
162      */
163     public PropertiesConfigExc(String msg) {
164       super(msg);
165     }
166   }
167
168   /**
169    * @author idefix
170    */
171   public static class PropertiesConfigFailure extends Failure {
172     /**
173      * Constructor for PropertiesConfigExc.
174      * @param arg0
175      */
176     public PropertiesConfigFailure(String msg, Throwable cause) {
177       super(msg, cause);
178     }
179   }
180 }