/**
   * Creates an account for the given user and password.
   *
   * @param providerFactory the ProtocolProviderFactory which will create the account
   * @param user the user identifier
   * @return the <tt>ProtocolProviderService</tt> for the new account.
   */
  public ProtocolProviderService installAccount(
      ProtocolProviderFactory providerFactory, String user) throws OperationFailedException {
    Hashtable<String, String> accountProperties = new Hashtable<String, String>();

    accountProperties.put(
        ProtocolProviderFactory.ACCOUNT_ICON_PATH,
        "resources/images/protocol/gibberish/gibberish32x32.png");

    if (registration.isRememberPassword()) {
      accountProperties.put(ProtocolProviderFactory.PASSWORD, registration.getPassword());
    }

    if (isModification()) {
      providerFactory.uninstallAccount(protocolProvider.getAccountID());
      this.protocolProvider = null;
      setModification(false);
    }

    try {
      AccountID accountID = providerFactory.installAccount(user, accountProperties);

      ServiceReference serRef = providerFactory.getProviderForAccount(accountID);

      protocolProvider =
          (ProtocolProviderService) GibberishAccRegWizzActivator.bundleContext.getService(serRef);
    } catch (IllegalStateException exc) {
      logger.warn(exc.getMessage());

      throw new OperationFailedException(
          "Account already exists.", OperationFailedException.IDENTIFICATION_CONFLICT);
    } catch (Exception exc) {
      logger.warn(exc.getMessage());

      throw new OperationFailedException(
          "Failed to add account", OperationFailedException.GENERAL_ERROR);
    }

    return protocolProvider;
  }
  /**
   * Creates an account for the given Account ID, Identity File and Known Hosts File
   *
   * @param providerFactory the ProtocolProviderFactory which will create the account
   * @param user the user identifier
   * @return the <tt>ProtocolProviderService</tt> for the new account.
   */
  public ProtocolProviderService installAccount(
      ProtocolProviderFactory providerFactory, String user) throws OperationFailedException {
    Hashtable<String, String> accountProperties = new Hashtable<String, String>();

    accountProperties.put(
        ProtocolProviderFactory.ACCOUNT_ICON_PATH, "resources/images/protocol/ssh/ssh32x32.png");

    accountProperties.put(
        ProtocolProviderFactory.NO_PASSWORD_REQUIRED, new Boolean(true).toString());

    accountProperties.put(
        ProtocolProviderFactorySSHImpl.IDENTITY_FILE, registration.getIdentityFile());

    accountProperties.put(
        ProtocolProviderFactorySSHImpl.KNOWN_HOSTS_FILE,
        String.valueOf(registration.getKnownHostsFile()));

    try {
      AccountID accountID = providerFactory.installAccount(user, accountProperties);

      ServiceReference serRef = providerFactory.getProviderForAccount(accountID);

      protocolProvider =
          (ProtocolProviderService) SSHAccRegWizzActivator.bundleContext.getService(serRef);
    } catch (IllegalStateException exc) {
      logger.warn(exc.getMessage());

      throw new OperationFailedException(
          "Account already exists.", OperationFailedException.IDENTIFICATION_CONFLICT);
    } catch (Exception exc) {
      logger.warn(exc.getMessage());

      throw new OperationFailedException(
          "Failed to add account", OperationFailedException.GENERAL_ERROR);
    }

    return protocolProvider;
  }
  /** Installs an account and verifies whether the installation has gone well. */
  public void testInstallAccount() {
    // first obtain a reference to the provider factory
    ServiceReference[] serRefs = null;
    String osgiFilter = "(" + ProtocolProviderFactory.PROTOCOL + "=" + ProtocolNames.JABBER + ")";
    try {
      serRefs =
          JabberSlickFixture.bc.getServiceReferences(
              ProtocolProviderFactory.class.getName(), osgiFilter);
    } catch (InvalidSyntaxException ex) {
      // this really shouldhn't occur as the filter expression is static.
      fail(osgiFilter + " is not a valid osgi filter");
    }

    assertTrue(
        "Failed to find a provider factory service for protocol Jabber",
        serRefs != null && serRefs.length > 0);

    // Enable always trust mode for testing tls jabber connections
    ServiceReference confReference =
        JabberSlickFixture.bc.getServiceReference(ConfigurationService.class.getName());
    ConfigurationService configurationService =
        (ConfigurationService) JabberSlickFixture.bc.getService(confReference);

    configurationService.setProperty(CertificateService.PNAME_ALWAYS_TRUST, Boolean.TRUE);

    // Keep the reference for later usage.
    ProtocolProviderFactory jabberProviderFactory =
        (ProtocolProviderFactory) JabberSlickFixture.bc.getService(serRefs[0]);

    // make sure the account is empty
    assertTrue(
        "There was an account registered with the account mananger " + "before we've installed any",
        jabberProviderFactory.getRegisteredAccounts().size() == 0);

    // Prepare the properties of the first jabber account.

    Hashtable<String, String> jabberAccount1Properties =
        getAccountProperties(JabberProtocolProviderServiceLick.ACCOUNT_1_PREFIX);
    Hashtable<String, String> jabberAccount2Properties =
        getAccountProperties(JabberProtocolProviderServiceLick.ACCOUNT_2_PREFIX);
    Hashtable<String, String> jabberAccount3Properties =
        getAccountProperties(JabberProtocolProviderServiceLick.ACCOUNT_3_PREFIX);

    // try to install an account with a null account id
    try {
      jabberProviderFactory.installAccount(null, jabberAccount1Properties);
      fail(
          "installing an account with a null account id must result "
              + "in a NullPointerException");
    } catch (NullPointerException exc) {
      // that's what had to happen
    }

    // now really install the accounts
    jabberProviderFactory.installAccount(
        jabberAccount1Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount1Properties);
    jabberProviderFactory.installAccount(
        jabberAccount2Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount2Properties);
    jabberProviderFactory.installAccount(
        jabberAccount3Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount3Properties);

    // try to install one of the accounts one more time and verify that an
    // excepion is thrown.
    try {
      jabberProviderFactory.installAccount(
          jabberAccount1Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount1Properties);

      fail(
          "An IllegalStateException must be thrown when trying to "
              + "install a duplicate account");

    } catch (IllegalStateException exc) {
      // that's what supposed to happen.
    }

    // Verify that the provider factory is aware of our installation
    assertTrue(
        "The newly installed account was not in the acc man's " + "registered accounts!",
        jabberProviderFactory.getRegisteredAccounts().size() == 3);

    // Verify protocol providers corresponding to the new account have
    // been properly registered with the osgi framework.

    osgiFilter =
        "(&("
            + ProtocolProviderFactory.PROTOCOL
            + "="
            + ProtocolNames.JABBER
            + ")"
            + "("
            + ProtocolProviderFactory.USER_ID
            + "="
            + jabberAccount1Properties.get(ProtocolProviderFactory.USER_ID)
            + "))";

    try {
      serRefs =
          JabberSlickFixture.bc.getServiceReferences(
              ProtocolProviderService.class.getName(), osgiFilter);
    } catch (InvalidSyntaxException ex) {
      // this really shouldhn't occur as the filter expression is static.
      fail(osgiFilter + "is not a valid osgi filter");
    }

    assertTrue(
        "An protocol provider was apparently not installed as " + "requested.",
        serRefs != null && serRefs.length > 0);

    Object jabberProtocolProvider = JabberSlickFixture.bc.getService(serRefs[0]);

    assertTrue(
        "The installed protocol provider does not implement " + "the protocol provider service.",
        jabberProtocolProvider instanceof ProtocolProviderService);
  }
  /**
   * Creates an account for the given user and password.
   *
   * @param providerFactory the ProtocolProviderFactory which will create the account
   * @param userName the user identifier
   * @param passwd the password
   * @return the <tt>ProtocolProviderService</tt> for the new account.
   * @throws OperationFailedException if the operation didn't succeed
   */
  protected ProtocolProviderService installAccount(
      ProtocolProviderFactory providerFactory, String userName, String passwd)
      throws OperationFailedException {
    if (logger.isTraceEnabled()) {
      logger.trace("Preparing to install account for user " + userName);
    }
    Hashtable<String, String> accountProperties = new Hashtable<String, String>();

    String protocolIconPath = getProtocolIconPath();

    String accountIconPath = getAccountIconPath();

    registration.storeProperties(
        userName, passwd, protocolIconPath, accountIconPath, accountProperties);

    accountProperties.put(
        ProtocolProviderFactory.IS_PREFERRED_PROTOCOL, Boolean.toString(isPreferredProtocol()));
    accountProperties.put(ProtocolProviderFactory.PROTOCOL, getProtocol());

    if (isModification()) {
      providerFactory.modifyAccount(protocolProvider, accountProperties);

      setModification(false);

      return protocolProvider;
    }

    try {
      if (logger.isTraceEnabled()) {
        logger.trace(
            "Will install account for user "
                + userName
                + " with the following properties."
                + accountProperties);
      }

      AccountID accountID = providerFactory.installAccount(userName, accountProperties);

      ServiceReference serRef = providerFactory.getProviderForAccount(accountID);

      protocolProvider =
          (ProtocolProviderService) JabberAccRegWizzActivator.bundleContext.getService(serRef);
    } catch (IllegalArgumentException exc) {
      logger.warn(exc.getMessage());

      throw new OperationFailedException(
          "Username, password or server is null.", OperationFailedException.ILLEGAL_ARGUMENT);
    } catch (IllegalStateException exc) {
      logger.warn(exc.getMessage());

      throw new OperationFailedException(
          "Account already exists.", OperationFailedException.IDENTIFICATION_CONFLICT);
    } catch (Throwable exc) {
      logger.warn(exc.getMessage());

      throw new OperationFailedException(
          "Failed to add account.", OperationFailedException.GENERAL_ERROR);
    }

    return protocolProvider;
  }