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.gbean;
    19
    20  import org.apache.commons.logging.Log;
    21  import org.apache.commons.logging.LogFactory;
    22  import org.apache.geronimo.gbean.GBeanInfo;
    23  import org.apache.geronimo.gbean.GBeanInfoFactory;
    24  import org.apache.geronimo.gbean.GBeanLifecycle;
    25  import org.apache.geronimo.gbean.WaitingException;
    26  import org.codehaus.activemq.broker.BrokerContainer;
    27  import org.codehaus.activemq.broker.impl.BrokerContainerImpl;
    28
    29  import javax.jms.JMSException;
    30
    31  /***
    32   * Default implementation of the ActiveMQ Message Server
    33   *
    34   * @version $Revision: 1.1 $
    35   */
    36  public class ActiveMQContainer implements GBeanLifecycle {
    37
    38      private Log log = LogFactory.getLog(getClass().getName());
    39
    40      private final String brokerName;
    41
    42      private BrokerContainer container;
    43
    44      //default constructor for use as gbean endpoint.
    45      public ActiveMQContainer() {
    46          brokerName = null;
    47      }
    48
    49      public ActiveMQContainer(String brokerName) {
    50          assert brokerName != null;
    51          this.brokerName = brokerName;
    52      }
    53
    54      public BrokerContainer getBrokerContainer() {
    55          return container;
    56      }
    57
    58      public synchronized void doStart() throws WaitingException, Exception {
    59          if (container == null) {
    60              container = createContainer();
    61              container.start();
    62          }
    63      }
    64
    65      public synchronized void doStop() throws WaitingException, Exception {
    66          if (container != null) {
    67              BrokerContainer temp = container;
    68              container = null;
    69              temp.stop();
    70          }
    71      }
    72
    73      public synchronized void doFail() {
    74          if (container != null) {
    75              BrokerContainer temp = container;
    76              container = null;
    77              try {
    78                  temp.stop();
    79              }
    80              catch (JMSException e) {
    81                  log.info("Caught while closing due to failure: " + e, e);
    82              }
    83          }
    84      }
    85
    86      protected BrokerContainer createContainer() throws Exception {
    87          return new BrokerContainerImpl(brokerName);
    88      }
    89
    90      public static final GBeanInfo GBEAN_INFO;
    91
    92      static {
    93          GBeanInfoFactory infoFactory = new GBeanInfoFactory("ActiveMQ Message Broker", ActiveMQContainer.class.getName());
    94          infoFactory.addAttribute("brokerName", String.class, true);
    95          infoFactory.addAttribute("BrokerContainer", BrokerContainer.class, false);
    96          infoFactory.setConstructor(new String[]{"brokerName"});
    97
    98          GBEAN_INFO = infoFactory.getBeanInfo();
    99      }
    100
    101     public static GBeanInfo getGBeanInfo() {
    102         return GBEAN_INFO;
    103     }
    104 }