View Javadoc

1   /***
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.spring;
19  
20  import org.springframework.beans.BeansException;
21  import org.springframework.beans.factory.support.BeanDefinitionRegistry;
22  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
23  import org.springframework.core.io.ClassPathResource;
24  import org.springframework.core.io.Resource;
25  import org.w3c.dom.Document;
26  import org.xml.sax.EntityResolver;
27  
28  import javax.xml.transform.Source;
29  import javax.xml.transform.Transformer;
30  import javax.xml.transform.TransformerConfigurationException;
31  import javax.xml.transform.TransformerException;
32  import javax.xml.transform.TransformerFactory;
33  import javax.xml.transform.URIResolver;
34  import javax.xml.transform.dom.DOMResult;
35  import javax.xml.transform.dom.DOMSource;
36  import javax.xml.transform.stream.StreamSource;
37  import java.io.IOException;
38  
39  /***
40   * @version $Revision: 1.2 $
41   */
42  public class ActiveMQBeanDefinitionReader extends XmlBeanDefinitionReader {
43      private String brokerName;
44  
45      public ActiveMQBeanDefinitionReader(BeanDefinitionRegistry beanDefinitionRegistry, String brokerName) {
46          super(beanDefinitionRegistry);
47          this.brokerName = brokerName;
48          setEntityResolver(createEntityResolver());
49      }
50  
51      public int registerBeanDefinitions(Document document, Resource resource) throws BeansException {
52          try {
53              Document newDocument = transformDocument(document);
54              return super.registerBeanDefinitions(newDocument, resource);
55          }
56          catch (Exception e) {
57              throw new ConfigurationParseException(resource, e);
58          }
59      }
60  
61      public static Transformer createTransformer(Source source) throws TransformerConfigurationException {
62          TransformerFactory factory = TransformerFactory.newInstance();
63          Transformer transformer = factory.newTransformer(source);
64          transformer.setURIResolver(new URIResolver() {
65              public Source resolve(String href, String base) {
66                  System.out.println("Called with href:  " + href + " base: " + base);
67                  return null;
68              }
69          });
70          return transformer;
71      }
72  
73  
74      // Properties
75      //-------------------------------------------------------------------------
76      public String getBrokerName() {
77          return brokerName;
78      }
79  
80      public void setBrokerName(String brokerName) {
81          this.brokerName = brokerName;
82      }
83  
84      // Implementation methods
85      //-------------------------------------------------------------------------
86  
87      /***
88       * A hook to transform the source document into a default Spring XML configuration
89       *
90       * @param document
91       * @return
92       */
93      protected Document transformDocument(Document document) throws IOException, TransformerException {
94          Transformer transformer = createTransformer(createXslSource());
95          transformer.setParameter("brokerName", getBrokerName());
96          DOMResult result = new DOMResult();
97          transformer.transform(new DOMSource(document), result);
98          return (Document) result.getNode();
99      }
100 
101     /***
102      * Creates the XSL resource for the transformation
103      *
104      * @return
105      */
106     protected Source createXslSource() throws IOException {
107         return new StreamSource(getXslResource().getInputStream(), getXslResource().getURL().toString());
108     }
109 
110     /***
111      * @return the resource to use for the XSLT
112      */
113     protected ClassPathResource getXslResource() {
114         return new ClassPathResource("org/codehaus/activemq/activemq-to-spring.xsl");
115     }
116 
117     /***
118      * @return a new EnittyResolver
119      */
120     protected EntityResolver createEntityResolver() {
121         return new ActiveMQDtdResolver();
122     }
123 }