incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1042199 - in /incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly: META-INF/ src/org/apache/aries/spifly/
Date Sat, 04 Dec 2010 15:09:53 GMT
Author: davidb
Date: Sat Dec  4 15:09:52 2010
New Revision: 1042199

URL: http://svn.apache.org/viewvc?rev=1042199&view=rev
Log:
Work in progress. Generating an additional method in the calling class that can pass appropriate
information to the classloader lookup component.

Modified:
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/META-INF/MANIFEST.MF
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/MyWeavingHook.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/TCCLSetterVisitor.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/META-INF/MANIFEST.MF?rev=1042199&r1=1042198&r2=1042199&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/META-INF/MANIFEST.MF
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/META-INF/MANIFEST.MF
Sat Dec  4 15:09:52 2010
@@ -1,7 +1,7 @@
 Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: SpiFly
-Bundle-SymbolicName: SpiFly
+Bundle-SymbolicName: SpiFly;singleton:=true
 Bundle-Version: 1.0.0.qualifier
 Bundle-Activator: org.apache.aries.spifly.Activator
 Import-Package: org.objectweb.asm;version="[3.2.0,4.0.0)",

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java?rev=1042199&r1=1042198&r2=1042199&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Activator.java
Sat Dec  4 15:09:52 2010
@@ -32,6 +32,8 @@ import org.osgi.util.tracker.BundleTrack
 import org.osgi.util.tracker.ServiceTracker;
 
 public class Activator implements BundleActivator {
+    static Activator activator;
+    
     private ServiceRegistration<WeavingHook> weavingHookService;
     private LogServiceTracker lst;
     private List<LogService> logServices = new CopyOnWriteArrayList<LogService>();
@@ -41,16 +43,19 @@ public class Activator implements Bundle
         lst = new LogServiceTracker(context);
         lst.open();
 
-        WeavingHook wh = new MyWeavingHook();
+        WeavingHook wh = new MyWeavingHook(context);
         weavingHookService = context.registerService(WeavingHook.class, wh,
                 null);
 
         bt = new BundleTracker<List<ServiceRegistration<?>>>(context,
                 Bundle.ACTIVE, new SPIBundleTrackerCustomizer(this, context.getBundle()));
         bt.open();
+        
+        activator = this;
     }
 
     public synchronized void stop(BundleContext context) throws Exception {
+        activator = null;
         bt.close();
         weavingHookService.unregister();
         lst.close();

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/MyWeavingHook.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/MyWeavingHook.java?rev=1042199&r1=1042198&r2=1042199&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/MyWeavingHook.java
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/MyWeavingHook.java
Sat Dec  4 15:09:52 2010
@@ -20,21 +20,37 @@ package org.apache.aries.spifly;
 
 import org.objectweb.asm.ClassReader;
 import org.objectweb.asm.ClassWriter;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.hooks.weaving.WeavingHook;
 import org.osgi.framework.hooks.weaving.WovenClass;
 
 public class MyWeavingHook implements WeavingHook {
+    private final String addedImport;
+    
+    MyWeavingHook(BundleContext context) {
+        Bundle b = context.getBundle();
+        String bver = b.getVersion().toString();
+        String bsn = b.getSymbolicName();
+        
+        addedImport = Util.class.getPackage().getName() + 
+            ";bundle-symbolic-name=" + bsn + 
+            ";bundle-version=" + bver;
+    }
+    
 	@Override
 	public void weave(WovenClass wovenClass) {
+	    // TODO base this on SPI-Consumer header
 	    if (wovenClass.getBundleWiring().getBundle().getSymbolicName().equals("MyTestBundle"))
 	    {
 	        System.out.println("*** WovenClass: " + wovenClass.getClassName());
 	        
 	        ClassReader cr = new ClassReader(wovenClass.getBytes());
 	        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
-	        TCCLSetterVisitor tsv = new TCCLSetterVisitor(cw);
+	        TCCLSetterVisitor tsv = new TCCLSetterVisitor(cw, wovenClass.getClassName());
 	        cr.accept(tsv, 0);
 	        wovenClass.setBytes(cw.toByteArray());
+	        wovenClass.getDynamicImports().add(addedImport);
 	    }			
 	}
 }

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/TCCLSetterVisitor.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/TCCLSetterVisitor.java?rev=1042199&r1=1042198&r2=1042199&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/TCCLSetterVisitor.java
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/TCCLSetterVisitor.java
Sat Dec  4 15:09:52 2010
@@ -25,13 +25,19 @@ import org.objectweb.asm.ClassVisitor;
 import org.objectweb.asm.MethodAdapter;
 import org.objectweb.asm.MethodVisitor;
 import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.Type;
 
 public class TCCLSetterVisitor extends ClassAdapter implements ClassVisitor, Opcodes {
+    private static final String GENERATED_METHOD_NAME = "$$fixContextClassLoader$$";
+
     private static final String UTIL_CLASS = Util.class.getName().replace('.', '/'); 
     private static final String VOID_RETURN_TYPE = "()V";
+    
+    private final String targetClass;
 
-    public TCCLSetterVisitor(ClassVisitor cv) {
+    public TCCLSetterVisitor(ClassVisitor cv, String className) {
         super(cv);
+        this.targetClass = className.replace('.', '/');
     }
 
     @Override
@@ -42,8 +48,32 @@ public class TCCLSetterVisitor extends C
         MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
         return new TCCLSetterMethodVisitor(mv);
     }
+    
+    @Override
+    public void visitEnd() {
+        // Add generated static method
+        MethodVisitor mv = cv.visitMethod(ACC_PRIVATE + ACC_STATIC,
+                GENERATED_METHOD_NAME, "()V", null, null);
+        mv.visitCode();
+        mv.visitLdcInsn("java.util.ServiceLoader");
+        mv.visitLdcInsn("load");
+        String typeIdentifier = "L" + targetClass + ";";
+        mv.visitLdcInsn(Type.getType(typeIdentifier));
+        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class",
+                "getClassLoader", "()Ljava/lang/ClassLoader;");
+        mv.visitMethodInsn(INVOKESTATIC, "org/apache/aries/spifly/Util",
+                "fixContextClassloader",
+                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;)V");
+        mv.visitInsn(RETURN);
+        mv.visitMaxs(3, 0);
+        mv.visitEnd();
+        
+        super.visitEnd();
+    }
+    
 
-    private static class TCCLSetterMethodVisitor extends MethodAdapter implements MethodVisitor
+
+    private class TCCLSetterMethodVisitor extends MethodAdapter implements MethodVisitor
     {
         public TCCLSetterMethodVisitor(MethodVisitor mv) {
             super(mv);
@@ -61,8 +91,8 @@ public class TCCLSetterVisitor extends C
                                 
                 mv.visitMethodInsn(INVOKESTATIC, UTIL_CLASS,
                         "storeContextClassloader", VOID_RETURN_TYPE);
-                mv.visitMethodInsn(INVOKESTATIC, UTIL_CLASS,
-                        "fixContextClassloader", VOID_RETURN_TYPE);
+                mv.visitMethodInsn(INVOKESTATIC, targetClass,
+                        GENERATED_METHOD_NAME, VOID_RETURN_TYPE);
 
                 super.visitMethodInsn(opcode, owner, name, desc);
 

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java?rev=1042199&r1=1042198&r2=1042199&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java
(original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/SpiFly/src/org/apache/aries/spifly/Util.java
Sat Dec  4 15:09:52 2010
@@ -34,11 +34,15 @@ public class Util {
         classLoaders.set(null);
     }
     
-    public static void fixContextClassloader() {
+    public static void fixContextClassloader(String cls, String method, ClassLoader loader)
{
+        System.out.println("~~~ cls: " + cls + " method: " + method + " cl:" + loader);
+        
         Thread.currentThread().setContextClassLoader(findClassLoader());
     }
     
     private static ClassLoader findClassLoader() {
+//        Activator.activator.getClassloaderAdvice()
+        
         ClassLoader cl = Activator.class.getClassLoader();
         if (!(cl instanceof BundleReference)) {
             return null;



Mime
View raw message