/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.java.sip.communicator.util;
import java.util.*;
import org.osgi.framework.*;
/**
* Gathers utility functions related to OSGi services such as getting a service
* registered in a BundleContext.
*
* @author Lyubomir Marinov
* @author Pawel Domas
*/
public class ServiceUtils
{
/**
* Gets an OSGi service registered in a specific BundleContext by
* its Class
*
* @param the very type of the OSGi service to get
* @param bundleContext the BundleContext in which the service to
* get has been registered
* @param serviceClass the Class with which the service to get has
* been registered in the bundleContext
* @return the OSGi service registered in bundleContext with the
* specified serviceClass if such a service exists there;
* otherwise, null
*/
public static T getService(
BundleContext bundleContext,
Class serviceClass)
{
ServiceReference serviceReference
= bundleContext.getServiceReference(serviceClass);
return
(serviceReference == null)
? null
: bundleContext.getService(serviceReference);
}
/**
* Gets an OSGi service references registered in a specific
* BundleContext by its Class.
*
* @param bundleContext the BundleContext in which the services to
* get have been registered
* @param serviceClass the Class of the OSGi service references to
* get
* @return the OSGi service references registered in bundleContext
* with the specified serviceClass if such a services exists there;
* otherwise, an empty Collection
*/
public static Collection> getServiceReferences(
BundleContext bundleContext,
Class serviceClass)
{
Collection> serviceReferences;
try
{
serviceReferences
= bundleContext.getServiceReferences(
serviceClass,
null);
}
catch (InvalidSyntaxException ex)
{
serviceReferences = null;
}
if (serviceReferences == null)
serviceReferences = Collections.emptyList();
return serviceReferences;
}
/** Prevents the creation of ServiceUtils instances. */
private ServiceUtils()
{
}
}