Skip to content

Instantly share code, notes, and snippets.

@jetgeng
Created December 12, 2012 03:25
Show Gist options
  • Save jetgeng/4264625 to your computer and use it in GitHub Desktop.
Save jetgeng/4264625 to your computer and use it in GitHub Desktop.
replace osgi bundle class loader
package org.gunn.osgi.web.demo;
import java.lang.reflect.Field;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate;
import org.eclipse.osgi.framework.internal.core.BundleHost;
import org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader;
import org.eclipse.osgi.internal.loader.BundleLoader;
import org.eclipse.osgi.internal.loader.BundleLoaderProxy;
@SuppressWarnings("restriction")
public class Activator implements BundleActivator {
private static BundleContext context;
static BundleContext getContext() {
return context;
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
BundleLoaderProxy bundleloaderProxy = ((BundleHost)bundleContext.getBundle()).getLoaderProxy(); //.getClass().getDeclaredField("loader");
Field bundleLoaderField = bundleloaderProxy.getClass().getDeclaredField("loader");
if(bundleLoaderField != null ){
GunnOSGiWebClassLoaderDelegate delegate = new GunnOSGiWebClassLoaderDelegate((BundleHost) bundleContext.getBundle(), bundleloaderProxy);
bundleLoaderField.setAccessible(true);
bundleLoaderField.set(bundleloaderProxy, delegate);
}
bundleContext.getBundle().loadClass("org.gunn.osgi.web.demo.Activator");
System.out.println(context.getBundle().getResources("META-INF/spring/spring.xml"));
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
}
}
package org.gunn.osgi.web.demo;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import org.eclipse.osgi.internal.loader.BundleLoader;
import org.eclipse.osgi.internal.loader.BundleLoaderProxy;
import org.eclipse.osgi.framework.adaptor.ClassLoaderDelegate;
import org.eclipse.osgi.framework.internal.core.BundleHost;
import org.osgi.framework.BundleException;
@SuppressWarnings("restriction")
public class GunnOSGiWebClassLoaderDelegate extends BundleLoader{
protected GunnOSGiWebClassLoaderDelegate(BundleHost bundle,
BundleLoaderProxy proxy) throws BundleException {
super(bundle, proxy);
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
System.out.println("GunnOSGiWebClassLoaderDelegate findClass");
return super.findClass(name);
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
System.out.println("i am in GunnOSGiWebClassLoaderDelegate getResources");
return super.getResources(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment