KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > extras > spring > config > MuleBeanDefinitionReader


1 /*
2  * $Id: MuleBeanDefinitionReader.java 4219 2006-12-09 10:15:14Z lajos $
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.extras.spring.config;
12
13 import java.io.IOException JavaDoc;
14
15 import javax.xml.transform.Source JavaDoc;
16 import javax.xml.transform.Transformer JavaDoc;
17 import javax.xml.transform.TransformerConfigurationException JavaDoc;
18 import javax.xml.transform.TransformerException JavaDoc;
19 import javax.xml.transform.TransformerFactory JavaDoc;
20 import javax.xml.transform.dom.DOMResult JavaDoc;
21 import javax.xml.transform.dom.DOMSource JavaDoc;
22 import javax.xml.transform.stream.StreamSource JavaDoc;
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.BeansDtdResolver;
32 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
33 import org.springframework.core.io.ClassPathResource;
34 import org.springframework.core.io.Resource;
35 import org.w3c.dom.Document JavaDoc;
36 import org.xml.sax.EntityResolver JavaDoc;
37
38 /**
39  * <code>MuleBeanDefinitionReader</code> Is a custom Spring Bean reader that will
40  * apply a transformation to Mule Xml configuration files before loading bean
41  * definitions allowing Mule Xml config to be parsed as Spring configuration.
42  */

43 public class MuleBeanDefinitionReader extends XmlBeanDefinitionReader
44 {
45     private int contextCount = 0;
46     private int configCount = 0;
47     private MuleDtdResolver dtdResolver = null;
48
49     public MuleBeanDefinitionReader(BeanDefinitionRegistry beanDefinitionRegistry, int configCount)
50     {
51         super(beanDefinitionRegistry);
52         // default resource loader
53
setResourceLoader(new MuleResourceLoader());
54         // TODO Make this configurable as a property somehow.
55
setValidationMode(VALIDATION_DTD);
56         setEntityResolver(createEntityResolver());
57         this.configCount = configCount;
58         ((DefaultListableBeanFactory)beanDefinitionRegistry).registerCustomEditor(UMOTransformer.class,
59             new TransformerEditor());
60     }
61
62     public int registerBeanDefinitions(Document JavaDoc document, Resource resource) throws BeansException
63     {
64         try
65         {
66             Document JavaDoc newDocument = transformDocument(document);
67             return super.registerBeanDefinitions(newDocument, resource);
68         }
69         catch (Exception JavaDoc e)
70         {
71             throw new FatalBeanException("Failed to read config resource: " + resource, e);
72         }
73         finally
74         {
75             incConfigCount();
76         }
77     }
78
79     public static Transformer JavaDoc createTransformer(Source JavaDoc source) throws TransformerConfigurationException JavaDoc
80     {
81         TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
82         Transformer JavaDoc transformer = factory.newTransformer(source);
83         return transformer;
84     }
85
86     protected Document JavaDoc transformDocument(Document JavaDoc document) throws IOException JavaDoc, TransformerException JavaDoc
87     {
88         if (getXslResource() != null)
89         {
90             Transformer JavaDoc transformer = createTransformer(createXslSource());
91             DOMResult JavaDoc result = new DOMResult JavaDoc();
92             transformer.setParameter("firstContext", Boolean.valueOf(isFirstContext()));
93             transformer.transform(new DOMSource JavaDoc(document), result);
94             if (logger.isDebugEnabled())
95             {
96                 try
97                 {
98                     String JavaDoc xml = new DOMReader().read((Document JavaDoc)result.getNode()).asXML();
99                     if (logger.isDebugEnabled())
100                     {
101                         logger.debug("Transformed document is:\n" + xml);
102                     }
103                 }
104                 catch (Exception JavaDoc e)
105                 {
106                     e.printStackTrace();
107                 }
108             }
109             return (Document JavaDoc)result.getNode();
110         }
111         else
112         {
113             return document;
114         }
115
116     }
117
118     protected Source JavaDoc createXslSource() throws IOException JavaDoc
119     {
120         return new StreamSource JavaDoc(getXslResource().getInputStream(), getXslResource().getURL().toString());
121     }
122
123     protected ClassPathResource getXslResource()
124     {
125         String JavaDoc xsl = dtdResolver.getXslForDtd();
126         if (xsl != null)
127         {
128             return new ClassPathResource(xsl);
129         }
130         else
131         {
132             return null;
133         }
134     }
135
136     protected EntityResolver JavaDoc createEntityResolver()
137     {
138         if (dtdResolver == null)
139         {
140             MuleDtdResolver muleSpringResolver = new MuleDtdResolver("mule-spring-configuration.dtd",
141                 "mule-to-spring.xsl", new BeansDtdResolver());
142             dtdResolver = new MuleDtdResolver("mule-configuration.dtd", "mule-to-spring.xsl",
143                 muleSpringResolver);
144         }
145         return dtdResolver;
146     }
147
148     public boolean isFirstContext()
149     {
150         return contextCount == 0;
151     }
152
153     private void incConfigCount()
154     {
155         contextCount++;
156         if (contextCount >= configCount)
157         {
158             contextCount = 0;
159         }
160     }
161 }
162
Popular Tags