About half a year ago I stumbled over the problem that Java does not allow certain types of changes on compiled coding on the fly (for good reason BTW). It is even not possible when using the debugger interface (see also JDPA for more details on this). On the other hand, however, I know from the ABAP/4 world that generating coding might be a very good idea sometimes, because in most cases it impacts performance less compared to writing generic coding (for example by using introspection or reflection). In this situation you often want to automize the generation thus immediately having to struggle with the problem that you need to reload changed compiled codes “on the fly” (during the runtime of the virtual machine) for example to correct errors or to deploy a new version. Since that time I was searching for some alternative which could work around many restrictions Java currently poses here. Looking around with Google I found that already some people before me had thought about this problem. I stumbled – for example – over the following papers:
- Overcoming JVM HotSwap constraints via binary rewriting
- Towards Unanticipated Runtime Adaptation of Java Applications
Finally, I read in a forum that the OSGi standard (besides some other things) would provide such a feature. Reading a bit more at their website I figured out that this is not just about hotswapping Java Byte Code, but an entire approach of implementing a certain component model coming from the embedded device world. I was quite eager getting to know more about it and planned to conduct a more thorough practical look at an implementation of the standard some time.
First Steps with Felix
Within the last weeks I found this opportunity and had a closer look at Apache Felix, one of the major implementations of the OSGi standard. At their website they have a quite interesting tutorial which describes how to use the OSGi standard in an example-driven manner. Unfortunately, I did not find a downloadable package of the examples used in the tutorial so I was forced to copy and paste it locally into my Netbeans environment. If you want you can download this package from this post.
Enhanced OSGi Felix Tutorial (1.8 MiB, 1,503 hits)
For those not familiar with this tutorial, here is a brief overview about which lesson/example is doing what:
|1||Event Listener for Bundle Activation|
|2||Implementation of a simple component which checks a word (represented as string) against a list of known English words (in a dictionary)|
|2b||Implementation of a simple component which checks a word (represented as string) against a list of known French words (in a dictionary)|
|3||Implementation of a client for the dictionary services in 2 and 2b|
|4||An enhanced version of the client of 3|
|5||An even enhanced version of the client compared to 4 using automatic detection of active and inactive components using a service tracker (a utility class which automatically keeps track of active components)|
|6||Implementation of a spell checker service (looking up multiple words in a dictionary)|
The tutorial goes on with further more complex scenarios. For my intent these examples already had been enough to understand the core concepts.