Most developers take it for granted that a static variable will only have one instance within their application (per JVM). The fact is that a static variable will only have one instance per class-loader. After dealing with the millions of cryptic stack traces I found when first learning about OSGi, I expect everyone on the same path will soon realise that each bundle has its own class-loader. After all, it is written in the spec which I'm sure you've all read.
What this means is that some classes can be loaded by two distinct class loader instances within the same JVM. I've been unable to find a definitive answer regarding this behavior in various framework implementations and my knowledge of class loading is quite shallow. If anyone can shed some light, I would appreciate it. Having said that, if you've ever come a across an exception like
java.lang.ClassCastException: org.example.Foo cannot be cast to org.example.Foothen you've witnessed the symptoms of uncoordinated class-loaders first hand. The number of times I saw this early on provides evidence to my claim.
This of course leads to some interesting errors, mostly from Singletons that have not been created completely. One major factor in an APIs OSGi compatibility (after a lack a manifest headers) is the use of static instances and how they behave in a multi-bundle, dynamic environment.

0 comments:
Post a Comment