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