1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.config.builders; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.ConfigurationException; |
15 | |
import org.mule.config.MuleDtdResolver; |
16 | |
import org.mule.config.MuleProperties; |
17 | |
import org.mule.config.ReaderResource; |
18 | |
import org.mule.config.builders.i18n.BuildersMessages; |
19 | |
import org.mule.impl.container.MuleContainerContext; |
20 | |
import org.mule.umo.UMOFilter; |
21 | |
|
22 | |
import java.io.Reader; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import org.apache.commons.digester.Digester; |
27 | |
import org.apache.commons.logging.Log; |
28 | |
import org.apache.commons.logging.LogFactory; |
29 | |
import org.xml.sax.ErrorHandler; |
30 | |
import org.xml.sax.SAXException; |
31 | |
import org.xml.sax.SAXParseException; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public abstract class AbstractDigesterConfiguration |
37 | |
{ |
38 | 0 | public static final String DEFAULT_CONTAINER_CONTEXT = MuleContainerContext.class.getName(); |
39 | 0 | public static final String FILTER_INTERFACE = UMOFilter.class.getName(); |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | protected transient final Log logger = LogFactory.getLog(getClass()); |
45 | |
|
46 | |
protected Digester digester; |
47 | 0 | protected List containerReferences = new ArrayList(); |
48 | |
protected String configEncoding; |
49 | |
|
50 | |
protected AbstractDigesterConfiguration(boolean validate, String dtd) |
51 | 0 | { |
52 | |
|
53 | |
|
54 | 0 | digester = new Digester() |
55 | |
{ |
56 | 0 | public void warning(SAXParseException e) throws SAXException |
57 | |
{ |
58 | 0 | if (errorHandler != null) |
59 | |
{ |
60 | 0 | errorHandler.warning(e); |
61 | |
} |
62 | 0 | } |
63 | |
}; |
64 | |
|
65 | 0 | configEncoding = System.getProperty(MuleProperties.MULE_ENCODING_SYSTEM_PROPERTY, |
66 | |
MuleManager.getConfiguration().getEncoding()); |
67 | |
|
68 | 0 | digester.setValidating(validate); |
69 | 0 | digester.setEntityResolver(new MuleDtdResolver(dtd)); |
70 | |
|
71 | 0 | digester.setErrorHandler(new ErrorHandler() |
72 | |
{ |
73 | |
public void error(SAXParseException exception) throws SAXException |
74 | |
{ |
75 | 0 | logger.error(exception.getMessage(), exception); |
76 | 0 | throw new SAXException(exception); |
77 | |
} |
78 | |
|
79 | |
public void fatalError(SAXParseException exception) throws SAXException |
80 | |
{ |
81 | 0 | logger.fatal(exception.getMessage(), exception); |
82 | 0 | throw new SAXException(exception); |
83 | |
} |
84 | |
|
85 | 0 | public void warning(SAXParseException exception) |
86 | |
{ |
87 | 0 | logger.warn(exception.getMessage()); |
88 | 0 | } |
89 | |
}); |
90 | 0 | } |
91 | |
|
92 | |
protected Object process(ReaderResource[] configResources) throws ConfigurationException |
93 | |
{ |
94 | 0 | Object result = null; |
95 | |
Reader configResource; |
96 | 0 | for (int i = 0; i < configResources.length; i++) |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 0 | configResource = configResources[i].getReader(); |
101 | 0 | result = digester.parse(configResource); |
102 | |
} |
103 | 0 | catch (Exception e) |
104 | |
{ |
105 | 0 | throw new ConfigurationException( |
106 | |
BuildersMessages.failedToParseConfigResource(configResources[i].getDescription()), e); |
107 | 0 | } |
108 | |
} |
109 | |
|
110 | 0 | return result; |
111 | |
} |
112 | |
|
113 | |
public abstract String getRootName(); |
114 | |
|
115 | |
protected void addContainerContextRules(String path, String setterMethod, int parentIndex) |
116 | |
throws ConfigurationException |
117 | |
{ |
118 | |
|
119 | 0 | digester.addObjectCreate(path, DEFAULT_CONTAINER_CONTEXT, "className"); |
120 | 0 | digester.addSetProperties(path, "name", "name"); |
121 | 0 | addMulePropertiesRule(path, digester); |
122 | |
|
123 | |
|
124 | 0 | digester.addSetNext(path, setterMethod); |
125 | 0 | } |
126 | |
|
127 | |
protected void addServerPropertiesRules(String path, String setterMethod, int parentIndex) |
128 | |
{ |
129 | |
|
130 | 0 | int i = path.lastIndexOf("/"); |
131 | 0 | addMulePropertiesRule(path.substring(0, i), digester, setterMethod, path.substring(i + 1)); |
132 | 0 | } |
133 | |
|
134 | |
protected void addSetPropertiesRule(String path, Digester digester) |
135 | |
{ |
136 | 0 | digester.addRule(path, new MuleSetPropertiesRule()); |
137 | 0 | } |
138 | |
|
139 | |
protected void addSetPropertiesRule(String path, Digester digester, String[] s1, String[] s2) |
140 | |
{ |
141 | 0 | digester.addRule(path, new MuleSetPropertiesRule(s1, s2)); |
142 | 0 | } |
143 | |
|
144 | |
protected void addMulePropertiesRule(String path, Digester digester) |
145 | |
{ |
146 | 0 | digester.addRuleSet(new MulePropertiesRuleSet(path, containerReferences)); |
147 | 0 | } |
148 | |
|
149 | |
protected void addMulePropertiesRule(String path, Digester digester, String propertiesSetter) |
150 | |
{ |
151 | 0 | digester.addRuleSet(new MulePropertiesRuleSet(path, propertiesSetter, containerReferences)); |
152 | 0 | } |
153 | |
|
154 | |
protected void addMulePropertiesRule(String path, |
155 | |
Digester digester, |
156 | |
String propertiesSetter, |
157 | |
String parentElement) |
158 | |
{ |
159 | 0 | digester.addRuleSet(new MulePropertiesRuleSet(path, propertiesSetter, containerReferences, |
160 | |
parentElement)); |
161 | 0 | } |
162 | |
|
163 | |
protected void addFilterRules(Digester digester, String path) throws ConfigurationException |
164 | |
{ |
165 | |
|
166 | 0 | addSingleFilterRule(digester, path); |
167 | 0 | path += "/filter"; |
168 | 0 | addFilterGroupRule(digester, path); |
169 | |
|
170 | 0 | addFilterGroupRule(digester, path + "/left-filter"); |
171 | 0 | addFilterGroupRule(digester, path + "/right-filter"); |
172 | 0 | addFilterGroupRule(digester, path + "/filter"); |
173 | |
|
174 | 0 | addFilterGroupRule(digester, path + "/left-filter/left-filter"); |
175 | 0 | addFilterGroupRule(digester, path + "/left-filter/right-filter"); |
176 | 0 | addFilterGroupRule(digester, path + "/left-filter/filter"); |
177 | |
|
178 | 0 | addFilterGroupRule(digester, path + "/right-filter/left-filter"); |
179 | 0 | addFilterGroupRule(digester, path + "/right-filter/right-filter"); |
180 | 0 | addFilterGroupRule(digester, path + "/right-filter/filter"); |
181 | |
|
182 | 0 | addFilterGroupRule(digester, path + "/filter/left-filter"); |
183 | 0 | addFilterGroupRule(digester, path + "/filter/right-filter"); |
184 | 0 | addFilterGroupRule(digester, path + "/filter/filter"); |
185 | |
|
186 | |
|
187 | 0 | } |
188 | |
|
189 | |
protected void addFilterGroupRule(Digester digester, String path) throws ConfigurationException |
190 | |
{ |
191 | 0 | addLeftFilterRule(digester, path); |
192 | 0 | addRightFilterRule(digester, path); |
193 | 0 | addSingleFilterRule(digester, path); |
194 | 0 | } |
195 | |
|
196 | |
protected void addLeftFilterRule(Digester digester, String path) throws ConfigurationException |
197 | |
{ |
198 | 0 | path += "/left-filter"; |
199 | 0 | digester.addObjectCreate(path, FILTER_INTERFACE, "className"); |
200 | 0 | addSetPropertiesRule(path, digester); |
201 | 0 | addMulePropertiesRule(path, digester); |
202 | 0 | digester.addSetNext(path, "setLeftFilter"); |
203 | 0 | } |
204 | |
|
205 | |
protected void addRightFilterRule(Digester digester, String path) throws ConfigurationException |
206 | |
{ |
207 | 0 | path += "/right-filter"; |
208 | 0 | digester.addObjectCreate(path, FILTER_INTERFACE, "className"); |
209 | 0 | addSetPropertiesRule(path, digester); |
210 | 0 | addMulePropertiesRule(path, digester); |
211 | 0 | digester.addSetNext(path, "setRightFilter"); |
212 | 0 | } |
213 | |
|
214 | |
protected void addSingleFilterRule(Digester digester, String path) throws ConfigurationException |
215 | |
{ |
216 | 0 | path += "/filter"; |
217 | 0 | digester.addObjectCreate(path, FILTER_INTERFACE, "className"); |
218 | 0 | addSetPropertiesRule(path, digester); |
219 | 0 | addMulePropertiesRule(path, digester); |
220 | 0 | digester.addSetNext(path, "setFilter"); |
221 | 0 | } |
222 | |
} |