1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.jsieve;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.xml.sax.SAXException;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class ConfigurationManager {
48
49 private static final String COMMANDSMAP_PROPERTIES = "org/apache/jsieve/commandsmap.properties";
50
51 private static final String TESTSMAP_PROPERTIES = "org/apache/jsieve/testsmap.properties";
52
53 private static final String COMPARATORSMAP_PROPERTIES = "org/apache/jsieve/comparatorsmap.properties";
54
55
56
57
58 private Map fieldCommandMap;
59
60
61
62
63 private Map fieldTestMap;
64
65
66
67
68 private Map fieldComparatorMap;
69
70 private static final Log LOG = LogFactory.getLog("org.apache.jsieve");
71
72 private Log log = LOG;
73
74
75
76
77
78
79 public ConfigurationManager() throws SieveConfigurationException {
80 super();
81 try {
82 parse();
83 } catch (SAXException e) {
84 if (log.isErrorEnabled())
85 log.error("Exception processing Configuration: ", e);
86 throw new SieveConfigurationException(e);
87 } catch (IOException e) {
88 if (log.isErrorEnabled())
89 log.error("Exception processing Configuration: ", e);
90 throw new SieveConfigurationException(e);
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 private InputStream getConfigStream(String configName) throws IOException {
110 InputStream stream = null;
111
112 final ClassLoader contextClassLoader = Thread.currentThread()
113 .getContextClassLoader();
114 if (contextClassLoader != null) {
115 stream = contextClassLoader.getResourceAsStream(configName);
116 }
117
118
119
120 if (null == stream) {
121 stream = ConfigurationManager.class.getClassLoader()
122 .getResourceAsStream(configName);
123 }
124
125 if (null == stream)
126 throw new IOException("Resource \"" + configName + "\" not found");
127 return stream;
128 }
129
130
131
132
133
134
135
136 public Map getCommandMap() {
137 if (null == fieldCommandMap) {
138 fieldCommandMap = new HashMap();
139 }
140 return Collections.synchronizedMap(fieldCommandMap);
141 }
142
143
144
145
146
147
148
149 public Map getTestMap() {
150 if (null == fieldTestMap) {
151 fieldTestMap = new HashMap();
152 }
153 return Collections.synchronizedMap(fieldTestMap);
154 }
155
156
157
158
159
160
161
162 public Map getComparatorMap() {
163 if (null == fieldComparatorMap) {
164 fieldComparatorMap = new HashMap();
165 }
166 return Collections.synchronizedMap(fieldComparatorMap);
167 }
168
169
170
171
172
173
174
175
176 private void parse() throws SAXException, IOException {
177 InputStream is;
178 Properties p;
179 is = getConfigStream(COMMANDSMAP_PROPERTIES);
180 p = new Properties();
181 p.load(is);
182 setCommandMap(p);
183 is = getConfigStream(TESTSMAP_PROPERTIES);
184 p = new Properties();
185 p.load(is);
186 setTestMap(p);
187 is = getConfigStream(COMPARATORSMAP_PROPERTIES);
188 p = new Properties();
189 p.load(is);
190 setComparatorMap(p);
191 }
192
193
194
195
196
197
198
199 private void setCommandMap(Map commandMap) {
200 fieldCommandMap = commandMap;
201 }
202
203
204
205
206
207
208
209 private void setTestMap(Map testMap) {
210 fieldTestMap = testMap;
211 }
212
213
214
215
216
217
218
219 private void setComparatorMap(Map comparatorMap) {
220 fieldComparatorMap = comparatorMap;
221 }
222
223 public ComparatorManager getComparatorManager() {
224 return new ComparatorManagerImpl(fieldComparatorMap);
225 }
226
227 public CommandManager getCommandManager() {
228 return new CommandManagerImpl(fieldCommandMap);
229 }
230
231 public TestManager getTestManager() {
232 return new TestManagerImpl(fieldTestMap);
233 }
234
235 public Log getLog() {
236 return log;
237 }
238
239 public void setLog(Log log) {
240 this.log = log;
241 }
242
243 public SieveFactory build() {
244 return new SieveFactory(getCommandManager(), getComparatorManager(),
245 getTestManager(), getLog());
246 }
247 }