ComponentContext

class cppmicroservices::service::component::ComponentContext
#include <cppmicroservices/servicecomponent/ComponentContext.hpp>

A Component Context object is used by a component instance to interact with its execution context including locating services by reference name.

Each component instance has a unique Component Context.

A component instance may obtain its Component Context object through its activate, modified, and deactivate methods.

Public Functions

virtual ~ComponentContext()
virtual std::unordered_map<std::string, cppmicroservices::Any> GetProperties() const = 0

Returns the component properties for this Component Context.

Return
The properties for this Component Context.

virtual cppmicroservices::BundleContext GetBundleContext() const = 0

Returns the BundleContext of the bundle which contains this component.

Return
The BundleContext of the bundle containing this component.

virtual cppmicroservices::Bundle GetUsingBundle() const = 0

If the component instance is registered as a service using the servicescope="bundle" or servicescope="prototype" attribute, then this method returns the bundle using the service provided by the component instance.

This method will return an invalid Bundle if:

  • The component instance is not a service, then no bundle can be using it as a service.
  • The component instance is a service but did not specify the servicescope="bundle" or servicescope="prototype" attribute, then all bundles using the service provided by the component instance will share the same component instance.
  • The service provided by the component instance is not currently being used by any bundle.

Return
The bundle using the component instance as a service or an invalid bundle.

virtual void EnableComponent(const std::string &name) = 0

Enables the specified component name.

The specified component name must be in the same bundle as this component.

This method must return after changing the enabled state of the specified component name. Any actions that result from this, such as activating or deactivating a component configuration, must occur asynchronously to this method call.

Parameters
  • name: The name of a component or empty string to indicate all components in the bundle.

virtual void DisableComponent(const std::string &name) = 0

Disables the specified component name.

The specified component name must be in the same bundle as this component.

This method must return after changing the enabled state of the specified component name. Any actions that result from this, such as activating or deactivating a component configuration, must occur asynchronously to this method call.

Parameters
  • name: The name of a component.

virtual cppmicroservices::ServiceReferenceBase GetServiceReference() const = 0

If the component instance is registered as a service using the service element, then this method returns the service reference of the service provided by this component instance.

This method will return an invalid ServiceReference object if the component instance is not registered as a service.

Return
The ServiceReference object for the component instance or invalid object if the component instance is not registered as a service.

template <class T>
std::shared_ptr<T> LocateService(const std::string &refName) const

Returns the service object for the specified reference name and type.

If the cardinality of the reference is 0..n or 1..n and multiple services are bound to the reference, the service with the highest ranking (as specified in its Constants::SERVICE_RANKING property) is returned. If there is a tie in ranking, the service with the lowest service id (as specified in its Constants::SERVICE_ID property); that is, the service that was registered first is returned.

Return
A service object for the referenced service or nullptr if the reference cardinality is 0..1 or 0..n and no bound service is available.
Parameters
  • refName: The name of a reference as specified in a reference element in this component’s description.
Exceptions
  • ComponentException: If Service Component Runtime catches an exception while activating the bound service.

template <class T>
std::vector<std::shared_ptr<T>> LocateServices(const std::string &refName) const

Returns the service objects for the specified reference name and type.

Return
A vector of service objects for the referenced service or empty vector if the reference cardinality is 0..1 or 0..n and no bound service is available. If the reference cardinality is 0..1 or 1..1 and a bound service is available, the vector will have exactly one element.
Parameters
  • refName: The name of a reference as specified in a reference element in this component’s description.
Exceptions
  • ComponentException: If Service Component Runtime catches an exception while activating a bound service.

Protected Functions

virtual std::shared_ptr<void> LocateService(const std::string &name, const std::string &type) const = 0

Returns the service object for the specified reference name and type.

If the cardinality of the reference is 0..n or 1..n and multiple services are bound to the reference, the service with the highest ranking (as specified in its Constants::SERVICE_RANKING property) is returned. If there is a tie in ranking, the service with the lowest service id (as specified in its Constants::SERVICE_ID property); that is, the service that was registered first is returned.

Return
A service object for the referenced service or nullptr if the reference cardinality is 0..1 or 0..n and no bound service is available.
Parameters
  • name: The name of a reference as specified in a reference element in this component’s description.
  • type: The service interface type.
Exceptions
  • ComponentException: If Service Component Runtime catches an exception while activating the bound service.

virtual std::vector<std::shared_ptr<void>> LocateServices(const std::string &name, const std::string &type) const = 0

Returns the service objects for the specified reference name and type.

Return
A vector of service objects for the referenced service or empty vector if the reference cardinality is 0..1 or 0..n and no bound service is available. If the reference cardinality is 0..1 or 1..1 and a bound service is available, the vector will have exactly one element.
Parameters
  • name: The name of a reference as specified in a reference element in this component’s description.
  • type: The service interface type.
Exceptions
  • ComponentException: If Service Component Runtime catches an exception while activating a bound service.