/** PreCreate the Master Channel for this GridTalk. */
  private void preCreateMasterChannel() throws Throwable {
    ConnectionSetupProperties props = DelegateHelper.getProperties();
    String refId = _ctx.getGridNodeID().toString();
    String namePrefix = "GT." + refId + ".";

    // NSL20051115 Use return values from the preCreateXXX() methods
    CommInfo _myCommInfo = preCreateCommInfo(namePrefix, refId, props);
    SecurityInfo _mySecInfo = preCreateSecurityInfo(namePrefix, refId, props);
    PackagingInfo _myPkgInfo = preCreatePackagingInfo(namePrefix, refId, props);

    String channelName = namePrefix + props.getMasterChannelName();

    //  create channelinfo with refId, commInfo, securityinfo, packagingInfo
    _myChannel = DelegateHelper.retrieveChannel(refId, channelName);
    if (_myChannel == null) {
      _myChannel =
          DelegateHelper.createChannelInfo(refId, channelName, props.getMasterChannelDesc());
      _myChannel.setTptCommInfo(_myCommInfo);
      _myChannel.setPackagingProfile(_myPkgInfo);
      _myChannel.setSecurityProfile(_mySecInfo);

      _myChannel = DelegateHelper.createChannelInfo(_myChannel); // NSL20051115
    } else {
      // update the channel info with new profiles
      DelegateHelper.updateChannelInfo(
          _myChannel, props.getMasterChannelDesc(), _mySecInfo, _myPkgInfo, _myCommInfo);
      DelegateHelper.updateChannelInfo(_myChannel);
    }
  }
  /**
   * Retrieve CommInfo for the Master channel. Create one if does not exist. If exist, update the
   * properties.
   *
   * @param namePrefix Prefix for the Name of the CommInfo.
   * @param refId ReferenceId for the CommInfo
   * @param props Connection setup properties
   * @param topic The topic (Destination)
   * @param user The user
   * @param password The password
   * @return The created CommInfo
   */
  private CommInfo preCreateCommInfo(
      String namePrefix, String refId, ConnectionSetupProperties props) throws Throwable {
    String name = namePrefix + props.getMasterChannelCommName();

    CommInfo _myCommInfo = DelegateHelper.retrieveCommInfo(refId, name);
    if (_myCommInfo == null) {
      _myCommInfo =
          DelegateHelper.createCommInfo(
              DelegateHelper.createCommInfo(
                  refId,
                  "",
                  props.getMasterChannelCommPort().intValue(),
                  "",
                  "",
                  "",
                  name,
                  props.getMasterChannelCommDesc(),
                  CommInfoFormatter.CURRENT_COMMINFO_VERSION));
    } else {
      // update the commInfo with no settings
      DelegateHelper.updateCommInfo(
          _myCommInfo,
          "",
          props.getMasterChannelCommPort().intValue(),
          "",
          "",
          "",
          props.getMasterChannelCommDesc(),
          CommInfoFormatter.CURRENT_COMMINFO_VERSION);
      DelegateHelper.updateCommInfo(_myCommInfo);
    }
    return _myCommInfo;
  }
  /**
   * Pre-create the PackagingInfo for this GridTalk. Require this when receive results from GmPrime.
   *
   * @param namePrefix Prefix for the Name of the PackagingInfo.
   * @param refId ReferenceID of the PackagingInfo.
   * @param props Properties for ConnectionSetup.
   * @return The created PackagingInfo
   */
  private PackagingInfo preCreatePackagingInfo(
      String namePrefix, String refId, ConnectionSetupProperties props) throws Throwable {
    String name = namePrefix + props.getMasterChannelPkgName();

    PackagingInfo _myPkgInfo = DelegateHelper.retrievePackagingInfo(refId, name);
    if (_myPkgInfo == null) {
      _myPkgInfo =
          DelegateHelper.createPackagingInfo(
              DelegateHelper.createPackagingInfo(refId, name, props.getMasterChannelPkgDesc()));
    } else {
      // update, make sure the info are correct
      DelegateHelper.updatePackagingInfo(_myPkgInfo, props.getMasterChannelPkgDesc());
      DelegateHelper.updatePackagingInfo(_myPkgInfo);
    }
    return _myPkgInfo;
  }
  /**
   * Pre-create the SecurityInfo for this GridTalk. Need to decrypt the returned results from
   * GmPrime.
   *
   * @param namePrefix Prefix for the Name of the SecurityInfo.
   * @param refId ReferenceId for the SecurityInfo
   * @param props Properties for Connection Setup.
   * @return The created SecurityInfo
   */
  private SecurityInfo preCreateSecurityInfo(
      String namePrefix, String refId, ConnectionSetupProperties props) throws Throwable {
    String name = namePrefix + props.getMasterChannelSecName();

    SecurityInfo _mySecInfo = DelegateHelper.retrieveSecurityInfo(refId, name);
    Long encryptCert = DelegateHelper.getMasterCertifcate(_ctx.getGridNodeID());
    if (_mySecInfo == null) {
      //  create securityinfo with refId, encryptCert
      _mySecInfo =
          DelegateHelper.createSecurityInfo(
              DelegateHelper.createSecurityInfo(
                  refId, encryptCert, null, name, props.getMasterChannelSecDesc()));
    } else {
      // update, make sure the info are correct
      DelegateHelper.updateSecurityInfo(
          _mySecInfo, encryptCert, null, props.getMasterChannelSecDesc());
      DelegateHelper.updateSecurityInfo(_mySecInfo);
    }
    return _mySecInfo;
  }
  /**
   * Create a Channel for sending messages to a GridMaster.
   *
   * @param refId the ReferenceId for the Channel.
   * @param topic The topic for the JMSCommInfo.
   * @param routerIp The Host for the JMSCommInfo.
   * @param signCert The UID of the Certificate to use to sign messages sending to the GridMaster.
   */
  private void createGridMasterChannel(String refId, String topic, String routerIp, Long signCert)
      throws Throwable {
    ConnectionSetupProperties props = DelegateHelper.getProperties();

    //  create comminfo with refId, topic, routerIp
    CommInfo commInfo =
        DelegateHelper.createCommInfo(
            DelegateHelper.createCommInfo(
                refId,
                routerIp,
                props.getGmChannelCommPort().intValue(),
                topic,
                _ctx.getGridNodeID().toString(),
                props.getGmChannelCommPassword(),
                props.getGmChannelCommName(),
                props.getGmChannelCommDesc()));

    //  create packaginginfo with refId
    PackagingInfo pkgInfo =
        (PackagingInfo)
            DelegateHelper.createPackagingInfo(
                DelegateHelper.createPackagingInfo(
                    refId, props.getGmChannelPkgName(), props.getGmChannelPkgDesc()));

    //  create securityinfo with refId, signCert, encryptCert=gmcert
    SecurityInfo secInfo =
        (SecurityInfo)
            DelegateHelper.createSecurityInfo(
                DelegateHelper.createSecurityInfo(
                    refId,
                    _ctx.getEncryptionCert(),
                    signCert,
                    props.getGmChannelSecName(),
                    props.getGmChannelSecDesc()));

    //  create channelinfo with refId, commInfo, securityinfo, packagingInfo
    ChannelInfo channel =
        DelegateHelper.createChannelInfo(refId, props.getGmChannelName(), props.getGmChannelDesc());

    channel.setIsPartner(false);
    channel.setIsMaster(false);
    channel.setIsRelay(true);

    channel.setTptCommInfo(commInfo);
    channel.setPackagingProfile(pkgInfo);
    channel.setSecurityProfile(secInfo);
    channel = (ChannelInfo) DelegateHelper.createChannelInfo(channel);
  }