accountPropertyNames
= configurationService.getPropertyNamesByPrefix(
accountRootPropertyName, false);
//set all account properties to null in order to remove them.
for (String propName : accountPropertyNames)
configurationService.setProperty(propName, null);
//and now remove the parent too.
configurationService.setProperty(accountRootPropertyName, null);
return true;
}
}
return false;
}
/**
* Removes all accounts which have been persistently stored.
*
* @see #removeStoredAccount(ProtocolProviderFactory, AccountID)
*/
public void removeStoredAccounts()
{
synchronized (loadStoredAccountsQueue)
{
/*
* Wait for the Thread which loads the stored account to complete so
* that we can be sure later on that it will not load a stored
* account while we are deleting it or another one for that matter.
*/
boolean interrupted = false;
while (loadStoredAccountsThread != null)
try
{
loadStoredAccountsQueue.wait(LOAD_STORED_ACCOUNTS_TIMEOUT);
}
catch (InterruptedException ie)
{
interrupted = true;
}
if (interrupted)
Thread.currentThread().interrupt();
synchronized (this.storedAccounts)
{
AccountID[] storedAccounts
= this.storedAccounts.toArray(
new AccountID[this.storedAccounts.size()]);
for (AccountID storedAccount : storedAccounts)
{
ProtocolProviderFactory ppf
= ProtocolProviderActivator.getProtocolProviderFactory(
storedAccount.getProtocolName());
if (ppf != null)
ppf.uninstallAccount(storedAccount);
}
}
}
}
/**
* Returns an Iterator over a list of all stored
* AccountIDs. The list of stored accounts include all registered
* accounts and all disabled accounts. In other words in this list we could
* find accounts that aren't loaded.
*
* In order to check if an account is already loaded please use the
* #isAccountLoaded(AccountID accountID) method. To load an account use the
* #loadAccount(AccountID accountID) method.
*
* @return an Iterator over a list of all stored
* AccountIDs
*/
public Collection getStoredAccounts()
{
synchronized (storedAccounts)
{
return new Vector(storedAccounts);
}
}
/**
* Loads the account corresponding to the given AccountID. An
* account is loaded when its ProtocolProviderService is registered
* in the bundle context. This method is meant to load the account through
* the corresponding ProtocolProviderFactory.
*
* @param accountID the identifier of the account to load
* @throws OperationFailedException if anything goes wrong while loading the
* account corresponding to the specified accountID
*/
public void loadAccount(AccountID accountID)
throws OperationFailedException
{
// If the account with the given id is already loaded we have nothing
// to do here.
if (isAccountLoaded(accountID))
return;
ProtocolProviderFactory providerFactory
= ProtocolProviderActivator.getProtocolProviderFactory(
accountID.getProtocolName());
if(providerFactory.loadAccount(accountID))
{
accountID.putAccountProperty(
ProtocolProviderFactory.IS_ACCOUNT_DISABLED,
String.valueOf(false));
// Finally store the modified properties.
storeAccount(providerFactory, accountID);
}
}
/**
* Unloads the account corresponding to the given AccountID. An
* account is unloaded when its ProtocolProviderService is
* unregistered in the bundle context. This method is meant to unload the
* account through the corresponding ProtocolProviderFactory.
*
* @param accountID the identifier of the account to load
* @throws OperationFailedException if anything goes wrong while unloading
* the account corresponding to the specified accountID
*/
public void unloadAccount(AccountID accountID)
throws OperationFailedException
{
// If the account with the given id is already unloaded we have nothing
// to do here.
if (!isAccountLoaded(accountID))
return;
ProtocolProviderFactory providerFactory
= ProtocolProviderActivator.getProtocolProviderFactory(
accountID.getProtocolName());
// Obtain the protocol provider.
ServiceReference serRef
= providerFactory.getProviderForAccount(accountID);
// If there's no such provider we have nothing to do here.
if (serRef == null)
return;
ProtocolProviderService protocolProvider
= bundleContext.getService(serRef);
// Set the account icon path for unloaded accounts.
String iconPathProperty = accountID.getAccountPropertyString(
ProtocolProviderFactory.ACCOUNT_ICON_PATH);
if (iconPathProperty == null)
{
accountID.putAccountProperty(
ProtocolProviderFactory.ACCOUNT_ICON_PATH,
protocolProvider.getProtocolIcon()
.getIconPath(ProtocolIcon.ICON_SIZE_32x32));
}
accountID.putAccountProperty(
ProtocolProviderFactory.IS_ACCOUNT_DISABLED,
String.valueOf(true));
if (!providerFactory.unloadAccount(accountID))
{
accountID.putAccountProperty(
ProtocolProviderFactory.IS_ACCOUNT_DISABLED,
String.valueOf(false));
}
// Finally store the modified properties.
storeAccount(providerFactory, accountID);
}
/**
* Checks if the account corresponding to the given accountID is
* loaded. An account is loaded if its ProtocolProviderService is
* registered in the bundle context. By default all accounts are loaded.
* However the user could manually unload an account, which would be
* unregistered from the bundle context, but would remain in the
* configuration file.
*
* @param accountID the identifier of the account to load
* @return true to indicate that the account with the given
* accountID is loaded, false - otherwise
*/
public boolean isAccountLoaded(AccountID accountID)
{
return storedAccounts.contains(accountID) && accountID.isEnabled();
}
private String stripPackagePrefix(String property)
{
int packageEndIndex = property.lastIndexOf('.');
if (packageEndIndex != -1)
property = property.substring(packageEndIndex + 1);
return property;
}
}