ServiceTracker

template<class S, class T = S>
class ServiceTracker : protected cppmicroservices::ServiceTrackerCustomizer<S, S>
#include <cppmicroservices/ServiceTracker.h>

The ServiceTracker class simplifies using services from the framework’s service registry.

A ServiceTracker object is constructed with search criteria and a ServiceTrackerCustomizer object. A ServiceTracker can use a ServiceTrackerCustomizer to customize the service objects to be tracked. The ServiceTracker can then be opened to begin tracking all services in the framework’s service registry that match the specified search criteria. The ServiceTracker correctly handles all of the details of listening to ServiceEvents and getting and ungetting services.

The GetServiceReferences method can be called to get references to the services being tracked. The GetService and GetServices methods can be called to get the service objects for the tracked service.

Customization of the services to be tracked requires the tracked type to be default constructible and convertible to bool. To customize a tracked service using a custom type with value-semantics like

struct MyTrackedClass
{
    explicit operator bool() const { return true; }
    /* ... */
};
a custom ServiceTrackerCustomizer is required. It provides code to associate the tracked service with the custom tracked type:
struct MyTrackingCustomizer : public ServiceTrackerCustomizer<IFooService, MyTrackedClass>
{
    virtual std::shared_ptr<MyTrackedClass>
    AddingService(ServiceReference<IFooService> const&)
    {
        return std::shared_ptr<MyTrackedClass>();
    }

    virtual void
    ModifiedService(ServiceReference<IFooService> const&, std::shared_ptr<MyTrackedClass> const&)
    {
    }

    virtual void
    RemovedService(ServiceReference<IFooService> const&, std::shared_ptr<MyTrackedClass> const&)
    {
    }
};
Instantiation of a ServiceTracker with the custom customizer looks like this:
        MyTrackingCustomizer myCustomizer;
        ServiceTracker<IFooService, MyTrackedClass> tracker(GetBundleContext(), &myCustomizer);

Remark

This class is thread safe.

Note

The ServiceTracker class is thread-safe. It does not call a ServiceTrackerCustomizer while holding any locks. ServiceTrackerCustomizer implementations must also be thread-safe.

Template Parameters:
  • S – The type of the service being tracked. The type S* must be an assignable datatype.

  • T – The tracked object.

Public Types

using TrackedParamType = typename ServiceTrackerCustomizer<S, T>::TrackedParamType

The type of the tracked object.

using TrackingMap = std::unordered_map<ServiceReference<S>, std::shared_ptr<TrackedParamType>>

Public Functions

~ServiceTracker() override

Automatically closes the ServiceTracker

ServiceTracker(BundleContext const &context, ServiceReference<S> const &reference, ServiceTrackerCustomizer<S, T> *customizer = nullptr)

Create a ServiceTracker on the specified ServiceReference.

The service referenced by the specified ServiceReference will be tracked by this ServiceTracker.

Parameters:
ServiceTracker(BundleContext const &context, std::string const &clazz, ServiceTrackerCustomizer<S, T> *customizer = nullptr)

Create a ServiceTracker on the specified class name.

Services registered under the specified class name will be tracked by this ServiceTracker.

Parameters:
ServiceTracker(BundleContext const &context, LDAPFilter const &filter, ServiceTrackerCustomizer<S, T> *customizer = nullptr)

Create a ServiceTracker on the specified LDAPFilter object.

Services which match the specified LDAPFilter object will be tracked by this ServiceTracker.

Parameters:
ServiceTracker(BundleContext const &context, ServiceTrackerCustomizer<S, T> *customizer = nullptr)

Create a ServiceTracker on the class template argument S.

Services registered under the interface name of the class template argument S will be tracked by this ServiceTracker.

Parameters:
Throws:

ServiceException – If the service interface name is empty.

virtual void Open()

Open this ServiceTracker and begin tracking services.

Services which match the search criteria specified when this ServiceTracker was created are now tracked by this ServiceTracker.

Throws:
  • std::runtime_error – If the BundleContext with which this ServiceTracker was created is no longer valid.

  • std::runtime_error – If the LDAP filter used to construct the ServiceTracker contains an invalid filter expression that cannot be parsed.

virtual void Close()

Close this ServiceTracker.

This method should be called when this ServiceTracker should end the tracking of services.

This implementation calls GetServiceReferences() to get the list of tracked services to remove.

Throws:

std::runtime_error – If the BundleContext with which this ServiceTracker was created is no longer valid.

std::shared_ptr<TrackedParamType> WaitForService()

Wait for at least one service to be tracked by this ServiceTracker.

This method will also return when this ServiceTracker is closed.

It is strongly recommended that WaitForService is not used during the calling of the BundleActivator methods. BundleActivator methods are expected to complete in a short period of time.

