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.BeanFactory;
    22  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
    23  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
    24  import org.springframework.core.io.InputStreamResource;
    25  import org.springframework.core.io.Resource;
    26
    27  import java.io.InputStream;
    28
    29  /***
    30   * A Spring BeanFactory for creating ActiveMQ objects
    31   *
    32   * @version $Revision: 1.1 $
    33   */
    34  public class ActiveMQBeanFactory extends DefaultListableBeanFactory {
    35
    36      private XmlBeanDefinitionReader reader;
    37
    38      /***
    39       * A static factory method which can be used easily from spring.xml
    40       *
    41       * @param resource XML resource to load bean definitions from
    42       */
    43      public static ActiveMQBeanFactory newInstance(String brokerName, Resource resource) {
    44          return new ActiveMQBeanFactory(brokerName, resource);
    45      }
    46
    47      /***
    48       * Create a new ActiveMQBeanFactory with the given resource,
    49       * which must be parsable using DOM.
    50       *
    51       * @param resource XML resource to load bean definitions from
    52       * @throws org.springframework.beans.BeansException
    53       *          in case of loading or parsing errors
    54       */
    55      public ActiveMQBeanFactory(String brokerName, Resource resource) throws BeansException {
    56          this(brokerName, resource, null);
    57      }
    58
    59      /***
    60       * Create a new ActiveMQBeanFactory with the given InputStream,
    61       * which must be parsable using DOM.
    62       * <p>It's preferable to use a Resource argument instead of an
    63       * InputStream, to retain location information. This constructor
    64       * is mainly kept for backward compatibility.
    65       *
    66       * @param is XML InputStream to load bean definitions from
    67       * @throws BeansException in case of loading or parsing errors
    68       * @see #ActiveMQBeanFactory(String, Resource)
    69       */
    70      public ActiveMQBeanFactory(String brokerName, InputStream is) throws BeansException {
    71          this(brokerName, new InputStreamResource(is, "(no description)"), null);
    72      }
    73
    74      /***
    75       * Create a new ActiveMQBeanFactory with the given input stream,
    76       * which must be parsable using DOM.
    77       *
    78       * @param resource          XML resource to load bean definitions from
    79       * @param parentBeanFactory parent bean factory
    80       * @throws BeansException in case of loading or parsing errors
    81       */
    82      public ActiveMQBeanFactory(String brokerName, Resource resource, BeanFactory parentBeanFactory) throws BeansException {
    83          super(parentBeanFactory);
    84          reader = createReader(brokerName);
    85          reader.loadBeanDefinitions(resource);
    86      }
    87
    88      protected XmlBeanDefinitionReader getReader() {
    89          return reader;
    90      }
    91
    92      /***
    93       * A hook to allow custom ActiveMQBeanFactory implementations to provide
    94       * their own custom parser of the XML to perform macro processing
    95       * or perform XSLT etc
    96       *
    97       * @return
    98       */
    99      protected XmlBeanDefinitionReader createReader(String brokerName) {
    100         return new ActiveMQBeanDefinitionReader(this, brokerName);
    101     }
    102 }