1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
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 * <p>
35 * <code>ConfigurationManager</code> parses the XML statements
36 * in the Sieve configuration file and translates them to Java objects.
37 * </p>
38 *
39 * <p>
40 * The Sieve configuration is read from 3 properties file. They are located by
41 * searching the classpath of the current ClassLoader.
42 * org/apache/jsieve/commandsmap.properties
43 * org/apache/jsieve/testsmap.properties
44 * org/apache/jsieve/comparatorsmap.properties
45 * </p>
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 * A Map of the Command names and their associated class names.
57 */
58 private Map fieldCommandMap;
59
60 /**
61 * A Map of the Test names and their associated class names.
62 */
63 private Map fieldTestMap;
64
65 /**
66 * A Map of the Comparator names and their associated class names.
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 * Constructor for ConfigurationManager.
76 *
77 * @throws SieveConfigurationException
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 * <p>
96 * Method getConfigStream answers an InputStream over the Sieve
97 * configuration file. It is located by searching the classpath of the
98 * current ClassLoader.
99 * </p>
100 * <p>
101 * The context classloader is searched first. If a suitably named resource
102 * is found then this is returned. Otherwise, the classloader used to load
103 * this class is searched for the resource.
104 * </p>
105 *
106 * @return InputStream
107 * @throws IOException
108 */
109 private InputStream getConfigStream(String configName) throws IOException {
110 InputStream stream = null;
111 // Context classloader is usually right in a JEE evironment
112 final ClassLoader contextClassLoader = Thread.currentThread()
113 .getContextClassLoader();
114 if (contextClassLoader != null) {
115 stream = contextClassLoader.getResourceAsStream(configName);
116 }
117
118 // Sometimes context classloader will not be set conventionally
119 // So, try class classloader
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 * Method getCommandMap answers a Map of Command names and their associated
132 * class names, lazily initialized if required.
133 *
134 * @return Map
135 */
136 public Map getCommandMap() {
137 if (null == fieldCommandMap) {
138 fieldCommandMap = new HashMap();
139 }
140 return Collections.synchronizedMap(fieldCommandMap);
141 }
142
143 /**
144 * Method getTestMap answers a Map of Test names and their associated class
145 * names, lazily initialized if required.
146 *
147 * @return Map
148 */
149 public Map getTestMap() {
150 if (null == fieldTestMap) {
151 fieldTestMap = new HashMap();
152 }
153 return Collections.synchronizedMap(fieldTestMap);
154 }
155
156 /**
157 * Method getComparatorMap answers a Map of Comparator names and their
158 * associated class names, lazily initialized if required.
159 *
160 * @return Map
161 */
162 public Map getComparatorMap() {
163 if (null == fieldComparatorMap) {
164 fieldComparatorMap = new HashMap();
165 }
166 return Collections.synchronizedMap(fieldComparatorMap);
167 }
168
169 /**
170 * Method parse uses the Digester to parse the XML statements in the Sieve
171 * configuration file into Java objects.
172 *
173 * @throws SAXException
174 * @throws IOException
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 * Sets the commandMap.
195 *
196 * @param commandMap
197 * The commandMap to set
198 */
199 private void setCommandMap(Map commandMap) {
200 fieldCommandMap = commandMap;
201 }
202
203 /**
204 * Sets the testMap.
205 *
206 * @param testMap
207 * The testMap to set
208 */
209 private void setTestMap(Map testMap) {
210 fieldTestMap = testMap;
211 }
212
213 /**
214 * Sets the comparatorMap.
215 *
216 * @param comparatorMap
217 * The comparatorMap to set
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 }