This implementation calls GetService() to determine if a service is being tracked.

Throws:

std::logic_error – If the BundleContext with which this ServiceTracker was created is no longer valid or became invalid while waiting on Service.

Returns:

The result of GetService().

template<class Rep, class Period>
std::shared_ptr<TrackedParamType> WaitForService(std::chrono::duration<Rep, Period> const &rel_time)

Wait for at least one service to be tracked by this ServiceTracker.

This method will also return when this ServiceTracker is closed.

It is strongly recommended that WaitForService is not used during the calling of the BundleActivator methods. BundleActivator methods are expected to complete in a short period of time.

This implementation calls GetService() to determine if a service is being tracked.

Parameters:

rel_time – The relative time duration to wait for a service. If zero, the method will wait indefinitely.

Throws:
  • std::invalid_argument – exception if rel_time is negative.

  • std::logic_error – If the BundleContext with which this ServiceTracker was created is no longer valid or became invalid while waiting on Service.

Returns:

The result of GetService().

virtual std::vector<ServiceReference<S>> GetServiceReferences() const

Return a list of ServiceReferences for all services being tracked by this ServiceTracker.

Returns:

A list of ServiceReference objects.

virtual ServiceReference<S> GetServiceReference() const

Returns a ServiceReference for one of the services being tracked by this ServiceTracker.

If multiple services are being tracked, the service with the highest ranking (as specified in its service.ranking property) is returned. If there is a tie in ranking, the service with the lowest service ID (as specified in its service.id property); that is, the service that was registered first is returned. This is the same algorithm used by BundleContext::GetServiceReference().

This implementation calls GetServiceReferences() to get the list of references for the tracked services.

Throws:

ServiceException – if no services are being tracked.

Returns:

A ServiceReference for a tracked service.

virtual std::shared_ptr<TrackedParamType> GetService(ServiceReference<S> const &reference) const

Returns the service object for the specified ServiceReference if the specified referenced service is being tracked by this ServiceTracker.

Parameters:

reference – The reference to the desired service.

Returns:

A service object or nullptr if the service referenced by the specified ServiceReference is not being tracked.

virtual std::vector<std::shared_ptr<TrackedParamType>> GetServices() const

Return a list of service objects for all services being tracked by this ServiceTracker.

This implementation calls GetServiceReferences() to get the list of references for the tracked services and then calls GetService(const ServiceReference&) for each reference to get the tracked service object.

Returns:

A list of service objects or an empty list if no services are being tracked.

virtual std::shared_ptr<TrackedParamType> GetService() const

Returns a service object for one of the services being tracked by this ServiceTracker.

If any services are being tracked, this implementation returns the result of calling GetService(GetServiceReference()).

Returns:

A service object or null if no services are being tracked.

virtual void Remove(ServiceReference<S> const &reference)

Remove a service from this ServiceTracker.

The specified service will be removed from this ServiceTracker. If the specified service was being tracked then the ServiceTrackerCustomizer::RemovedService method will be called for that service.

Parameters:

reference – The reference to the service to be removed.

virtual int Size() const

Return the number of services being tracked by this ServiceTracker.

Returns:

The number of services being tracked.

virtual int GetTrackingCount() const

Returns the tracking count for this ServiceTracker.

The tracking count is initialized to 0 when this ServiceTracker is opened. Every time a service is added, modified or removed from this ServiceTracker, the tracking count is incremented.

The tracking count can be used to determine if this ServiceTracker has added, modified or removed a service by comparing a tracking count value previously collected with the current tracking count value. If the value has not changed, then no service has been added, modified or removed from this ServiceTracker since the previous tracking count was collected.

Returns:

The tracking count for this ServiceTracker or -1 if this ServiceTracker is not open.

virtual void GetTracked(TrackingMap &tracked) const

Return a sorted map of the ServiceReferences and service objects for all services being tracked by this ServiceTracker.

The map is sorted in natural order of ServiceReference. That is, the last entry is the service with the highest ranking and the lowest service id.

Parameters:

tracked – A TrackingMap with the ServiceReferences and service objects for all services being tracked by this ServiceTracker. If no services are being tracked, then the returned map is empty.

virtual bool IsEmpty() const

Return if this ServiceTracker is empty.

Returns:

true if this ServiceTracker is not tracking any services.

template<class Rep, class Period>
std::shared_ptr<typename ServiceTracker<S, T>::TrackedParamType> WaitForService(std::chrono::duration<Rep, Period> const &rel_time)

Protected Functions

virtual std::shared_ptr<TrackedParamType> AddingService(ServiceReference<S> const &reference) override

Default implementation of the ServiceTrackerCustomizer::AddingService method.

