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.apache.commons.logging.Log;
    21  import org.apache.commons.logging.LogFactory;
    22  import org.codehaus.activemq.broker.BrokerContainer;
    23  import org.codehaus.activemq.broker.BrokerContainerFactory;
    24  import org.codehaus.activemq.broker.BrokerContext;
    25  import org.springframework.core.io.Resource;
    26  import org.springframework.core.io.ResourceEditor;
    27
    28  /***
    29   * A Spring implementatation of {@link BrokerContainerFactory} which uses an XML
    30   * deployment configuration file to load and configure a {@link BrokerContainer}
    31   *
    32   * @version $Revision: 1.3 $
    33   */
    34  public class SpringBrokerContainerFactory implements BrokerContainerFactory {
    35      private static final Log log = LogFactory.getLog(SpringBrokerContainerFactory.class);
    36
    37      private Resource resource;
    38
    39
    40      /***
    41       * A static factory method that can be used in Spring config files using a factory method
    42       * mechanism to create a broker container easily.
    43       */
    44      public static BrokerContainer newInstance(Resource resource, String brokerName) {
    45          SpringBrokerContainerFactory factory = new SpringBrokerContainerFactory(resource);
    46          return factory.createBrokerContainer(brokerName, BrokerContext.getInstance());
    47      }
    48
    49      /***
    50       * A helper method, invoked via reflection, to create a new factory from a given configuration
    51       * file String which if it starts with classpath: is a classpath URI otherwise a URL is assumed.
    52       *
    53       * @param resourceName
    54       * @return
    55       */
    56      public static SpringBrokerContainerFactory newFactory(String resourceName) {
    57          ResourceEditor editor = new ResourceEditor();
    58          editor.setAsText(resourceName);
    59          Resource resource = (Resource) editor.getValue();
    60          if (resource == null) {
    61              throw new IllegalArgumentException("Could not convert '" + resourceName + "' into a Spring Resource");
    62          }
    63          return new SpringBrokerContainerFactory(resource);
    64      }
    65
    66      public SpringBrokerContainerFactory() {
    67      }
    68
    69      public SpringBrokerContainerFactory(Resource resource) {
    70          this.resource = resource;
    71      }
    72
    73      public BrokerContainer createBrokerContainer(String brokerName, BrokerContext context) {
    74          log.info("Loading ActiveMQ broker from configuration: " + resource);
    75
    76          ActiveMQBeanFactory beanFactory = new ActiveMQBeanFactory(brokerName, resource);
    77          return (BrokerContainer) beanFactory.getBean("broker");
    78      }
    79
    80      // Properties
    81      //-------------------------------------------------------------------------
    82      public Resource getResource() {
    83          return resource;
    84      }
    85
    86      public void setResource(Resource resource) {
    87          this.resource = resource;
    88      }
    89  }