Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "BundleProxyClassLoader recipe"

 
Line 1: Line 1:
<pre>
+
{{warning|Better solution with R5 OSGi APIs|From [https://www.eclipse.org/forums/index.php/mv/msg/164914/1103228/#msg_1103228 Equinox forum]: A better way with the latest R5 OSGi APIs is to get the bundle class loader directly. No real need to create BundleProxyClassLoader ... <pre>ClassLoader bundleCL = bundle.adapt(BundleWiring.class).getClassLoader();</pre> Keep in mind, if the bundle is not resolved then adapting it to org.osgi.framework.wiring.BundleWiring will return null.}}
 +
 
 +
<syntaxhighlight lang="java5">
 
import java.io.IOException;
 
import java.io.IOException;
 
import java.net.URL;
 
import java.net.URL;
Line 51: Line 53:
 
}
 
}
 
}
 
}
</pre>
+
</syntaxhighlight>
  
 
[[Category:Equinox]]
 
[[Category:Equinox]]

Latest revision as of 08:31, 10 June 2014

Warning2.png
Better solution with R5 OSGi APIs
From Equinox forum: A better way with the latest R5 OSGi APIs is to get the bundle class loader directly. No real need to create BundleProxyClassLoader ...
ClassLoader bundleCL = bundle.adapt(BundleWiring.class).getClassLoader();
Keep in mind, if the bundle is not resolved then adapting it to org.osgi.framework.wiring.BundleWiring will return null.


import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
 
import org.osgi.framework.Bundle;
 
public class BundleProxyClassLoader extends ClassLoader {
	private Bundle bundle;
	private ClassLoader parent;
 
	public BundleProxyClassLoader(Bundle bundle) {
		this.bundle = bundle;
	}
 
	public BundleProxyClassLoader(Bundle bundle, ClassLoader parent) {
		super(parent);
		this.parent = parent;
	    this.bundle = bundle;
	}
 
	// Note: Both ClassLoader.getResources(...) and bundle.getResources(...) consult 
	// the boot classloader. As a result, BundleProxyClassLoader.getResources(...) 
	// might return duplicate results from the boot classloader. Prior to Java 5 
	// Classloader.getResources was marked final. If your target environment requires
	// at least Java 5 you can prevent the occurence of duplicate boot classloader 
	// resources by overriding ClassLoader.getResources(...) instead of 
	// ClassLoader.findResources(...).   
	public Enumeration findResources(String name) throws IOException {
	    return bundle.getResources(name);
	}
 
	public URL findResource(String name) {
	    return bundle.getResource(name);
	}
 
	public Class findClass(String name) throws ClassNotFoundException {
	    return bundle.loadClass(name);
	}
 
	public URL getResource(String name) {
		return (parent == null) ? findResource(name) : super.getResource(name);
	}
 
	protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
		Class clazz = (parent == null) ? findClass(name) : super.loadClass(name, false);
		if (resolve)
			super.resolveClass(clazz);
 
		return clazz;
	}
}

Back to the top