/**
 * 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;

/**
 * Component definition for starting Active MQ.
 */
@BeanDefinition
public class SpringActiveMqComponentFactory extends AbstractSpringComponentFactory implements ComponentFactory {

  //-------------------------------------------------------------------------
  @Override
  public void init(ComponentRepository repo, LinkedHashMap<String, String> configuration) {
    GenericApplicationContext appContext = createApplicationContext(repo);
    
    String[] beanNames = appContext.getBeanNamesForType(BrokerService.class);
    if (beanNames.length != 1) {
      throw new IllegalStateException("Expected 1 broker, but found " + beanNames.length);
    }
    BrokerService broker = appContext.getBean(beanNames[0], BrokerService.class);
    repo.registerInfrastructure(BrokerService.class, "jetty", broker);
    repo.registerLifecycle(new ActiveMQLifecycle(broker));
  }

  //-------------------------------------------------------------------------
  /**
   * Wraps ActiveMQ LifeCycle in Spring's Lifecycle.
   */
  static class ActiveMQLifecycle implements Lifecycle {
    private final BrokerService _broker;

    public ActiveMQLifecycle(BrokerService broker) {
      _broker = broker;
    }

    @Override
    public void start() {
      try {
        _broker.start();
      } catch (Exception ex) {
        throw new OpenGammaRuntimeException(ex.getMessage(), ex);
      }
    }

    @Override
    public void stop() {
      try {
        _broker.stop();
      } catch (Exception ex) {
        throw new OpenGammaRuntimeException(ex.getMessage(), ex);
      }
    }

    @Override
    public boolean isRunning() {
      return _broker.isStarted();
    }
  }

  //------------------------- AUTOGENERATED START -------------------------
  ///CLOVER:OFF
  /**
   * The meta-bean for {@code SpringInfrastructureComponentFactory}.
   * @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
  protected Object propertyGet(String propertyName, boolean quiet) {
    return super.propertyGet(propertyName, quiet);
  }

  @Override
  protected void propertySet(String propertyName, Object newValue, boolean quiet) {
    super.propertySet(propertyName, newValue, quiet);
  }

  @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();
  }

  //-----------------------------------------------------------------------
  /**
   * The meta-bean for {@code SpringInfrastructureComponentFactory}.
   */
  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<Object>> _map = 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<Object>> metaPropertyMap() {
      return _map;
    }

    //-----------------------------------------------------------------------
  }

  ///CLOVER:ON
  //-------------------------- AUTOGENERATED END --------------------------
}

Add a local ActiveMQ component factory