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

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.Connector;

/**
 * Connector used to access Redis databases.
 * <p>
 * This class is usually configured using the associated factory bean.
 */
public class RedisConnector implements Connector {

  /**
   * The configuration name.
   */
  private final String _name;
  
  private final String _host;
  
  private final int _port;
  
  private final String _password;
  
  private final JedisPool _jedisPool;
  
  private final JedisPoolConfig _jedisPoolConfig;
  
  /**
   * Creates an instance.
   *  
   * @param name the configuration name, not null
   * @param host the redis server host, not null
   * @param port the redis server port, not null
   */
  public RedisConnector(final String name, final String host, final int port) {
    this(name, host, port, (String) null);
  }

  /**
   * Creates an instance.
   *  
   * @param name the configuration name, not null
   * @param host the redis server host, not null
   * @param port the redis server port, not null
   * @param password the redis server password, may be null
   */
  public RedisConnector(final String name, final String host, final int port, String password) {
    this(name, host, port, password, new JedisPoolConfig());
  }

  /**
   * Creates an instance.
   * 
   * @param name  the configuration name, not null
   * @param host  the redis server host, not null
   * @param port  the redis server port, not null
   * @param config the redis pool config, not null
   */
  public RedisConnector(final String name, final String host, final int port, final JedisPoolConfig config) {
    this(name, host, port, null, config);
  }

  /**
   * Creates an instance.
   * 
   * @param name  the configuration name, not null
   * @param host  the redis server host, not null
   * @param port  the redis server port, not null
   * @param password the redis server password. may be null
   * @param config the redis pool config, not null
   */
  public RedisConnector(final String name, final String host, final int port, final String password, final JedisPoolConfig config) {
    ArgumentChecker.notNull(name, "name");
    ArgumentChecker.notNull(host, "redisServer");
    ArgumentChecker.notNull(port, "port");
    ArgumentChecker.notNull(config, "jedis pool config");
    _name = name;
    _host = host;
    _port = port;
    _password = password;
    _jedisPoolConfig = config;
    JedisPool pool = null;
    if (password == null) {
      pool = new JedisPool(config, _host, _port);
    } else {
      pool = new JedisPool(config, _host, _port, Protocol.DEFAULT_TIMEOUT, _password);
    }
    _jedisPool = pool;
  }

  //-------------------------------------------------------------------------
  @Override
  public final String getName() {
    return _name;
  }
  
  /**
   * Gets the host.
   * @return the host
   */
  public String getHost() {
    return _host;
  }

  /**
   * Gets the port.
   * @return the port
   */
  public int getPort() {
    return _port;
  }

  /**
   * Gets the password.
   * @return the password
   */
  public String getPassword() {
    return _password;
  }

  /**
   * Gets the jedisPool.
   * @return the jedisPool
   */
  public JedisPool getJedisPool() {
    return _jedisPool;
  }
  
  /**
   * Gets the jedisPoolConfig.
   * @return the jedisPoolConfig
   */
  public JedisPoolConfig getJedisPoolConfig() {
    return _jedisPoolConfig;
  }

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

  //-------------------------------------------------------------------------
  @Override
  public void close() {
    _jedisPool.destroy();
  }

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

}

@@ -815,24 +815,57 @@
 lConfig;%0A  %0A
+  private final int _timeOut;%0A  %0A
   /**%0A   * C
@@ -1581,16 +1581,42 @@
 Config()
+, Protocol.DEFAULT_TIMEOUT
 );%0A  %7D%0A%0A
@@ -2010,16 +2010,42 @@
 , config
+, Protocol.DEFAULT_TIMEOUT
 );%0A  %7D%0A%0A
@@ -2336,32 +2336,90 @@
 onfig, not null%0A
+   * @param timeOut the time out in millisecond, not null%0A
    */%0A  public R
@@ -2529,32 +2529,51 @@
 oolConfig config
+, final int timeOut
 ) %7B%0A    Argument
@@ -2750,24 +2750,78 @@
 l config%22);%0A
+    ArgumentChecker.notNull(timeOut, %22timeOut%22);%0A    %0A
     _name = 
@@ -2919,16 +2919,45 @@
 config;%0A
+    _timeOut = timeOut;%0A    %0A
     Jedi
@@ -3050,16 +3050,26 @@
 t, _port
+, _timeOut
 );%0A    %7D
@@ -3129,32 +3129,16 @@
 rt, 
-Protocol.DEFAULT_TIMEOUT
+_timeOut
 , _p
@@ -3780,32 +3780,146 @@
 disPool;%0A  %7D%0A  %0A
+  /**%0A   * Gets the timeOut.%0A   * @return the timeOut%0A   */%0A  public int getTimeOut() %7B%0A    return _timeOut;%0A  %7D%0A%0A
   /**%0A   * Gets 

Add socket timeOut to redis connector