/**
*
* 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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.gbean.GAttributeInfo;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;
import org.apache.geronimo.gbean.GConstructorInfo;
import org.apache.geronimo.gbean.GReferenceInfo;
import org.apache.geronimo.gbean.WaitingException;
import org.codehaus.activemq.ActiveMQConnection;
import org.codehaus.activemq.ActiveMQConnectionFactory;
import org.codehaus.activemq.broker.BrokerConnector;
import org.codehaus.activemq.broker.impl.BrokerConnectorImpl;
import org.codehaus.activemq.io.WireFormat;
import org.codehaus.activemq.io.impl.DefaultWireFormat;
import javax.jms.JMSException;
/**
* Default implementation of the ActiveMQ connector
*
* @version $Revision: 1.4 $
*/
public class ActiveMQConnectorGBean implements GBeanLifecycle {
private Log log = LogFactory.getLog(getClass().getName());
private BrokerConnector brokerConnector;
private ActiveMQContainer container;
private WireFormat wireFormat = new DefaultWireFormat();
private String url = ActiveMQConnection.DEFAULT_URL;
public ActiveMQConnectorGBean(ActiveMQContainer container) {
this.container = container;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public WireFormat getWireFormat() {
return wireFormat;
}
public void setWireFormat(WireFormat wireFormat) {
//seems like a bug in attribute handling???
if (wireFormat == null) {
this.wireFormat = new DefaultWireFormat();
}
else {
this.wireFormat = wireFormat;
}
}
public synchronized void doStart() throws WaitingException, Exception {
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(ActiveMQContainerGBean.class.getClassLoader());
try {
if (brokerConnector == null) {
brokerConnector = createBrokerConnector();
brokerConnector.start();
ActiveMQConnectionFactory.registerBroker(url, brokerConnector);
}
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
public synchronized void doStop() throws WaitingException, Exception {
if (brokerConnector != null) {
ActiveMQConnectionFactory.unregisterBroker(url);
BrokerConnector temp = brokerConnector;
brokerConnector = null;
temp.stop();
}
}
public synchronized void doFail() {
if (brokerConnector != null) {
BrokerConnector temp = brokerConnector;
brokerConnector = null;
try {
temp.stop();
}
catch (JMSException e) {
log.info("Caught while closing due to failure: " + e, e);
}
}
}
protected BrokerConnector createBrokerConnector() throws Exception {
return new BrokerConnectorImpl(container.getBrokerContainer(), url, wireFormat);
}
public static final GBeanInfo GBEAN_INFO;
static {
GBeanInfoBuilder infoFactory = new GBeanInfoBuilder("ActiveMQ Message Broker Connector", ActiveMQConnectorGBean.class.getName());
infoFactory.addAttribute(new GAttributeInfo("Url", String.class.getName(), true));
infoFactory.addAttribute(new GAttributeInfo("WireFormat", WireFormat.class.getName(), true));
infoFactory.addReference(new GReferenceInfo("ActiveMQContainer", ActiveMQContainer.class));
infoFactory.setConstructor(new GConstructorInfo(new String[]{"ActiveMQContainer"}));
GBEAN_INFO = infoFactory.getBeanInfo();
}
public static GBeanInfo getGBeanInfo() {
return GBEAN_INFO;
}
}