/**
   * Fetches the preferred TLS (TCP) port for secure communications in the user preferences or
   * search is default value set in settings or fallback on a default value.
   *
   * @return the preferred network port for secure communications.
   */
  private int getPreferredSecurePort() {
    int preferredPort =
        SipActivator.getConfigurationService().getInt(PREFERRED_SECURE_PORT_PROPERTY_NAME, -1);

    if (preferredPort <= 1) {
      // check for default value
      preferredPort =
          SipActivator.getResources().getSettingsInt(PREFERRED_SECURE_PORT_PROPERTY_NAME);
    }

    if (preferredPort <= 1) return ListeningPoint.PORT_5061;
    else return preferredPort;
  }
  /**
   * Shows an error and a short description.
   *
   * @param ex the exception
   */
  static void showError(Throwable ex, String title, String message) {
    try {
      if (title == null) title = "Error in SIP contactlist storage";

      if (message == null)
        message = title + "\n" + ex.getClass().getName() + ": " + ex.getLocalizedMessage();

      if (SipActivator.getUIService() != null)
        SipActivator.getUIService()
            .getPopupDialog()
            .showMessagePopupDialog(message, title, PopupDialog.ERROR_MESSAGE);
    } catch (Throwable t) {
      logger.error("Error for error dialog", t);
    }
  }
  /** Handles new incoming object. */
  private void handle(JSONObject incomingObject) {
    if (!incomingObject.containsKey("class")) return;

    try {
      String classField = (String) incomingObject.get("class");

      if (classField.equals("loginko")) {
        showError(null, null, "Unauthorized. Cannot login: "******"errorstring"));

        logger.error("Error login: "******"errorstring"));

        destroy();

        return;
      } else if (classField.equals("login_id_ok")) {
        SipAccountIDImpl accountID = (SipAccountIDImpl) sipProvider.getAccountID();

        boolean useSipCredentials = accountID.isClistOptionUseSipCredentials();

        String password;
        if (useSipCredentials) {
          password = SipActivator.getProtocolProviderFactory().loadPassword(accountID);
        } else {
          password = accountID.getClistOptionPassword();
        }

        if (!authorize((String) incomingObject.get("sessionid"), password))
          logger.error("Error login authorization!");

        return;
      } else if (classField.equals("login_pass_ok")) {
        if (!sendCapas((JSONArray) incomingObject.get("capalist")))
          logger.error("Error send capas!");

        return;
      } else if (classField.equals("login_capas_ok")) {
        if (!sendFeatures(
            (String) incomingObject.get("astid"), (String) incomingObject.get("xivo_userid")))
          logger.error("Problem send features get!");

        return;
      } else if (classField.equals("features")) {
        if (!getPhoneList()) logger.error("Problem send get phones!");

        return;
      } else if (classField.equals("phones")) {
        phonesRecieved(incomingObject);
        return;
      } else if (classField.equals("disconn")) {
        destroy();
        return;
      } else {
        if (logger.isTraceEnabled()) logger.trace("unhandled classField: " + incomingObject);
        return;
      }
    } catch (Throwable t) {
      logger.error("Error handling incoming object", t);
    }
  }
  /**
   * Constructor for this class. Creates the JAIN-SIP stack.
   *
   * @throws OperationFailedException if creating the stack fails.
   */
  SipStackSharing() throws OperationFailedException {
    // init of the stack
    try {
      SipFactory sipFactory = SipFactory.getInstance();
      sipFactory.setPathName("org.jitsi.gov.nist");

      Properties sipStackProperties = new SipStackProperties();

      // Create SipStack object
      this.stack = sipFactory.createSipStack(sipStackProperties);
      if (logger.isTraceEnabled()) logger.trace("Created stack: " + this.stack);

      // set our custom address resolver managing SRV records
      AddressResolverImpl addressResolver = new AddressResolverImpl();
      ((SIPTransactionStack) this.stack).setAddressResolver(addressResolver);

      SipActivator.getNetworkAddressManagerService().addNetworkConfigurationChangeListener(this);
    } catch (Exception ex) {
      logger.fatal("Failed to get SIP Factory.", ex);
      throw new OperationFailedException(
          "Failed to get SIP Factory", OperationFailedException.INTERNAL_ERROR, ex);
    }
  }
 /**
  * Fetches the number of times to retry when the binding of a JAIN-SIP <tt>ListeningPoint</tt>
  * fails. Looks in the user preferences or fallbacks on a default value.
  *
  * @return the number of times to retry a failed bind.
  */
 private int getBindRetriesValue() {
   return SipActivator.getConfigurationService()
       .getInt(
           ProtocolProviderService.BIND_RETRIES_PROPERTY_NAME,
           ProtocolProviderService.BIND_RETRIES_DEFAULT_VALUE);
 }