I'm a bit new in OSGI and I want the following: to activate my bundle only when some prerequisites are satisfied (which, btw, we get form a native library, but that's another story). AFAIK it could be achieved through the @Reference DS, but I don't get the idea fully. I mean if I write something like this before my @Activate DS:
@Reference
public AnotherService as
@Activate
public void activate() {
//some code
}
this actually means, that my bundle won't be activated until the AnotherService is activated. But could I write in AnotherService
or in my bundle something like this?:
@Activate
public void activate() {
if (condition){
deactivate()
}
//some code
}
@Deactivate
public void deactivate() {
//some code
}
As far as I understand, that's impossible. Then the questions arises: how could I control the activation of my bundle or its references depending on certain condition(s)? I.e. I want my bundle to be either activated, when the condition is satisfied (before activation) or deactivated, when not. It won't suit for me the way: "just make if-statement, if it is not satisfied, do nothing, but still be activated", because the 'activiy' of this bundle is very resource heavy. Maybe I just have a fully wrong idea of OSGI.
This is not something you should do. Your bundle should always be activated as long as it can be resolved (i.e. all of its imports and other static dependencies are satisfied).
In fact, unless you are coding OSGi at an advanced level, you should not write BundleActivators at all. If you are reading an OSGi tutorial in 2020 that tells you to write a BundleActivator, stop reading... that tutorial is junk.
You should use Declarative Services components. A component is an object managed by OSGi and can have its lifecycle bound to the availability of a service. For example:
@Component
public class MyComponent implements MyService {
private final AnotherService service;
@Activate
public MyComponent(@Reference AnotherService service) {
this.service = service;
}
@Override
public void doSomething() {
// ...
}
}
In this example, an instance of MyComponent
will be created and published as a service of type MyService
only when a service of type AnotherService
is available. You will be able to invoke the AnotherService
via the final service
field. This is a mandatory service reference, which is the default in DS, but it's also possible to create optional and dynamic references (i.e. references that can re-bind to other service instances during the lifetime of your component), but don't worry about those use-cases until later in your OSGi learning.