/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.bbg;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bloomberglp.blpapi.Event;
import com.bloomberglp.blpapi.EventHandler;
import com.bloomberglp.blpapi.Message;
import com.bloomberglp.blpapi.MessageIterator;
import com.bloomberglp.blpapi.Session;
import com.bloomberglp.blpapi.SessionOptions;
import com.bloomberglp.blpapi.Event.EventType;
import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.bbg.util.SessionOptionsUtils;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.Connector;

/**
 * Connector used to access Bloomberg.
 * <p>
 * This class does not perform any connecting.
 * Instead it acts as a data holder for connectivity.
 * <p>
 * This class is usually configured using the associated factory bean.
 */
public class BloombergConnector implements Connector {

  private static final Logger s_logger = LoggerFactory.getLogger(BloombergConnector.class);
  /**
   * The configuration name.
   */
  private final String _name;
  /**
   * The Bloomberg Session Options.
   */
  private final SessionOptions _sessionOptions;
//  /**
//   * Whether the shared session is initialized.
//   */
//  private volatile boolean _sharedSessionStarted;
//  /**
//   * The shared Bloomberg Session.
//   */
//  private volatile Session _sharedSession;

  /**
   * Creates an instance.
   * 
   * @param name  the configuration name, not null
   * @param sessionOptions  the Bloomberg session options, not null
   */
  public BloombergConnector(String name, SessionOptions sessionOptions) {
    ArgumentChecker.notNull(name, "name");
    ArgumentChecker.notNull(sessionOptions, "sessionOptions");
    ArgumentChecker.notNull(sessionOptions.getServerHost(), "sessionOptions.serverHost");
    _name = name;
    _sessionOptions = sessionOptions;
  }

  /**
   * Creates an instance.
   * <p>
   * Subclasses must override the session options getter.
   * 
   * @param name  the configuration name, not null
   */
  protected BloombergConnector(String name) {
    ArgumentChecker.notNull(name, "name");
    _name = name;
    _sessionOptions = null;
  }

  //-------------------------------------------------------------------------
  @Override
  public final String getName() {
    return _name;
  }

  @Override
  public final Class<? extends Connector> getType() {
    return BloombergConnector.class;
  }

  //-------------------------------------------------------------------------
  /**
   * Gets the Bloomberg session options.
   * <p>
   * DO NOT MODIFY this object.
   * 
   * @return the Bloomberg session options, not null
   */
  public SessionOptions getSessionOptions() {
    return _sessionOptions;
  }

  //-------------------------------------------------------------------------
//  /**
//   * Returns the shared Bloomberg {@code Session} owned by this connector.
//   * <p>
//   * The shared session is started synchronously if not yet started.
//   * See {@link #obtainNewSession()} to create a session managed by the caller.
//   * 
//   * @return the shared Bloomberg session, not null
//   * @throws RuntimeException if an error occurs
//   */
//  public Session obtainSharedSession() {
//    if (_sharedSessionStarted == false) {
//      synchronized (this) {
//        if (_sharedSessionStarted == false) {
//          Session session = obtainNewSession();
//          _sharedSession = session;
//          _sharedSessionStarted = true;
//          return session;
//        }
//      }
//    }
//    return _sharedSession;
//  }

  /**
   * Creates and starts a new Bloomberg {@code Session}.
   * <p>
   * The session is started synchronously.
   * The connector does not retain the state of the session, thus the caller
   * is responsible for its lifecycle.
   * 
   * @return the started Bloomberg session, not null
   * @throws RuntimeException if an error occurs
   */
  public Session createOpenSession() {
    Session session = createSession();
    try {
      if (session.start() == false) {
        throw new OpenGammaRuntimeException("Bloomberg session failed to start: " + SessionOptionsUtils.toString(getSessionOptions()));
      }
    } catch (InterruptedException ex) {
      Thread.interrupted();
      throw new OpenGammaRuntimeException("Bloomberg session failed to start: " + SessionOptionsUtils.toString(getSessionOptions()), ex);
    } catch (Exception ex) {
      throw new OpenGammaRuntimeException("Bloomberg session failed to start: " + SessionOptionsUtils.toString(getSessionOptions()), ex);
    }
    return session;
  }

  /**
   * Creates a Bloomberg session that uses the session options.
   * <p>
   * The session is not opened.
   * 
   * @return the Bloomberg session, not null
   */
  public Session createSession() {
    return new Session(getSessionOptions());
  }

  //-------------------------------------------------------------------------
  @Override
  public void close() {
//    if (_sharedSessionStarted) {
//      try {
//        _sharedSession.stop();
//      } catch (InterruptedException ex) {
//        Thread.interrupted();
//        throw new OpenGammaRuntimeException("Interrupted while starting session with options " + getSessionOptions(), ex);
//      }
//    }
  }

  //-------------------------------------------------------------------------
  /**
   * Returns a description of this object suitable for debugging.
   * 
   * @return the description, not null
   */
  @Override
  public String toString() {
    return getClass().getSimpleName() + "[" + _name + "]";
  }

}

@@ -163,233 +163,8 @@
 g;%0A%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport com.bloomberglp.blpapi.Event;%0Aimport com.bloomberglp.blpapi.EventHandler;%0Aimport com.bloomberglp.blpapi.Message;%0Aimport com.bloomberglp.blpapi.MessageIterator;%0A
 impo
@@ -248,55 +248,8 @@
 ns;%0A
-import com.bloomberglp.blpapi.Event.EventType;%0A
 impo
@@ -717,100 +717,8 @@
  %7B%0A%0A
-  private static final Logger s_logger = LoggerFactory.getLogger(BloombergConnector.class);%0A
   /*
@@ -883,224 +883,8 @@
 ons;
-%0A//  /**%0A//   * Whether the shared session is initialized.%0A//   */%0A//  private volatile boolean _sharedSessionStarted;%0A//  /**%0A//   * The shared Bloomberg Session.%0A//   */%0A//  private volatile Session _sharedSession;
 %0A%0A  
@@ -1228,98 +1228,8 @@
 %22);%0A
-    ArgumentChecker.notNull(sessionOptions.getServerHost(), %22sessionOptions.serverHost%22);%0A
     
@@ -2234,767 +2234,8 @@
 ---%0A
-//  /**%0A//   * Returns the shared Bloomberg %7B@code Session%7D owned by this connector.%0A//   * %3Cp%3E%0A//   * The shared session is started synchronously if not yet started.%0A//   * See %7B@link #obtainNewSession()%7D to create a session managed by the caller.%0A//   * %0A//   * @return the shared Bloomberg session, not null%0A//   * @throws RuntimeException if an error occurs%0A//   */%0A//  public Session obtainSharedSession() %7B%0A//    if (_sharedSessionStarted == false) %7B%0A//      synchronized (this) %7B%0A//        if (_sharedSessionStarted == false) %7B%0A//          Session session = obtainNewSession();%0A//          _sharedSession = session;%0A//          _sharedSessionStarted = true;%0A//          return session;%0A//        %7D%0A//      %7D%0A//    %7D%0A//    return _sharedSession;%0A//  %7D%0A%0A
   /*
@@ -3620,308 +3620,67 @@
 ) %7B%0A
-//
     
-if (_sharedSessionStarted) %7B%0A//      try %7B%0A//        _sharedSession.stop();%0A//      %7D catch (InterruptedException ex) %7B%0A//        Thread.interrupted();%0A//        throw new OpenGammaRuntimeException(%22Interrupted while starting session with options %22 + getSessionOptions(), ex);%0A//      %7D%0A//    %7D
+// no action, as the connector holds no closeable state
 %0A  %7D

Remove old code in BloombergConnector