KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > builders > AbstractDigesterConfiguration


1 /*
2  * $Id: AbstractDigesterConfiguration.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.config.builders;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.Reader JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.apache.commons.digester.Digester;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.mule.MuleManager;
23 import org.mule.config.ConfigurationException;
24 import org.mule.config.MuleDtdResolver;
25 import org.mule.config.MuleProperties;
26 import org.mule.config.ReaderResource;
27 import org.mule.config.i18n.Message;
28 import org.mule.config.i18n.Messages;
29 import org.mule.impl.container.MuleContainerContext;
30 import org.mule.umo.UMOFilter;
31 import org.mule.util.IOUtils;
32 import org.xml.sax.ErrorHandler JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34 import org.xml.sax.SAXParseException JavaDoc;
35
36 /**
37  * A base classs for configuration schemes that use digester to parse the documents.
38  *
39  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
40  * @version $Revision: 3798 $
41  */

42 public abstract class AbstractDigesterConfiguration
43 {
44     public static final String JavaDoc DEFAULT_CONTAINER_CONTEXT = MuleContainerContext.class.getName();
45     public static final String JavaDoc FILTER_INTERFACE = UMOFilter.class.getName();
46
47     /**
48      * logger used by this class
49      */

50     protected transient Log logger = LogFactory.getLog(getClass());
51
52     protected Digester digester;
53     protected List JavaDoc containerReferences = new ArrayList JavaDoc();
54     protected String JavaDoc configEncoding;
55
56     protected AbstractDigesterConfiguration(boolean validate, String JavaDoc dtd)
57     {
58         // This is a hack to stop Digester spitting out unnecessary warnings
59
// when there is a customer error handler registered
60
digester = new Digester()
61         {
62             public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc
63             {
64                 if (errorHandler != null)
65                 {
66                     errorHandler.warning(e);
67                 }
68             }
69         };
70
71         configEncoding = System.getProperty(MuleProperties.MULE_ENCODING_SYSTEM_PROPERTY,
72             MuleManager.getConfiguration().getEncoding());
73
74         digester.setValidating(validate);
75         digester.setEntityResolver(new MuleDtdResolver(dtd));
76
77         digester.setErrorHandler(new ErrorHandler JavaDoc()
78         {
79             public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc
80             {
81                 logger.error(exception.getMessage(), exception);
82                 throw new SAXException JavaDoc(exception);
83             }
84
85             public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc
86             {
87                 logger.fatal(exception.getMessage(), exception);
88                 throw new SAXException JavaDoc(exception);
89             }
90
91             public void warning(SAXParseException JavaDoc exception)
92             {
93                 logger.warn(exception.getMessage());
94             }
95         });
96     }
97
98     // protected ReaderResource[] parseResources(String configResources) throws
99
// ConfigurationException {
100
// String[] resources = Utility.split(configResources, ",");
101
// MuleManager.getConfiguration().setConfigResources(resources);
102
// ReaderResource[] readers = new ReaderResource[resources.length];
103
// for (int i = 0; i < resources.length; i++) {
104
// try {
105
// readers[i] = new ReaderResource(resources[i].trim(),
106
// new InputStreamReader(loadConfig(resources[i].trim()), "UTF-8"));
107
// } catch (UnsupportedEncodingException e) {
108
// throw new ConfigurationException(e);
109
// }
110
// }
111
// return readers;
112
// }
113

114     protected Object JavaDoc process(ReaderResource[] configResources) throws ConfigurationException
115     {
116         Object JavaDoc result = null;
117         Reader JavaDoc configResource;
118         for (int i = 0; i < configResources.length; i++)
119         {
120             try
121             {
122                 configResource = configResources[i].getReader();
123                 result = digester.parse(configResource);
124             }
125             catch (Exception JavaDoc e)
126             {
127                 throw new ConfigurationException(new Message(Messages.FAILED_TO_PARSE_CONFIG_RESOURCE_X,
128                     configResources[i].getDescription()), e);
129             }
130         }
131
132         return result;
133     }
134
135     /**
136      * Attempt to load a configuration resource from the file system, classpath, or
137      * as a URL, in that order.
138      *
139      * @param configResource Mule configuration resources
140      * @return an InputStream to the resource
141      * @throws ConfigurationException if the resource could not be loaded by any
142      * means
143      */

144     protected InputStream JavaDoc loadConfig(String JavaDoc configResource) throws ConfigurationException
145     {
146         InputStream JavaDoc is = null;
147         try
148         {
149             is = IOUtils.getResourceAsStream(configResource, getClass());
150         }
151         catch (IOException JavaDoc e)
152         {
153             throw new ConfigurationException(new Message(Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE,
154                 configResource), e);
155         }
156
157         if (is != null)
158         {
159             return is;
160         }
161         else
162         {
163             throw new ConfigurationException(new Message(Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE,
164                 configResource));
165         }
166     }
167
168     public abstract String JavaDoc getRootName();
169
170     protected void addContainerContextRules(String JavaDoc path, String JavaDoc setterMethod, int parentIndex)
171         throws ConfigurationException
172     {
173         // Create container Context
174
digester.addObjectCreate(path, DEFAULT_CONTAINER_CONTEXT, "className");
175         digester.addSetProperties(path, "name", "name");
176         addMulePropertiesRule(path, digester);
177
178         // Set the container on the parent object
179
digester.addSetNext(path, setterMethod);
180     }
181
182     protected void addServerPropertiesRules(String JavaDoc path, String JavaDoc setterMethod, int parentIndex)
183     {
184         // Set environment properties
185
int i = path.lastIndexOf("/");
186         addMulePropertiesRule(path.substring(0, i), digester, setterMethod, path.substring(i + 1));
187     }
188
189     protected void addSetPropertiesRule(String JavaDoc path, Digester digester)
190     {
191         digester.addRule(path, new MuleSetPropertiesRule());
192     }
193
194     protected void addSetPropertiesRule(String JavaDoc path, Digester digester, String JavaDoc[] s1, String JavaDoc[] s2)
195     {
196         digester.addRule(path, new MuleSetPropertiesRule(s1, s2));
197     }
198
199     protected void addMulePropertiesRule(String JavaDoc path, Digester digester)
200     {
201         digester.addRuleSet(new MulePropertiesRuleSet(path, containerReferences));
202     }
203
204     protected void addMulePropertiesRule(String JavaDoc path, Digester digester, String JavaDoc propertiesSetter)
205     {
206         digester.addRuleSet(new MulePropertiesRuleSet(path, propertiesSetter, containerReferences));
207     }
208
209     protected void addMulePropertiesRule(String JavaDoc path,
210                                          Digester digester,
211                                          String JavaDoc propertiesSetter,
212                                          String JavaDoc parentElement)
213     {
214         digester.addRuleSet(new MulePropertiesRuleSet(path, propertiesSetter, containerReferences,
215             parentElement));
216     }
217
218     protected void addFilterRules(Digester digester, String JavaDoc path) throws ConfigurationException
219     {
220         // three levels
221
addSingleFilterRule(digester, path);
222         path += "/filter";
223         addFilterGroupRule(digester, path);
224
225         addFilterGroupRule(digester, path + "/left-filter");
226         addFilterGroupRule(digester, path + "/right-filter");
227         addFilterGroupRule(digester, path + "/filter");
228
229         addFilterGroupRule(digester, path + "/left-filter/left-filter");
230         addFilterGroupRule(digester, path + "/left-filter/right-filter");
231         addFilterGroupRule(digester, path + "/left-filter/filter");
232
233         addFilterGroupRule(digester, path + "/right-filter/left-filter");
234         addFilterGroupRule(digester, path + "/right-filter/right-filter");
235         addFilterGroupRule(digester, path + "/right-filter/filter");
236
237         addFilterGroupRule(digester, path + "/filter/left-filter");
238         addFilterGroupRule(digester, path + "/filter/right-filter");
239         addFilterGroupRule(digester, path + "/filter/filter");
240
241         // digester.addSetNext(path, "setFilter");
242
}
243
244     protected void addFilterGroupRule(Digester digester, String JavaDoc path) throws ConfigurationException
245     {
246         addLeftFilterRule(digester, path);
247         addRightFilterRule(digester, path);
248         addSingleFilterRule(digester, path);
249     }
250
251     protected void addLeftFilterRule(Digester digester, String JavaDoc path) throws ConfigurationException
252     {
253         path += "/left-filter";
254         digester.addObjectCreate(path, FILTER_INTERFACE, "className");
255         addSetPropertiesRule(path, digester);
256         addMulePropertiesRule(path, digester);
257         digester.addSetNext(path, "setLeftFilter");
258     }
259
260     protected void addRightFilterRule(Digester digester, String JavaDoc path) throws ConfigurationException
261     {
262         path += "/right-filter";
263         digester.addObjectCreate(path, FILTER_INTERFACE, "className");
264         addSetPropertiesRule(path, digester);
265         addMulePropertiesRule(path, digester);
266         digester.addSetNext(path, "setRightFilter");
267     }
268
269     protected void addSingleFilterRule(Digester digester, String JavaDoc path) throws ConfigurationException
270     {
271         path += "/filter";
272         digester.addObjectCreate(path, FILTER_INTERFACE, "className");
273         addSetPropertiesRule(path, digester);
274         addMulePropertiesRule(path, digester);
275         digester.addSetNext(path, "setFilter");
276     }
277 }
278
Popular Tags