1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.extras.spring.config; |
12 | |
|
13 | |
import java.io.IOException; |
14 | |
|
15 | |
import javax.xml.transform.Source; |
16 | |
import javax.xml.transform.Transformer; |
17 | |
import javax.xml.transform.TransformerConfigurationException; |
18 | |
import javax.xml.transform.TransformerException; |
19 | |
import javax.xml.transform.TransformerFactory; |
20 | |
import javax.xml.transform.dom.DOMResult; |
21 | |
import javax.xml.transform.dom.DOMSource; |
22 | |
import javax.xml.transform.stream.StreamSource; |
23 | |
|
24 | |
import org.dom4j.io.DOMReader; |
25 | |
import org.mule.config.MuleDtdResolver; |
26 | |
import org.mule.umo.transformer.UMOTransformer; |
27 | |
import org.springframework.beans.BeansException; |
28 | |
import org.springframework.beans.FatalBeanException; |
29 | |
import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
30 | |
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
31 | |
import org.springframework.beans.factory.xml.DelegatingEntityResolver; |
32 | |
import org.springframework.beans.factory.xml.ResourceEntityResolver; |
33 | |
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
34 | |
import org.springframework.core.io.ClassPathResource; |
35 | |
import org.springframework.core.io.Resource; |
36 | |
import org.springframework.util.ClassUtils; |
37 | |
import org.springframework.util.xml.XmlValidationModeDetector; |
38 | |
import org.w3c.dom.Document; |
39 | |
import org.xml.sax.EntityResolver; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class MuleBeanDefinitionReader extends XmlBeanDefinitionReader |
47 | |
{ |
48 | 60 | private int contextCount = 0; |
49 | 60 | private int configCount = 0; |
50 | 60 | private MuleDtdResolver dtdResolver = null; |
51 | |
|
52 | |
public MuleBeanDefinitionReader(BeanDefinitionRegistry beanDefinitionRegistry, int configCount) |
53 | |
{ |
54 | 60 | super(beanDefinitionRegistry); |
55 | |
|
56 | 64 | setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class); |
57 | 60 | setResourceLoader(new MuleResourceLoader()); |
58 | |
|
59 | 60 | setValidationMode(XmlValidationModeDetector.VALIDATION_AUTO); |
60 | 60 | setEntityResolver(createEntityResolver()); |
61 | 60 | this.configCount = configCount; |
62 | |
|
63 | |
|
64 | 60 | ((DefaultListableBeanFactory)beanDefinitionRegistry).registerCustomEditor(UMOTransformer.class, |
65 | |
new TransformerEditor()); |
66 | 60 | } |
67 | |
|
68 | |
public int registerBeanDefinitions(Document document, Resource resource) throws BeansException |
69 | |
{ |
70 | |
try |
71 | |
{ |
72 | 134 | Document newDocument = transformDocument(document); |
73 | 134 | return super.registerBeanDefinitions(newDocument, resource); |
74 | |
} |
75 | 0 | catch (Exception e) |
76 | |
{ |
77 | 0 | throw new FatalBeanException("Failed to read config resource: " + resource, e); |
78 | |
} |
79 | |
finally |
80 | |
{ |
81 | 134 | incConfigCount(); |
82 | |
} |
83 | |
} |
84 | |
|
85 | |
public static Transformer createTransformer(Source source) throws TransformerConfigurationException |
86 | |
{ |
87 | 24 | TransformerFactory factory = TransformerFactory.newInstance(); |
88 | 24 | Transformer transformer = factory.newTransformer(source); |
89 | 24 | return transformer; |
90 | |
} |
91 | |
|
92 | |
protected Document transformDocument(Document document) throws IOException, TransformerException |
93 | |
{ |
94 | 134 | if (getXslResource() != null) |
95 | |
{ |
96 | 24 | Transformer transformer = createTransformer(createXslSource()); |
97 | 24 | DOMResult result = new DOMResult(); |
98 | 24 | transformer.setParameter("firstContext", Boolean.valueOf(isFirstContext())); |
99 | 24 | transformer.transform(new DOMSource(document), result); |
100 | 24 | if (logger.isDebugEnabled()) |
101 | |
{ |
102 | |
try |
103 | |
{ |
104 | 0 | String xml = new DOMReader().read((Document)result.getNode()).asXML(); |
105 | 0 | if (logger.isDebugEnabled()) |
106 | |
{ |
107 | 0 | logger.debug("Transformed document is:\n" + xml); |
108 | |
} |
109 | |
} |
110 | 0 | catch (Exception e) |
111 | |
{ |
112 | 0 | e.printStackTrace(); |
113 | 0 | } |
114 | |
} |
115 | 24 | return (Document)result.getNode(); |
116 | |
} |
117 | |
else |
118 | |
{ |
119 | 110 | return document; |
120 | |
} |
121 | |
|
122 | |
} |
123 | |
|
124 | |
protected Source createXslSource() throws IOException |
125 | |
{ |
126 | 24 | return new StreamSource(getXslResource().getInputStream(), getXslResource().getURL().toString()); |
127 | |
} |
128 | |
|
129 | |
protected ClassPathResource getXslResource() |
130 | |
{ |
131 | 182 | String xsl = dtdResolver.getXslForDtd(); |
132 | 182 | if (xsl != null) |
133 | |
{ |
134 | 72 | return new ClassPathResource(xsl); |
135 | |
} |
136 | |
else |
137 | |
{ |
138 | 110 | return null; |
139 | |
} |
140 | |
} |
141 | |
|
142 | |
protected EntityResolver createEntityResolver() |
143 | |
{ |
144 | 60 | if (dtdResolver == null) |
145 | |
{ |
146 | 60 | MuleDtdResolver muleSpringResolver = new MuleDtdResolver("mule-spring-configuration.dtd", |
147 | |
"mule-to-spring.xsl", this.createSpringEntityResolver()); |
148 | 60 | dtdResolver = new MuleDtdResolver("mule-configuration.dtd", "mule-to-spring.xsl", |
149 | |
muleSpringResolver); |
150 | |
} |
151 | 60 | return dtdResolver; |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
protected EntityResolver createSpringEntityResolver() |
164 | |
{ |
165 | 60 | EntityResolver springEntityResolver = null; |
166 | |
|
167 | 60 | if (getResourceLoader() != null) |
168 | |
{ |
169 | 60 | springEntityResolver = new ResourceEntityResolver(getResourceLoader()); |
170 | |
} |
171 | |
else |
172 | |
{ |
173 | 0 | springEntityResolver = new DelegatingEntityResolver(ClassUtils.getDefaultClassLoader()); |
174 | |
} |
175 | 60 | return springEntityResolver; |
176 | |
} |
177 | |
|
178 | |
public boolean isFirstContext() |
179 | |
{ |
180 | 24 | return contextCount == 0; |
181 | |
} |
182 | |
|
183 | |
private void incConfigCount() |
184 | |
{ |
185 | 134 | contextCount++; |
186 | 134 | if (contextCount >= configCount) |
187 | |
{ |
188 | 108 | contextCount = 0; |
189 | |
} |
190 | 134 | } |
191 | |
} |