/**
* Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
*
* Please see distribution for license.
*/
package com.opengamma.component.factory;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.activemq.broker.BrokerService;
import org.joda.beans.BeanBuilder;
import org.joda.beans.BeanDefinition;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaProperty;
import org.joda.beans.impl.direct.DirectBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;
import org.springframework.context.Lifecycle;
import org.springframework.context.support.GenericApplicationContext;
import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.component.ComponentFactory;
import com.opengamma.component.ComponentRepository;
import com.opengamma.transport.jms.ActiveMQJmsConfiguration;
/**
* Component definition for starting Active MQ.
*/
@BeanDefinition
public class SpringActiveMqComponentFactory extends AbstractSpringComponentFactory implements ComponentFactory {
//-------------------------------------------------------------------------
@Override
public void init(final ComponentRepository repo, final LinkedHashMap<String, String> configuration) {
final GenericApplicationContext appContext = createApplicationContext(repo);
final String[] beanNames = appContext.getBeanNamesForType(BrokerService.class);
if (beanNames.length != 1) {
throw new IllegalStateException("Expected 1 broker, but found " + beanNames.length);
}
final BrokerService broker = appContext.getBean(beanNames[0], BrokerService.class);
repo.registerComponent(BrokerService.class, "jetty", broker);
repo.registerLifecycle(new ActiveMQLifecycle(broker));
repo.registerComponent(ActiveMQJmsConfiguration.class, "main", appContext.getBean("mainActiveMQJmsConfiguration", ActiveMQJmsConfiguration.class));
}
//-------------------------------------------------------------------------
/**
* Wraps ActiveMQ LifeCycle in Spring's Lifecycle.
*/
static class ActiveMQLifecycle implements Lifecycle {
private final BrokerService _broker;
public ActiveMQLifecycle(final BrokerService broker) {
_broker = broker;
}
@Override
public void start() {
try {
_broker.start();
} catch (final Exception ex) {
throw new OpenGammaRuntimeException(ex.getMessage(), ex);
}
}
@Override
public void stop() {
try {
_broker.stop();
} catch (final Exception ex) {
throw new OpenGammaRuntimeException(ex.getMessage(), ex);
}
}
@Override
public boolean isRunning() {
return _broker.isStarted();
}
}
//------------------------- AUTOGENERATED START -------------------------
///CLOVER:OFF
/**
* The meta-bean for {@code SpringActiveMqComponentFactory}.
* @return the meta-bean, not null
*/
public static SpringActiveMqComponentFactory.Meta meta() {
return SpringActiveMqComponentFactory.Meta.INSTANCE;
}
static {
JodaBeanUtils.registerMetaBean(SpringActiveMqComponentFactory.Meta.INSTANCE);
}
@Override
public SpringActiveMqComponentFactory.Meta metaBean() {
return SpringActiveMqComponentFactory.Meta.INSTANCE;
}
//-----------------------------------------------------------------------
@Override
public SpringActiveMqComponentFactory clone() {
return JodaBeanUtils.cloneAlways(this);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj != null && obj.getClass() == this.getClass()) {
return super.equals(obj);
}
return false;
}
@Override
public int hashCode() {
int hash = 7;
return hash ^ super.hashCode();
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder(32);
buf.append("SpringActiveMqComponentFactory{");
int len = buf.length();
toString(buf);
if (buf.length() > len) {
buf.setLength(buf.length() - 2);
}
buf.append('}');
return buf.toString();
}
@Override
protected void toString(StringBuilder buf) {
super.toString(buf);
}
//-----------------------------------------------------------------------
/**
* The meta-bean for {@code SpringActiveMqComponentFactory}.
*/
public static class Meta extends AbstractSpringComponentFactory.Meta {
/**
* The singleton instance of the meta-bean.
*/
static final Meta INSTANCE = new Meta();
/**
* The meta-properties.
*/
private final Map<String, MetaProperty<?>> _metaPropertyMap$ = new DirectMetaPropertyMap(
this, (DirectMetaPropertyMap) super.metaPropertyMap());
/**
* Restricted constructor.
*/
protected Meta() {
}
@Override
public BeanBuilder<? extends SpringActiveMqComponentFactory> builder() {
return new DirectBeanBuilder<SpringActiveMqComponentFactory>(new SpringActiveMqComponentFactory());
}
@Override
public Class<? extends SpringActiveMqComponentFactory> beanType() {
return SpringActiveMqComponentFactory.class;
}
@Override
public Map<String, MetaProperty<?>> metaPropertyMap() {
return _metaPropertyMap$;
}
//-----------------------------------------------------------------------
}
///CLOVER:ON
//-------------------------- AUTOGENERATED END --------------------------
}