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.GAttributeInfo;
23  import org.apache.geronimo.gbean.GBeanInfo;
24  import org.apache.geronimo.gbean.GBeanInfoFactory;
25  import org.apache.geronimo.gbean.GBeanLifecycle;
26  import org.apache.geronimo.gbean.GConstructorInfo;
27  import org.apache.geronimo.gbean.GReferenceInfo;
28  import org.apache.geronimo.gbean.WaitingException;
29  import org.codehaus.activemq.ActiveMQConnection;
30  import org.codehaus.activemq.ActiveMQConnectionFactory;
31  import org.codehaus.activemq.broker.BrokerConnector;
32  import org.codehaus.activemq.broker.impl.BrokerConnectorImpl;
33  import org.codehaus.activemq.message.DefaultWireFormat;
34  import org.codehaus.activemq.message.WireFormat;
35  
36  import javax.jms.JMSException;
37  
38  /***
39   * Default implementation of the ActiveMQ connector
40   *
41   * @version $Revision: 1.1 $
42   */
43  public class ActiveMQConnectorGBean implements GBeanLifecycle {
44  
45      private Log log = LogFactory.getLog(getClass().getName());
46  
47      private BrokerConnector brokerConnector;
48      private ActiveMQContainer container;
49      private WireFormat wireFormat = new DefaultWireFormat();
50      private String url = ActiveMQConnection.DEFAULT_URL;
51  
52      public ActiveMQConnectorGBean(ActiveMQContainer container) {
53          this.container = container;
54      }
55  
56      public String getUrl() {
57          return url;
58      }
59  
60      public void setUrl(String url) {
61          this.url = url;
62      }
63  
64      public WireFormat getWireFormat() {
65          return wireFormat;
66      }
67  
68      public void setWireFormat(WireFormat wireFormat) {
69          //seems like a bug in attribute handling???
70          if (wireFormat == null) {
71              this.wireFormat = new DefaultWireFormat();
72          }
73          else {
74              this.wireFormat = wireFormat;
75          }
76      }
77  
78      public synchronized void doStart() throws WaitingException, Exception {
79          if (brokerConnector == null) {
80              brokerConnector = createBrokerConnector();
81              brokerConnector.start();
82              ActiveMQConnectionFactory.registerBroker(url, brokerConnector);
83          }
84      }
85  
86      public synchronized void doStop() throws WaitingException, Exception {
87          if (brokerConnector != null) {
88              ActiveMQConnectionFactory.unregisterBroker(url);
89              BrokerConnector temp = brokerConnector;
90              brokerConnector = null;
91              temp.stop();
92          }
93      }
94  
95      public synchronized void doFail() {
96          if (brokerConnector != null) {
97              BrokerConnector temp = brokerConnector;
98              brokerConnector = null;
99              try {
100                 temp.stop();
101             }
102             catch (JMSException e) {
103                 log.info("Caught while closing due to failure: " + e, e);
104             }
105         }
106     }
107 
108     protected BrokerConnector createBrokerConnector() throws Exception {
109         return new BrokerConnectorImpl(container.getBrokerContainer(), url, wireFormat);
110     }
111 
112     public static final GBeanInfo GBEAN_INFO;
113 
114     static {
115         GBeanInfoFactory infoFactory = new GBeanInfoFactory("ActiveMQ Message Broker Connector", ActiveMQConnectorGBean.class.getName());
116         infoFactory.addAttribute(new GAttributeInfo("Url", String.class.getName(), true));
117         infoFactory.addAttribute(new GAttributeInfo("WireFormat", WireFormat.class.getName(), true));
118         infoFactory.addReference(new GReferenceInfo("ActiveMQContainer", ActiveMQContainer.class));
119         infoFactory.setConstructor(new GConstructorInfo(new String[]{"ActiveMQContainer"}));
120         GBEAN_INFO = infoFactory.getBeanInfo();
121     }
122 
123     public static GBeanInfo getGBeanInfo() {
124         return GBEAN_INFO;
125     }
126 }