<commit_before>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.financial.user.rest;

import org.fudgemsg.FudgeContext;

import com.opengamma.financial.analytics.ircurve.InterpolatedYieldCurveDefinitionMaster;
import com.opengamma.financial.analytics.ircurve.rest.RemoteInterpolatedYieldCurveDefinitionMaster;
import com.opengamma.financial.portfolio.rest.RemotePortfolioMaster;
import com.opengamma.financial.position.rest.RemotePositionMaster;
import com.opengamma.financial.security.rest.RemoteSecurityMaster;
import com.opengamma.financial.view.ManageableViewDefinitionRepository;
import com.opengamma.financial.view.rest.RemoteManageableViewDefinitionRepository;
import com.opengamma.master.portfolio.PortfolioMaster;
import com.opengamma.master.position.PositionMaster;
import com.opengamma.master.security.SecurityMaster;
import com.opengamma.transport.jaxrs.RestClient;
import com.opengamma.transport.jaxrs.RestTarget;
import com.opengamma.util.GUIDGenerator;

/**
 * Provides access to a remote representation of a client
 */
public class RemoteClient {

  private final String _clientId;
  private final FudgeContext _fudgeContext;
  private final RestTarget _baseTarget;
  private PortfolioMaster _portfolioMaster;
  private PositionMaster _positionMaster;
  private SecurityMaster _securityMaster;
  private ManageableViewDefinitionRepository _viewDefinitionRepository;
  private InterpolatedYieldCurveDefinitionMaster _interpolatedYieldCurveDefinitionMaster;

  public RemoteClient(String clientId, FudgeContext fudgeContext, RestTarget baseTarget) {
    _clientId = clientId;
    _fudgeContext = fudgeContext;
    _baseTarget = baseTarget;
  }

  public String getClientId() {
    return _clientId;
  }

  public PortfolioMaster getPortfolioMaster() {
    if (_portfolioMaster == null) {
      _portfolioMaster = new RemotePortfolioMaster(_baseTarget.getURI());
    }
    return _portfolioMaster;
  }

  public PositionMaster getPositionMaster() {
    if (_positionMaster == null) {
      _positionMaster = new RemotePositionMaster(_baseTarget.getURI());
    }
    return _positionMaster;
  }

  public SecurityMaster getSecurityMaster() {
    if (_securityMaster == null) {
      _securityMaster = new RemoteSecurityMaster(_fudgeContext, _baseTarget.resolveBase(ClientResource.SECURITIES_PATH));
    }
    return _securityMaster;
  }

  public ManageableViewDefinitionRepository getViewDefinitionRepository() {
    if (_viewDefinitionRepository == null) {
      _viewDefinitionRepository = new RemoteManageableViewDefinitionRepository(_baseTarget.resolveBase(ClientResource.VIEW_DEFINITIONS_PATH).getURI());
    }
    return _viewDefinitionRepository;
  }

  public InterpolatedYieldCurveDefinitionMaster getInterpolatedYieldCurveDefinitionMaster() {
    if (_interpolatedYieldCurveDefinitionMaster == null) {
      _interpolatedYieldCurveDefinitionMaster = new RemoteInterpolatedYieldCurveDefinitionMaster(_fudgeContext, _baseTarget.resolveBase(ClientResource.INTERPOLATED_YIELD_CURVE_DEFINITIONS_PATH));
    }
    return _interpolatedYieldCurveDefinitionMaster;
  }

  /**
   * Creates a heartbeat sender. If nothing has happened for a timeout duration, that would result in messages being sent to the server,
   * the heartbeat signal should be sent as a keep-alive.
   * 
   * @return a runnable sender. Each invocation of {@link Runnable#run} will send a heartbeat signal
   */
  public Runnable createHeartbeatSender() {
    final RestClient client = RestClient.getInstance(_fudgeContext, null);
    final RestTarget target = _baseTarget.resolve(ClientResource.HEARTBEAT_PATH);
    return new Runnable() {
      @Override
      public void run() {
        client.post(target);
      }
    };
  }

  /**
   * A hack to allow the Excel side to get hold of a RemoteClient without it having to be aware of the URI. Eventually
   * we will need a UserMaster to host users and their clients, and the entry point for Excel will be a
   * RemoteUserMaster.
   *
   * @param fudgeContext  the Fudge context
   * @param usersUri  uri as far as /users
   * @param username  the username
   * @return  a {@link RemoteClient} instance for the new client
   */
  public static RemoteClient forNewClient(FudgeContext fudgeContext, RestTarget usersUri, String username) {
    return forClient(fudgeContext, usersUri, username, GUIDGenerator.generate().toString());
  }

  public static RemoteClient forClient(FudgeContext fudgeContext, RestTarget usersUri, String username, String clientId) {
    RestTarget uri = usersUri.resolveBase(username).resolveBase("clients").resolveBase(clientId);
    return new RemoteClient(clientId, fudgeContext, uri);
  }

}
<commit_msg>Extend the RemoteClient definition so it can be used for a single user, group of users, or to connect to the set of "shared" masters.<commit_after>/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.financial.user.rest;

