1 package org.jmage;
2
3 import org.jmage.cache.CachingFactoryProxy;
4 import org.jmage.dispatcher.RequestDispatcher;
5 import org.jmage.encoder.EncoderManager;
6 import org.jmage.encoder.ImageEncoder;
7 import org.jmage.filterchain.FilterChainFactory;
8 import org.jmage.filterchain.FilterChainManager;
9 import org.jmage.resource.ResourceFactory;
10 import org.jmage.resource.ResourceManager;
11 import org.jmage.resource.DefaultResourceManager;
12 import org.jmage.util.ReflectionUtil;
13 import org.jmage.util.XmlUtil;
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17 import org.xml.sax.SAXException;
18
19 import javax.naming.directory.NoSuchAttributeException;
20 import javax.xml.parsers.ParserConfigurationException;
21 import java.util.ArrayList;
22 import java.util.Properties;
23 import java.io.IOException;
24
25 /***
26 * Creates ApplicationContext from an xml config file
27 */
28 public class XmlApplicationContext extends ApplicationContext {
29 public static final String CONFIGFILE = "jmage.xml";
30 private static final String PROPERTIES = "properties";
31 private static final String PROPERTY = "property";
32 private static final String NAME = "name";
33 private static final String VALUE = "value";
34 private static final String RESOURCEMANAGER = "resourcemanager";
35 private static final String CLASS = "class";
36 private static final String FILTERCHAINMANAGER = "filterchainmanager";
37 private static final String FACTORY = "factory";
38 private static final String ENCODERMANAGER = "encodermanager";
39 private static final String REQUESTDISPATCHER = "requestdispatcher";
40 private static final String POOL = "pool";
41 private static final String DEFAULTPOOLSIZE = "1";
42
43 private static final String CACHE_SIZE = "cachesize";
44 private static final String CACHE_TYPE = "cachetype";
45 private static final String CACHE_DIR = "cachedir";
46
47 private static XmlUtil xmlUtil = new XmlUtil();
48 private static ReflectionUtil reflectionUtil = new ReflectionUtil();
49 private static final String INITIALIZATION_EXCEPTION = "unable to initialize jmage application context, cause: ";
50 private static final String FILTERCHAINFACTORY_INIT = " initialized FilterChainFactory: ";
51 private static final String FILTERCHAINMANAGER_INIT = " initialized FilterChainManager: ";
52 private static final String IMAGEENCODER_INIT = " initialized ImageEncoder: ";
53 private static final String ENCODERMANAGER_INIT = " initialized EncoderManager: ";
54 private static final String REQUESTDISPATCHER_INIT = " initialized RequestDispatcher";
55 private static final String CONTEXT_INIT = " initialized ApplicationContext using xml config file";
56 private static final String RESOURCEFACTORY_INIT = " initialized ResourceFactory: ";
57 private static final String RESOURCEMANAGER_INIT = " initialized ResourceManager: ";
58 private static final String CACHE_NOTINIT = "unable to initialize cache, either not configured or missing/false params, cause: ";
59
60 /***
61 * Use getContext instead.
62 */
63 protected XmlApplicationContext() {
64 super();
65 }
66
67 /***
68 * Create an XmlApplicationContext instance.
69 *
70 * @return the applicationContext
71 */
72 public static ApplicationContext getContext(ApplicationContext context) {
73 if (context == null) {
74 context = new XmlApplicationContext();
75 }
76
77 try {
78 Document document = getConfigFile();
79 Node documentNode = document.getDocumentElement();
80 assert documentNode != null : INITIALIZATION_EXCEPTION;
81
82
83 XmlApplicationContext.getProperties(documentNode, context);
84
85
86 XmlApplicationContext.getResourceManager(documentNode, context);
87
88
89 XmlApplicationContext.getFilterChainManager(documentNode, context);
90
91
92 XmlApplicationContext.getEncoderManager(documentNode, context);
93
94
95 XmlApplicationContext.getRequestDispatcher(documentNode, context);
96
97 if (log.isInfoEnabled()) log.info(CONTEXT_INIT);
98 return context;
99 } catch (Exception e) {
100 log.fatal(INITIALIZATION_EXCEPTION + e.getMessage());
101 throw new RuntimeException(INITIALIZATION_EXCEPTION + e.getMessage());
102 }
103 }
104
105 private static Document getConfigFile() throws IOException, SAXException, ParserConfigurationException {
106 Document config = null;
107 try {
108 config = xmlUtil.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIGFILE));
109 } catch (Exception e) {
110 config = xmlUtil.read(XmlApplicationContext.class.getClassLoader().getResourceAsStream(CONFIGFILE));
111 }
112 return config;
113 }
114
115 private static void getResourceManager(Node documentNode, ApplicationContext context) throws NoSuchAttributeException, ClassNotFoundException, IllegalAccessException, InstantiationException, ConfigurationException {
116 ResourceManager resourceManager = null;
117 Node resourceManagerNode = xmlUtil.getNamedChildNode(documentNode, RESOURCEMANAGER);
118 NodeList resourceFactoryNodes = resourceManagerNode.getChildNodes();
119
120 String poolSize = null;
121 try {
122 poolSize = xmlUtil.getAttribute(resourceManagerNode, POOL);
123 } catch (NoSuchAttributeException e) {
124 poolSize = DEFAULTPOOLSIZE;
125 }
126 int iPoolSize = Integer.parseInt(poolSize);
127 for (int p = 0; p < iPoolSize; p++) {
128 ArrayList resourceFactories = new ArrayList();
129 for (int i = 0; i <= resourceFactoryNodes.getLength() - 1; i++) {
130 Node resourceFactoryNode = resourceFactoryNodes.item(i);
131 if (FACTORY.equals(resourceFactoryNode.getNodeName())) {
132 String clazz = xmlUtil.getAttribute(resourceFactoryNode, CLASS);
133 ResourceFactory resourceFactory = (ResourceFactory) reflectionUtil.instantiate(clazz);
134 try {
135
136 Properties cacheProps = new Properties();
137 cacheProps.setProperty(CACHE_SIZE, xmlUtil.getAttribute(resourceFactoryNode, CACHE_SIZE));
138 cacheProps.setProperty(CACHE_TYPE, xmlUtil.getAttribute(resourceFactoryNode, CACHE_TYPE));
139 cacheProps.setProperty(CACHE_DIR, context.getProperty(CACHE_DIR));
140 resourceFactory = (ResourceFactory) CachingFactoryProxy.newInstance(resourceFactory, cacheProps);
141 } catch (NoSuchAttributeException e) {
142 if (log.isDebugEnabled()) log.debug(CACHE_NOTINIT + e.getMessage());
143 }
144
145 resourceFactory.configure(context);
146 resourceFactories.add(resourceFactory);
147 if (log.isInfoEnabled()) log.info(RESOURCEFACTORY_INIT + resourceFactory.getClass().getName());
148 }
149 }
150 resourceManager = new DefaultResourceManager(resourceFactories);
151 resourceManager.configure(context);
152 context.addResourceManager(resourceManager);
153 if (log.isInfoEnabled()) log.info(RESOURCEMANAGER_INIT + resourceManager + p);
154 }
155 }
156
157 private static void getRequestDispatcher(Node documentNode, ApplicationContext context) throws NoSuchAttributeException, ClassNotFoundException, IllegalAccessException, InstantiationException, ConfigurationException {
158 RequestDispatcher requestDispatcher = null;
159 Node requestDispatcherNode = xmlUtil.getNamedChildNode(documentNode, REQUESTDISPATCHER);
160 String requestDispatcherClass = xmlUtil.getAttribute(requestDispatcherNode, CLASS);
161
162 String poolSize = null;
163 try {
164 poolSize = xmlUtil.getAttribute(requestDispatcherNode, POOL);
165 } catch (NoSuchAttributeException e) {
166 poolSize = DEFAULTPOOLSIZE;
167 }
168 int iPoolSize = Integer.parseInt(poolSize);
169 for (int i = 0; i < iPoolSize; i++) {
170 requestDispatcher = (RequestDispatcher) reflectionUtil.instantiate(requestDispatcherClass);
171 try {
172
173 Properties cacheProps = new Properties();
174 cacheProps.setProperty(CACHE_SIZE, xmlUtil.getAttribute(requestDispatcherNode, CACHE_SIZE));
175 cacheProps.setProperty(CACHE_TYPE, xmlUtil.getAttribute(requestDispatcherNode, CACHE_TYPE));
176 cacheProps.setProperty(CACHE_DIR, context.getProperty(CACHE_DIR));
177 requestDispatcher = (RequestDispatcher) CachingFactoryProxy.newInstance(requestDispatcher, cacheProps);
178 } catch (Exception e) {
179 if (log.isDebugEnabled()) log.debug(CACHE_NOTINIT + e.getMessage());
180 }
181 requestDispatcher.configure(context);
182 context.addRequestDispatcher(requestDispatcher);
183
184 if (log.isInfoEnabled()) log.info(REQUESTDISPATCHER_INIT + i);
185 }
186 }
187
188 private static void getFilterChainManager(Node documentNode, ApplicationContext context) throws NoSuchAttributeException, ClassNotFoundException, IllegalAccessException, InstantiationException, ConfigurationException {
189 FilterChainManager filterChainManager = null;
190 Node filterChainManagerNode = xmlUtil.getNamedChildNode(documentNode, FILTERCHAINMANAGER);
191 NodeList filterChainFactoryNodes = filterChainManagerNode.getChildNodes();
192
193 String poolSize = null;
194 try {
195 poolSize = xmlUtil.getAttribute(filterChainManagerNode, POOL);
196 } catch (NoSuchAttributeException e) {
197 poolSize = DEFAULTPOOLSIZE;
198 }
199 int iPoolSize = Integer.parseInt(poolSize);
200 for (int p = 0; p < iPoolSize; p++) {
201 ArrayList filterChainFactories = new ArrayList();
202 for (int i = 0; i <= filterChainFactoryNodes.getLength() - 1; i++) {
203 Node filterChainFactoryNode = filterChainFactoryNodes.item(i);
204 if (FACTORY.equals(filterChainFactoryNode.getNodeName())) {
205 String clazz = xmlUtil.getAttribute(filterChainFactoryNode, CLASS);
206 FilterChainFactory filterChainFactory = (FilterChainFactory) reflectionUtil.instantiate(clazz);
207 try {
208
209 Properties cacheProps = new Properties();
210 cacheProps.setProperty(CACHE_SIZE, xmlUtil.getAttribute(filterChainFactoryNode, CACHE_SIZE));
211 cacheProps.setProperty(CACHE_TYPE, xmlUtil.getAttribute(filterChainFactoryNode, CACHE_TYPE));
212 cacheProps.setProperty(CACHE_DIR, context.getProperty(CACHE_DIR));
213 filterChainFactory = (FilterChainFactory) CachingFactoryProxy.newInstance(filterChainFactory, cacheProps);
214 } catch (NoSuchAttributeException e) {
215 if (log.isDebugEnabled()) log.debug(CACHE_NOTINIT + e.getMessage());
216 }
217
218 filterChainFactory.configure(context);
219 filterChainFactories.add(filterChainFactory);
220 if (log.isInfoEnabled()) log.info(FILTERCHAINFACTORY_INIT + filterChainFactory.getClass().getName());
221 }
222 }
223 filterChainManager = new FilterChainManager(filterChainFactories);
224 filterChainManager.configure(context);
225 context.addFilterChainManager(filterChainManager);
226 if (log.isInfoEnabled()) log.info(FILTERCHAINMANAGER_INIT + filterChainManager + p);
227 }
228 }
229
230 private static void getEncoderManager(Node documentNode, ApplicationContext context) throws NoSuchAttributeException, ClassNotFoundException, IllegalAccessException, InstantiationException, ConfigurationException {
231 EncoderManager encoderManager = null;
232 Node encoderManagerNode = xmlUtil.getNamedChildNode(documentNode, ENCODERMANAGER);
233 NodeList encoderFactoryNodes = encoderManagerNode.getChildNodes();
234
235 String poolSize = null;
236 try {
237 poolSize = xmlUtil.getAttribute(encoderManagerNode, POOL);
238 } catch (NoSuchAttributeException e) {
239 poolSize = DEFAULTPOOLSIZE;
240 }
241 int iPoolSize = Integer.parseInt(poolSize);
242 for (int p = 0; p < iPoolSize; p++) {
243 ArrayList encoderFactories = new ArrayList();
244 for (int i = 0; i <= encoderFactoryNodes.getLength() - 1; i++) {
245 Node encoderFactoryNode = encoderFactoryNodes.item(i);
246 if (FACTORY.equals(encoderFactoryNode.getNodeName())) {
247 String clazz = xmlUtil.getAttribute(encoderFactoryNode, CLASS);
248 ImageEncoder imageEncoder = (ImageEncoder) reflectionUtil.instantiate(clazz);
249
250 imageEncoder.configure(context);
251 encoderFactories.add(imageEncoder);
252 if (log.isInfoEnabled()) log.info(IMAGEENCODER_INIT + imageEncoder.getClass().getName());
253 }
254 }
255 encoderManager = new EncoderManager(encoderFactories);
256 encoderManager.configure(context);
257 context.addEncoderManager(encoderManager);
258 if (log.isInfoEnabled()) log.info(ENCODERMANAGER_INIT + encoderManager);
259 }
260 }
261
262 private static void getProperties(Node documentNode, ApplicationContext context) throws NoSuchAttributeException {
263 Node propertiesNode = xmlUtil.getNamedChildNode(documentNode, PROPERTIES);
264 NodeList propertyNodes = propertiesNode.getChildNodes();
265 for (int i = 0; i <= propertyNodes.getLength() - 1; i++) {
266 Node propertyNode = propertyNodes.item(i);
267 if (PROPERTY.equals(propertyNode.getNodeName())) {
268 String propertyName = xmlUtil.getAttribute(propertyNode, NAME);
269 String propertyValue = xmlUtil.getAttribute(propertyNode, VALUE);
270 context.properties.setProperty(propertyName, propertyValue);
271 }
272 }
273 System.getProperties().putAll(context.properties);
274 }
275 }