Bundle¶
-
std::ostream &operator<<(std::ostream &os, Bundle const &bundle)¶
Streams a textual representation of
bundle
into the streamos
.
-
std::ostream &operator<<(std::ostream &os, Bundle const *bundle)¶
This is the same as calling
os << *bundle
.
-
std::ostream &operator<<(std::ostream &os, Bundle::State state)¶
Streams a textual representation of the bundle state enumeration.
-
class Bundle¶
- #include <cppmicroservices/Bundle.h>
An installed bundle in the Framework.
A
Bundle
object is the access point to define the lifecycle of an installed bundle. Each bundle installed in the CppMicroServices environment has an associatedBundle
object.A bundle has a unique identity, a
long
, chosen by the Framework. This identity does not change during the lifecycle of a bundle. Uninstalling and then reinstalling the bundle creates a new unique identity.A bundle can be in one of six states:
STATE_UNINSTALLED
STATE_INSTALLED
STATE_RESOLVED
STATE_STARTING
STATE_STOPPING
STATE_ACTIVE
Values assigned to these states have no specified ordering; they represent bit values that may be ORed together to determine if a bundle is in one of the valid states.
A bundle should only have active threads of execution when its state is one of
STATE_STARTING
,STATE_ACTIVE
, orSTATE_STOPPING
. ASTATE_UNINSTALLED
bundle can not be set to another state; it is a zombie and can only be reached because references are kept somewhere.The framework is the only entity that is allowed to create
Bundle
objects, and these objects are only valid within the Framework that created them.Bundles have a natural ordering such that if two
Bundles
have the samebundle id
they are equal. ABundle
is less than anotherBundle
if it has a lowerbundle id
and is greater if it has a higher bundle id.Remark
This class is thread safe.
Subclassed by cppmicroservices::Framework
Public Types
-
enum State¶
The bundle state.
Values:
-
enumerator STATE_UNINSTALLED¶
The bundle is uninstalled and may not be used.
The
STATE_UNINSTALLED
state is only visible after a bundle is uninstalled; the bundle is in an unusable state but references to theBundle
object may still be available and used for introspection.The value of
STATE_UNINSTALLED
is 0x00000001.
-
enumerator STATE_INSTALLED¶
The bundle is installed but not yet resolved.
A bundle is in the
STATE_INSTALLED
state when it has been installed in the Framework but is not or cannot be resolved.This state is visible if the bundle’s code dependencies are not resolved. The Framework may attempt to resolve a
STATE_INSTALLED
bundle’s code dependencies and move the bundle to theSTATE_RESOLVED
state.The value of
STATE_INSTALLED
is 0x00000002.
-
enumerator STATE_RESOLVED¶
The bundle is resolved and is able to be started.
A bundle is in the
STATE_RESOLVED
state when the Framework has successfully resolved the bundle’s code dependencies. These dependencies include:None (this may change in future versions)
Note that the bundle is not active yet. A bundle is put in the
STATE_RESOLVED
state before it can be started. The Framework may attempt to resolve a bundle at any time.The value of
STATE_RESOLVED
is 0x00000004.
-
enumerator STATE_STARTING¶
The bundle is in the process of starting.
A bundle is in the
STATE_STARTING
state when itsStart
method is active. A bundle must be in this state when the bundle’s BundleActivator::Start(BundleContext) is called. If theBundleActivator::Start
method completes without exception, then the bundle has successfully started and moves to theSTATE_ACTIVE
state.If the bundle has a
lazy activation policy
, then the bundle may remain in this state for some time until the activation is triggered.The value of
STATE_STARTING
is 0x00000008.
-
enumerator STATE_STOPPING¶
The bundle is in the process of stopping.
A bundle is in the
STATE_STOPPING
state when itsStop
method is active. A bundle is in this state when the bundle’sBundleActivator#Stop(BundleContext)
method is called. When theBundleActivator::Stop
method completes the bundle is stopped and moves to theSTATE_RESOLVED
state.The value of
STATE_STOPPING
is 0x00000010.
-
enumerator STATE_ACTIVE¶
The bundle is now running.
A bundle is in the
STATE_ACTIVE
state when it has been successfully started and activated.The value of
STATE_ACTIVE
is 0x00000020.
-
enumerator STATE_UNINSTALLED¶
-
enum StartOptions¶
Values:
-
enumerator START_TRANSIENT¶
The bundle start operation is transient and the persistent autostart setting of the bundle is not modified.
This bit may be set when calling
Start(uint32_t)
to notify the framework that the autostart setting of the bundle must not be modified. If this bit is not set, then the autostart setting of the bundle is modified.See also
Note
This option is reserved for future use and not supported yet.
-
enumerator START_ACTIVATION_POLICY¶
The bundle start operation must activate the bundle according to the bundle’s declared
activation policy
.This bit may be set when calling
Start(uint32_t)
to notify the framework that the bundle must be activated using the bundle’s declared activation policy.See also
See also
Note
This option is reserved for future use and not supported yet.
-
enumerator START_TRANSIENT¶
-
enum StopOptions¶
Values:
-
enumerator STOP_TRANSIENT¶
The bundle stop is transient and the persistent autostart setting of the bundle is not modified.
This bit may be set when calling
Stop(uint32_t)
to notify the framework that the autostart setting of the bundle must not be modified. If this bit is not set, then the autostart setting of the bundle is modified.See also
Note
This option is reserved for future use and not supported yet.
-
enumerator STOP_TRANSIENT¶
-
using TimeStamp = std::chrono::steady_clock::time_point¶
Public Functions
-
Bundle()¶
Constructs an invalid Bundle object.
Valid bundle objects can only be created by the framework in response to certain calls to a
BundleContext
object. ABundleContext
object is supplied to a bundle via itsBundleActivator
or as a return value of theGetBundleContext()
method.See also
operator bool() const
-
virtual ~Bundle()¶
-
bool operator==(Bundle const &rhs) const¶
Compares this
Bundle
object with the specified bundle.Valid
Bundle
objects compare equal if and only if they are installed in the same framework instance and their bundle id is equal. InvalidBundle
objects are always considered to be equal.
-
bool operator!=(Bundle const &rhs) const¶
Compares this
Bundle
object with the specified bundle for inequality.- Parameters:
rhs – The
Bundle
object to compare this object with.- Returns:
Returns the result of
!(*this == rhs)
.
-
bool operator<(Bundle const &rhs) const¶
Compares this
Bundle
with the specified bundle for order.Bundle objects are ordered first by their framework id and then according to their bundle id. Invalid
Bundle
objects will always compare greater then validBundle
objects.- Parameters:
rhs – The
Bundle
object to compare this object with.- Returns:
-
explicit operator bool() const¶
Tests this Bundle object for validity.
Invalid
Bundle
objects are created by the default constructor or can be returned by certain framework methods if the bundle has been uninstalled.- Returns:
true
if this Bundle object is valid and can safely be used,false
otherwise.
-
Bundle &operator=(std::nullptr_t)¶
Releases any resources held or locked by this
Bundle
and renders it invalid.
-
State GetState() const¶
Returns this bundle’s current state.
A bundle can be in only one state at any time.
- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
An element of
STATE_UNINSTALLED
,STATE_INSTALLED
,STATE_RESOLVED
,STATE_STARTING
,STATE_STOPPING
,STATE_ACTIVE
.
-
BundleContext GetBundleContext() const¶
Returns this bundle’s
BundleContext
.The returned
BundleContext
can be used by the caller to act on behalf of this bundle.If this bundle is not in the
STATE_STARTING
,STATE_ACTIVE
, orSTATE_STOPPING
states, then this bundle has no validBundleContext
and this method will return an invalidBundleContext
object.- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
A valid or invalid
BundleContext
for this bundle.
-
long GetBundleId() const¶
Returns this bundle’s unique identifier.
This bundle is assigned a unique identifier by the framework when it was installed.
A bundle’s unique identifier has the following attributes:
Is unique.
Is a
long
.Its value is not reused for another bundle, even after a bundle is uninstalled.
Does not change while a bundle remains installed.
Does not change when a bundle is re-started.
This method continues to return this bundle’s unique identifier while this bundle is in the
STATE_UNINSTALLED
state.- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
The unique identifier of this bundle.
-
std::string GetLocation() const¶
Returns this bundle’s location.
The location is the full path to the bundle’s shared library. This method continues to return this bundle’s location while this bundle is in the
STATE_UNINSTALLED
state.- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
The string representation of this bundle’s location.
-
void *GetSymbol(void *handle, std::string const &symname) const¶
Retrieves the resolved symbol from bundle shared library and returns a function pointer associated with it.
- Parameters:
handle – Handle to the Bundle’s shared library
symname – Name of the symbol
- Throws:
std::runtime_error – if the bundle is not started or active
std::invalid_argument – if handle or symname is empty
std::invalid_argument – if this bundle is not initialized
- Returns:
A function pointer to the desired symbol or nullptr if the library is not loaded
- Pre:
Bundle is already started and active
- Post:
The symbol(s) associated with the bundle gets fetched if the library is loaded
- Post:
If the symbol does not exist, the API returns nullptr
-
std::string GetSymbolicName() const¶
Returns the symbolic name of this bundle as specified by the US_BUNDLE_NAME preprocessor definition.
The bundle symbolic name together with a version must identify a unique bundle.
This method continues to return this bundle’s symbolic name while this bundle is in the
STATE_UNINSTALLED
state.- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
The symbolic name of this bundle.
-
BundleVersion GetVersion() const¶
Returns the version of this bundle as specified in its
manifest.json
file.If this bundle does not have a specified version then
BundleVersion::EmptyVersion
is returned.This method continues to return this bundle’s version while this bundle is in the
STATE_UNINSTALLED
state.- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
The version of this bundle.
-
std::map<std::string, Any> GetProperties() const¶
Returns this bundle’s Manifest properties as key/value pairs.
Deprecated since version 3.0: Use
GetHeaders()
instead.See also
- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
A map containing this bundle’s Manifest properties as key/value pairs.
-
AnyMap const &GetHeaders() const¶
Returns this bundle’s Manifest headers and values.
Manifest header names are case-insensitive. The methods of the returned
AnyMap
object operate on header names in a case-insensitive manner.If a Manifest header value starts with “%”, it is localized according to the default locale. If no localization is found for a header value, the header value without the leading “%” is returned.
This method continues to return Manifest header information while this bundle is in the
UNINSTALLED
state.See also
Note
Localization is not yet supported, hence the leading “%” is always removed.
Note
The lifetime of the returned reference is bound to the lifetime of this
Bundle
object.- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
A map containing this bundle’s Manifest headers and values.
-
Any GetProperty(std::string const &key) const¶
Returns the value of the specified property for this bundle.
If not found, the framework’s properties are searched. The method returns an empty Any if the property is not found.
Deprecated since version 3.0: Use
GetHeaders()
or :any:`BundleContext::GetProperty(conststd::string&) <cppmicroservices::BundleContext::GetProperty>` instead.
See also
- Parameters:
key – The name of the requested property.
- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
The value of the requested property, or an empty string if the property is undefined.
-
std::vector<std::string> GetPropertyKeys() const¶
Returns a list of top-level property keys for this bundle.
Deprecated since version 3.0: Use
GetHeaders()
instead.See also
- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
A list of available property keys.
-
std::vector<ServiceReferenceU> GetRegisteredServices() const¶
Returns this bundle’s ServiceReference list for all services it has registered or an empty list if this bundle has no registered services.
The list is valid at the time of the call to this method, however, as the framework is a very dynamic environment, services can be modified or unregistered at anytime.
- Throws:
std::logic_error – If this bundle has been uninstalled, if the ServiceRegistrationBase object is invalid, or if the service is unregistered.
std::invalid_argument – if this bundle is not initialized.
- Returns:
A list of ServiceReference objects for services this bundle has registered.
-
std::vector<ServiceReferenceU> GetServicesInUse() const¶
Returns this bundle’s ServiceReference list for all services it is using or returns an empty list if this bundle is not using any services.
A bundle is considered to be using a service if its use count for that service is greater than zero.
The list is valid at the time of the call to this method, however, as the framework is a very dynamic environment, services can be modified or unregistered at anytime.
- Throws:
std::logic_error – If this bundle has been uninstalled, if the ServiceRegistrationBase object is invalid, or if the service is unregistered.
std::invalid_argument – if this bundle is not initialized.
- Returns:
A list of ServiceReference objects for all services this bundle is using.
-
BundleResource GetResource(std::string const &path) const¶
Returns the resource at the specified
path
in this bundle.The specified
path
is always relative to the root of this bundle and may begin with ‘/’. A path value of “/” indicates the root of this bundle.- Parameters:
path – The path name of the resource.
- Throws:
std::logic_error – If this bundle has been uninstalled.
std::invalid_argument – if this bundle is not initialized.
- Returns:
A BundleResource object for the given
path
. If thepath
cannot be found in this bundle an invalid BundleResource object is returned.
-
std::vector<BundleResource> FindResources(std::string const &path, std::string const &filePattern, bool recurse) const¶
Returns resources in this bundle.
This method is intended to be used to obtain configuration, setup, localization and other information from this bundle.
This method can either return only resources in the specified
path
or recurse into subdirectories returning resources in the directory tree beginning at the specified path.Examples:
auto bundleContext = GetBundleContext(); auto bundle = bundleContext.GetBundle(); // List all XML files in the config directory std::vector<BundleResource> xmlFiles = bundle.FindResources("config", "*.xml", false); // Find the resource named vertex_shader.txt starting at the root directory std::vector<BundleResource> shaders = bundle.FindResources("", "vertex_shader.txt", true);
- Parameters:
path – The path name in which to look. The path is always relative to the root of this bundle and may begin with ‘/’. A path value of “/” indicates the root of this bundle.
filePattern – The resource name pattern for selecting entries in the specified path. The pattern is only matched against the last element of the resource path. Substring matching is supported using the wildcard charachter (‘*’). If
filePattern
is empty, this is equivalent to “*” and matches all resources.recurse – If
true
, recurse into subdirectories. Otherwise only return resources from the specified path.
- Throws:
std::logic_error – If this bundle has been uninstalled.
std::invalid_argument – if this bundle is not initialized.
- Returns:
A vector of BundleResource objects for each matching entry.
-
TimeStamp GetLastModified() const¶
Returns the time when this bundle was last modified.
A bundle is considered to be modified when it is installed, updated or uninstalled.
- Throws:
std::invalid_argument – if this bundle is not initialized.
- Returns:
The time when this bundle was last modified.
-
void Start(uint32_t options)¶
Starts this bundle.
If this bundle’s state is
STATE_UNINSTALLED
then astd::logic_error
is thrown.The Framework sets this bundle’s persistent autostart setting to Started with declared activation if the
START_ACTIVATION_POLICY
option is set or Started with eager activation if not set.The following steps are executed to start this bundle:
If this bundle is in the process of being activated or deactivated, then this method waits for activation or deactivation to complete before continuing. If this does not occur in a reasonable time, a
std::runtime_error
is thrown to indicate this bundle was unable to be started.If this bundle’s state is
STATE_ACTIVE
, then this method returns immediately.If the
START_TRANSIENT
option is not set, then set this bundle’s autostart setting to Started with declared activation if theSTART_ACTIVATION_POLICY
option is set or Started with eager activation if not set. When the Framework is restarted and this bundle’s autostart setting is not Stopped, this bundle must be automatically started.If this bundle’s state is not
STATE_RESOLVED
, an attempt is made to resolve this bundle. If the Framework cannot resolve this bundle, astd::runtime_error
is thrown.If the
START_ACTIVATION_POLICY
option is set and this bundle’s declared activation policy islazy
then:If this bundle’s state is
STATE_STARTING
, then this method returns immediately.This bundle’s state is set to
STATE_STARTING
.A bundle event of type
BundleEvent#BUNDLE_LAZY_ACTIVATION
is fired.This method returns immediately and the remaining steps will be followed when this bundle’s activation is later triggered.
This bundle’s state is set to
STATE_STARTING
.A bundle event of type
BundleEvent#BUNDLE_STARTING
is fired.If the bundle is contained in a shared library, the library is loaded and the
BundleActivator#Start(BundleContext)
method of this bundle’sBundleActivator
(if one is specified) is called. If the shared library could not be loaded, or theBundleActivator
is invalid or throws an exception then:This bundle’s state is set to
STATE_STOPPING
.A bundle event of type
BundleEvent#BUNDLE_STOPPING
is fired.Any services registered by this bundle are unregistered.
Any services used by this bundle are released.
Any listeners registered by this bundle are removed.
This bundle’s state is set to
STATE_RESOLVED
.A bundle event of type
BundleEvent#BUNDLE_STOPPED
is fired.A
std::runtime_error
exception is then thrown.
If this bundle’s state is
STATE_UNINSTALLED
, because this bundle was uninstalled while theBundleActivator::Start
method was running, astd::logic_error
is thrown.This bundle’s state is set to
STATE_ACTIVE
.A bundle event of type
BundleEvent#BUNDLE_STARTED
is fired.
Preconditions
GetState()
in {STATE_INSTALLED
,STATE_RESOLVED
} or {STATE_INSTALLED
,STATE_RESOLVED
,STATE_STARTING
} if this bundle has a lazy activation policy.
Postconditions, no exceptions thrown
Bundle autostart setting is modified unless the
START_TRANSIENT
option was set.GetState()
in {STATE_ACTIVE
} unless the lazy activation policy was used.BundleActivator::Start()
has been called and did not throw an exception unless the lazy activation policy was used.
Postconditions, when an exception is thrown
Depending on when the exception occurred, the bundle autostart setting is modified unless the
START_TRANSIENT
option was set.GetState()
not in {STATE_STARTING
,STATE_ACTIVE
}.
- Parameters:
options – The options for starting this bundle. See
START_TRANSIENT
andSTART_ACTIVATION_POLICY
. The Framework ignores unrecognized options.- Throws:
std::runtime_error – If this bundle could not be started.
std::logic_error – If this bundle has been uninstalled or this bundle tries to change its own state.
std::invalid_argument – if this bundle is not initialized.
cppmicroservices::SecurityException – if the bundle’s shared library to be loaded failed a security check.
-
void Start()¶
Starts this bundle with no options.
This method performs the same function as calling
Start(0)
.See also
- Throws:
std::runtime_error – If this bundle could not be started.
std::logic_error – If this bundle has been uninstalled or this bundle tries to change its own state.
std::invalid_argument – if this bundle is not initialized.
cppmicroservices::SecurityException – if the bundle’s shared library to be loaded failed a security check.
-
void Stop(uint32_t options)¶
Stops this bundle.
The following steps are executed when stopping a bundle:
If this bundle’s state is
STATE_UNINSTALLED
then astd::logic_error
is thrown.If this bundle is in the process of being activated or deactivated then this method waits for activation or deactivation to complete before continuing. If this does not occur in a reasonable time, a
std::runtime_error
is thrown to indicate this bundle was unable to be stopped.If the
STOP_TRANSIENT
option is not set then set this bundle’s persistent autostart setting to Stopped. When the Framework is restarted and this bundle’s autostart setting is Stopped, this bundle will not be automatically started.If this bundle’s state is not
STATE_STARTING
orSTATE_ACTIVE
then this method returns immediately.This bundle’s state is set to
STATE_STOPPING
.A bundle event of type
BundleEvent#BUNDLE_STOPPING
is fired.If this bundle’s state was
STATE_ACTIVE
prior to setting the state toSTATE_STOPPING
, theBundleActivator#Stop(BundleContext)
method of this bundle’sBundleActivator
, if one is specified, is called. If that method throws an exception, this method continues to stop this bundle and a std::runtime_error is thrown after completion of the remaining steps.Any services registered by this bundle are unregistered.
Any services used by this bundle are released.
Any listeners registered by this bundle are removed.
If this bundle’s state is
STATE_UNINSTALLED
, because this bundle was uninstalled while theBundleActivator::Stop
method was running, a std::runtime_error is thrown.This bundle’s state is set to
STATE_RESOLVED
.A bundle event of type
BundleEvent#BUNDLE_STOPPED
is fired.
Preconditions
GetState()
in {STATE_ACTIVE
}.
Postconditions, no exceptions thrown
Bundle autostart setting is modified unless the
STOP_TRANSIENT
option was set.GetState()
not in {STATE_ACTIVE
,STATE_STOPPING
}.BundleActivator::Stop
has been called and did not throw an exception.
Postconditions, when an exception is thrown
Bundle autostart setting is modified unless the
STOP_TRANSIENT
option was set.
- Parameters:
options – The options for stopping this bundle. See
STOP_TRANSIENT
. The Framework ignores unrecognized options.- Throws:
std::runtime_error – If the bundle failed to stop.
std::logic_error – If this bundle has been uninstalled or this bundle tries to change its own state.
std::invalid_argument – if this bundle is not initialized.
-
void Stop()¶
Stops this bundle with no options.
This method performs the same function as calling
Stop(0)
.See also
- Throws:
std::runtime_error – If the bundle failed to stop.
std::logic_error – If this bundle has been uninstalled or this bundle tries to change its own state.
-
void Uninstall()¶
Uninstalls this bundle.
This method causes the Framework to notify other bundles that this bundle is being uninstalled, and then puts this bundle into the
STATE_UNINSTALLED
state. The Framework removes any resources related to this bundle that it is able to remove.The following steps are executed to uninstall a bundle:
If this bundle’s state is
STATE_UNINSTALLED
, then a std::logic_error is thrown.If this bundle’s state is
STATE_ACTIVE
,STATE_STARTING
orSTATE_STOPPING
, this bundle is stopped as described in theBundle::Stop
method. IfBundle::Stop
throws an exception, a Framework event of typeFrameworkEvent#FRAMEWORK_ERROR
is fired containing the exception.This bundle’s state is set to
STATE_UNINSTALLED
.A bundle event of type
BundleEvent#BUNDLE_UNINSTALLED
is fired.This bundle and any persistent storage area provided for this bundle by the Framework are removed.
Preconditions
GetState()
not in {STATE_UNINSTALLED
}.
Postconditions, no exceptions thrown
GetState()
in {STATE_UNINSTALLED
}.This bundle has been uninstalled.
Postconditions, when an exception is thrown
GetState()
not in {STATE_UNINSTALLED
}.This Bundle has not been uninstalled.
See also
- Throws:
std::runtime_error – If the uninstall failed. This can occur if another thread is attempting to change this bundle’s state and does not complete in a timely manner.
std::logic_error – If this bundle has been uninstalled or this bundle tries to change its own state.
std::invalid_argument – if this bundle is not initialized.
Protected Functions
Friends
- friend class BundleRegistry