/**
   * Get the <tt>AccountManager</tt> of the protocol.
   *
   * @return <tt>AccountManager</tt> of the protocol
   */
  private AccountManager getAccountManager() {
    BundleContext bundleContext = getBundleContext();
    ServiceReference serviceReference =
        bundleContext.getServiceReference(AccountManager.class.getName());

    return (AccountManager) bundleContext.getService(serviceReference);
  }
  /**
   * Returns the prefix for all persistently stored properties of the account with the specified id.
   *
   * @param bundleContext a currently valid bundle context.
   * @param accountID the AccountID of the account whose properties we're looking for.
   * @param sourcePackageName a String containing the package name of the concrete factory class
   *     that extends us.
   * @return a String indicating the ConfigurationService property name prefix under which all
   *     account properties are stored or null if no account corresponding to the specified id was
   *     found.
   */
  public static String findAccountPrefix(
      BundleContext bundleContext, AccountID accountID, String sourcePackageName) {
    ServiceReference confReference =
        bundleContext.getServiceReference(ConfigurationService.class.getName());
    ConfigurationService configurationService =
        (ConfigurationService) bundleContext.getService(confReference);

    // first retrieve all accounts that we've registered
    List<String> storedAccounts =
        configurationService.getPropertyNamesByPrefix(sourcePackageName, true);

    // find an account with the corresponding id.
    for (String accountRootPropertyName : storedAccounts) {
      // unregister the account in the configuration service.
      // all the properties must have been registered in the following
      // hierarchy:
      // net.java.sip.communicator.impl.protocol.PROTO_NAME.ACC_ID.PROP_NAME
      String accountUID =
          configurationService.getString(
              accountRootPropertyName // node id
                  + "."
                  + ACCOUNT_UID); // propname

      if (accountID.getAccountUniqueID().equals(accountUID)) {
        return accountRootPropertyName;
      }
    }
    return null;
  }
  /**
   * Removes the specified account from the list of accounts that this provider factory is handling.
   * If the specified accountID is unknown to the ProtocolProviderFactory, the call has no effect
   * and false is returned. This method is persistent in nature and once called the account
   * corresponding to the specified ID will not be loaded during future runs of the project.
   *
   * @param accountID the ID of the account to remove.
   * @return true if an account with the specified ID existed and was removed and false otherwise.
   */
  public boolean uninstallAccount(AccountID accountID) {
    // Unregister the protocol provider.
    ServiceReference serRef = getProviderForAccount(accountID);

    boolean wasAccountExisting = false;

    // If the protocol provider service is registered, first unregister the
    // service.
    if (serRef != null) {
      BundleContext bundleContext = getBundleContext();
      ProtocolProviderService protocolProvider =
          (ProtocolProviderService) bundleContext.getService(serRef);

      try {
        protocolProvider.unregister();
      } catch (OperationFailedException ex) {
        logger.error(
            "Failed to unregister protocol provider for account: "
                + accountID
                + " caused by: "
                + ex);
      }
    }

    ServiceRegistration registration;

    synchronized (registeredAccounts) {
      registration = registeredAccounts.remove(accountID);
    }

    // first remove the stored account so when PP is unregistered we can
    // distinguish between deleted or just disabled account
    wasAccountExisting = removeStoredAccount(accountID);

    if (registration != null) {
      // Kill the service.
      registration.unregister();
    }

    return wasAccountExisting;
  }
  /**
   * Unloads the account corresponding to the given <tt>accountID</tt>. Unregisters the
   * corresponding protocol provider, but keeps the account in contrast to the uninstallAccount
   * method.
   *
   * @param accountID the account identifier
   * @return true if an account with the specified ID existed and was unloaded and false otherwise.
   */
  public boolean unloadAccount(AccountID accountID) {
    // Unregister the protocol provider.
    ServiceReference serRef = getProviderForAccount(accountID);

    if (serRef == null) {
      return false;
    }

    BundleContext bundleContext = getBundleContext();
    ProtocolProviderService protocolProvider =
        (ProtocolProviderService) bundleContext.getService(serRef);

    try {
      protocolProvider.unregister();
    } catch (OperationFailedException ex) {
      logger.error(
          "Failed to unregister protocol provider for account : "
              + accountID
              + " caused by: "
              + ex);
    }

    ServiceRegistration registration;

    synchronized (registeredAccounts) {
      registration = registeredAccounts.remove(accountID);
    }
    if (registration == null) {
      return false;
    }

    // Kill the service.
    registration.unregister();

    return true;
  }
  /**
   * Creates a protocol provider for the given <tt>accountID</tt> and registers it in the bundle
   * context. This method has a persistent effect. Once created the resulting account will remain
   * installed until removed through the uninstallAccount method.
   *
   * @param accountID the account identifier
   * @return <tt>true</tt> if the account with the given <tt>accountID</tt> is successfully loaded,
   *     otherwise returns <tt>false</tt>
   */
  public boolean loadAccount(AccountID accountID) {
    // Need to obtain the original user id property, instead of calling
    // accountID.getUserID(), because this method could return a modified
    // version of the user id property.
    String userID = accountID.getAccountPropertyString(ProtocolProviderFactory.USER_ID);

    ProtocolProviderService service = createService(userID, accountID);

    Dictionary<String, String> properties = new Hashtable<String, String>();
    properties.put(PROTOCOL, protocolName);
    properties.put(USER_ID, userID);

    ServiceRegistration serviceRegistration =
        bundleContext.registerService(ProtocolProviderService.class.getName(), service, properties);

    if (serviceRegistration == null) return false;
    else {
      synchronized (registeredAccounts) {
        registeredAccounts.put(accountID, serviceRegistration);
      }
      return true;
    }
  }