Package com.opengamma.util.redis

Source Code of com.opengamma.util.redis.RedisConnector

/**
* 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 + "]";
  }

}
TOP

Related Classes of com.opengamma.util.redis.RedisConnector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.