Source code: marauroa/Configuration.java
1 /* $Id: Configuration.java,v 1.6 2003/12/15 16:38:08 arianne_rpg Exp $ */
2 /***************************************************************************
3 * (C) Copyright 2003 - Marauroa *
4 ***************************************************************************
5 ***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 ***************************************************************************/
13 package marauroa;
14
15 import marauroa.*;
16 import java.util.Properties;
17 import java.io.*;
18
19 /** This class is a basic configuration file manager */
20 public class Configuration
21 {
22 private static String configurationFile="marauroa.ini";
23
24 private Properties properties;
25 private static Configuration configuration=null;
26
27 public static void setConfigurationFile(String conf)
28 {
29 configurationFile=conf;
30 }
31
32 public static class PropertyNotFoundException extends Exception
33 {
34 PropertyNotFoundException(String property)
35 {
36 super("Property ["+property+"] not found");
37 }
38 }
39
40 public static class PropertyFileNotFoundException extends Exception
41 {
42 private String message;
43 PropertyFileNotFoundException()
44 {
45 super();
46 String file=getClass().getClassLoader().getResource(configurationFile).getPath();
47 message="Property File ["+file+"] not found";
48 }
49
50 public String getMessage()
51 {
52 return message;
53 }
54 }
55
56 /** Constructor */
57 private Configuration() throws PropertyFileNotFoundException
58 {
59 marauroad.trace("Configuration",">");
60 try
61 {
62 properties=new Properties();
63 InputStream is = getClass().getClassLoader().getResourceAsStream(configurationFile);
64 if(is!=null)
65 {
66 properties.load(is);
67 }
68 else
69 {
70 // The configuration file is not found.
71 throw new FileNotFoundException();
72 }
73 }
74 catch(FileNotFoundException e)
75 {
76 marauroad.trace("Configuration","X","Configuration file not found: "+e.getMessage());
77 throw new PropertyFileNotFoundException();
78 }
79 catch(IOException e)
80 {
81 marauroad.trace("Configuration","X","Error loading Configuration file: "+e.getMessage());
82 throw new PropertyFileNotFoundException();
83 }
84 finally
85 {
86 marauroad.trace("Configuration","<");
87 }
88 }
89
90 /** This method returns an instance of Configuration
91 * @return A shared instance of Configuration */
92 public static Configuration getConfiguration() throws PropertyFileNotFoundException
93 {
94 marauroad.trace("Configuration::getConfiguration",">");
95
96 try
97 {
98 if(configuration==null)
99 {
100 configuration=new Configuration();
101 }
102
103 return configuration;
104 }
105 finally
106 {
107 marauroad.trace("Configuration::getConfiguration","<");
108 }
109 }
110
111 /** This method returns a String with the value of the property.
112 * @param property the property we want the value
113 * @throw PropertyNotFound if the property is not found. */
114 public String get(String property) throws PropertyNotFoundException
115 {
116 marauroad.trace("Configuration::get",">");
117
118 try
119 {
120 String result=properties.getProperty(property);
121
122 if(result==null)
123 {
124 marauroad.trace("Configuration::get","X","Property ["+property+"] not found");
125 throw new PropertyNotFoundException(property);
126 }
127
128 marauroad.trace("Configuration::get","D","Property ["+property+"]="+result);
129 return result;
130 }
131 finally
132 {
133 marauroad.trace("Configuration::get","<");
134 }
135 }
136
137 /** This method set a property with the value we pass as parameter
138 * @param property the property we want to set the value
139 * @param value the value to set */
140 public void set(String property, String value)
141 {
142 marauroad.trace("Configuration::set",">");
143
144 try
145 {
146 marauroad.trace("Configuration::set","D","Property ["+property+"]="+value);
147 properties.put(property,value);
148 }
149 finally
150 {
151 marauroad.trace("Configuration::set","<");
152 }
153 }
154
155 public void store() throws PropertyFileNotFoundException
156 {
157 marauroad.trace("Configuration::store",">");
158 try
159 {
160 String file=getClass().getClassLoader().getResource(configurationFile).getPath();
161 properties.store(new FileOutputStream(file),"Marauroa Configuration file");
162 }
163 catch(FileNotFoundException e)
164 {
165 marauroad.trace("Configuration::store","X","Configuration file not found: "+e.getMessage());
166 throw new PropertyFileNotFoundException();
167 }
168 catch(IOException e)
169 {
170 marauroad.trace("Configuration::store","X","Error loading Configuration file: "+e.getMessage());
171 throw new PropertyFileNotFoundException();
172 }
173 finally
174 {
175 marauroad.trace("Configuration::store","<");
176 }
177 }
178 }