Managing bundles programmatically
Searching for bundles
The BundleContext also provides methods to search for bundles installed in the App Runtime using getBundles()
.
export default class {
start(){
// returns array of all bundles
let allbundles = bundleContext.getBundles();
// search bundles with symbolic name "test"
let bundles = bundleContext.getBundles("(name=test)");
// expect one test bundle
let testBundle = bundles[0];
}
}
getBundles()
accepts a filter string that can define complex filter expressions.
This expression is used to filter bundles based on their manifest properties.
Filter expressions are specified by the RFC-1960 and known as LDAP filter syntax.
Here are some examples of filter expressions:
Sample | Description |
---|---|
|
Property |
|
Property |
|
Property |
|
Property |
|
Property |
|
Property |
|
Property |
|
Property |
|
Property |
|
Property |
|
Property |
Starting a bundle
If a bundle activator needs to start a bundle, it can use the start
method of a bundle object.
export default class {
start() {
// search bundles with symbolic name "test"
let bundles = bundleContext.getBundles("(name=test)");
// expect one test bundle
let testBundle = bundles[0];
// start the test bundle
testBundle.start().then(()=>{
console.debug("test bundle started");
});
}
}
Stopping a bundle
If a bundle activator needs to stop a bundle, it can use the stop
method of a bundle object.
A bundle should not stop itself.
export default class {
start() {
// search bundles with symbolic name "test"
let bundles = bundleContext.getBundles("(name=test)");
// expect one test bundle
let testBundle = bundles[0];
// stop the test bundle
testBundle.stop().then(function() {
console.debug("test bundle stopped");
});
}
}
Installing a bundle
If a bundle activator needs to install new bundles into the system, it can use the installBundle
method of the BundleContext.
This is a very powerful feature, because it allows to add features to a running application lazily.
installBundle()
expects three parameters and can be used in different ways.
installBundle(
location, // URL pointing to the bundle directory
manifest, // manifest.json object (optional)
name // the name of the bundle (optional), overwrites the name provided in the manifest.json
);
The following code sample shows the most simple way to install a new bundle:
export default class {
start() {
// install a test bundle
let promise = bundleContext.installBundle("http://localhost:8080/js/externalBundles/test");
// The installBundle method, now fetches the manifest.json provided
// at http://localhost:8080/js/externalBundles/test/manifest.json
// interprets it and constructs a bundle instance
promise.then((testBundle) => {
// after successful installation, start the bundle
testBundle.start();
});
}
}
Uninstalling a bundle
If a bundle activator needs to uninstall a bundle, it can use the uninstall
method of a bundle object.
export default class {
start() {
// search bundles with symbolic name "test"
let bundles = bundleContext.getBundles("(name=test)");
// expect one test bundle
let testBundle = bundles[0];
// uninstall the test bundle
testBundle.uninstall().then(() => {
console.debug("test bundle uninstalled");
});
}
}