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.Map;
25 import java.util.Properties;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ConcurrentMap;
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public class ConfigurationManager {
71
72 private static final int DEFAULT_INITIAL_CONCURRENCY_LEVEL = 8;
73
74 private static final String COMMANDSMAP_PROPERTIES = "org/apache/jsieve/commandsmap.properties";
75
76 private static final String TESTSMAP_PROPERTIES = "org/apache/jsieve/testsmap.properties";
77
78 private static final String COMPARATORSMAP_PROPERTIES = "org/apache/jsieve/comparatorsmap.properties";
79
80
81
82
83 private ConcurrentMap<String, String> fieldCommandMap;
84
85
86
87
88 private ConcurrentMap<String, String> fieldTestMap;
89
90
91
92
93 private ConcurrentMap<String, String> fieldComparatorMap;
94
95
96
97
98 private int initialConcurrencyLevel = DEFAULT_INITIAL_CONCURRENCY_LEVEL;
99
100 private static final Log LOG = LogFactory.getLog("org.apache.jsieve");
101
102 private Log log = LOG;
103
104
105
106
107
108
109 public ConfigurationManager() throws SieveConfigurationException {
110 super();
111 try {
112 parse();
113 } catch (SAXException e) {
114 if (log.isErrorEnabled())
115 log.error("Exception processing Configuration: ", e);
116 throw new SieveConfigurationException(e);
117 } catch (IOException e) {
118 if (log.isErrorEnabled())
119 log.error("Exception processing Configuration: ", e);
120 throw new SieveConfigurationException(e);
121 }
122 }
123
124
125
126
127
128 public int getInitialConcurrencyLevel() {
129 return initialConcurrencyLevel;
130 }
131
132
133
134
135
136 public void setInitialConcurrencyLevel(int initialConcurrencyLevel) {
137 this.initialConcurrencyLevel = initialConcurrencyLevel;
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 private InputStream getConfigStream(String configName) throws IOException {
158 InputStream stream = null;
159
160 final ClassLoader contextClassLoader = Thread.currentThread()
161 .getContextClassLoader();
162 if (contextClassLoader != null) {
163 stream = contextClassLoader.getResourceAsStream(configName);
164 }
165
166
167
168 if (null == stream) {
169 stream = ConfigurationManager.class.getClassLoader()
170 .getResourceAsStream(configName);
171 }
172
173 if (null == stream)
174 throw new IOException("Resource \"" + configName + "\" not found");
175 return stream;
176 }
177
178
179
180
181
182
183
184 public ConcurrentMap<String, String> getCommandMap() {
185 if (null == fieldCommandMap) {
186 fieldCommandMap = new ConcurrentHashMap<String, String>();
187 }
188 return fieldCommandMap;
189 }
190
191
192
193
194
195
196
197 public ConcurrentMap<String, String> getTestMap() {
198 if (null == fieldTestMap) {
199 fieldTestMap = new ConcurrentHashMap<String, String>();
200 }
201 return fieldTestMap;
202 }
203
204
205
206
207
208
209
210 public ConcurrentMap<String, String> getComparatorMap() {
211 if (null == fieldComparatorMap) {
212 fieldComparatorMap = new ConcurrentHashMap<String, String>();
213 }
214 return fieldComparatorMap;
215 }
216
217
218
219
220
221
222
223
224 private void parse() throws SAXException, IOException {
225 setCommandMap(loadConfiguration(COMMANDSMAP_PROPERTIES));
226 setTestMap(loadConfiguration(TESTSMAP_PROPERTIES));
227 setComparatorMap(loadConfiguration(COMPARATORSMAP_PROPERTIES));
228 }
229
230 private ConcurrentMap<String,String> loadConfiguration(final String name) throws IOException {
231 final Properties properties = loadProperties(name);
232 final ConcurrentMap<String, String> result =
233 new ConcurrentHashMap<String, String>(properties.size(), 1.0f, initialConcurrencyLevel);
234 for (final Map.Entry<Object, Object> entry: properties.entrySet()) {
235 result.put(entry.getKey().toString(), entry.getValue().toString());
236 }
237 return result;
238 }
239
240 private Properties loadProperties(final String name) throws IOException {
241 final InputStream is = getConfigStream(name);
242 final Properties p = new Properties();
243 p.load(is);
244 return p;
245 }
246
247
248
249
250
251
252
253 private void setCommandMap(ConcurrentMap<String, String> commandMap) {
254 fieldCommandMap = commandMap;
255 }
256
257
258
259
260
261
262
263 private void setTestMap(ConcurrentMap<String, String> testMap) {
264 fieldTestMap = testMap;
265 }
266
267
268
269
270
271
272
273 private void setComparatorMap(ConcurrentMap<String, String> comparatorMap) {
274 fieldComparatorMap = comparatorMap;
275 }
276
277 public ComparatorManager getComparatorManager() {
278 return new ComparatorManagerImpl(fieldComparatorMap);
279 }
280
281 public CommandManager getCommandManager() {
282 return new CommandManagerImpl(fieldCommandMap);
283 }
284
285 public TestManager getTestManager() {
286 return new TestManagerImpl(fieldTestMap);
287 }
288
289 public Log getLog() {
290 return log;
291 }
292
293 public void setLog(Log log) {
294 this.log = log;
295 }
296
297 public SieveFactory build() {
298 return new SieveFactory(getCommandManager(), getComparatorManager(),
299 getTestManager(), getLog());
300 }
301 }