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.BeanFactory;
022    import org.springframework.beans.factory.support.DefaultListableBeanFactory;
023    import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
024    import org.springframework.core.io.InputStreamResource;
025    import org.springframework.core.io.Resource;
026    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
027    
028    import java.io.InputStream;
029    
030    /**
031     * A Spring BeanFactory for creating ActiveMQ objects
032     *
033     * @version $Revision$
034     */
035    public class ActiveMQBeanFactory extends DefaultListableBeanFactory {
036    
037        private XmlBeanDefinitionReader reader;
038    
039        /**
040         * A static factory method which can be used easily from spring.xml
041         *
042         * @param resource XML resource to load bean definitions from
043         */
044        public static ActiveMQBeanFactory newInstance(String brokerName, Resource resource) {
045            return new ActiveMQBeanFactory(brokerName, resource);
046        }
047    
048        /**
049         * Create a new ActiveMQBeanFactory with the given resource,
050         * which must be parsable using DOM.
051         *
052         * @param resource XML resource to load bean definitions from
053         * @throws org.springframework.beans.BeansException
054         *          in case of loading or parsing errors
055         */
056        public ActiveMQBeanFactory(String brokerName, Resource resource) throws BeansException {
057            this(brokerName, resource, null);
058        }
059    
060        /**
061         * Create a new ActiveMQBeanFactory with the given InputStream,
062         * which must be parsable using DOM.
063         * <p>It's preferable to use a Resource argument instead of an
064         * InputStream, to retain location information. This constructor
065         * is mainly kept for backward compatibility.
066         *
067         * @param is XML InputStream to load bean definitions from
068         * @throws BeansException in case of loading or parsing errors
069         * @see #ActiveMQBeanFactory(String, Resource)
070         */
071        public ActiveMQBeanFactory(String brokerName, InputStream is) throws BeansException {
072            this(brokerName, new InputStreamResource(is, "(no description)"), null);
073        }
074    
075        /**
076         * Create a new ActiveMQBeanFactory with the given input stream,
077         * which must be parsable using DOM.
078         *
079         * @param resource          XML resource to load bean definitions from
080         * @param parentBeanFactory parent bean factory
081         * @throws BeansException in case of loading or parsing errors
082         */
083        public ActiveMQBeanFactory(String brokerName, Resource resource, BeanFactory parentBeanFactory) throws BeansException {
084            super(parentBeanFactory);
085            reader = createReader(brokerName);
086            reader.loadBeanDefinitions(resource);
087            PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
088            configurer.setSystemPropertiesMode(PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
089            configurer.postProcessBeanFactory(this);
090        }
091    
092        protected XmlBeanDefinitionReader getReader() {
093            return reader;
094        }
095    
096        /**
097         * A hook to allow custom ActiveMQBeanFactory implementations to provide
098         * their own custom parser of the XML to perform macro processing
099         * or perform XSLT etc
100         *
101         * @return
102         */
103        protected XmlBeanDefinitionReader createReader(String brokerName) {
104            return new ActiveMQBeanDefinitionReader(this, brokerName);
105        }
106    }