/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.gbean;
import javax.jms.JMSException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoFactory;
import org.apache.geronimo.gbean.GBeanLifecycle;
import org.apache.geronimo.gbean.WaitingException;
import org.apache.geronimo.system.serverinfo.ServerInfo;
import org.codehaus.activemq.broker.BrokerContainer;
import org.codehaus.activemq.broker.BrokerContext;
import org.codehaus.activemq.broker.impl.BrokerContainerImpl;
import org.codehaus.activemq.store.jdbm.JdbmPersistenceAdapter;
/**
* Default implementation of the ActiveMQ Message Server
*
* @version $Revision: 1.1 $
*/
public class ActiveMQContainerGBean implements GBeanLifecycle, ActiveMQContainer {
private Log log = LogFactory.getLog(getClass().getName());
private final String brokerName;
private BrokerContext context = BrokerContext.getInstance();
private BrokerContainer container;
private final ServerInfo serverInfo;
private final String dataDirectory;
//default constructor for use as gbean endpoint.
public ActiveMQContainerGBean() {
serverInfo=null;
brokerName = null;
dataDirectory = "/var/activemq";
}
public ActiveMQContainerGBean(ServerInfo serverInfo, String brokerName, String dataDirectory) {
assert serverInfo != null;
assert brokerName != null;
assert dataDirectory != null;
this.serverInfo=serverInfo;
this.brokerName = brokerName;
this.dataDirectory = dataDirectory;
}
public synchronized BrokerContainer getBrokerContainer() {
return container;
}
public synchronized void doStart() throws WaitingException, Exception {
if (container == null) {
container = createContainer();
container.start();
}
}
public synchronized void doStop() throws WaitingException, Exception {
if (container != null) {
BrokerContainer temp = container;
container = null;
temp.stop();
}
}
public synchronized void doFail() {
if (container != null) {
BrokerContainer temp = container;
container = null;
try {
temp.stop();
}
catch (JMSException e) {
log.info("Caught while closing due to failure: " + e, e);
}
}
}
protected BrokerContainer createContainer() throws Exception {
BrokerContainerImpl answer = new BrokerContainerImpl(brokerName, context);
JdbmPersistenceAdapter pa = new JdbmPersistenceAdapter( serverInfo.resolve(dataDirectory) );
answer.setPersistenceAdapter( pa );
return answer;
}
public static final GBeanInfo GBEAN_INFO;
static {
GBeanInfoFactory infoFactory = new GBeanInfoFactory("ActiveMQ Message Broker", ActiveMQContainerGBean.class.getName());
infoFactory.addReference("serverInfo", ServerInfo.class);
infoFactory.addAttribute("brokerName", String.class, true);
infoFactory.addAttribute("dataDirectory", String.class, true);
infoFactory.addAttribute("BrokerContainer", BrokerContainer.class, false);
infoFactory.setConstructor(new String[]{"serverInfo", "brokerName", "dataDirectory"});
GBEAN_INFO = infoFactory.getBeanInfo();
}
public static GBeanInfo getGBeanInfo() {
return GBEAN_INFO;
}
}