diff options
author | Boris Grozev <boris@jitsi.org> | 2016-02-23 14:36:54 -0600 |
---|---|---|
committer | Boris Grozev <boris@jitsi.org> | 2016-02-23 14:36:54 -0600 |
commit | 655874e96ea0b28ade82d212cb00f3a76751e288 (patch) | |
tree | 0089ec13efb46c8f0916ad4dd0a3c029b55d9ab0 /src/net/java/sip/communicator/util | |
parent | e9bd5ed5e3b94177198303dae8ef27c3fc8ab3eb (diff) | |
download | jitsi-655874e96ea0b28ade82d212cb00f3a76751e288.zip jitsi-655874e96ea0b28ade82d212cb00f3a76751e288.tar.gz jitsi-655874e96ea0b28ade82d212cb00f3a76751e288.tar.bz2 |
Converts CRLF line endings to LF, as suggested by @champtar.
The conversion was done with the script provided by @champtar on a
linux
machine:
find -name '*.java' | xargs file | grep 'CRLF, LF' | cut -d ':' -f1 |
xargs -n1 dos2unix
Diffstat (limited to 'src/net/java/sip/communicator/util')
5 files changed, 1277 insertions, 1277 deletions
diff --git a/src/net/java/sip/communicator/util/GenericBuffer.java b/src/net/java/sip/communicator/util/GenericBuffer.java index 756d22f..3647174 100644 --- a/src/net/java/sip/communicator/util/GenericBuffer.java +++ b/src/net/java/sip/communicator/util/GenericBuffer.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,117 +15,117 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.util;
-
-import java.util.*;
-
-/**
- * The GenericBuffer class provides a way to minimize the effort needed to
- * buffer any kind of information. This class is particularly suited to
- * optimizations based on reusing already computed data.
- *
- * @author Benoit Pradelle
- */
-public class GenericBuffer<T>
-{
- private final Hashtable<String, GenericBufferPair> buffer;
-
- private int minAge = 0;
-
- private int curAge = 0;
-
- private final int maxCapacity;
-
- /**
- * Sole constructor.
- *
- * @param bufferSize The buffer size. Adding data to a full buffer will
- * cause the oldest data present in the buffer to be overwritten;
- */
- public GenericBuffer(final int bufferSize)
- {
- assert bufferSize > 0;
-
- buffer = new Hashtable<String, GenericBufferPair>(bufferSize);
- maxCapacity = bufferSize;
- }
-
- /**
- * Adds a value to the buffer. If the buffer is full, the oldest value in
- * the buffer will be overwritten by this new value.
- *
- * @param value The value to add. Can't be null.
- * @param context The context for which this value is valid. This basically
- * represents the current value of all the variables which
- * control the value is correct. The context is used to find this
- * value in the buffer. If the context is already associated in
- * the buffer with a value, nothing is added nor modified.
- */
- public void addValue(final T value, final String context)
- {
- assert value != null && context != null;
-
- GenericBufferPair storage = buffer.get(context);
-
- if (storage == null)
- {
- storage = new GenericBufferPair();
- }
- else
- {
- return; // don't override values
- }
-
- // if the amount of data has reach the limit, search the oldest data
- if (buffer.size() == maxCapacity)
- {
- for (Map.Entry<String, GenericBufferPair> e : buffer.entrySet())
- {
- if (e.getValue().age == minAge)
- {
- buffer.remove(e.getKey());
- minAge++;
- break;
- }
- }
- }
-
- storage.age = curAge++;
- storage.value = value;
-
- buffer.put(context, storage);
- }
-
- /**
- * Retrieves the value in the buffer corresponding to the context if it
- * exists.
- *
- * @param context The context of the searched value. The context represents
- * all the variables values for which this value is correct.
- * @return The bufferized value with the searched context if it exists or
- * null if no value is found.
- */
- public T getValue(final String context)
- {
- assert context != null;
-
- GenericBufferPair res = buffer.get(context);
-
- if (res == null)
- {
- return null;
- }
-
- return res.value;
- }
-
- /**
- * This class is a simple structure to store a pair context-value
- */
- private class GenericBufferPair
- {
- public T value = null;
-
- public int age = 0;
- }
-}
+package net.java.sip.communicator.util; + +import java.util.*; + +/** + * The GenericBuffer class provides a way to minimize the effort needed to + * buffer any kind of information. This class is particularly suited to + * optimizations based on reusing already computed data. + * + * @author Benoit Pradelle + */ +public class GenericBuffer<T> +{ + private final Hashtable<String, GenericBufferPair> buffer; + + private int minAge = 0; + + private int curAge = 0; + + private final int maxCapacity; + + /** + * Sole constructor. + * + * @param bufferSize The buffer size. Adding data to a full buffer will + * cause the oldest data present in the buffer to be overwritten; + */ + public GenericBuffer(final int bufferSize) + { + assert bufferSize > 0; + + buffer = new Hashtable<String, GenericBufferPair>(bufferSize); + maxCapacity = bufferSize; + } + + /** + * Adds a value to the buffer. If the buffer is full, the oldest value in + * the buffer will be overwritten by this new value. + * + * @param value The value to add. Can't be null. + * @param context The context for which this value is valid. This basically + * represents the current value of all the variables which + * control the value is correct. The context is used to find this + * value in the buffer. If the context is already associated in + * the buffer with a value, nothing is added nor modified. + */ + public void addValue(final T value, final String context) + { + assert value != null && context != null; + + GenericBufferPair storage = buffer.get(context); + + if (storage == null) + { + storage = new GenericBufferPair(); + } + else + { + return; // don't override values + } + + // if the amount of data has reach the limit, search the oldest data + if (buffer.size() == maxCapacity) + { + for (Map.Entry<String, GenericBufferPair> e : buffer.entrySet()) + { + if (e.getValue().age == minAge) + { + buffer.remove(e.getKey()); + minAge++; + break; + } + } + } + + storage.age = curAge++; + storage.value = value; + + buffer.put(context, storage); + } + + /** + * Retrieves the value in the buffer corresponding to the context if it + * exists. + * + * @param context The context of the searched value. The context represents + * all the variables values for which this value is correct. + * @return The bufferized value with the searched context if it exists or + * null if no value is found. + */ + public T getValue(final String context) + { + assert context != null; + + GenericBufferPair res = buffer.get(context); + + if (res == null) + { + return null; + } + + return res.value; + } + + /** + * This class is a simple structure to store a pair context-value + */ + private class GenericBufferPair + { + public T value = null; + + public int age = 0; + } +} diff --git a/src/net/java/sip/communicator/util/account/AccountStatusUtils.java b/src/net/java/sip/communicator/util/account/AccountStatusUtils.java index 583fb0a..e5152b4 100644 --- a/src/net/java/sip/communicator/util/account/AccountStatusUtils.java +++ b/src/net/java/sip/communicator/util/account/AccountStatusUtils.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,196 +15,196 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.util.account;
-
-import net.java.sip.communicator.service.protocol.*;
-import net.java.sip.communicator.service.protocol.globalstatus.*;
-import net.java.sip.communicator.util.*;
-
-import java.util.*;
-
-/**
- * The <tt>AccountStatusUtils</tt> provides utility methods for account status
- * management.
- *
- * @author Yana Stamcheva
- */
-public class AccountStatusUtils
-{
- private static GlobalStatusService globalStatusService;
-
- /**
- * If the protocol provider supports presence operation set searches the
- * last status which was selected, otherwise returns null.
- *
- * @param protocolProvider the protocol provider we're interested in.
- * @return the last protocol provider presence status, or null if this
- * provider doesn't support presence operation set
- */
- public static Object getProtocolProviderLastStatus(
- ProtocolProviderService protocolProvider)
- {
- if(getProtocolPresenceOpSet(protocolProvider) != null)
- return getLastPresenceStatus(protocolProvider);
- else
- return getGlobalStatusService()
- .getLastStatusString(protocolProvider);
- }
-
- /**
- * Returns the presence operation set for the given protocol provider.
- *
- * @param protocolProvider The protocol provider for which the
- * presence operation set is searched.
- * @return the presence operation set for the given protocol provider.
- */
- public static OperationSetPresence getProtocolPresenceOpSet(
- ProtocolProviderService protocolProvider)
- {
- OperationSet opSet
- = protocolProvider.getOperationSet(OperationSetPresence.class);
-
- return
- (opSet instanceof OperationSetPresence)
- ? (OperationSetPresence) opSet
- : null;
- }
-
- /**
- * Returns the last status that was stored in the configuration xml for the
- * given protocol provider.
- *
- * @param protocolProvider the protocol provider
- * @return the last status that was stored in the configuration xml for the
- * given protocol provider
- */
- public static PresenceStatus getLastPresenceStatus(
- ProtocolProviderService protocolProvider)
- {
- if (getGlobalStatusService() != null)
- return getGlobalStatusService().getLastPresenceStatus(
- protocolProvider);
-
- return null;
- }
-
- /**
- * Returns the current status for protocol provider.
- *
- * @param protocolProvider the protocol provider
- * @return the current status for protocol provider
- */
- public static PresenceStatus getPresenceStatus(
- ProtocolProviderService protocolProvider)
- {
- PresenceStatus status = null;
-
- OperationSetPresence opSet
- = protocolProvider.getOperationSet(OperationSetPresence.class);
-
- if(opSet != null)
- status = opSet.getPresenceStatus();
-
- return status;
- }
-
- /**
- * Returns the online status of provider.
- * @param protocolProvider the protocol provider
- * @return the online status of provider.
- */
- public static PresenceStatus getOnlineStatus(
- ProtocolProviderService protocolProvider)
- {
- PresenceStatus onlineStatus = null;
-
- OperationSetPresence presence
- = protocolProvider.getOperationSet(OperationSetPresence.class);
-
- // presence can be not supported
- if(presence != null)
- {
- Iterator<PresenceStatus> statusIterator
- = presence.getSupportedStatusSet();
- while (statusIterator.hasNext())
- {
- PresenceStatus status = statusIterator.next();
- int connectivity = status.getStatus();
-
- if ((onlineStatus != null
- && (onlineStatus.getStatus() < connectivity))
- || (onlineStatus == null
- && (connectivity > 50 && connectivity < 80)))
- {
- onlineStatus = status;
- }
- }
- }
-
- return onlineStatus;
- }
-
- /**
- * Returns the offline status of provider.
- * @param protocolProvider the protocol provider
- * @return the offline status of provider.
- */
- public static PresenceStatus getOfflineStatus(
- ProtocolProviderService protocolProvider)
- {
- PresenceStatus offlineStatus = null;
-
- OperationSetPresence presence
- = protocolProvider.getOperationSet(OperationSetPresence.class);
-
- // presence can be not supported
- if(presence != null)
- {
- Iterator<PresenceStatus> statusIterator
- = presence.getSupportedStatusSet();
- while (statusIterator.hasNext())
- {
- PresenceStatus status = statusIterator.next();
- int connectivity = status.getStatus();
-
- if (connectivity < 1)
- {
- offlineStatus = status;
- }
- }
- }
-
- return offlineStatus;
- }
-
- /**
- * Returns the last contact status saved in the configuration.
- *
- * @param protocolProvider the protocol provider to which the status
- * corresponds
- * @return the last contact status saved in the configuration.
- */
- public String getLastStatusString(ProtocolProviderService protocolProvider)
- {
- return getGlobalStatusService().getLastStatusString(protocolProvider);
- }
-
- /**
- * Returns the <tt>GlobalStatusService</tt> obtained from the bundle
- * context.
- * @return the <tt>GlobalStatusService</tt> obtained from the bundle
- * context
- */
- public static GlobalStatusService getGlobalStatusService()
- {
- if (globalStatusService == null)
- {
- globalStatusService
- = ServiceUtils.getService(
- UtilActivator.bundleContext,
- GlobalStatusService.class);
- }
-
- return globalStatusService;
- }
+package net.java.sip.communicator.util.account; + +import net.java.sip.communicator.service.protocol.*; +import net.java.sip.communicator.service.protocol.globalstatus.*; +import net.java.sip.communicator.util.*; + +import java.util.*; + +/** + * The <tt>AccountStatusUtils</tt> provides utility methods for account status + * management. + * + * @author Yana Stamcheva + */ +public class AccountStatusUtils +{ + private static GlobalStatusService globalStatusService; + + /** + * If the protocol provider supports presence operation set searches the + * last status which was selected, otherwise returns null. + * + * @param protocolProvider the protocol provider we're interested in. + * @return the last protocol provider presence status, or null if this + * provider doesn't support presence operation set + */ + public static Object getProtocolProviderLastStatus( + ProtocolProviderService protocolProvider) + { + if(getProtocolPresenceOpSet(protocolProvider) != null) + return getLastPresenceStatus(protocolProvider); + else + return getGlobalStatusService() + .getLastStatusString(protocolProvider); + } + + /** + * Returns the presence operation set for the given protocol provider. + * + * @param protocolProvider The protocol provider for which the + * presence operation set is searched. + * @return the presence operation set for the given protocol provider. + */ + public static OperationSetPresence getProtocolPresenceOpSet( + ProtocolProviderService protocolProvider) + { + OperationSet opSet + = protocolProvider.getOperationSet(OperationSetPresence.class); + + return + (opSet instanceof OperationSetPresence) + ? (OperationSetPresence) opSet + : null; + } + + /** + * Returns the last status that was stored in the configuration xml for the + * given protocol provider. + * + * @param protocolProvider the protocol provider + * @return the last status that was stored in the configuration xml for the + * given protocol provider + */ + public static PresenceStatus getLastPresenceStatus( + ProtocolProviderService protocolProvider) + { + if (getGlobalStatusService() != null) + return getGlobalStatusService().getLastPresenceStatus( + protocolProvider); + + return null; + } + + /** + * Returns the current status for protocol provider. + * + * @param protocolProvider the protocol provider + * @return the current status for protocol provider + */ + public static PresenceStatus getPresenceStatus( + ProtocolProviderService protocolProvider) + { + PresenceStatus status = null; + + OperationSetPresence opSet + = protocolProvider.getOperationSet(OperationSetPresence.class); + + if(opSet != null) + status = opSet.getPresenceStatus(); + + return status; + } + + /** + * Returns the online status of provider. + * @param protocolProvider the protocol provider + * @return the online status of provider. + */ + public static PresenceStatus getOnlineStatus( + ProtocolProviderService protocolProvider) + { + PresenceStatus onlineStatus = null; + + OperationSetPresence presence + = protocolProvider.getOperationSet(OperationSetPresence.class); + + // presence can be not supported + if(presence != null) + { + Iterator<PresenceStatus> statusIterator + = presence.getSupportedStatusSet(); + while (statusIterator.hasNext()) + { + PresenceStatus status = statusIterator.next(); + int connectivity = status.getStatus(); + + if ((onlineStatus != null + && (onlineStatus.getStatus() < connectivity)) + || (onlineStatus == null + && (connectivity > 50 && connectivity < 80))) + { + onlineStatus = status; + } + } + } + + return onlineStatus; + } + + /** + * Returns the offline status of provider. + * @param protocolProvider the protocol provider + * @return the offline status of provider. + */ + public static PresenceStatus getOfflineStatus( + ProtocolProviderService protocolProvider) + { + PresenceStatus offlineStatus = null; + + OperationSetPresence presence + = protocolProvider.getOperationSet(OperationSetPresence.class); + + // presence can be not supported + if(presence != null) + { + Iterator<PresenceStatus> statusIterator + = presence.getSupportedStatusSet(); + while (statusIterator.hasNext()) + { + PresenceStatus status = statusIterator.next(); + int connectivity = status.getStatus(); + + if (connectivity < 1) + { + offlineStatus = status; + } + } + } + + return offlineStatus; + } + + /** + * Returns the last contact status saved in the configuration. + * + * @param protocolProvider the protocol provider to which the status + * corresponds + * @return the last contact status saved in the configuration. + */ + public String getLastStatusString(ProtocolProviderService protocolProvider) + { + return getGlobalStatusService().getLastStatusString(protocolProvider); + } + + /** + * Returns the <tt>GlobalStatusService</tt> obtained from the bundle + * context. + * @return the <tt>GlobalStatusService</tt> obtained from the bundle + * context + */ + public static GlobalStatusService getGlobalStatusService() + { + if (globalStatusService == null) + { + globalStatusService + = ServiceUtils.getService( + UtilActivator.bundleContext, + GlobalStatusService.class); + } + + return globalStatusService; + } } diff --git a/src/net/java/sip/communicator/util/account/AccountUtils.java b/src/net/java/sip/communicator/util/account/AccountUtils.java index ec69aca..82c9108 100644 --- a/src/net/java/sip/communicator/util/account/AccountUtils.java +++ b/src/net/java/sip/communicator/util/account/AccountUtils.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,305 +15,305 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.util.account;
-
-import java.util.*;
-
-import net.java.sip.communicator.service.protocol.*;
-import net.java.sip.communicator.util.*;
-
-import org.osgi.framework.*;
-
-/**
- * The <tt>AccountUtils</tt> provides utility methods helping us to easily
- * obtain an account or a groups of accounts or protocol providers by some
- * specific criteria.
- *
- * @author Yana Stamcheva
- */
-public class AccountUtils
-{
- /**
- * The logger.
- */
- private static Logger logger = Logger.getLogger(AccountUtils.class);
-
- /**
- * Returns an iterator over a list of all stored <tt>AccountID</tt>-s.
- *
- * @return an iterator over a list of all stored <tt>AccountID</tt>-s
- */
- public static Collection<AccountID> getStoredAccounts()
- {
- AccountManager accountManager
- = ServiceUtils.getService( UtilActivator.bundleContext,
- AccountManager.class);
-
- return accountManager.getStoredAccounts();
- }
-
- /**
- * Return the <tt>AccountID</tt> corresponding to the given string account
- * identifier.
- *
- * @param accountID the account identifier string
- * @return the <tt>AccountID</tt> corresponding to the given string account
- * identifier
- */
- public static AccountID getAccountForID(String accountID)
- {
- Collection<AccountID> allAccounts = getStoredAccounts();
-
- for(AccountID account : allAccounts)
- {
- if(account.getAccountUniqueID().equals(accountID))
- return account;
- }
- return null;
- }
-
- /**
- * Returns a list of all currently registered providers, which support the
- * given <tt>operationSetClass</tt>.
- *
- * @param opSetClass the operation set class for which we're looking
- * for providers
- * @return a list of all currently registered providers, which support the
- * given <tt>operationSetClass</tt>
- */
- public static List<ProtocolProviderService> getRegisteredProviders(
- Class<? extends OperationSet> opSetClass)
- {
- List<ProtocolProviderService> opSetProviders
- = new LinkedList<ProtocolProviderService>();
-
- for (ProtocolProviderFactory providerFactory
- : UtilActivator.getProtocolProviderFactories().values())
- {
- for (AccountID accountID : providerFactory.getRegisteredAccounts())
- {
- ServiceReference<ProtocolProviderService> ref
- = providerFactory.getProviderForAccount(accountID);
-
- if (ref != null)
- {
- ProtocolProviderService protocolProvider
- = UtilActivator.bundleContext.getService(ref);
-
- if ((protocolProvider.getOperationSet(opSetClass) != null)
- && protocolProvider.isRegistered())
- {
- opSetProviders.add(protocolProvider);
- }
- }
- }
- }
- return opSetProviders;
- }
-
-
- /**
- * Returns a list of all currently registered telephony providers for the
- * given protocol name.
- * @param protocolName the protocol name
- * @param opSetClass the operation set class for which we're looking for
- * providers
- * @return a list of all currently registered providers for the given
- * <tt>protocolName</tt> and supporting the given <tt>operationSetClass</tt>
- */
- public static List<ProtocolProviderService> getRegisteredProviders(
- String protocolName,
- Class<? extends OperationSet> opSetClass)
- {
- List<ProtocolProviderService> opSetProviders
- = new LinkedList<ProtocolProviderService>();
- ProtocolProviderFactory providerFactory
- = getProtocolProviderFactory(protocolName);
-
- if (providerFactory != null)
- {
- for (AccountID accountID : providerFactory.getRegisteredAccounts())
- {
- ServiceReference<ProtocolProviderService> ref
- = providerFactory.getProviderForAccount(accountID);
-
- if (ref != null)
- {
- ProtocolProviderService protocolProvider
- = UtilActivator.bundleContext.getService(ref);
-
- if ((protocolProvider.getOperationSet(opSetClass) != null)
- && protocolProvider.isRegistered())
- {
- opSetProviders.add(protocolProvider);
- }
- }
- }
- }
- return opSetProviders;
- }
-
- /**
- * Returns a list of all registered protocol providers that could be used
- * for the operation given by the operation set. Prefers the given preferred
- * protocol provider and preferred protocol name if they're available and
- * registered.
- *
- * @param opSet
- * @param preferredProvider
- * @param preferredProtocolName
- * @return a list of all registered protocol providers that could be used
- * for the operation given by the operation set
- */
- public static List<ProtocolProviderService> getOpSetRegisteredProviders(
- Class<? extends OperationSet> opSet,
- ProtocolProviderService preferredProvider,
- String preferredProtocolName)
- {
- List<ProtocolProviderService> providers
- = new ArrayList<ProtocolProviderService>();
-
- if (preferredProvider != null)
- {
- if (preferredProvider.isRegistered())
- {
- providers.add(preferredProvider);
- }
- // If we have a provider, but it's not registered we try to
- // obtain all registered providers for the same protocol as the
- // given preferred provider.
- else
- {
- providers
- = getRegisteredProviders(
- preferredProvider.getProtocolName(),
- opSet);
- }
- }
- // If we don't have a preferred provider we try to obtain a
- // preferred protocol name and all registered providers for it.
- else
- {
- if (preferredProtocolName != null)
- {
- providers
- = getRegisteredProviders(preferredProtocolName, opSet);
- }
- // If the protocol name is null we simply obtain all telephony
- // providers.
- else
- {
- providers = getRegisteredProviders(opSet);
- }
- }
-
- return providers;
- }
-
- /**
- * Returns the <tt>ProtocolProviderService</tt> corresponding to the given
- * account identifier that is registered in the given factory
- * @param accountID the identifier of the account
- * @return the <tt>ProtocolProviderService</tt> corresponding to the given
- * account identifier that is registered in the given factory
- */
- public static ProtocolProviderService getRegisteredProviderForAccount(
- AccountID accountID)
- {
- for (ProtocolProviderFactory factory
- : UtilActivator.getProtocolProviderFactories().values())
- {
- if (factory.getRegisteredAccounts().contains(accountID))
- {
- ServiceReference<ProtocolProviderService> ref
- = factory.getProviderForAccount(accountID);
-
- if (ref != null)
- {
- return UtilActivator.bundleContext.getService(ref);
- }
- }
- }
- return null;
- }
-
- /**
- * Returns a <tt>ProtocolProviderFactory</tt> for a given protocol
- * provider.
- * @param protocolProvider the <tt>ProtocolProviderService</tt>, which
- * factory we're looking for
- * @return a <tt>ProtocolProviderFactory</tt> for a given protocol
- * provider
- */
- public static ProtocolProviderFactory getProtocolProviderFactory(
- ProtocolProviderService protocolProvider)
- {
- return getProtocolProviderFactory(protocolProvider.getProtocolName());
- }
-
- /**
- * Returns a <tt>ProtocolProviderFactory</tt> for a given protocol
- * provider.
- * @param protocolName the name of the protocol
- * @return a <tt>ProtocolProviderFactory</tt> for a given protocol
- * provider
- */
- public static ProtocolProviderFactory getProtocolProviderFactory(
- String protocolName)
- {
- String osgiFilter
- = "(" + ProtocolProviderFactory.PROTOCOL + "=" + protocolName + ")";
- ProtocolProviderFactory protocolProviderFactory = null;
-
- try
- {
- Collection<ServiceReference<ProtocolProviderFactory>> refs
- = UtilActivator.bundleContext.getServiceReferences(
- ProtocolProviderFactory.class,
- osgiFilter);
-
- if ((refs != null) && !refs.isEmpty())
- {
- protocolProviderFactory
- = UtilActivator.bundleContext.getService(
- refs.iterator().next());
- }
- }
- catch (InvalidSyntaxException ex)
- {
- logger.error("AccountUtils : " + ex);
- }
- return protocolProviderFactory;
- }
-
-
- /**
- * Returns all registered protocol providers.
- *
- * @return a list of all registered providers
- */
- public static Collection<ProtocolProviderService> getRegisteredProviders()
- {
- List<ProtocolProviderService> registeredProviders
- = new LinkedList<ProtocolProviderService>();
-
- for (ProtocolProviderFactory providerFactory
- : UtilActivator.getProtocolProviderFactories().values())
- {
- for (AccountID accountID : providerFactory.getRegisteredAccounts())
- {
- ServiceReference<ProtocolProviderService> ref
- = providerFactory.getProviderForAccount(accountID);
-
- if (ref != null)
- {
- ProtocolProviderService protocolProvider
- = UtilActivator.bundleContext.getService(ref);
-
- registeredProviders.add(protocolProvider);
- }
- }
- }
- return registeredProviders;
- }
-}
+package net.java.sip.communicator.util.account; + +import java.util.*; + +import net.java.sip.communicator.service.protocol.*; +import net.java.sip.communicator.util.*; + +import org.osgi.framework.*; + +/** + * The <tt>AccountUtils</tt> provides utility methods helping us to easily + * obtain an account or a groups of accounts or protocol providers by some + * specific criteria. + * + * @author Yana Stamcheva + */ +public class AccountUtils +{ + /** + * The logger. + */ + private static Logger logger = Logger.getLogger(AccountUtils.class); + + /** + * Returns an iterator over a list of all stored <tt>AccountID</tt>-s. + * + * @return an iterator over a list of all stored <tt>AccountID</tt>-s + */ + public static Collection<AccountID> getStoredAccounts() + { + AccountManager accountManager + = ServiceUtils.getService( UtilActivator.bundleContext, + AccountManager.class); + + return accountManager.getStoredAccounts(); + } + + /** + * Return the <tt>AccountID</tt> corresponding to the given string account + * identifier. + * + * @param accountID the account identifier string + * @return the <tt>AccountID</tt> corresponding to the given string account + * identifier + */ + public static AccountID getAccountForID(String accountID) + { + Collection<AccountID> allAccounts = getStoredAccounts(); + + for(AccountID account : allAccounts) + { + if(account.getAccountUniqueID().equals(accountID)) + return account; + } + return null; + } + + /** + * Returns a list of all currently registered providers, which support the + * given <tt>operationSetClass</tt>. + * + * @param opSetClass the operation set class for which we're looking + * for providers + * @return a list of all currently registered providers, which support the + * given <tt>operationSetClass</tt> + */ + public static List<ProtocolProviderService> getRegisteredProviders( + Class<? extends OperationSet> opSetClass) + { + List<ProtocolProviderService> opSetProviders + = new LinkedList<ProtocolProviderService>(); + + for (ProtocolProviderFactory providerFactory + : UtilActivator.getProtocolProviderFactories().values()) + { + for (AccountID accountID : providerFactory.getRegisteredAccounts()) + { + ServiceReference<ProtocolProviderService> ref + = providerFactory.getProviderForAccount(accountID); + + if (ref != null) + { + ProtocolProviderService protocolProvider + = UtilActivator.bundleContext.getService(ref); + + if ((protocolProvider.getOperationSet(opSetClass) != null) + && protocolProvider.isRegistered()) + { + opSetProviders.add(protocolProvider); + } + } + } + } + return opSetProviders; + } + + + /** + * Returns a list of all currently registered telephony providers for the + * given protocol name. + * @param protocolName the protocol name + * @param opSetClass the operation set class for which we're looking for + * providers + * @return a list of all currently registered providers for the given + * <tt>protocolName</tt> and supporting the given <tt>operationSetClass</tt> + */ + public static List<ProtocolProviderService> getRegisteredProviders( + String protocolName, + Class<? extends OperationSet> opSetClass) + { + List<ProtocolProviderService> opSetProviders + = new LinkedList<ProtocolProviderService>(); + ProtocolProviderFactory providerFactory + = getProtocolProviderFactory(protocolName); + + if (providerFactory != null) + { + for (AccountID accountID : providerFactory.getRegisteredAccounts()) + { + ServiceReference<ProtocolProviderService> ref + = providerFactory.getProviderForAccount(accountID); + + if (ref != null) + { + ProtocolProviderService protocolProvider + = UtilActivator.bundleContext.getService(ref); + + if ((protocolProvider.getOperationSet(opSetClass) != null) + && protocolProvider.isRegistered()) + { + opSetProviders.add(protocolProvider); + } + } + } + } + return opSetProviders; + } + + /** + * Returns a list of all registered protocol providers that could be used + * for the operation given by the operation set. Prefers the given preferred + * protocol provider and preferred protocol name if they're available and + * registered. + * + * @param opSet + * @param preferredProvider + * @param preferredProtocolName + * @return a list of all registered protocol providers that could be used + * for the operation given by the operation set + */ + public static List<ProtocolProviderService> getOpSetRegisteredProviders( + Class<? extends OperationSet> opSet, + ProtocolProviderService preferredProvider, + String preferredProtocolName) + { + List<ProtocolProviderService> providers + = new ArrayList<ProtocolProviderService>(); + + if (preferredProvider != null) + { + if (preferredProvider.isRegistered()) + { + providers.add(preferredProvider); + } + // If we have a provider, but it's not registered we try to + // obtain all registered providers for the same protocol as the + // given preferred provider. + else + { + providers + = getRegisteredProviders( + preferredProvider.getProtocolName(), + opSet); + } + } + // If we don't have a preferred provider we try to obtain a + // preferred protocol name and all registered providers for it. + else + { + if (preferredProtocolName != null) + { + providers + = getRegisteredProviders(preferredProtocolName, opSet); + } + // If the protocol name is null we simply obtain all telephony + // providers. + else + { + providers = getRegisteredProviders(opSet); + } + } + + return providers; + } + + /** + * Returns the <tt>ProtocolProviderService</tt> corresponding to the given + * account identifier that is registered in the given factory + * @param accountID the identifier of the account + * @return the <tt>ProtocolProviderService</tt> corresponding to the given + * account identifier that is registered in the given factory + */ + public static ProtocolProviderService getRegisteredProviderForAccount( + AccountID accountID) + { + for (ProtocolProviderFactory factory + : UtilActivator.getProtocolProviderFactories().values()) + { + if (factory.getRegisteredAccounts().contains(accountID)) + { + ServiceReference<ProtocolProviderService> ref + = factory.getProviderForAccount(accountID); + + if (ref != null) + { + return UtilActivator.bundleContext.getService(ref); + } + } + } + return null; + } + + /** + * Returns a <tt>ProtocolProviderFactory</tt> for a given protocol + * provider. + * @param protocolProvider the <tt>ProtocolProviderService</tt>, which + * factory we're looking for + * @return a <tt>ProtocolProviderFactory</tt> for a given protocol + * provider + */ + public static ProtocolProviderFactory getProtocolProviderFactory( + ProtocolProviderService protocolProvider) + { + return getProtocolProviderFactory(protocolProvider.getProtocolName()); + } + + /** + * Returns a <tt>ProtocolProviderFactory</tt> for a given protocol + * provider. + * @param protocolName the name of the protocol + * @return a <tt>ProtocolProviderFactory</tt> for a given protocol + * provider + */ + public static ProtocolProviderFactory getProtocolProviderFactory( + String protocolName) + { + String osgiFilter + = "(" + ProtocolProviderFactory.PROTOCOL + "=" + protocolName + ")"; + ProtocolProviderFactory protocolProviderFactory = null; + + try + { + Collection<ServiceReference<ProtocolProviderFactory>> refs + = UtilActivator.bundleContext.getServiceReferences( + ProtocolProviderFactory.class, + osgiFilter); + + if ((refs != null) && !refs.isEmpty()) + { + protocolProviderFactory + = UtilActivator.bundleContext.getService( + refs.iterator().next()); + } + } + catch (InvalidSyntaxException ex) + { + logger.error("AccountUtils : " + ex); + } + return protocolProviderFactory; + } + + + /** + * Returns all registered protocol providers. + * + * @return a list of all registered providers + */ + public static Collection<ProtocolProviderService> getRegisteredProviders() + { + List<ProtocolProviderService> registeredProviders + = new LinkedList<ProtocolProviderService>(); + + for (ProtocolProviderFactory providerFactory + : UtilActivator.getProtocolProviderFactories().values()) + { + for (AccountID accountID : providerFactory.getRegisteredAccounts()) + { + ServiceReference<ProtocolProviderService> ref + = providerFactory.getProviderForAccount(accountID); + + if (ref != null) + { + ProtocolProviderService protocolProvider + = UtilActivator.bundleContext.getService(ref); + + registeredProviders.add(protocolProvider); + } + } + } + return registeredProviders; + } +} diff --git a/src/net/java/sip/communicator/util/account/LoginManager.java b/src/net/java/sip/communicator/util/account/LoginManager.java index 1dffaf8..09b3092 100644 --- a/src/net/java/sip/communicator/util/account/LoginManager.java +++ b/src/net/java/sip/communicator/util/account/LoginManager.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,577 +15,577 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.util.account;
-
-import net.java.sip.communicator.service.protocol.*;
-import net.java.sip.communicator.service.protocol.event.*;
-import net.java.sip.communicator.service.protocol.globalstatus.*;
-import net.java.sip.communicator.util.*;
-
-import org.osgi.framework.*;
-
-/**
- * The <tt>LoginManager</tt> manages the login operation. Here we obtain the
- * <tt>ProtocolProviderFactory</tt>, we make the account installation and we
- * handle all events related to the registration state.
- * <p>
- * The <tt>LoginManager</tt> is the one that opens one or more
- * <tt>LoginWindow</tt>s for each <tt>ProtocolProviderFactory</tt>. The
- * <tt>LoginWindow</tt> is where user could enter an identifier and password.
- * <p>
- * Note that the behavior of this class will be changed when the Configuration
- * Service is ready.
- *
- * @author Yana Stamcheva
- */
-public class LoginManager
- implements ServiceListener,
- RegistrationStateChangeListener,
- AccountManagerListener
-{
- private static final Logger logger = Logger.getLogger(LoginManager.class);
-
- private boolean manuallyDisconnected = false;
-
- private final LoginRenderer loginRenderer;
-
- /**
- * Creates an instance of the <tt>LoginManager</tt>, by specifying the main
- * application window.
- *
- * @param loginRenderer the main application window
- */
- public LoginManager(LoginRenderer loginRenderer)
- {
- this.loginRenderer = loginRenderer;
-
- UtilActivator.bundleContext.addServiceListener(this);
- }
-
- /**
- * Registers the given protocol provider.
- *
- * @param protocolProvider the ProtocolProviderService to register.
- */
- public void login(ProtocolProviderService protocolProvider)
- {
- loginRenderer.startConnectingUI(protocolProvider);
-
- new RegisterProvider(protocolProvider,
- loginRenderer.getSecurityAuthorityImpl(protocolProvider)).start();
- }
-
- /**
- * Unregisters the given protocol provider.
- *
- * @param protocolProvider the ProtocolProviderService to unregister
- */
- public static void logoff(ProtocolProviderService protocolProvider)
- {
- new UnregisterProvider(protocolProvider).start();
- }
-
- /**
- * Shows login window for each registered account.
- */
- public void runLogin()
- {
- // if someone is late registering catch it
- UtilActivator.getAccountManager().addListener(this);
-
- for (ProtocolProviderFactory providerFactory : UtilActivator
- .getProtocolProviderFactories().values())
- {
- addAccountsForProtocolProviderFactory(providerFactory);
- }
- }
-
- /**
- * Notifies that the loading of the stored accounts of a
- * specific <code>ProtocolProviderFactory</code> has finished.
- *
- * @param event the <code>AccountManagerEvent</code> describing the
- * <code>AccountManager</code> firing the notification and the
- * other details of the specific notification.
- */
- public void handleAccountManagerEvent(AccountManagerEvent event)
- {
- if(event.getType()
- == AccountManagerEvent.STORED_ACCOUNTS_LOADED)
- {
- addAccountsForProtocolProviderFactory(event.getFactory());
- }
- }
-
- /**
- * Handles stored accounts for a protocol provider factory and add them
- * to the UI and register them if needed.
- * @param providerFactory the factory to handle.
- */
- private void addAccountsForProtocolProviderFactory(
- ProtocolProviderFactory providerFactory)
- {
- for (AccountID accountID : providerFactory.getRegisteredAccounts())
- {
- ServiceReference<ProtocolProviderService> serRef
- = providerFactory.getProviderForAccount(accountID);
- ProtocolProviderService protocolProvider
- = UtilActivator.bundleContext.getService(serRef);
-
- handleProviderAdded(protocolProvider);
- }
- }
-
- /**
- * The method is called by a ProtocolProvider implementation whenever a
- * change in the registration state of the corresponding provider had
- * occurred.
- *
- * @param evt ProviderStatusChangeEvent the event describing the status
- * change.
- */
- public void registrationStateChanged(RegistrationStateChangeEvent evt)
- {
- RegistrationState newState = evt.getNewState();
- ProtocolProviderService protocolProvider = evt.getProvider();
- AccountID accountID = protocolProvider.getAccountID();
-
- if (logger.isTraceEnabled())
- logger.trace("Protocol provider: " + protocolProvider
- + " changed its state to: " + evt.getNewState().getStateName());
-
- if (newState.equals(RegistrationState.REGISTERED)
- || newState.equals(RegistrationState.UNREGISTERED)
- || newState.equals(RegistrationState.EXPIRED)
- || newState.equals(RegistrationState.AUTHENTICATION_FAILED)
- || newState.equals(RegistrationState.CONNECTION_FAILED)
- || newState.equals(RegistrationState.CHALLENGED_FOR_AUTHENTICATION)
- || newState.equals(RegistrationState.REGISTERED))
- {
- loginRenderer.stopConnectingUI(protocolProvider);
- }
-
- if (newState.equals(RegistrationState.REGISTERED))
- {
- loginRenderer.protocolProviderConnected(protocolProvider,
- System.currentTimeMillis());
- }
- else
- {
- String msgText;
- if (newState.equals(RegistrationState.AUTHENTICATION_FAILED))
- {
- switch (evt.getReasonCode())
- {
- case RegistrationStateChangeEvent
- .REASON_RECONNECTION_RATE_LIMIT_EXCEEDED:
-
- msgText = UtilActivator.getResources().getI18NString(
- "service.gui.RECONNECTION_LIMIT_EXCEEDED", new String[]
- { accountID.getUserID(), accountID.getService() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- msgText);
- break;
-
- case RegistrationStateChangeEvent.REASON_NON_EXISTING_USER_ID:
- msgText = UtilActivator.getResources().getI18NString(
- "service.gui.NON_EXISTING_USER_ID",
- new String[]
- { protocolProvider.getProtocolDisplayName() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- msgText);
- break;
- case RegistrationStateChangeEvent.REASON_TLS_REQUIRED:
- msgText = UtilActivator.getResources().getI18NString(
- "service.gui.NON_SECURE_CONNECTION",
- new String[]
- { accountID.getAccountAddress() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- msgText);
- break;
- default:
- break;
- }
-
- if (logger.isTraceEnabled())
- logger.trace(evt.getReason());
- }
-// CONNECTION_FAILED events are now dispatched in reconnect plugin
-// else if (newState.equals(RegistrationState.CONNECTION_FAILED))
-// {
-// loginRenderer.protocolProviderConnectionFailed(
-// protocolProvider,
-// this);
-//
-// logger.trace(evt.getReason());
-// }
- else if (newState.equals(RegistrationState.EXPIRED))
- {
- msgText = UtilActivator.getResources().getI18NString(
- "service.gui.CONNECTION_EXPIRED_MSG",
- new String[]
- { protocolProvider.getProtocolDisplayName() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- msgText);
-
- logger.error(evt.getReason());
- }
- else if (newState.equals(RegistrationState.UNREGISTERED))
- {
- if (!manuallyDisconnected)
- {
- if (evt.getReasonCode() == RegistrationStateChangeEvent
- .REASON_MULTIPLE_LOGINS)
- {
- msgText = UtilActivator.getResources().getI18NString(
- "service.gui.MULTIPLE_LOGINS",
- new String[]
- { accountID.getUserID(), accountID.getService() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- msgText);
- }
- else if (evt.getReasonCode() == RegistrationStateChangeEvent
- .REASON_CLIENT_LIMIT_REACHED_FOR_IP)
- {
- msgText = UtilActivator.getResources().getI18NString(
- "service.gui.LIMIT_REACHED_FOR_IP", new String[]
- { protocolProvider.getProtocolDisplayName() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- msgText);
- }
- else if (evt.getReasonCode() == RegistrationStateChangeEvent
- .REASON_USER_REQUEST)
- {
- // do nothing
- }
- else
- {
- msgText = UtilActivator.getResources().getI18NString(
- "service.gui.UNREGISTERED_MESSAGE", new String[]
- { accountID.getUserID(), accountID.getService() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- msgText);
- }
- if (logger.isTraceEnabled())
- logger.trace(evt.getReason());
- }
- }
- }
- }
-
- /**
- * Implements the <tt>ServiceListener</tt> method. Verifies whether the
- * passed event concerns a <tt>ProtocolProviderService</tt> and adds the
- * corresponding UI controls.
- *
- * @param event The <tt>ServiceEvent</tt> object.
- */
- public void serviceChanged(ServiceEvent event)
- {
- ServiceReference<?> serviceRef = event.getServiceReference();
-
- // if the event is caused by a bundle being stopped, we don't want to
- // know
- if (serviceRef.getBundle().getState() == Bundle.STOPPING)
- return;
-
- Object service = UtilActivator.bundleContext.getService(serviceRef);
-
- // we don't care if the source service is not a protocol provider
- if (!(service instanceof ProtocolProviderService))
- return;
-
- switch (event.getType())
- {
- case ServiceEvent.REGISTERED:
- handleProviderAdded((ProtocolProviderService) service);
- break;
- case ServiceEvent.UNREGISTERING:
- handleProviderRemoved((ProtocolProviderService) service);
- break;
- }
- }
-
- /**
- * Adds all UI components (status selector box, etc) related to the given
- * protocol provider.
- *
- * @param protocolProvider the <tt>ProtocolProviderService</tt>
- */
- private void handleProviderAdded(ProtocolProviderService protocolProvider)
- {
- if (logger.isTraceEnabled())
- logger.trace("The following protocol provider was just added: "
- + protocolProvider.getAccountID().getAccountAddress());
-
- synchronized(loginRenderer)
- {
- if(!loginRenderer.containsProtocolProviderUI(protocolProvider))
- {
- protocolProvider.addRegistrationStateChangeListener(this);
- loginRenderer.addProtocolProviderUI(protocolProvider);
- }
- // we have already added this provider and scheduled
- // a login if needed
- // we've done our work, if it fails or something else
- // reconnect or other plugins will take care
- else
- return;
- }
-
- Object status = AccountStatusUtils
- .getProtocolProviderLastStatus(protocolProvider);
-
- if (status == null
- || status.equals(GlobalStatusEnum.ONLINE_STATUS)
- || ((status instanceof PresenceStatus) && (((PresenceStatus) status)
- .getStatus() >= PresenceStatus.ONLINE_THRESHOLD)))
- {
- login(protocolProvider);
- }
- }
-
- /**
- * Removes all UI components related to the given protocol provider.
- *
- * @param protocolProvider the <tt>ProtocolProviderService</tt>
- */
- private void handleProviderRemoved(ProtocolProviderService protocolProvider)
- {
- loginRenderer.removeProtocolProviderUI(protocolProvider);
- }
-
- /**
- * Returns <tt>true</tt> to indicate the jitsi has been manually
- * disconnected, <tt>false</tt> - otherwise.
- *
- * @return <tt>true</tt> to indicate the jitsi has been manually
- * disconnected, <tt>false</tt> - otherwise
- */
- public boolean isManuallyDisconnected()
- {
- return manuallyDisconnected;
- }
-
- /**
- * Sets the manually disconnected property.
- *
- * @param manuallyDisconnected <tt>true</tt> to indicate the jitsi has been
- * manually disconnected, <tt>false</tt> - otherwise
- */
- public void setManuallyDisconnected(boolean manuallyDisconnected)
- {
- this.manuallyDisconnected = manuallyDisconnected;
- }
-
- /**
- * Registers a protocol provider in a separate thread.
- */
- private class RegisterProvider
- extends Thread
- {
- private final ProtocolProviderService protocolProvider;
-
- private final SecurityAuthority secAuth;
-
- RegisterProvider( ProtocolProviderService protocolProvider,
- SecurityAuthority secAuth)
- {
- this.protocolProvider = protocolProvider;
- this.secAuth = secAuth;
-
- if(logger.isTraceEnabled())
- logger.trace("Registering provider: "
- + protocolProvider.getAccountID().getAccountAddress(),
- new Exception(
- "Just tracing, provider registering, not an error!"));
- }
-
- /**
- * Registers the contained protocol provider and process all possible
- * errors that may occur during the registration process.
- */
- @Override
- public void run()
- {
- try
- {
- protocolProvider.register(secAuth);
- }
- catch (OperationFailedException ex)
- {
- handleOperationFailedException(ex);
- }
- catch (Throwable ex)
- {
- logger.error("Failed to register protocol provider. ", ex);
-
- AccountID accountID = protocolProvider.getAccountID();
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- UtilActivator.getResources()
- .getI18NString("service.gui.LOGIN_GENERAL_ERROR",
- new String[]
- { accountID.getUserID(),
- accountID.getProtocolName(),
- accountID.getService() }));
- }
- }
-
- private void handleOperationFailedException(OperationFailedException ex)
- {
- String errorMessage = "";
-
- switch (ex.getErrorCode())
- {
- case OperationFailedException.GENERAL_ERROR:
- {
- logger.error("Provider could not be registered"
- + " due to the following general error: ", ex);
-
- AccountID accountID = protocolProvider.getAccountID();
- errorMessage =
- UtilActivator.getResources().getI18NString(
- "service.gui.LOGIN_GENERAL_ERROR",
- new String[]
- { accountID.getUserID(),
- accountID.getProtocolName(),
- accountID.getService() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"), errorMessage, ex);
- }
- break;
- case OperationFailedException.INTERNAL_ERROR:
- {
- logger.error("Provider could not be registered"
- + " due to the following internal error: ", ex);
-
- AccountID accountID = protocolProvider.getAccountID();
- errorMessage =
- UtilActivator.getResources().getI18NString(
- "service.gui.LOGIN_INTERNAL_ERROR",
- new String[]
- { accountID.getUserID(), accountID.getService() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources().getI18NString(
- "service.gui.ERROR"), errorMessage, ex);
- }
- break;
- case OperationFailedException.NETWORK_FAILURE:
- {
- if (logger.isInfoEnabled())
- {
- logger.info("Provider could not be registered"
- + " due to a network failure: " + ex);
- }
-
- loginRenderer.protocolProviderConnectionFailed(
- protocolProvider,
- LoginManager.this);
- }
- break;
- case OperationFailedException.INVALID_ACCOUNT_PROPERTIES:
- {
- logger.error("Provider could not be registered"
- + " due to an invalid account property: ", ex);
-
- AccountID accountID = protocolProvider.getAccountID();
- errorMessage =
- UtilActivator.getResources().getI18NString(
- "service.gui.LOGIN_INVALID_PROPERTIES_ERROR",
- new String[]
- { accountID.getUserID(), accountID.getService() });
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- errorMessage, ex);
- }
- break;
- default:
- logger.error("Provider could not be registered.", ex);
- }
- }
- }
-
- /**
- * Unregisters a protocol provider in a separate thread.
- */
- private static class UnregisterProvider
- extends Thread
- {
- ProtocolProviderService protocolProvider;
-
- UnregisterProvider(ProtocolProviderService protocolProvider)
- {
- this.protocolProvider = protocolProvider;
- }
-
- /**
- * Unregisters the contained protocol provider and process all possible
- * errors that may occur during the un-registration process.
- */
- @Override
- public void run()
- {
- try
- {
- protocolProvider.unregister(true);
- }
- catch (OperationFailedException ex)
- {
- int errorCode = ex.getErrorCode();
-
- if (errorCode == OperationFailedException.GENERAL_ERROR)
- {
- logger.error("Provider could not be unregistered"
- + " due to the following general error: " + ex);
- }
- else if (errorCode == OperationFailedException.INTERNAL_ERROR)
- {
- logger.error("Provider could not be unregistered"
- + " due to the following internal error: " + ex);
- }
- else if (errorCode == OperationFailedException.NETWORK_FAILURE)
- {
- logger.error("Provider could not be unregistered"
- + " due to a network failure: " + ex);
- }
-
- UtilActivator.getAlertUIService().showAlertDialog(
- UtilActivator.getResources()
- .getI18NString("service.gui.ERROR"),
- UtilActivator.getResources()
- .getI18NString("service.gui.LOGOFF_NOT_SUCCEEDED",
- new String[]
- { protocolProvider.getAccountID().getUserID(),
- protocolProvider.getAccountID().getService() }));
- }
- }
- }
-}
+package net.java.sip.communicator.util.account; + +import net.java.sip.communicator.service.protocol.*; +import net.java.sip.communicator.service.protocol.event.*; +import net.java.sip.communicator.service.protocol.globalstatus.*; +import net.java.sip.communicator.util.*; + +import org.osgi.framework.*; + +/** + * The <tt>LoginManager</tt> manages the login operation. Here we obtain the + * <tt>ProtocolProviderFactory</tt>, we make the account installation and we + * handle all events related to the registration state. + * <p> + * The <tt>LoginManager</tt> is the one that opens one or more + * <tt>LoginWindow</tt>s for each <tt>ProtocolProviderFactory</tt>. The + * <tt>LoginWindow</tt> is where user could enter an identifier and password. + * <p> + * Note that the behavior of this class will be changed when the Configuration + * Service is ready. + * + * @author Yana Stamcheva + */ +public class LoginManager + implements ServiceListener, + RegistrationStateChangeListener, + AccountManagerListener +{ + private static final Logger logger = Logger.getLogger(LoginManager.class); + + private boolean manuallyDisconnected = false; + + private final LoginRenderer loginRenderer; + + /** + * Creates an instance of the <tt>LoginManager</tt>, by specifying the main + * application window. + * + * @param loginRenderer the main application window + */ + public LoginManager(LoginRenderer loginRenderer) + { + this.loginRenderer = loginRenderer; + + UtilActivator.bundleContext.addServiceListener(this); + } + + /** + * Registers the given protocol provider. + * + * @param protocolProvider the ProtocolProviderService to register. + */ + public void login(ProtocolProviderService protocolProvider) + { + loginRenderer.startConnectingUI(protocolProvider); + + new RegisterProvider(protocolProvider, + loginRenderer.getSecurityAuthorityImpl(protocolProvider)).start(); + } + + /** + * Unregisters the given protocol provider. + * + * @param protocolProvider the ProtocolProviderService to unregister + */ + public static void logoff(ProtocolProviderService protocolProvider) + { + new UnregisterProvider(protocolProvider).start(); + } + + /** + * Shows login window for each registered account. + */ + public void runLogin() + { + // if someone is late registering catch it + UtilActivator.getAccountManager().addListener(this); + + for (ProtocolProviderFactory providerFactory : UtilActivator + .getProtocolProviderFactories().values()) + { + addAccountsForProtocolProviderFactory(providerFactory); + } + } + + /** + * Notifies that the loading of the stored accounts of a + * specific <code>ProtocolProviderFactory</code> has finished. + * + * @param event the <code>AccountManagerEvent</code> describing the + * <code>AccountManager</code> firing the notification and the + * other details of the specific notification. + */ + public void handleAccountManagerEvent(AccountManagerEvent event) + { + if(event.getType() + == AccountManagerEvent.STORED_ACCOUNTS_LOADED) + { + addAccountsForProtocolProviderFactory(event.getFactory()); + } + } + + /** + * Handles stored accounts for a protocol provider factory and add them + * to the UI and register them if needed. + * @param providerFactory the factory to handle. + */ + private void addAccountsForProtocolProviderFactory( + ProtocolProviderFactory providerFactory) + { + for (AccountID accountID : providerFactory.getRegisteredAccounts()) + { + ServiceReference<ProtocolProviderService> serRef + = providerFactory.getProviderForAccount(accountID); + ProtocolProviderService protocolProvider + = UtilActivator.bundleContext.getService(serRef); + + handleProviderAdded(protocolProvider); + } + } + + /** + * The method is called by a ProtocolProvider implementation whenever a + * change in the registration state of the corresponding provider had + * occurred. + * + * @param evt ProviderStatusChangeEvent the event describing the status + * change. + */ + public void registrationStateChanged(RegistrationStateChangeEvent evt) + { + RegistrationState newState = evt.getNewState(); + ProtocolProviderService protocolProvider = evt.getProvider(); + AccountID accountID = protocolProvider.getAccountID(); + + if (logger.isTraceEnabled()) + logger.trace("Protocol provider: " + protocolProvider + + " changed its state to: " + evt.getNewState().getStateName()); + + if (newState.equals(RegistrationState.REGISTERED) + || newState.equals(RegistrationState.UNREGISTERED) + || newState.equals(RegistrationState.EXPIRED) + || newState.equals(RegistrationState.AUTHENTICATION_FAILED) + || newState.equals(RegistrationState.CONNECTION_FAILED) + || newState.equals(RegistrationState.CHALLENGED_FOR_AUTHENTICATION) + || newState.equals(RegistrationState.REGISTERED)) + { + loginRenderer.stopConnectingUI(protocolProvider); + } + + if (newState.equals(RegistrationState.REGISTERED)) + { + loginRenderer.protocolProviderConnected(protocolProvider, + System.currentTimeMillis()); + } + else + { + String msgText; + if (newState.equals(RegistrationState.AUTHENTICATION_FAILED)) + { + switch (evt.getReasonCode()) + { + case RegistrationStateChangeEvent + .REASON_RECONNECTION_RATE_LIMIT_EXCEEDED: + + msgText = UtilActivator.getResources().getI18NString( + "service.gui.RECONNECTION_LIMIT_EXCEEDED", new String[] + { accountID.getUserID(), accountID.getService() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + msgText); + break; + + case RegistrationStateChangeEvent.REASON_NON_EXISTING_USER_ID: + msgText = UtilActivator.getResources().getI18NString( + "service.gui.NON_EXISTING_USER_ID", + new String[] + { protocolProvider.getProtocolDisplayName() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + msgText); + break; + case RegistrationStateChangeEvent.REASON_TLS_REQUIRED: + msgText = UtilActivator.getResources().getI18NString( + "service.gui.NON_SECURE_CONNECTION", + new String[] + { accountID.getAccountAddress() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + msgText); + break; + default: + break; + } + + if (logger.isTraceEnabled()) + logger.trace(evt.getReason()); + } +// CONNECTION_FAILED events are now dispatched in reconnect plugin +// else if (newState.equals(RegistrationState.CONNECTION_FAILED)) +// { +// loginRenderer.protocolProviderConnectionFailed( +// protocolProvider, +// this); +// +// logger.trace(evt.getReason()); +// } + else if (newState.equals(RegistrationState.EXPIRED)) + { + msgText = UtilActivator.getResources().getI18NString( + "service.gui.CONNECTION_EXPIRED_MSG", + new String[] + { protocolProvider.getProtocolDisplayName() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + msgText); + + logger.error(evt.getReason()); + } + else if (newState.equals(RegistrationState.UNREGISTERED)) + { + if (!manuallyDisconnected) + { + if (evt.getReasonCode() == RegistrationStateChangeEvent + .REASON_MULTIPLE_LOGINS) + { + msgText = UtilActivator.getResources().getI18NString( + "service.gui.MULTIPLE_LOGINS", + new String[] + { accountID.getUserID(), accountID.getService() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + msgText); + } + else if (evt.getReasonCode() == RegistrationStateChangeEvent + .REASON_CLIENT_LIMIT_REACHED_FOR_IP) + { + msgText = UtilActivator.getResources().getI18NString( + "service.gui.LIMIT_REACHED_FOR_IP", new String[] + { protocolProvider.getProtocolDisplayName() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + msgText); + } + else if (evt.getReasonCode() == RegistrationStateChangeEvent + .REASON_USER_REQUEST) + { + // do nothing + } + else + { + msgText = UtilActivator.getResources().getI18NString( + "service.gui.UNREGISTERED_MESSAGE", new String[] + { accountID.getUserID(), accountID.getService() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + msgText); + } + if (logger.isTraceEnabled()) + logger.trace(evt.getReason()); + } + } + } + } + + /** + * Implements the <tt>ServiceListener</tt> method. Verifies whether the + * passed event concerns a <tt>ProtocolProviderService</tt> and adds the + * corresponding UI controls. + * + * @param event The <tt>ServiceEvent</tt> object. + */ + public void serviceChanged(ServiceEvent event) + { + ServiceReference<?> serviceRef = event.getServiceReference(); + + // if the event is caused by a bundle being stopped, we don't want to + // know + if (serviceRef.getBundle().getState() == Bundle.STOPPING) + return; + + Object service = UtilActivator.bundleContext.getService(serviceRef); + + // we don't care if the source service is not a protocol provider + if (!(service instanceof ProtocolProviderService)) + return; + + switch (event.getType()) + { + case ServiceEvent.REGISTERED: + handleProviderAdded((ProtocolProviderService) service); + break; + case ServiceEvent.UNREGISTERING: + handleProviderRemoved((ProtocolProviderService) service); + break; + } + } + + /** + * Adds all UI components (status selector box, etc) related to the given + * protocol provider. + * + * @param protocolProvider the <tt>ProtocolProviderService</tt> + */ + private void handleProviderAdded(ProtocolProviderService protocolProvider) + { + if (logger.isTraceEnabled()) + logger.trace("The following protocol provider was just added: " + + protocolProvider.getAccountID().getAccountAddress()); + + synchronized(loginRenderer) + { + if(!loginRenderer.containsProtocolProviderUI(protocolProvider)) + { + protocolProvider.addRegistrationStateChangeListener(this); + loginRenderer.addProtocolProviderUI(protocolProvider); + } + // we have already added this provider and scheduled + // a login if needed + // we've done our work, if it fails or something else + // reconnect or other plugins will take care + else + return; + } + + Object status = AccountStatusUtils + .getProtocolProviderLastStatus(protocolProvider); + + if (status == null + || status.equals(GlobalStatusEnum.ONLINE_STATUS) + || ((status instanceof PresenceStatus) && (((PresenceStatus) status) + .getStatus() >= PresenceStatus.ONLINE_THRESHOLD))) + { + login(protocolProvider); + } + } + + /** + * Removes all UI components related to the given protocol provider. + * + * @param protocolProvider the <tt>ProtocolProviderService</tt> + */ + private void handleProviderRemoved(ProtocolProviderService protocolProvider) + { + loginRenderer.removeProtocolProviderUI(protocolProvider); + } + + /** + * Returns <tt>true</tt> to indicate the jitsi has been manually + * disconnected, <tt>false</tt> - otherwise. + * + * @return <tt>true</tt> to indicate the jitsi has been manually + * disconnected, <tt>false</tt> - otherwise + */ + public boolean isManuallyDisconnected() + { + return manuallyDisconnected; + } + + /** + * Sets the manually disconnected property. + * + * @param manuallyDisconnected <tt>true</tt> to indicate the jitsi has been + * manually disconnected, <tt>false</tt> - otherwise + */ + public void setManuallyDisconnected(boolean manuallyDisconnected) + { + this.manuallyDisconnected = manuallyDisconnected; + } + + /** + * Registers a protocol provider in a separate thread. + */ + private class RegisterProvider + extends Thread + { + private final ProtocolProviderService protocolProvider; + + private final SecurityAuthority secAuth; + + RegisterProvider( ProtocolProviderService protocolProvider, + SecurityAuthority secAuth) + { + this.protocolProvider = protocolProvider; + this.secAuth = secAuth; + + if(logger.isTraceEnabled()) + logger.trace("Registering provider: " + + protocolProvider.getAccountID().getAccountAddress(), + new Exception( + "Just tracing, provider registering, not an error!")); + } + + /** + * Registers the contained protocol provider and process all possible + * errors that may occur during the registration process. + */ + @Override + public void run() + { + try + { + protocolProvider.register(secAuth); + } + catch (OperationFailedException ex) + { + handleOperationFailedException(ex); + } + catch (Throwable ex) + { + logger.error("Failed to register protocol provider. ", ex); + + AccountID accountID = protocolProvider.getAccountID(); + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + UtilActivator.getResources() + .getI18NString("service.gui.LOGIN_GENERAL_ERROR", + new String[] + { accountID.getUserID(), + accountID.getProtocolName(), + accountID.getService() })); + } + } + + private void handleOperationFailedException(OperationFailedException ex) + { + String errorMessage = ""; + + switch (ex.getErrorCode()) + { + case OperationFailedException.GENERAL_ERROR: + { + logger.error("Provider could not be registered" + + " due to the following general error: ", ex); + + AccountID accountID = protocolProvider.getAccountID(); + errorMessage = + UtilActivator.getResources().getI18NString( + "service.gui.LOGIN_GENERAL_ERROR", + new String[] + { accountID.getUserID(), + accountID.getProtocolName(), + accountID.getService() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), errorMessage, ex); + } + break; + case OperationFailedException.INTERNAL_ERROR: + { + logger.error("Provider could not be registered" + + " due to the following internal error: ", ex); + + AccountID accountID = protocolProvider.getAccountID(); + errorMessage = + UtilActivator.getResources().getI18NString( + "service.gui.LOGIN_INTERNAL_ERROR", + new String[] + { accountID.getUserID(), accountID.getService() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources().getI18NString( + "service.gui.ERROR"), errorMessage, ex); + } + break; + case OperationFailedException.NETWORK_FAILURE: + { + if (logger.isInfoEnabled()) + { + logger.info("Provider could not be registered" + + " due to a network failure: " + ex); + } + + loginRenderer.protocolProviderConnectionFailed( + protocolProvider, + LoginManager.this); + } + break; + case OperationFailedException.INVALID_ACCOUNT_PROPERTIES: + { + logger.error("Provider could not be registered" + + " due to an invalid account property: ", ex); + + AccountID accountID = protocolProvider.getAccountID(); + errorMessage = + UtilActivator.getResources().getI18NString( + "service.gui.LOGIN_INVALID_PROPERTIES_ERROR", + new String[] + { accountID.getUserID(), accountID.getService() }); + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + errorMessage, ex); + } + break; + default: + logger.error("Provider could not be registered.", ex); + } + } + } + + /** + * Unregisters a protocol provider in a separate thread. + */ + private static class UnregisterProvider + extends Thread + { + ProtocolProviderService protocolProvider; + + UnregisterProvider(ProtocolProviderService protocolProvider) + { + this.protocolProvider = protocolProvider; + } + + /** + * Unregisters the contained protocol provider and process all possible + * errors that may occur during the un-registration process. + */ + @Override + public void run() + { + try + { + protocolProvider.unregister(true); + } + catch (OperationFailedException ex) + { + int errorCode = ex.getErrorCode(); + + if (errorCode == OperationFailedException.GENERAL_ERROR) + { + logger.error("Provider could not be unregistered" + + " due to the following general error: " + ex); + } + else if (errorCode == OperationFailedException.INTERNAL_ERROR) + { + logger.error("Provider could not be unregistered" + + " due to the following internal error: " + ex); + } + else if (errorCode == OperationFailedException.NETWORK_FAILURE) + { + logger.error("Provider could not be unregistered" + + " due to a network failure: " + ex); + } + + UtilActivator.getAlertUIService().showAlertDialog( + UtilActivator.getResources() + .getI18NString("service.gui.ERROR"), + UtilActivator.getResources() + .getI18NString("service.gui.LOGOFF_NOT_SUCCEEDED", + new String[] + { protocolProvider.getAccountID().getUserID(), + protocolProvider.getAccountID().getService() })); + } + } + } +} diff --git a/src/net/java/sip/communicator/util/account/LoginRenderer.java b/src/net/java/sip/communicator/util/account/LoginRenderer.java index ee39940..e1ae850 100644 --- a/src/net/java/sip/communicator/util/account/LoginRenderer.java +++ b/src/net/java/sip/communicator/util/account/LoginRenderer.java @@ -1,4 +1,4 @@ -/*
+/* * Jitsi, the OpenSource Java VoIP and Instant Messaging client. * * Copyright @ 2015 Atlassian Pty Ltd @@ -15,94 +15,94 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package net.java.sip.communicator.util.account;
-
-import net.java.sip.communicator.service.protocol.*;
-
-/**
- * The <tt>LoginRenderer</tt> is the renderer of all login related operations.
- *
- * @author Yana Stamcheva
- */
-public interface LoginRenderer
-{
- /**
- * Adds the user interface related to the given protocol provider.
- *
- * @param protocolProvider the protocol provider for which we add the user
- * interface
- */
- public void addProtocolProviderUI(
- ProtocolProviderService protocolProvider);
-
- /**
- * Removes the user interface related to the given protocol provider.
- *
- * @param protocolProvider the protocol provider to remove
- */
- public void removeProtocolProviderUI(
- ProtocolProviderService protocolProvider);
-
- /**
- * Starts the connecting user interface for the given protocol provider.
- *
- * @param protocolProvider the protocol provider for which we add the
- * connecting user interface
- */
- public void startConnectingUI(ProtocolProviderService protocolProvider);
-
- /**
- * Stops the connecting user interface for the given protocol provider.
- *
- * @param protocolProvider the protocol provider for which we remove the
- * connecting user interface
- */
- public void stopConnectingUI(ProtocolProviderService protocolProvider);
-
- /**
- * Indicates that the given protocol provider is now connected.
- *
- * @param protocolProvider the <tt>ProtocolProviderService</tt> that is
- * connected
- * @param date the date on which the event occured
- */
- public void protocolProviderConnected(
- ProtocolProviderService protocolProvider,
- long date);
-
- /**
- * Indicates that a protocol provider connection has failed.
- *
- * @param protocolProvider the <tt>ProtocolProviderService</tt>, which
- * connection failed
- * @param loginManagerCallback the <tt>LoginManager</tt> implementation,
- * which is managing the process
- */
- public void protocolProviderConnectionFailed(
- ProtocolProviderService protocolProvider,
- LoginManager loginManagerCallback);
-
- /**
- * Returns the <tt>SecurityAuthority</tt> implementation related to this
- * login renderer.
- *
- * @param protocolProvider the specific <tt>ProtocolProviderService</tt>,
- * for which we're obtaining a security authority
- * @return the <tt>SecurityAuthority</tt> implementation related to this
- * login renderer
- */
- public SecurityAuthority getSecurityAuthorityImpl(
- ProtocolProviderService protocolProvider);
-
- /**
- * Indicates if the given <tt>protocolProvider</tt> related user interface
- * is already rendered.
- *
- * @param protocolProvider the <tt>ProtocolProviderService</tt>, which
- * related user interface we're looking for
- * @return <tt>true</tt> if the given <tt>protocolProvider</tt> related user
- * interface is already rendered
- */
- public boolean containsProtocolProviderUI(
- ProtocolProviderService protocolProvider);
+package net.java.sip.communicator.util.account; + +import net.java.sip.communicator.service.protocol.*; + +/** + * The <tt>LoginRenderer</tt> is the renderer of all login related operations. + * + * @author Yana Stamcheva + */ +public interface LoginRenderer +{ + /** + * Adds the user interface related to the given protocol provider. + * + * @param protocolProvider the protocol provider for which we add the user + * interface + */ + public void addProtocolProviderUI( + ProtocolProviderService protocolProvider); + + /** + * Removes the user interface related to the given protocol provider. + * + * @param protocolProvider the protocol provider to remove + */ + public void removeProtocolProviderUI( + ProtocolProviderService protocolProvider); + + /** + * Starts the connecting user interface for the given protocol provider. + * + * @param protocolProvider the protocol provider for which we add the + * connecting user interface + */ + public void startConnectingUI(ProtocolProviderService protocolProvider); + + /** + * Stops the connecting user interface for the given protocol provider. + * + * @param protocolProvider the protocol provider for which we remove the + * connecting user interface + */ + public void stopConnectingUI(ProtocolProviderService protocolProvider); + + /** + * Indicates that the given protocol provider is now connected. + * + * @param protocolProvider the <tt>ProtocolProviderService</tt> that is + * connected + * @param date the date on which the event occured + */ + public void protocolProviderConnected( + ProtocolProviderService protocolProvider, + long date); + + /** + * Indicates that a protocol provider connection has failed. + * + * @param protocolProvider the <tt>ProtocolProviderService</tt>, which + * connection failed + * @param loginManagerCallback the <tt>LoginManager</tt> implementation, + * which is managing the process + */ + public void protocolProviderConnectionFailed( + ProtocolProviderService protocolProvider, + LoginManager loginManagerCallback); + + /** + * Returns the <tt>SecurityAuthority</tt> implementation related to this + * login renderer. + * + * @param protocolProvider the specific <tt>ProtocolProviderService</tt>, + * for which we're obtaining a security authority + * @return the <tt>SecurityAuthority</tt> implementation related to this + * login renderer + */ + public SecurityAuthority getSecurityAuthorityImpl( + ProtocolProviderService protocolProvider); + + /** + * Indicates if the given <tt>protocolProvider</tt> related user interface + * is already rendered. + * + * @param protocolProvider the <tt>ProtocolProviderService</tt>, which + * related user interface we're looking for + * @return <tt>true</tt> if the given <tt>protocolProvider</tt> related user + * interface is already rendered + */ + public boolean containsProtocolProviderUI( + ProtocolProviderService protocolProvider); } |