2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
34 import multex.Failure;
36 import org.apache.commons.configuration.PropertiesConfiguration;
39 import java.io.FileNotFoundException;
40 import java.io.IOException;
42 import java.util.HashMap;
43 import java.util.Iterator;
46 import javax.servlet.ServletContext;
52 public class MirPropertiesConfiguration extends PropertiesConfiguration {
53 private static MirPropertiesConfiguration instance;
54 private static ServletContext context;
55 private static String contextPath;
57 //if one of these properties is not present a new
58 //emtpy property is added
59 private String[] needed = {"Producer.DocRoot"};
62 * Constructor for MirPropertiesConfiguration.
64 private MirPropertiesConfiguration(ServletContext ctx, String ctxPath)
66 super(ctx.getRealPath("/WEB-INF/etc/") + "/config.properties",
67 ctx.getRealPath("/WEB-INF/etc/") + "/default.properties");
68 addProperty("Home", ctx.getRealPath("/WEB-INF/") + "/");
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");
81 instance = new MirPropertiesConfiguration(context, contextPath);
82 } catch (IOException e) {
92 * @param context The context to set
94 public static void setContext(ServletContext context) {
95 MirPropertiesConfiguration.context = context;
98 public Map allSettings() {
99 Iterator iterator = this.getKeys();
100 Map returnMap = new HashMap();
102 while (iterator.hasNext()) {
103 String key = (String) iterator.next();
104 Object o = this.getString(key);
108 returnMap.put(key, o);
115 * Returns the context.
116 * @return ServletContext
118 public static ServletContext getContext() {
122 public String getStringWithHome(String key) {
123 String returnString = getString(key);
125 if (returnString == null) {
126 returnString = new String();
129 return getString("Home") + returnString;
132 private void checkMissing(){
133 for(int i = 0; i < needed.length; i++){
134 if(super.getProperty(needed[i]) == null){
135 addProperty(needed[i],"");
140 public File getFile(String key) throws FileNotFoundException {
141 String path = getStringWithHome(key);
142 File returnFile = new File(path);
144 if (returnFile.exists()) {
147 throw new FileNotFoundException();
152 * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
154 public String getString(String key) {
155 if (super.getString(key) == null) {
158 return super.getString(key);
162 * @see org.apache.commons.configuration.Configuration#getString(java.lang.String)
164 public Object getProperty(String key) {
165 if (super.getProperty(key) == null) {
168 return super.getProperty(key);
174 public static class PropertiesConfigExc extends Exc {
176 * Constructor for PropertiesConfigExc.
179 public PropertiesConfigExc(String msg) {
187 public static class PropertiesConfigFailure extends Failure {
189 * Constructor for PropertiesConfigExc.
192 public PropertiesConfigFailure(String msg, Throwable cause) {