OSGi Classloading - Jfokus
OSGi Classloading - Jfokus
OSGi Classloading - Jfokus
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
JFocus 2009 2009-01-28<br />
<strong>OSGi</strong> Class Loading<br />
<strong>OSGi</strong> Class Loading<br />
Bundle Classloader<br />
• An <strong>OSGi</strong> framework must create one class loader per bundle<br />
that is resolved and that is not a fragment bundle<br />
• The creation of the class loader may be delayed until it is<br />
actually needed<br />
Makewave AB<br />
Gunnar Ekolin<br />
2<br />
1
JFocus 2009 2009-01-28<br />
<strong>OSGi</strong> Class Loading<br />
Parent Delegation<br />
• The <strong>OSGi</strong> framework must always delegate classes that start<br />
with java.* to the Parent Classloader<br />
• The framework can be instructed to delegate additional classes<br />
with the org.osgi.framework.bootdelegation system<br />
property<br />
• Examples:<br />
• org.osgi.framework.bootdelegation=*<br />
• org.osgi.framework.bootdelegation=sun.*,com.sun.*<br />
Makewave AB<br />
Gunnar Ekolin<br />
3<br />
<strong>OSGi</strong> Class Loading<br />
Parent Classloader<br />
• The framework must explicitly export packages other than<br />
java.* from the System Bundle<br />
• The property org.osgi.framework.system.packages<br />
contains those exports in the same format as the Export-<br />
Package manifest header<br />
• Framework implementations must take extra care to make<br />
sure that classes exported from the boot classpath are loaded<br />
from the boot classpath because some classes on the boot<br />
class path assumes that they can be loaded by any<br />
classloader because of the prevalent hierarchical model<br />
Makewave AB<br />
Gunnar Ekolin<br />
4<br />
2
JFocus 2009 2009-01-28<br />
<strong>OSGi</strong> Class Loading<br />
Wires<br />
• A bundle that has been resolved is said to have a wire to the<br />
class loader of the exporting bundle for each of its imported<br />
packages<br />
Makewave AB<br />
Gunnar Ekolin<br />
5<br />
<strong>OSGi</strong> Class Loading<br />
Fragments<br />
«bundle»<br />
FragmentHost<br />
Bundle-SymbolicName: example.host<br />
Bundle-Version: 1.0.0<br />
+attaches<br />
«bundle»<br />
Fragment<br />
Fragment-Host: example.host;bundleversion="[1.0.0,2.0.0)”<br />
Makewave AB<br />
Gunnar Ekolin<br />
6<br />
3
JFocus 2009 2009-01-28<br />
<strong>OSGi</strong> Class Loading<br />
Fragments<br />
• A fragment<br />
• must no have a Bundle-Activator header,<br />
• can only attach to one host,<br />
• is in the state Resolved when attached.<br />
• A fragment host bundle<br />
• can have 0...n fragments attached,<br />
• will export all packages exported by attached fragments,<br />
• will import all packages imported by attached fragments,<br />
• will require all bundles that attached fragments requires.<br />
• Fragments are<br />
• attached to the host when it becomes resolved,<br />
• detached from the host when it becomes unresolved.<br />
• Attached fragments are search in bundle id order (lowest first).<br />
Makewave AB<br />
Gunnar Ekolin<br />
7<br />
<strong>OSGi</strong> Class Loading<br />
Extension (framework)<br />
«jar»<br />
Framework<br />
Bundle-SymbolicName: system.bundle<br />
+attaches<br />
«bundle»<br />
Fragment<br />
Fragment-Host: system.bundle;<br />
extension:=framework<br />
Makewave AB<br />
Gunnar Ekolin<br />
8<br />
4
JFocus 2009 2009-01-28<br />
<strong>OSGi</strong> Class Loading<br />
Extension (framework)<br />
• Extension bundles are fragments that attaches to the system<br />
bundle.<br />
• Delivers optional parts of the framework implementation.<br />
• The entire framework must be re-launched if an extension<br />
bundle is refreshed.<br />
• If an extension bundle is updated the new version must not<br />
resolve unless a refresh on it have been made (i.e., the entire<br />
framework must be re-launched).<br />
• An extension bundle<br />
• must not specify Import-Package, Require-Bundle, Bundle-<br />
NativeCode, DynamicImport-Package, Bundle-Activator,<br />
• exports packages via the system bundle.<br />
• Extension bundles must be appended to the class path of the class<br />
loader of the framework implementation in the order in which the<br />
extension bundles are installed: that is, ascending bundle ID order.<br />
Makewave AB<br />
Gunnar Ekolin<br />
9<br />
<strong>OSGi</strong> Class Loading<br />
Extension (bootclasspath)<br />
«jar»<br />
Framework<br />
Bundle-SymbolicName: example.framework<br />
+attaches<br />
«bundle»<br />
Fragment<br />
Fragment-Host: system.bundle;<br />
extension:=bootclasspath<br />
Makewave AB<br />
Gunnar Ekolin<br />
10<br />
5
JFocus 2009 2009-01-28<br />
<strong>OSGi</strong> Class Loading<br />
Extension (bootclasspath)<br />
• Same rules as for extension bundles with a single exception:<br />
• The jar-files of boot class path extension must be added to the<br />
boot class path of the JVM executing the framework.<br />
• Used for JDBC drivers and other legacy code that must be on<br />
the boot class path to work properly.<br />
Makewave AB<br />
Gunnar Ekolin<br />
11<br />
<strong>OSGi</strong> Class Loading<br />
Classloader Search Order 1(2)<br />
Success<br />
Start<br />
no<br />
Failure<br />
1<br />
java?<br />
yes<br />
Delegate to<br />
parent classloader<br />
found?<br />
yes<br />
2<br />
boot<br />
delegation?<br />
yes<br />
Delegate to<br />
parent classloader<br />
found?<br />
no<br />
yes<br />
3<br />
4<br />
imported?<br />
Search<br />
Required bundles<br />
yes<br />
Delegate to<br />
wire's exporter<br />
found?<br />
yes<br />
found?<br />
no<br />
yes<br />
no<br />
Makewave AB<br />
Gunnar Ekolin<br />
12<br />
6
JFocus 2009 2009-01-28<br />
<strong>OSGi</strong> Class Loading<br />
5<br />
Classloader Search Order 2(2)<br />
Search bundle<br />
classpath<br />
found?<br />
no<br />
yes<br />
Success<br />
Failure<br />
6<br />
Search fragments<br />
bundle classpath<br />
found?<br />
yes<br />
no<br />
7<br />
package<br />
exported?<br />
no<br />
yes<br />
9<br />
8<br />
dynamic<br />
import?<br />
yes<br />
Delegate to<br />
wire's exporter<br />
found?<br />
yes<br />
no<br />
no<br />
Makewave AB<br />
Gunnar Ekolin<br />
13<br />
<strong>OSGi</strong> Class Loading<br />
Thank You!<br />
Gunnar Ekolin<br />
www.makewave.com<br />
ekolin@makewave.com<br />
Makewave AB<br />
Gunnar Ekolin<br />
14<br />
7