ServiceTracker¶
- template <class S, class T = S>
-
class
cppmicroservices::
ServiceTracker
¶ - #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 aServiceTrackerCustomizer
object. AServiceTracker
can use aServiceTrackerCustomizer
to customize the service objects to be tracked. TheServiceTracker
can then be opened to begin tracking all services in the framework’s service registry that match the specified search criteria. TheServiceTracker
correctly handles all of the details of listening toServiceEvent
s and getting and ungetting services.The
GetServiceReferences
method can be called to get references to the services being tracked. TheGetService
andGetServices
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 likea custom ServiceTrackerCustomizer is required. It provides code to associate the tracked service with the custom tracked type:struct MyTrackedClass { explicit operator bool() const { return true; } /* ... */ };
Instantiation of a ServiceTracker with the custom customizer looks like this:struct MyTrackingCustomizer : public ServiceTrackerCustomizer<IFooService, MyTrackedClass> { virtual std::shared_ptr<MyTrackedClass> AddingService( const ServiceReference<IFooService>&) { return std::shared_ptr<MyTrackedClass>(); } virtual void ModifiedService(const ServiceReference<IFooService>&, const std::shared_ptr<MyTrackedClass>&) {} virtual void RemovedService(const ServiceReference<IFooService>&, const std::shared_ptr<MyTrackedClass>&) {} };
MyTrackingCustomizer myCustomizer; ServiceTracker<IFooService, MyTrackedClass> tracker(GetBundleContext(), &myCustomizer);
- Note
- The
ServiceTracker
class is thread-safe. It does not call aServiceTrackerCustomizer
while holding any locks.ServiceTrackerCustomizer
implementations must also be thread-safe. - Remark
- This class is thread safe.
- Template Parameters
S
: The type of the service being tracked. The type S* must be an assignable datatype.T
: The tracked object.
Inherits from cppmicroservices::ServiceTrackerCustomizer< S, T >
Public Types
-
template<>
usingTrackedParamType
= typename ServiceTrackerCustomizer::TrackedParamType¶ The type of the tracked object.
-
template<>
usingTrackingMap
= std::unordered_map<ServiceReference<S>, std::shared_ptr<TrackedParamType>>¶
Public Functions
-
~ServiceTracker
()¶ Automatically closes the
ServiceTracker
-
ServiceTracker
(const BundleContext &context, const ServiceReference<S> &reference, ServiceTrackerCustomizer<S, T> *customizer = nullptr)¶ Create a
ServiceTracker
on the specifiedServiceReference
.The service referenced by the specified
ServiceReference
will be tracked by thisServiceTracker
.- Parameters
context
: TheBundleContext
against which the tracking is done.reference
: TheServiceReference
for the service to be tracked.customizer
: The customizer object to call when services are added, modified, or removed in thisServiceTracker
. If customizer isnull
, then thisServiceTracker
will be used as theServiceTrackerCustomizer
and thisServiceTracker
will call theServiceTrackerCustomizer
methods on itself.
-
ServiceTracker
(const BundleContext &context, const std::string &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
context
: TheBundleContext
against which the tracking is done.clazz
: The class name of the services to be tracked.customizer
: The customizer object to call when services are added, modified, or removed in thisServiceTracker
. If customizer isnull
, then thisServiceTracker
will be used as theServiceTrackerCustomizer
and thisServiceTracker
will call theServiceTrackerCustomizer
methods on itself.
-
ServiceTracker
(const BundleContext &context, const LDAPFilter &filter, ServiceTrackerCustomizer<S, T> *customizer = nullptr)¶ Create a
ServiceTracker
on the specifiedLDAPFilter
object.Services which match the specified
LDAPFilter
object will be tracked by thisServiceTracker
.- Parameters
context
: TheBundleContext
against which the tracking is done.filter
: TheLDAPFilter
to select the services to be tracked.customizer
: The customizer object to call when services are added, modified, or removed in thisServiceTracker
. If customizer is null, then thisServiceTracker
will be used as theServiceTrackerCustomizer
and thisServiceTracker
will call theServiceTrackerCustomizer
methods on itself.
-
ServiceTracker
(const BundleContext &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
context
: TheBundleContext
against which the tracking is done.customizer
: The customizer object to call when services are added, modified, or removed in thisServiceTracker
. If customizer is null, then thisServiceTracker
will be used as theServiceTrackerCustomizer
and thisServiceTracker
will call theServiceTrackerCustomizer
methods on itself.
- Exceptions
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 thisServiceTracker
.- Exceptions
std::runtime_error
: If theBundleContext
with which thisServiceTracker
was created is no longer valid.std::runtime_error
: If the LDAP filter used to construct theServiceTracker
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.
- Exceptions
std::runtime_error
: If theBundleContext
with which thisServiceTracker
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 theBundleActivator
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.
- Return
- The result of GetService().
- template <class Rep, class Period>
-
std::shared_ptr<TrackedParamType>
WaitForService
(const std::chrono::duration<Rep, Period> &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 theBundleActivator
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.
- Return
- The result of GetService().
- Parameters
rel_time
: The relative time duration to wait for a service. If zero, the method will wait indefinitely.
- Exceptions
std::invalid_argument
: exception ifrel_time
is negative.
-
virtual std::vector<ServiceReference<S>>
GetServiceReferences
() const¶ Return a list of
ServiceReference
s for all services being tracked by thisServiceTracker
.- Return
- A list of
ServiceReference
objects.
-
virtual ServiceReference<S>
GetServiceReference
() const¶ Returns a
ServiceReference
for one of the services being tracked by thisServiceTracker
.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 itsservice.id
property); that is, the service that was registered first is returned. This is the same algorithm used byBundleContext::GetServiceReference()
.This implementation calls GetServiceReferences() to get the list of references for the tracked services.
- Return
- A
ServiceReference
for a tracked service. - Exceptions
ServiceException
: if no services are being tracked.
-
virtual std::shared_ptr<TrackedParamType>
GetService
(const ServiceReference<S> &reference) const¶ Returns the service object for the specified
ServiceReference
if the specified referenced service is being tracked by thisServiceTracker
.- Return
- A service object or
nullptr
if the service referenced by the specifiedServiceReference
is not being tracked. - Parameters
reference
: The reference to the desired service.
-
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.
- Return
- 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())
.- Return
- A service object or
null
if no services are being tracked.
-
virtual void
Remove
(const ServiceReference<S> &reference)¶ Remove a service from this
ServiceTracker
.The specified service will be removed from this
ServiceTracker
. If the specified service was being tracked then theServiceTrackerCustomizer::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
.- Return
- 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 thisServiceTracker
, 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 thisServiceTracker
since the previous tracking count was collected.- Return
- The tracking count for this
ServiceTracker
or -1 if thisServiceTracker
is not open.
-
virtual void
GetTracked
(TrackingMap &tracked) const¶ Return a sorted map of the
ServiceReference
s and service objects for all services being tracked by thisServiceTracker
.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
: ATrackingMap
with theServiceReference
s and service objects for all services being tracked by thisServiceTracker
. If no services are being tracked, then the returned map is empty.
-
virtual bool
IsEmpty
() const¶ Return if this
ServiceTracker
is empty.- Return
true
if thisServiceTracker
is not tracking any services.
Protected Functions
-
std::shared_ptr<TrackedParamType>
AddingService
(const ServiceReference<S> &reference)¶ Default implementation of the
ServiceTrackerCustomizer::AddingService
method.This method is only called when this
ServiceTracker
has been constructed with anullptr
ServiceTrackerCustomizer argument.This implementation returns the result of calling
GetService
on theBundleContext
with which thisServiceTracker
was created passing the specifiedServiceReference
.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.
- Return
- The service object to be tracked for the service added to this
ServiceTracker
. - See
- ServiceTrackerCustomizer::AddingService(const ServiceReference&)
- Parameters
reference
: The reference to the service being added to thisServiceTracker
.
- Exceptions
std::runtime_error
: If this BundleContext is no longer valid.std::invalid_argument
: If the specifiedServiceReference
is invalid (default constructed).
Default implementation of the
ServiceTrackerCustomizer::ModifiedService
method.This method is only called when this
ServiceTracker
has been constructed with anullptr
ServiceTrackerCustomizer argument.This implementation does nothing.
- See
- ServiceTrackerCustomizer::ModifiedService(const ServiceReference&, TrackedArgType)
- Parameters
reference
: The reference to modified service.service
: The service object for the modified service.
Default implementation of the
ServiceTrackerCustomizer::RemovedService
method.This method is only called when this
ServiceTracker
has been constructed with anullptr
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
- 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
cppmicroservices::
ServiceTrackerCustomizer
¶ - #include <cppmicroservices/ServiceTrackerCustomizer.h>
The
ServiceTrackerCustomizer
interface allows aServiceTracker
to customize the service objects that are tracked.A
ServiceTrackerCustomizer
is called when a service is being added to aServiceTracker
. TheServiceTrackerCustomizer
can then return an object for the tracked service. AServiceTrackerCustomizer
is also called when a tracked service is modified or has been removed from aServiceTracker
.The methods in this interface may be called as the result of a
ServiceEvent
being received by aServiceTracker
. SinceServiceEvent
s 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.- Note
- The
ServiceTracker
class implementation ofServiceTrackerCustomizer
is thread-safe. It does not call aServiceTrackerCustomizer
while holding any locks.ServiceTrackerCustomizer
implementations must also be thread-safe. - Remark
ServiceTrackerCustomizer
implementations must also be thread-safe.- Template Parameters
S
: The type of the service being trackedT
: The type of the tracked object. The default isS
.
Subclassed by cppmicroservices::ServiceTracker< S, T >
Public Types
-
template<>
usingTrackedParamType
= typename TypeTraits::TrackedParamType¶
Public Functions
-
virtual
~ServiceTrackerCustomizer
()¶
-
virtual std::shared_ptr<TrackedParamType>
AddingService
(const ServiceReference<S> &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 theServiceTracker
. This method should return the service object to be tracked for the specifiedServiceReference
. The returned service object is stored in theServiceTracker
and is available from theGetService
andGetServices
methods.- Return
- The service object to be tracked for the specified referenced service or
0
if the specified referenced service should not be tracked. - Parameters
reference
: The reference to the service being added to theServiceTracker
.
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.
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.