| BundleBuilding-Utils Overview |
|
|
BundleBuilding-Utils is a project that provides an API for building OSGi bundles (OSGi-compliant JAR files) with few lines of code. BundleBuilding-Utils is the first project in the OSGi space that provides such an API and is usable in both OSGi and non-osgi environments. The project can be in handy when testing bundles that process other bundles since it allows you to create many bundles with slight differences in order to test the behavior of your main bundle under different circumstances. Take for example Dynamic-JPA, Dynamic-JPA processes the persistence.xml file contained in bundles and generates from it an Entity Manager Factory, to validate that it works with Toplink, OpenJPA, Hibernate and EclipseLink we create test bundles which contain persistence.xml that use each of the JPA implementations above, using traditional tools we will have to create 4 Maven projects (one for each of the mentioned JPA providers) + a bundle which contains the entities + a bundle which contains client code (6 bundles in sum). Creating a single project for each of them, generating JARs then installing them to the OSGi Environment is surely not an easy thing, and to make things worse, lets say that we want to test the behavior of Dynamic-JPA when the bundles which contain the persistence.xml file don't import entity classes, don't import JPA implementation classes or has the entities in the same bundle instead of importing it. Obviously, creating a Maven project for each test case is very inefficient? Observing different projects (including projects at DynamicJava.org), I noticed that such tests are ignored, I think that mostly due the time/resources consuming process of maintaining separate projects for each test case. But lets see how we can install two bundles, one which contains persistence.xml file and imports the needed entities, the other contains persistence.xml file but don't import the entities using BundleBuilding-Utils.
import org.dynamicjava.osgi.bundle_building_utils;
import ...;
public class Activator implements BundleActivator {
public void start(BundleContext bundleContext) throws Exception {
createAndInstallFirstBundle();
createAndInstallSecondBundle();
}
protected void createAndInstallFirstBundle(BundleContext bundleContext) {
BundleBuilder builder = BundleBuilderFactory.getDefaultFactory().create();
builder.metadata().setSymbolicName("org.test.test_bundle1");
builder.metadata().packageImports().add("org.test.entities", "1.0.0");
builder.putResource("META-INF/persistence.xml",
new FileInputStream("c:/persistence.xml"));
bundleContext.installBundle("inmemory:test_bundle1", builder.build());
}
protected void createAndInstallSecondBundle(BundleContext bundleContext) {
BundleBuilder builder = BundleBuilderFactory.getDefaultFactory().create();
builder.metadata().setSymbolicName("org.test.test_bundle2");
builder.putResource("META-INF/persistence.xml",
new FileInputStream("c:/persistence.xml"));
bundleContext.installBundle("inmemory:test_bundle2", builder.build());
}
}
As you saw, creating a bundle which contains persistence.xml file and imports some packages was done in 5 lines of code. Adding classes to bundles can be done in a single (yes, a single) line of code. That said, does it worth it to create a bundle for each test case you have when you can do it in 5-10 lines of code? I think yes since every test you have is already 15-25 lines of code, so 5-10 lines wouldn't make much difference. The API is clear and simple so you can learn it by simply trying it in your favourite IDE, but if you prefer to check code examples first, you can check the file "src/test/java/org/dynamicjava/osgi/bundle_building_utils/BundleBuilderTest.java" which contains tests on all of the functionalities provided by BundleBuilding-Utils. If you use BundleBuilding-Utils from your bundle, don't forget to import these packages:org.dynamicjava.osgi.bundle_building_utils org.dynamicjava.osgi.bundle_building_utils.support org.dynamicjava.osgi.bundle_building_utils.support.metadata org.dynamicjava.osgi.bundle_building_utils.exceptions Of course, the usage of BundleBuilding-Utils is not limited to tests, I created this project initially to use it to generate bundles from OSGi-incompliant JAR archives, knowing that this functionality could be needed for other developers and that it's very useful for testing OSGi Applications, I decided to make it as a separate project. bundle-building-utils-example.zip is an example application which contains a bundle that creates a bundle with an activator that shows a hello world message. |