Apache Celix
2.3.0
An implementation of the OSGi specification adapted to C and C++
|
The bundle context is used to interact with the Celix framework. More...
#include <BundleContext.h>
Public Member Functions | |
BundleContext (celix_bundle_context_t *_cCtx) | |
template<typename I , typename Implementer > | |
ServiceRegistrationBuilder< I > | registerService (std::shared_ptr< Implementer > implementer, std::string_view name={}) |
Register a service in the Celix framework using a fluent builder API. More... | |
template<typename I , typename Implementer > | |
ServiceRegistrationBuilder< I > | registerUnmanagedService (Implementer *svc, std::string_view name={}) |
Register a (unmanaged) service in the Celix framework using a fluent builder API. More... | |
template<typename I > | |
UseServiceBuilder< I > | useService (std::string_view name={}) |
Use a service registered in the Celix framework using a fluent builder API. More... | |
template<typename I > | |
UseServiceBuilder< I > | useServices (std::string_view name={}) |
Use services registered in the Celix framework using a fluent builder API. More... | |
template<typename I > | |
long | findService (std::string_view filter={}, std::string_view versionRange={}) |
Finds the highest ranking service using the optional provided (LDAP) filter and version range. More... | |
long | findServiceWithName (std::string_view name, std::string_view filter={}, std::string_view versionRange={}) |
Finds the highest ranking service using the provided service name and the optional (LDAP) filter and version range. More... | |
template<typename I > | |
std::vector< long > | findServices (std::string_view filter={}, std::string_view versionRange={}) |
Finds all services matching the optional provided (LDAP) filter and version range. More... | |
std::vector< long > | findServicesWithName (std::string_view name, std::string_view filter={}, std::string_view versionRange={}) |
Finds all service matching the provided service name and the optional (LDAP) filter and version range. More... | |
template<typename I > | |
ServiceTrackerBuilder< I > | trackServices (std::string_view name={}) |
Track services in the Celix framework using a fluent builder API. More... | |
ServiceTrackerBuilder< void > | trackAnyServices () |
Track services in the Celix framework using a fluent builder API. More... | |
BundleTrackerBuilder | trackBundles () |
Track bundles in the Celix framework using a fluent builder API. More... | |
template<typename I > | |
MetaTrackerBuilder | trackServiceTrackers (std::string_view name={}) |
Track service trackers in the Celix framework using a fluent builder API. More... | |
MetaTrackerBuilder | trackAnyServiceTrackers () |
Track service trackers in the Celix framework using a fluent builder API. More... | |
long | installBundle (std::string_view bndLocation, bool autoStart=true) |
Install and optional start a bundle. More... | |
bool | uninstallBundle (long bndId) |
Uninstall the bundle with the provided bundle id. More... | |
bool | startBundle (long bndId) |
Start the bundle with the provided bundle id. More... | |
bool | stopBundle (long bndId) |
Stop the bundle with the provided bundle id. More... | |
std::vector< long > | listBundleIds () const |
List the installed and started bundle ids. The bundle ids does not include the framework bundle (bundle id CELIX_FRAMEWORK_BUNDLE_ID). More... | |
std::vector< long > | listInstalledBundleIds () |
List the installed bundle ids. The bundle ids does not include the framework bundle (bundle id CELIX_FRAMEWORK_BUNDLE_ID). More... | |
std::string | getConfigProperty (std::string_view name, std::string_view defaultValue) const |
Gets the config property for the provided name. More... | |
long | getConfigPropertyAsLong (std::string_view name, long defaultValue) const |
Gets the config property for the provided name and returns it as a long. More... | |
double | getConfigPropertyAsDouble (std::string_view name, double defaultValue) const |
Gets the config property for the provided name and returns it as a double. More... | |
long | getConfigPropertyAsBool (std::string_view name, bool defaultValue) const |
Gets the config property for the provided name and returns it as a bool. More... | |
const Bundle & | getBundle () const |
Get the bundle of this bundle context. More... | |
long | getBundleId () const |
Get the bundle id for the bundle of this bundle context. More... | |
std::shared_ptr< Framework > | getFramework () const |
Get the Celix framework for this bundle context. More... | |
std::shared_ptr< dm::DependencyManager > | getDependencyManager () const |
Get the Celix dependency manager for this bundle context. More... | |
celix_bundle_context_t * | getCBundleContext () const |
Get the C bundle context. More... | |
void | logTrace (const char *format...) |
Logs a message to the Celix framework logger using the TRACE log level. More... | |
void | logDebug (const char *format...) |
Logs a message to the Celix framework logger using the DEBUG log level. More... | |
void | logInfo (const char *format...) |
Logs a message to the Celix framework logger using the INFO log level. More... | |
void | logWarn (const char *format...) |
Logs a message to the Celix framework logger using the WARNING log level. More... | |
void | logError (const char *format...) |
Logs a message to the Celix framework logger using the ERROR log level. More... | |
void | logFatal (const char *format...) |
Logs a message to the Celix framework logger using the FATAL log level. More... | |
void | waitForEvents () const |
Wait until all Celix events for this bundle are completed. More... | |
void | waitIfAbleForEvents () const |
Wait (if not on the Celix event thread) until all Celix events for this bundle are completed. More... | |
void | waitForAllEvents () const |
Wait until all Celix events (for all bundles) are completed. More... | |
void | waitIfAbleForAllEvents () const |
Wait (if not on the Celix event thread) until all Celix events (for all bundles) are completed. More... | |
The bundle context is used to interact with the Celix framework.
The bundle context represent a bundle and can be used to:
std::string_view
values must be null terminated strings.
|
inlineexplicit |
|
inline |
Finds the highest ranking service using the optional provided (LDAP) filter and version range.
Uses celix::typeName<I> to defer the service name.
I | the service type to found. |
filter | An optional LDAP filter. |
versionRange | An optional version range. |
|
inline |
Finds all services matching the optional provided (LDAP) filter and version range.
Note uses celix::typeName<I> to defer the service name.
I | the service type to found. |
filter | An optional LDAP filter. |
versionRange | An optional version range. |
|
inline |
Finds all service matching the provided service name and the optional (LDAP) filter and version range.
The | service name. (Can be empty to find service with any name). |
filter | An optional LDAP filter. |
versionRange | An optional version range. |
|
inline |
Finds the highest ranking service using the provided service name and the optional (LDAP) filter and version range.
The | service name. (Can be empty to find service with any name). |
filter | An optional LDAP filter. |
versionRange | An optional version range. |
|
inline |
Get the bundle of this bundle context.
|
inline |
Get the bundle id for the bundle of this bundle context.
|
inline |
Get the C bundle context.
|
inline |
Gets the config property for the provided name.
First the provided name will be used to lookup an environment variable of that name. If this is not found, the config properties of the Celix framework will be used (config.properties). Note that by design this means that environment variable can be used to override config properties.
name | The name of the property to receive. |
defaultVal | The default value to use if the property is not found. |
|
inline |
Gets the config property for the provided name and returns it as a bool.
First the provided name will be used to lookup an environment variable of that name. If no this is not found or not a valid bool, the config properties of the Celix framework will be used (config.properties). Note that by design this means that environment variable can be used to override config properties.
Valid bools are "true" and "false" (case insensitive)
name | The name of the property to receive. |
defaultVal | The default value to use if the property is not found. |
|
inline |
Gets the config property for the provided name and returns it as a double.
First the provided name will be used to lookup an environment variable of that name. If no this is not found or not a valid double, the config properties of the Celix framework will be used (config.properties). Note that by design this means that environment variable can be used to override config properties.
name | The name of the property to receive. |
defaultVal | The default value to use if the property is not found. |
|
inline |
Gets the config property for the provided name and returns it as a long.
First the provided name will be used to lookup an environment variable of that name. If no this is not found or not a valid long, the config properties of the Celix framework will be used (config.properties). Note that by design this means that environment variable can be used to override config properties.
name | The name of the property to receive. |
defaultVal | The default value to use if the property is not found. |
|
inline |
Get the Celix dependency manager for this bundle context.
|
inline |
Get the Celix framework for this bundle context.
|
inline |
Install and optional start a bundle.
Will silently ignore bundle ids < 0.
bndLocation | The bundle location to the bundle zip file. |
autoStart | If the bundle should also be started. |
|
inline |
List the installed and started bundle ids. The bundle ids does not include the framework bundle (bundle id CELIX_FRAMEWORK_BUNDLE_ID).
|
inline |
List the installed bundle ids. The bundle ids does not include the framework bundle (bundle id CELIX_FRAMEWORK_BUNDLE_ID).
|
inline |
Logs a message to the Celix framework logger using the DEBUG log level.
|
inline |
Logs a message to the Celix framework logger using the ERROR log level.
|
inline |
Logs a message to the Celix framework logger using the FATAL log level.
|
inline |
Logs a message to the Celix framework logger using the INFO log level.
|
inline |
Logs a message to the Celix framework logger using the TRACE log level.
|
inline |
Logs a message to the Celix framework logger using the WARNING log level.
|
inline |
Register a service in the Celix framework using a fluent builder API.
The service registration can be fine tuned using the returned ServiceRegistrationBuilder API.
std::shared_ptr<celix::BundleContext> ctx = ... auto svcReg = ctx->registerService<IExample>(std::make_shared<ExampleImpl>()) .setVersion("1.0.0") .addProperty("key1", "value1") .addOnRegistered([](const std::shared_ptr<celix::ServiceRegistration>& reg) { std::cout << "Done registering service '" << reg->getServiceName() << "' with id " << reg->getServiceId() << std::endl; }) .build();
By default the service registration is configure to register and unregister the service async.
I | The service type (Note should be the abstract interface, not the interface implementer) |
Implementer | The service implementer. |
implementer | The service implementer. |
name | The optional name of the service. If not provided celix::typeName<I> will be used to defer the service name. |
|
inline |
Register a (unmanaged) service in the Celix framework using a fluent builder API.
Same as registerService, but then with an unmanaged service pointer. Note that the user is responsible for ensuring that the service pointer is valid as long as the service is registered in the Celix framework.
By default the service registration is configure to register the service async, but to unregister the service sync (because the svc pointer is unmanaged).
|
inline |
Start the bundle with the provided bundle id.
Will silently ignore bundle ids < 0.
bndId | The bundle id to start. |
|
inline |
Stop the bundle with the provided bundle id.
Will silently ignore bundle ids < 0.
bndId | The bundle id to stop. |
|
inline |
Track services in the Celix framework using a fluent builder API.
Same as trackerService, but than for any service. Note that the service shared ptr is of the type std::shared_ptr<void>.
|
inline |
Track service trackers in the Celix framework using a fluent builder API.
Same as trackServiceTrackers, but than for service tracker for any service types.
|
inline |
Track bundles in the Celix framework using a fluent builder API.
The bundle tracker can be fine tuned using the returned BundleTrackerBuilder API.
std::shared_ptr<celix::BundleContext> ctx = ... auto tracker = ctx->trackBundles<>() .addOnInstallCallback([](const celix::Bundle& bnd) { std::cout << "Bundle installed with id '" << bnd.getId() << std::endl; }) .build();
|
inline |
Track services in the Celix framework using a fluent builder API.
The service tracker can be fine tuned using the returned ServiceTrackerBuilder API.
std::shared_ptr<celix::BundleContext> ctx = ... auto tracker = ctx->trackServices<IExample>() .setFilter("(property_key=value)") .addAddCallback([](std::shared_ptr<IExample>, std::shared_ptr<const celix::Properties> props) { std::cout << "Adding service with id '" << props->get("service.id", "-1") << std::endl; }) .addRemCallback([](std::shared_ptr<IExample>, std::shared_ptr<const celix::Properties> props) { std::cout << "Removing service with id '" << props->get("service.id", "-1") << std::endl; }) .build();
I | The service type to track |
name | The optional service name. If empty celix::typeName<I> will be used to defer the service name. |
|
inline |
Track service trackers in the Celix framework using a fluent builder API.
The meta tracker (service tracker tracker) can be fine tuned using the returned MetaTrackerBuilder API.
std::shared_ptr<celix::BundleContext> ctx = ... auto tracker = ctx->trackServiceTrackers<IExample>() .addOnTrackerCreatedCallback([](const ServiceTrackerInfo& info) { std::cout << "Tracker created for service name '" << info.serviceName << std::endl; }) .addOnTrackerDestroyedCallback([](const ServiceTrackerInfo& info) { std::cout << "Tracker destroyed for service name '" << info.serviceName << std::endl; }) .build();
I | The service tracker service type to track. |
name | The optional service name. If empty celix::typeName<I> will be used to defer the service name. |
|
inline |
Uninstall the bundle with the provided bundle id.
If needed the bundle will be stopped first. Will silently ignore bundle ids < 0.
bndId | The bundle id to uninstall. |
|
inline |
Use a service registered in the Celix framework using a fluent builder API.
The service use can be fine tuned using the returned UseServiceBuilder API.
With this API a Celix service can be used by providing use functions. The use function will be executed on the Celix event thread and the Celix framework will ensure that the service cannot be removed while in use.
If there are more 1 matching service, the highest ranking service will be used. If no service can be found the use callbacks with not be called.
std::shared_ptr<celix::BundleContext> ctx = ... auto callCount = ctx->useService<IExample>() .setTimeout(std::chrono::seconds{1}) .addUseCallback([](IExample& service, const celix::Properties& props){ std::cout << "Calling service with id " << props.get("service.id", "-1") << std::endl; service.method(); }) .build();
I | The service type to use |
name | The optional service name to use. If not provided celix::typeName<I> will be used to defer the service name. |
|
inline |
Use services registered in the Celix framework using a fluent builder API.
The service use can be fine tuned using the returned UseServiceBuilder API.
With this API Celix services can be used by providing use functions. The use function will be executed on the Celix event thread and the Celix framework will ensure that the service cannot be removed while in use.
The use callbacks will be called for every matching service found.
std::shared_ptr<celix::BundleContext> ctx = ... auto callCount = ctx->useServices<IExample>() .addUseCallback([](IExample& service, const celix::Properties& props){ std::cout << "Calling service with id " << props.get("service.id", "-1") << std::endl; service.method(); }) .build();
I | The service type to use |
name | The optional service name to use. If not provided celix::typeName<I> will be used to defer the service name. |
|
inline |
Wait until all Celix events (for all bundles) are completed.
|
inline |
Wait until all Celix events for this bundle are completed.
|
inline |
Wait (if not on the Celix event thread) until all Celix events (for all bundles) are completed.
|
inline |
Wait (if not on the Celix event thread) until all Celix events for this bundle are completed.