An OSGi BundleActivator that uses reflection to find JSR 330 Provider implementations and injects to provide and consume OSGi services.
|
7 vuotta sitten | |
---|---|---|
.utilities | 9 vuotta sitten | |
jsr330activator.gogoshell | 9 vuotta sitten | |
jsr330activator.implementation | 7 vuotta sitten | |
jsr330activator.mocks | 8 vuotta sitten | |
jsr330activator.testbundle1 | 7 vuotta sitten | |
jsr330activator.testbundle2 | 9 vuotta sitten | |
jsr330activator.testbundle3 | 7 vuotta sitten | |
jsr330activator.testbundle4 | 9 vuotta sitten | |
jsr330activator.testbundle5 | 9 vuotta sitten | |
jsr330activator.testbundle6 | 9 vuotta sitten | |
jsr330activator.testbundle7 | 9 vuotta sitten | |
jsr330activator.testbundle8 | 9 vuotta sitten | |
jsr330activator.tests | 7 vuotta sitten | |
.gitignore | 9 vuotta sitten | |
.travis.yml | 9 vuotta sitten | |
LICENSE | 9 vuotta sitten | |
README.org | 7 vuotta sitten | |
pom.xml | 9 vuotta sitten |
This is an implementation of the OSGi BundleActivator interface, that will scan the bundle for implementations of the JSR 330 Provider<> interface, instantiate the providers, and use them to register the provided interface as an OSGi service.
BundleActivators exposing services and setting up service listeners for dependecies, are the "four wheel drives" of OSGi plugins. A bundle using a BundleActivator can be dropped in any OSGi container using any (or none) component model, e.g. eclipse, Spring dynamic modules, iPOJO, and make its services available there.
This project is an attempt at replacing the handwritten code with something that keeps the "four wheel drive" capability, but at the same time is much simpler to maintain. To do so, it should be simple and small, and to stay small it has to limit what it can support.
And that's it. It's formed from what I did with bundle activators.
If this is not enough for your requirements, you're probably better off with a real component framework.
The preferred way to use the Jsr330Activator, is to embed the Jsr330Activator together with javax.inject inside your bundle. This makes your bundle work without any dependencies outside of OSGi itself and whatever it needs to do its work. The jsr330activator.testbundle3 is an example of this.
Bundle-Activator
and Embed-Dependency
settings in the maven-bundle-plugin configuration:@Inject public void setHelloService(HelloService helloService) { this.helloService = helloService; }
// This is from the HelloService3a interface public String getMessage() { return helloService.getMessage(); }
// This is from the Provider interface public HelloService3a get() { return this; } } #+END_SRC
Both the Jsr330Activator and javax.bundle can be provided as OSGi dependencies instead of embedding them. The jsr330activator.testbundle1 project is an example of this. Or one of the dependencies can be embedded and one can be provided as an OSGi dependency. The jsr330activator.testbundle2 project is an example of this, it embeds the Jsr330Activator and gets javax.inject from the OSGi runtime.
The config of the maven-bundle-plugin in jsr330activator.testbundle1/pom.xml
doesn't contain an Embed-Dependency
setting, and the Embed-Dependency
setting in jsr330activator.testbundle2/pom.xml
only mentions the artifactId of the dependency that is to be embedded.
I haven't been able to think of a use case for not embedding the Jsr330Activator itself. The Jsr330Activator's reason for existence, is to create self-contained service-providinng and service-listening bundles, that don't require anything from the run-time other than basic OSGi support.
There is a use-case for not embedding javax.inject, and that is if one wish to access the @Named annotation of an injected service. If the javax.inject package is provided through OSGi, then one can access the name this way:
private String getNamedAnnotationValue(StorageService storageService) {
Named named = storageService.getClass().getAnnotation(Named.class);
if (named != null) {
return named.value();
}
return null;
}
If javax.inject has been embedded in either the service-providing bundles or the service receiving bundles, or both, the @Named annotation has to be accessed using reflection on a dynamic proxy (that itself uses reflection):
private String getNamedAnnotationValue(StorageService storageService) {
Annotation[] annotations = storageService.getClass().getAnnotations();
for (Annotation annotation : annotations) {
Class<?>[] interfaces = annotation.getClass().getInterfaces();
for (Class<?> annotationInterface : interfaces) {
if (Named.class.getCanonicalName().equals(annotationInterface.getCanonicalName())) {
try {
Method valueMethod = annotationInterface.getMethod("value", new Class<?>[0]);
String namedValue = (String) valueMethod.invoke(annotation, new Object[0]);
return namedValue;
} catch (Exception e) { }
}
}
}
return null;
}
Since this is an OSGi project it seemed wrong not to make the Jsr330Activator a bundle.
The Jsr330Activator is under the Eclipse public license v. 1.0. See the LICENSE file for the full license text.
file:https://travis-ci.org/sbang/jsr330activator.png file:https://coveralls.io/repos/sbang/jsr330activator/badge.svg file:https://maven-badges.herokuapp.com/maven-central/no.steria.osgi.jsr330activator/jsr330activator.implementation/badge.svg The jsr330activator.gogoshell module isn't used for anything directly in the build process. This module is used to start an OSGi shell, where the bundles and their behaviour can be examined.
This is the place to go if the integration tests starts failing: error messages and exception stack traces from the gogo shell start and stop can be illuminating. Examining what the bundles actually provide and expect can also be illuminating.
All bundles that should be loaded for the testing, should be listed as "provided" dependencies of type "jar", in the jsr330activator.gogoshell/provision/pom.xml
file.
To use the shell for debugging, do the following:
provision/pom.xml
file, they should be studied carefully: look for missing bundle dependencies, and look for missing services (often indicating that the bundle activator hasn't been successfully started)bundles
This command lists all bundles. Check that all bundles show up as "Active". If they have a different state, something probably went wrong in the initialization phase
exit 0
If the Provider<> implementations that are activated by the Jsr330Activator needs to load resources other than constant resources in the bundles themselves, they will need to know the BundleContext. One way this could be allowing @Inject of BundleContext, i.e.:
class SomeServiceProvider implements Provider<SomeService>, SomeService {
@Inject
private BundleContext context;
SomeService get() { return this; }
}
One thing that I need for the project that prompted the existence of the Jsr330Activator and that is multiple injections of the same service, and start of the provided service without all of the instances present (several different storage backends in my case).
Alternative 2. would seem to be the most complete solution (because it lets the injection point determine which services are injected. It can also be used to require a particular implementation of a service and let the rest be optional), but alternative 1. is the simplest one to implement (it doesn't require an extra annotation and reflection code looking for two extra annotations).
/Note/: Alternative 1 i.e. injection into a collection is now on master. /Note2/: I have discovered a need for @Optional in my project using the Jsr330Activator: I need logging and the logservice @Inject should probably be optional, so I will probably end up pulling all of the branches below in on master and making a release. I will make tags on the branch points so that it will be possible to make slimmer versions (won't be made by me, though...).
There is now also an implementation of @Named injections. @Named annotations on @Inject members means that only those service Provider<> classes annotated with @Named with the same value will be matched. Services from @Named Providers<> can be injected into @Inject members that has no @Named annotations.
There are no clear semantics of what happens when a collection @Inject point gets a @Named annotation.
The property name "id" has been picked for the service property holding the @Named.value(). This is the property name used by the blueprint-maven-plugin for the same annotation, and if we're lucky they might be compatible.
There is also an implementation of @Optonal on top of @Named in the branch add-optional-injection-support.
I haven't decided whether to pull the @Optional branch into master or not.
This is because the primary use case of the Jsr330Activator is to embed it, and it's therefore important to keep it as small as possible. And one of the ways of keeping it small is to not pull in features that aren't strictly necessary.
Here is a little table to show what the extra costs for the new features are, wrt. to increasing the size of the jar (testbundle1 which embeds nothing is shown for comparison). These are sizes in bytes of the jar files, testbundle3 is the one to track through all of the changes, the implementation also gives a good indication of the code growth:
implementation | testbundle1 | testbundle2 | testbundle3 | testbundle4 | testbundle5 | testbundle6 | testbundle7 | |
1.0.1 | 14527 | 5886 | 19002 | 23556 | ||||
collection injections | 15996 | 6101 | 20696 | 25331 | 23103 | 23193 | 22310 | 25247 |
named injections | 16623 | 6102 | 21321 | 25956 | 23729 | 23818 | 22935 | 26675 |
optional injections | 17244 | 6101 | 21942 | 26580 | 24351 | 24441 | 23556 | 28787 |
Collection injections add around 1800 bytes to the size of the jar (1775 bytes on testbundle3, and 1249 bytes on the implementation). @Named injections add around 625 bytes on top of this (implementation 627 bytes, testbundle3 625 bytes), and @Optional adds around 625 bytes on top of this again (implementation 621 bytes, testbundle3 624 bytes). Total for @Named and @Optional is an additional 1250 bytes (implementation 1248 bytes, testbundle3 1249 bytes).
The @Named injections will go into the next release.
Whether @Optional will go in is more in question, especially since @Optional introduced a felix runtime dependency on the Jsr330Activator. This is more than an annoyance than an actual problem, beause if the launcher is felix it doesn't really matter if the Jsr330Activator is pulled in or not, except for a small extra runtime cost. But it is still an annoyance...
This provider is recognized:
public class HelloServiceProvider2 implements Provider<HelloService>, HelloService {
public String getMessage() {
return "Hello from HelloServiceProvider2";
}
public HelloService get() {
return this;
}
}
This provider isn't recognized:
public class HelloService2Provider2 implements HelloService2, Provider<HelloService2> {
public String getMessage() {
return "Hello from HelloService2Provider2";
}
public HelloService2 get() {
return this;
}
}
The unit test Jsr330ActivatorTest.testFindProvidersMultipleInterfacesWithProviderNotFirst() reproduces this bug (the failing test is checked in as @Ignored to avoid failing the continous integration).