This method is only called when this ServiceTracker has been constructed with a nullptr ServiceTrackerCustomizer argument.

This implementation returns the result of calling GetService on the BundleContext with which this ServiceTracker was created passing the specified ServiceReference.

This method can be overridden in a subclass to customize the service object to be tracked for the service being added. In that case, take care not to rely on the default implementation of RemovedService to unget the service.

Parameters:

reference – The reference to the service being added to this ServiceTracker.

Throws:
  • std::runtime_error – If this BundleContext is no longer valid.

  • std::invalid_argument – If the specified ServiceReference is invalid (default constructed).

Returns:

The service object to be tracked for the service added to this ServiceTracker.

void ModifiedService(ServiceReference<S> const &reference, std::shared_ptr<TrackedParamType> const &service) override

Default implementation of the ServiceTrackerCustomizer::ModifiedService method.

This method is only called when this ServiceTracker has been constructed with a nullptr ServiceTrackerCustomizer argument.

This implementation does nothing.

See also

ServiceTrackerCustomizer::ModifiedService(const ServiceReference&, TrackedArgType)

Parameters:
  • reference – The reference to modified service.

  • service – The service object for the modified service.

void RemovedService(ServiceReference<S> const &reference, std::shared_ptr<TrackedParamType> const &service) override

Default implementation of the ServiceTrackerCustomizer::RemovedService method.

This method is only called when this ServiceTracker has been constructed with a nullptr ServiceTrackerCustomizer argument.

This method can be overridden in a subclass. If the default implementation of the AddingService method was used, this method must unget the service.

See also

ServiceTrackerCustomizer::RemovedService(const ServiceReferenceType&, TrackedArgType)

Parameters:
  • reference – The reference to removed service.

  • service – The service object for the removed service.

template<class S, class T = S>
struct ServiceTrackerCustomizer
#include <cppmicroservices/ServiceTrackerCustomizer.h>

The ServiceTrackerCustomizer interface allows a ServiceTracker to customize the service objects that are tracked.

A ServiceTrackerCustomizer is called when a service is being added to a ServiceTracker. The ServiceTrackerCustomizer can then return an object for the tracked service. A ServiceTrackerCustomizer is also called when a tracked service is modified or has been removed from a ServiceTracker.

The methods in this interface may be called as the result of a ServiceEvent being received by a ServiceTracker. Since ServiceEvents are synchronously delivered, it is highly recommended that implementations of these methods do not register (BundleContext::RegisterService), modify ( ServiceRegistration::SetProperties) or unregister ( ServiceRegistration::Unregister) a service while being synchronized on any object.

Remark

ServiceTrackerCustomizer implementations must also be thread-safe.

Note

The ServiceTracker class implementation of ServiceTrackerCustomizer is thread-safe. It does not call a ServiceTrackerCustomizer while holding any locks. ServiceTrackerCustomizer implementations must also be thread-safe.

Template Parameters:
  • S – The type of the service being tracked

  • T – The type of the tracked object. The default is S.

Public Types

using TrackedParamType = typename TypeTraits::TrackedParamType

Public Functions

virtual ~ServiceTrackerCustomizer() = default
virtual std::shared_ptr<TrackedParamType> AddingService(ServiceReference<S> const &reference) = 0

A service is being added to the ServiceTracker.

This method is called before a service which matched the search parameters of the ServiceTracker is added to the ServiceTracker. This method should return the service object to be tracked for the specified ServiceReference. The returned service object is stored in the ServiceTracker and is available from the GetService and GetServices methods.

Parameters:

reference – The reference to the service being added to the ServiceTracker.

Returns:

The service object to be tracked for the specified referenced service or 0 if the specified referenced service should not be tracked.

virtual void ModifiedService(ServiceReference<S> const &reference, std::shared_ptr<TrackedParamType> const &service) = 0

A service tracked by the ServiceTracker has been modified.

This method is called when a service being tracked by the ServiceTracker has had it properties modified.

Parameters:
  • reference – The reference to the service that has been modified.

  • service – The service object for the specified referenced service.

virtual void RemovedService(ServiceReference<S> const &reference, std::shared_ptr<TrackedParamType> const &service) = 0

A service tracked by the ServiceTracker has been removed.

This method is called after a service is no longer being tracked by the ServiceTracker.

Parameters:
  • reference – The reference to the service that has been removed.

  • service – The service object for the specified referenced service.

struct TypeTraits
#include <cppmicroservices/ServiceTrackerCustomizer.h>

Public Types

using ServiceType = S
using TrackedType = T
using TrackedParamType = T

Public Static Functions

static inline std::shared_ptr<TrackedType> ConvertToTrackedType(std::shared_ptr<S> const&)