felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sahoo <sanjeeb.sa...@oracle.com>
Subject ClassLoading issues involving java.lang.reflect.Proxy in OSGi
Date Wed, 01 Jun 2011 20:11:51 GMT
Hi,

I am having a strange problem using dynamic proxies. I am being forced 
to import more classes than required. I have reduced it to a simple 
scenario as shown below:

module 1:
package p1;
public interface A1 extends javax.sql.PooledConnection {
}

Export-Package: p1
Import-Package: javax.sql

module 2:
package p2;
public interface A2 extends javax.naming.Context, p1.A1 {
}

Export-Package: p2
Import-Package: javax.naming, p1

module 3:
Bundle-Activator: p3.Activator
Import-Package: p2, org.osgi.framework

public class Activator implements BundleActivator {
   public void start(BundleContext ctx) throws Exception {
    for (ClassLoader cl : new ClassLoader[]{getClass().getClassLoader(), 
p2.A2.class.getClassLoader()}) {
     try {
      System.out.println(cl);
      Object obj = Proxy.newProxyInstance(cl, new Class[]{p2.A2.class},
                 new InvocationHandler() {
                     public Object invoke(Object proxy, Method method, 
Object[] args) throws Throwable {return null;}
                 });
      System.out.println(obj);
     } catch(Throwable e) {
      e.printStackTrace();
     }
    } // for
   }
   public void stop(BundleContext ctx) throws Exception {}
}

It fails like this:

3.0
java.lang.NoClassDefFoundError: javax.naming.Name not found by m3 [3]
     at $Proxy0.<clinit>(Unknown Source)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 
Method)
     at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
     at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
     at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:588)
     at p3.Activator.start(Activator.java:11)
     at 
org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:633)
     at org.apache.felix.framework.Felix.activateBundle(Felix.java:1822)
     at org.apache.felix.framework.Felix.startBundle(Felix.java:1739)
     at 
org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1143)
     at 
org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:264)
     at java.lang.Thread.run(Thread.java:619)
2.0
java.lang.NoClassDefFoundError: javax.sql.StatementEventListener not 
found by m2 [2]
     at $Proxy1.<clinit>(Unknown Source)
     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native 
Method)
     at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
     at 
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
     at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:588)
     at p3.Activator.start(Activator.java:11)
     at 
org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:633)
     at org.apache.felix.framework.Felix.activateBundle(Felix.java:1822)
     at org.apache.felix.framework.Felix.startBundle(Felix.java:1739)
     at 
org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1143)
     at 
org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:264)
     at java.lang.Thread.run(Thread.java:619)

It seems to me that JVM is assuming that the defining loader of the 
proxy class can load every class that appear in the signature of methods 
that can be invoked using the proxy class. Anybody interested to debug 
can use the attached sample. Just unzip and run run.sh after editing 
path to Felix.

Thanks,
Sahoo


Mime
View raw message