/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.service.protocol;
import org.osgi.framework.*;
import net.java.sip.communicator.service.configuration.*;
import net.java.sip.communicator.service.resources.*;
import net.java.sip.communicator.util.*;
/**
* Implements BundleActivator
for the purposes of
* protocol.jar/protocol.provider.manifest.mf and in order to register and start
* services independent of the specifics of a particular protocol.
*
* @author Lubomir Marinov
* @author Yana Stamcheva
*/
public class ProtocolProviderActivator
implements BundleActivator
{
/**
* The object used for logging.
*/
private final static Logger logger
= Logger.getLogger(ProtocolProviderActivator.class);
/**
* The ServiceRegistration
of the AccountManager
* implementation registered as a service by this activator and cached so
* that the service in question can be properly disposed of upon stopping
* this activator.
*/
private ServiceRegistration accountManagerServiceRegistration;
/**
* The BundleContext
of the one and only
* ProtocolProviderActivator
instance which is currently
* started.
*/
private static BundleContext bundleContext;
/**
* The ConfigurationService
used by the classes in the bundle
* represented by ProtocolProviderActivator
.
*/
private static ConfigurationService configurationService;
/**
* The resource service through which we obtain localized strings.
*/
private static ResourceManagementService resourceService;
/**
* The SingleCallInProgressPolicy
making sure that the
* Call
s accessible in the BundleContext
of this
* activator will obey to the rule that a new Call
should put
* the other existing Call
s on hold.
*/
private SingleCallInProgressPolicy singleCallInProgressPolicy;
/**
* Gets the ConfigurationService
to be used by the classes in
* the bundle represented by ProtocolProviderActivator
.
*
* @return the ConfigurationService
to be used by the classes
* in the bundle represented by
* ProtocolProviderActivator
*/
static ConfigurationService getConfigurationService()
{
if (configurationService == null)
{
configurationService
= (ConfigurationService)
bundleContext.getService(
bundleContext.getServiceReference(
ConfigurationService.class.getName()));
}
return configurationService;
}
/**
* Gets the ResourceManagementService
to be used by the classes
* in the bundle represented by ProtocolProviderActivator
.
*
* @return the ResourceManagementService
to be used by the
* classes in the bundle represented by
* ProtocolProviderActivator
*/
public static ResourceManagementService getResourceService()
{
if (resourceService == null)
{
resourceService
= (ResourceManagementService)
bundleContext.getService(
bundleContext.getServiceReference(
ResourceManagementService.class.getName()));
}
return resourceService;
}
/**
* Returns a ProtocolProviderFactory for a given protocol
* provider.
* @param protocolName the name of the protocol, which factory we're
* looking for
* @return a ProtocolProviderFactory for a given protocol
* provider
*/
public static ProtocolProviderFactory getProtocolProviderFactory(
String protocolName)
{
String osgiFilter = "("
+ ProtocolProviderFactory.PROTOCOL
+ "="+protocolName+")";
ProtocolProviderFactory protocolProviderFactory = null;
try
{
ServiceReference[] serRefs
= bundleContext.getServiceReferences(
ProtocolProviderFactory.class.getName(), osgiFilter);
protocolProviderFactory = (ProtocolProviderFactory)
bundleContext.getService(serRefs[0]);
}
catch (InvalidSyntaxException ex)
{
if (logger.isInfoEnabled())
logger.info("ProtocolProviderActivator : " + ex);
}
return protocolProviderFactory;
}
/**
* Registers a new AccountManagerImpl
instance as an
* AccountManager
service and starts a new
* SingleCallInProgressPolicy
instance to ensure that only one
* of the Call
s accessible in the BundleContext
* in which this activator is to execute will be in progress and the others
* will automatically be put on hold.
*
* @param bundleContext the BundleContext
in which the bundle
* activation represented by this BundleActivator
* executes
*/
public void start(BundleContext bundleContext)
{
ProtocolProviderActivator.bundleContext = bundleContext;
accountManagerServiceRegistration =
bundleContext.registerService(AccountManager.class.getName(),
new AccountManager(bundleContext), null);
singleCallInProgressPolicy =
new SingleCallInProgressPolicy(bundleContext);
}
/**
* Unregisters the AccountManagerImpl
instance registered as an
* AccountManager
service in {@link #start(BundleContext)} and
* stops the SingleCallInProgressPolicy
started there as well.
*
* @param bundleContext the BundleContext
in which the bundle
* activation represented by this BundleActivator
* executes
*/
public void stop(BundleContext bundleContext)
{
if (accountManagerServiceRegistration != null)
{
accountManagerServiceRegistration.unregister();
accountManagerServiceRegistration = null;
}
if (singleCallInProgressPolicy != null)
{
singleCallInProgressPolicy.dispose();
singleCallInProgressPolicy = null;
}
if (bundleContext.equals(ProtocolProviderActivator.bundleContext))
ProtocolProviderActivator.bundleContext = null;
}
}