import java.net.URI;

import org.fudgemsg.FudgeContext;

import com.opengamma.OpenGammaRuntimeException;
import com.opengamma.financial.analytics.ircurve.InterpolatedYieldCurveDefinitionMaster;
import com.opengamma.financial.analytics.ircurve.rest.RemoteInterpolatedYieldCurveDefinitionMaster;
import com.opengamma.financial.portfolio.rest.RemotePortfolioMaster;
import com.opengamma.financial.position.rest.RemotePositionMaster;
import com.opengamma.financial.security.rest.RemoteSecurityMaster;
import com.opengamma.financial.view.ManageableViewDefinitionRepository;
import com.opengamma.financial.view.rest.RemoteManageableViewDefinitionRepository;
import com.opengamma.master.portfolio.PortfolioMaster;
import com.opengamma.master.position.PositionMaster;
import com.opengamma.master.security.SecurityMaster;
import com.opengamma.transport.jaxrs.RestClient;
import com.opengamma.transport.jaxrs.RestTarget;
import com.opengamma.util.ArgumentChecker;
import com.opengamma.util.GUIDGenerator;

/**
 * Provides access to a remote representation of a "client". A client is defined as a set of coherent
 * and related masters. These might be the set of masters corresponding to the "shared" data used by
 * all users of an OpenGamma instance, or the masters containing data specific to a single user or
 * perhaps a group of users.
 */
public class RemoteClient {

  /**
   * Source of targets for the entities this client will connect to.
   */
  public abstract static class TargetProvider {

    protected <T> T notImplemented(final String what) {
      throw new OpenGammaRuntimeException("The '" + what + "' is not available for this client");
    }

    public URI getPortfolioMaster() {
      return notImplemented("portfolioMaster");
    }

    public URI getPositionMaster() {
      return notImplemented("positionMaster");
    }

    public RestTarget getSecurityMaster() {
      return notImplemented("securityMaster");
    }

    public URI getViewDefinitionRepository() {
      return notImplemented("viewDefinitionRepository");
    }

    public RestTarget getInterpolatedYieldCurveDefinitionMaster() {
      return notImplemented("interpolatedYieldCurveDefinitionMaster");
    }

    public RestTarget getHeartbeat() {
      return notImplemented("heartbeat");
    }

  }

  /**
   * Source of targets externally specified.
   */
  public static final class TargetProviderBean extends TargetProvider {

    private URI _portfolioMaster;
    private URI _positionMaster;
    private RestTarget _securityMaster;
    private URI _viewDefinitionRepository;
    private RestTarget _interpolatedYieldCurveDefinitionMaster;
    private RestTarget _heartbeat;

    @Override
    public URI getPortfolioMaster() {
      return (_portfolioMaster != null) ? _portfolioMaster : super.getPortfolioMaster();
    }

    public void setPortfolioMaster(final URI portfolioMaster) {
      _portfolioMaster = portfolioMaster;
    }

    @Override
    public URI getPositionMaster() {
      return (_positionMaster != null) ? _positionMaster : super.getPositionMaster();
    }

    public void setSecurityMaster(final RestTarget securityMaster) {
      _securityMaster = securityMaster;
    }

    @Override
    public RestTarget getSecurityMaster() {
      return (_securityMaster != null) ? _securityMaster : super.getSecurityMaster();
    }

    public void setViewDefinitionRepositoryUri(final URI viewDefinitionRepository) {
      _viewDefinitionRepository = viewDefinitionRepository;
    }

    @Override
    public URI getViewDefinitionRepository() {
      return (_viewDefinitionRepository != null) ? _viewDefinitionRepository : super.getViewDefinitionRepository();
    }

    public void setInterpolatedYieldCurveDefinitionMaster(final RestTarget interpolatedYieldCurveDefinitionMaster) {
      _interpolatedYieldCurveDefinitionMaster = interpolatedYieldCurveDefinitionMaster;
    }

    @Override
    public RestTarget getInterpolatedYieldCurveDefinitionMaster() {
      return (_interpolatedYieldCurveDefinitionMaster != null) ? _interpolatedYieldCurveDefinitionMaster : super.getInterpolatedYieldCurveDefinitionMaster();
    }

    public void setHeartbeat(final RestTarget heartbeat) {
      _heartbeat = heartbeat;
    }

    public RestTarget getHeartbeat() {
      return (_heartbeat != null) ? _heartbeat : super.getHeartbeat();
    }

  }

  /**
   * Source of targets built from a single "base" URI.
   */
  public static final class BaseUriTargetProvider extends TargetProvider {

    private final RestTarget _baseTarget;

    public BaseUriTargetProvider(final RestTarget baseTarget) {
      ArgumentChecker.notNull(baseTarget, "baseTarget");
      _baseTarget = baseTarget;
    }

