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