public class OsgiContext extends DynamicCapsuleContext implements org.osgi.framework.BundleActivator, ServiceWatching, ServiceProviding, ServiceConsuming
By having your bundle activator extend from this class, you get *full* access to the Domino Java DSL.
By calling whenBundleActive(Procedure1), you implicitly create a
Capsule which is tighly coupled to the bundles actived state. All
(most) other methods of this class should be called inside this capsule (in
the given procedure).
Note that if you use
watchAdvancedServices(Class, String, Procedure1), you might
additionally want to import the relevant watcher events.
== Example 1: Wait for a service
// file: MyService.java
package org.example.domino_test_one
import org.osgi.service.http.HttpService
public class MyService {
private final HttpService httpService;
public MyService(HttpService httpService) {
this.httpService = httpService;
}
}
// file: Activator.java
package org.example.domino_test_one
import domino.java.OsgiContext
import org.osgi.service.http.HttpService
public class Activator extends OsgiContext {
public Activator() {
whenBundleActive(bundleContext -> {
// Make MyService available as long as HttpService is present
whenServicePresent(HttpService.class, httpService -> {
MyService myService = new MyService(httpService);
providesService(myService, MyService.class);
});
});
}
}
TODO: Do not allow per default multiple calls to
whenBundleActive(Procedure1), and introduce a new setter to allow
overriding whenBundleActive(Procedure1)
| Constructor and Description |
|---|
OsgiContext()
Will be called by the OSGi framework, if you inherit from this class.
|
| Modifier and Type | Method and Description |
|---|---|
protected <S> org.osgi.framework.ServiceRegistration<S> |
internalProvideService(S service,
java.lang.Iterable<java.lang.Class<? super S>> interfaces,
java.util.Map<java.lang.String,java.lang.Object> properties) |
boolean |
isActive()
Returns `true` as long as the bundle is active and it's bundle context is
valid.
|
void |
onStop(java.lang.Runnable f)
Provides convenient `onStop` method which the end user can use for ad-hoc
adding stop logic to the current scope.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service)
Registers the service under it's own specified type and without service
properties.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service,
java.lang.Class<? super S> type)
Registers the service under the specified type and without service
properties.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2)
Registers the service under the specified types and without service
properties.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.lang.Class<? super S> type3)
Registers the service under the specified types and without service
properties.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.lang.Class<? super S> type3,
java.lang.Class<? super S> type4)
Registers the service under the specified types and without service
properties.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.lang.Class<? super S> type3,
java.lang.Class<? super S> type4,
java.util.Map<java.lang.String,java.lang.Object> properties)
Registers the service under the specified types with the given service
properties.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.lang.Class<? super S> type3,
java.util.Map<java.lang.String,java.lang.Object> properties)
Registers the service under the specified types with the given service
properties.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.util.Map<java.lang.String,java.lang.Object> properties)
Registers the service under the specified types with the given service
properties.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service,
java.lang.Class<? super S> type,
java.util.Map<java.lang.String,java.lang.Object> properties)
Registers the service under the specified type with the given service
properties.
|
<S> org.osgi.framework.ServiceRegistration<S> |
providesService(S service,
java.util.Map<java.lang.String,java.lang.Object> properties)
Registers the service under it's own specified type with the given service
properties.
|
<S> de.tototec.utils.functional.Optional<S> |
service(java.lang.Class<S> type)
Returns the highest-ranked service of the specified type if available.
|
<S> de.tototec.utils.functional.Optional<S> |
service(java.lang.Class<S> type,
java.lang.String filter)
Returns the first available service of the specified class which satisfies
the filter if available.
|
<S> de.tototec.utils.functional.Optional<org.osgi.framework.ServiceReference<S>> |
serviceRef(java.lang.Class<? super S> type)
Like
ServiceConsuming.service(Class) but returns the reference so you can access meta
information
about that service. |
<S> de.tototec.utils.functional.Optional<org.osgi.framework.ServiceReference<S>> |
serviceRef(java.lang.Class<S> type,
java.lang.String filter)
Like
ServiceConsuming.service(Class, String) with filter but returns the service
reference. |
<S> java.util.Collection<org.osgi.framework.ServiceReference<S>> |
serviceRefs(java.lang.Class<S> type,
java.lang.String filter)
Like
ServiceConsuming.services(Class, String) with filters but returns the
references. |
<S> java.util.List<S> |
services(java.lang.Class<S> type)
Returns all services of the given type.
|
<S> java.util.List<S> |
services(java.lang.Class<S> type,
java.lang.String filter)
Returns all services of the specified type which satisfy the given filter.
|
void |
start(org.osgi.framework.BundleContext context) |
void |
stop(org.osgi.framework.BundleContext context) |
<S> org.osgi.util.tracker.ServiceTracker<S,S> |
watchAdvancedServices(java.lang.Class<S> type,
java.lang.String filter,
de.tototec.utils.functional.Procedure1<ServiceWatcherEvent<S>> f)
Lets you react to service events for services with the specified type
which match the given filter.
|
<S> org.osgi.util.tracker.ServiceTracker<S,S> |
whenAdvancedServicePresent(java.lang.Class<S> type,
java.lang.String filter,
de.tototec.utils.functional.Procedure1<S> f)
Activates the given inner logic as long as the first service of the given
type is present.
|
void |
whenBundleActive(de.tototec.utils.functional.Procedure1<org.osgi.framework.BundleContext> f)
Defines a handler `f` to be executed when the bundle becomes active.
|
<S> org.osgi.util.tracker.ServiceTracker<S,S> |
whenServicePresent(java.lang.Class<S> type,
de.tototec.utils.functional.Procedure1<S> f)
Waits until a service of the specified type is available and executes the
given event handler with it.
|
<S1,S2,S3,S4> |
whenServicesPresent(java.lang.Class<S1> type1,
java.lang.Class<S2> type2,
java.lang.Class<S3> type3,
java.lang.Class<S4> type4,
de.tototec.utils.functional.Procedure4<S1,S2,S3,S4> f) |
<S1,S2,S3> org.osgi.util.tracker.ServiceTracker<S1,S1> |
whenServicesPresent(java.lang.Class<S1> type1,
java.lang.Class<S2> type2,
java.lang.Class<S3> type3,
de.tototec.utils.functional.Procedure3<S1,S2,S3> f) |
<S1,S2> org.osgi.util.tracker.ServiceTracker<S1,S1> |
whenServicesPresent(java.lang.Class<S1> type1,
java.lang.Class<S2> type2,
de.tototec.utils.functional.Procedure2<S1,S2> f) |
<S,R> R |
withService(java.lang.Class<S> type,
de.tototec.utils.functional.F1<de.tototec.utils.functional.Optional<S>,R> f)
Executes the given handler with the highest-ranked service of the specified
type.
|
addCapsule, executeWithinNewCapsuleScopepublic OsgiContext()
If you construct this class manually, you have to take care to properly
call the start(BundleContext) and stop(BundleContext)
methods.
public boolean isActive()
public void whenBundleActive(de.tototec.utils.functional.Procedure1<org.osgi.framework.BundleContext> f)
In `f`, you have the opportunity to add so called capsules, which have their own `start` and `stop` methods (a kind of mini bundles). Their `stop` methods will be invoked as soon as the bundle activator's `stop` method is called. So you have the big chance here to encapsulate start and stop logic at one place, making the bundle activator less error-prone, better readable and easier to write.
f - Handlerpublic void start(org.osgi.framework.BundleContext context)
start in interface org.osgi.framework.BundleActivatorpublic void stop(org.osgi.framework.BundleContext context)
throws java.lang.Exception
stop in interface org.osgi.framework.BundleActivatorjava.lang.Exceptionpublic void onStop(java.lang.Runnable f)
f - stop logicprotected <S> org.osgi.framework.ServiceRegistration<S> internalProvideService(S service,
java.lang.Iterable<java.lang.Class<? super S>> interfaces,
java.util.Map<java.lang.String,java.lang.Object> properties)
public <S> org.osgi.framework.ServiceRegistration<S> providesService(S service)
ServiceProvidingprovidesService in interface ServiceProvidingpublic <S> org.osgi.framework.ServiceRegistration<S> providesService(S service,
java.lang.Class<? super S> type)
ServiceProvidingprovidesService in interface ServiceProvidingpublic <S> org.osgi.framework.ServiceRegistration<S> providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2)
ServiceProvidingprovidesService in interface ServiceProvidingpublic <S> org.osgi.framework.ServiceRegistration<S> providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.lang.Class<? super S> type3)
ServiceProvidingprovidesService in interface ServiceProvidingservice - The service.to easily create a map in-place.public <S> org.osgi.framework.ServiceRegistration<S> providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.lang.Class<? super S> type3,
java.lang.Class<? super S> type4)
ServiceProvidingprovidesService in interface ServiceProvidingservice - The service.to easily create a map in-place.public <S> org.osgi.framework.ServiceRegistration<S> providesService(S service,
java.util.Map<java.lang.String,java.lang.Object> properties)
ServiceProvidingprovidesService in interface ServiceProvidingservice - The service.properties - The service properties.to easily create a map in-place.public <S> org.osgi.framework.ServiceRegistration<S> providesService(S service,
java.lang.Class<? super S> type,
java.util.Map<java.lang.String,java.lang.Object> properties)
ServiceProvidingprovidesService in interface ServiceProvidingservice - The service.properties - The service properties.to easily create a map in-place.public <S> org.osgi.framework.ServiceRegistration<S> providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.util.Map<java.lang.String,java.lang.Object> properties)
ServiceProvidingprovidesService in interface ServiceProvidingservice - The service.properties - The service properties.to easily create a map in-place.public <S> org.osgi.framework.ServiceRegistration<S> providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.lang.Class<? super S> type3,
java.util.Map<java.lang.String,java.lang.Object> properties)
ServiceProvidingprovidesService in interface ServiceProvidingservice - The service.properties - The service properties.to easily create a map in-place.public <S> org.osgi.framework.ServiceRegistration<S> providesService(S service,
java.lang.Class<? super S> type1,
java.lang.Class<? super S> type2,
java.lang.Class<? super S> type3,
java.lang.Class<? super S> type4,
java.util.Map<java.lang.String,java.lang.Object> properties)
ServiceProvidingprovidesService in interface ServiceProvidingservice - The service.properties - The service properties.to easily create a map in-place.public <S> org.osgi.util.tracker.ServiceTracker<S,S> watchAdvancedServices(java.lang.Class<S> type,
java.lang.String filter,
de.tototec.utils.functional.Procedure1<ServiceWatcherEvent<S>> f)
ServiceWatchingwatchAdvancedServices in interface ServiceWatchingtype - The `Class` representing the service type.f - Service event handlerpublic <S> org.osgi.util.tracker.ServiceTracker<S,S> whenAdvancedServicePresent(java.lang.Class<S> type,
java.lang.String filter,
de.tototec.utils.functional.Procedure1<S> f)
ServiceWatchingwhenAdvancedServicePresent in interface ServiceWatchingtype - The `Class` representing the service type.public <S> org.osgi.util.tracker.ServiceTracker<S,S> whenServicePresent(java.lang.Class<S> type,
de.tototec.utils.functional.Procedure1<S> f)
ServiceWatchingwhenServicePresent in interface ServiceWatchingf - Handlerpublic <S1,S2> org.osgi.util.tracker.ServiceTracker<S1,S1> whenServicesPresent(java.lang.Class<S1> type1,
java.lang.Class<S2> type2,
de.tototec.utils.functional.Procedure2<S1,S2> f)
whenServicesPresent in interface ServiceWatchingpublic <S1,S2,S3> org.osgi.util.tracker.ServiceTracker<S1,S1> whenServicesPresent(java.lang.Class<S1> type1,
java.lang.Class<S2> type2,
java.lang.Class<S3> type3,
de.tototec.utils.functional.Procedure3<S1,S2,S3> f)
whenServicesPresent in interface ServiceWatchingpublic <S1,S2,S3,S4> org.osgi.util.tracker.ServiceTracker<S1,S1> whenServicesPresent(java.lang.Class<S1> type1,
java.lang.Class<S2> type2,
java.lang.Class<S3> type3,
java.lang.Class<S4> type4,
de.tototec.utils.functional.Procedure4<S1,S2,S3,S4> f)
whenServicesPresent in interface ServiceWatchingpublic <S,R> R withService(java.lang.Class<S> type,
de.tototec.utils.functional.F1<de.tototec.utils.functional.Optional<S>,R> f)
ServiceConsumingBundleContext.ungetService(org.osgi.framework.ServiceReference).withService in interface ServiceConsumingf - Handler that uses the service.public <S> de.tototec.utils.functional.Optional<org.osgi.framework.ServiceReference<S>> serviceRef(java.lang.Class<? super S> type)
ServiceConsumingServiceConsuming.service(Class) but returns the reference so you can access meta
information
about that service. An implicit conversion adds a `service` property to the
reference, so you can simply use that to obtain the service. Doesn't take
type parameters into account!serviceRef in interface ServiceConsumingpublic <S> de.tototec.utils.functional.Optional<org.osgi.framework.ServiceReference<S>> serviceRef(java.lang.Class<S> type,
java.lang.String filter)
ServiceConsumingServiceConsuming.service(Class, String) with filter but returns the service
reference.serviceRef in interface ServiceConsumingpublic <S> java.util.Collection<org.osgi.framework.ServiceReference<S>> serviceRefs(java.lang.Class<S> type,
java.lang.String filter)
ServiceConsumingServiceConsuming.services(Class, String) with filters but returns the
references.serviceRefs in interface ServiceConsumingpublic <S> de.tototec.utils.functional.Optional<S> service(java.lang.Class<S> type)
ServiceConsumingservice in interface ServiceConsumingpublic <S> de.tototec.utils.functional.Optional<S> service(java.lang.Class<S> type,
java.lang.String filter)
ServiceConsumingservice in interface ServiceConsumingfilter - filter expressionpublic <S> java.util.List<S> services(java.lang.Class<S> type,
java.lang.String filter)
ServiceConsumingservices in interface ServiceConsumingfilter - filter expressionpublic <S> java.util.List<S> services(java.lang.Class<S> type)
ServiceConsumingservices in interface ServiceConsuming