    @Override
    public URI getPortfolioMaster() {
      return _baseTarget.getURI();
    }

    @Override
    public URI getPositionMaster() {
      return _baseTarget.getURI();
    }

    @Override
    public RestTarget getSecurityMaster() {
      return _baseTarget.resolveBase(ClientResource.SECURITIES_PATH);
    }

    @Override
    public URI getViewDefinitionRepository() {
      return _baseTarget.resolveBase(ClientResource.VIEW_DEFINITIONS_PATH).getURI();
    }

    @Override
    public RestTarget getInterpolatedYieldCurveDefinitionMaster() {
      return _baseTarget.resolveBase(ClientResource.INTERPOLATED_YIELD_CURVE_DEFINITIONS_PATH);
    }

    @Override
    public RestTarget getHeartbeat() {
      return _baseTarget.resolve(ClientResource.HEARTBEAT_PATH);
    }

  }

  private final String _clientId;
  private final FudgeContext _fudgeContext;
  private final TargetProvider _targetProvider;
  private PortfolioMaster _portfolioMaster;
  private PositionMaster _positionMaster;
  private SecurityMaster _securityMaster;
  private ManageableViewDefinitionRepository _viewDefinitionRepository;
  private InterpolatedYieldCurveDefinitionMaster _interpolatedYieldCurveDefinitionMaster;

  public RemoteClient(String clientId, FudgeContext fudgeContext, TargetProvider uriProvider) {
    _clientId = clientId;
    _fudgeContext = fudgeContext;
    _targetProvider = uriProvider;
  }

  public String getClientId() {
    return _clientId;
  }

  public PortfolioMaster getPortfolioMaster() {
    if (_portfolioMaster == null) {
      _portfolioMaster = new RemotePortfolioMaster(_targetProvider.getPortfolioMaster());
    }
    return _portfolioMaster;
  }

  public PositionMaster getPositionMaster() {
    if (_positionMaster == null) {
      _positionMaster = new RemotePositionMaster(_targetProvider.getPositionMaster());
    }
    return _positionMaster;
  }

  public SecurityMaster getSecurityMaster() {
    if (_securityMaster == null) {
      _securityMaster = new RemoteSecurityMaster(_fudgeContext, _targetProvider.getSecurityMaster());
    }
    return _securityMaster;
  }

  public ManageableViewDefinitionRepository getViewDefinitionRepository() {
    if (_viewDefinitionRepository == null) {
      _viewDefinitionRepository = new RemoteManageableViewDefinitionRepository(_targetProvider.getViewDefinitionRepository());
    }
    return _viewDefinitionRepository;
  }

  public InterpolatedYieldCurveDefinitionMaster getInterpolatedYieldCurveDefinitionMaster() {
    if (_interpolatedYieldCurveDefinitionMaster == null) {
      _interpolatedYieldCurveDefinitionMaster = new RemoteInterpolatedYieldCurveDefinitionMaster(_fudgeContext, _targetProvider.getInterpolatedYieldCurveDefinitionMaster());
    }
    return _interpolatedYieldCurveDefinitionMaster;
  }

  /**
   * Creates a heartbeat sender. If nothing has happened for a timeout duration, that would result in messages being sent to the server,
   * the heartbeat signal should be sent as a keep-alive.
   * 
   * @return a runnable sender. Each invocation of {@link Runnable#run} will send a heartbeat signal
   */
  public Runnable createHeartbeatSender() {
    final RestClient client = RestClient.getInstance(_fudgeContext, null);
    final RestTarget target = _targetProvider.getHeartbeat();
    return new Runnable() {
      @Override
      public void run() {
        client.post(target);
      }
    };
  }

  /**
   * A hack to allow the Excel side to get hold of a RemoteClient without it having to be aware of the URI. Eventually
   * we will need a UserMaster to host users and their clients, and the entry point for Excel will be a
   * RemoteUserMaster.
   *
   * @param fudgeContext  the Fudge context
   * @param usersUri  uri as far as /users
   * @param username  the username
   * @return  a {@link RemoteClient} instance for the new client
   */
  public static RemoteClient forNewClient(FudgeContext fudgeContext, RestTarget usersUri, String username) {
    return forClient(fudgeContext, usersUri, username, GUIDGenerator.generate().toString());
  }

  public static RemoteClient forClient(FudgeContext fudgeContext, RestTarget usersUri, String username, String clientId) {
    RestTarget uri = usersUri.resolveBase(username).resolveBase("clients").resolveBase(clientId);
    return new RemoteClient(clientId, fudgeContext, new BaseUriTargetProvider(uri));
  }

}

{'fraction_non_alphanumeric': 0.07097047008245824, 'fraction_numerical': 0.0005638170413700754, 'mean_word_length': 4.577830188679245, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7394493', 'n_tokens_mistral': 3814, 'n_tokens_neox': 3737, 'n_words': 983}