001    /**
002     * 
003     * Copyright 2004 Protique Ltd
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.activemq.spring;
019    
020    import org.springframework.beans.BeansException;
021    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
022    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
023    import org.springframework.core.io.ClassPathResource;
024    import org.springframework.core.io.Resource;
025    import org.w3c.dom.Document;
026    import org.xml.sax.EntityResolver;
027    
028    import javax.xml.transform.Source;
029    import javax.xml.transform.Transformer;
030    import javax.xml.transform.TransformerConfigurationException;
031    import javax.xml.transform.TransformerException;
032    import javax.xml.transform.TransformerFactory;
033    import javax.xml.transform.URIResolver;
034    import javax.xml.transform.dom.DOMResult;
035    import javax.xml.transform.dom.DOMSource;
036    import javax.xml.transform.stream.StreamSource;
037    import java.io.IOException;
038    
039    /**
040     * @version $Revision$
041     */
042    public class ActiveMQBeanDefinitionReader extends XmlBeanDefinitionReader {
043        private String brokerName;
044    
045        public ActiveMQBeanDefinitionReader(BeanDefinitionRegistry beanDefinitionRegistry, String brokerName) {
046            super(beanDefinitionRegistry);
047            this.brokerName = brokerName;
048            setEntityResolver(createEntityResolver());
049        }
050    
051        public int registerBeanDefinitions(Document document, Resource resource) throws BeansException {
052            try {
053                Document newDocument = transformDocument(document);
054                return super.registerBeanDefinitions(newDocument, resource);
055            }
056            catch (Exception e) {
057                throw new ConfigurationParseException(resource, e);
058            }
059        }
060    
061        public static Transformer createTransformer(Source source) throws TransformerConfigurationException {
062            TransformerFactory factory = TransformerFactory.newInstance();
063            Transformer transformer = factory.newTransformer(source);
064            transformer.setURIResolver(new URIResolver() {
065                public Source resolve(String href, String base) {
066                    System.out.println("Called with href:  " + href + " base: " + base);
067                    return null;
068                }
069            });
070            return transformer;
071        }
072    
073    
074        // Properties
075        //-------------------------------------------------------------------------
076        public String getBrokerName() {
077            return brokerName;
078        }
079    
080        public void setBrokerName(String brokerName) {
081            this.brokerName = brokerName;
082        }
083    
084        // Implementation methods
085        //-------------------------------------------------------------------------
086    
087        /**
088         * A hook to transform the source document into a default Spring XML configuration
089         *
090         * @param document
091         * @return
092         */
093        protected Document transformDocument(Document document) throws IOException, TransformerException {
094            Transformer transformer = createTransformer(createXslSource());
095            transformer.setParameter("brokerName", getBrokerName());
096            DOMResult result = new DOMResult();
097            transformer.transform(new DOMSource(document), result);
098            return (Document) result.getNode();
099        }
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/activemq/activemq-to-spring.xsl");
115        }
116    
117        /**
118         * @return a new EnittyResolver
119         */
120        protected EntityResolver createEntityResolver() {
121            return new ActiveMQDtdResolver();
122        }
123    }