You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by da...@apache.org on 2010/12/04 11:55:09 UTC

svn commit: r1042137 - in /incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook: META-INF/ src/testweavinghook/

Author: davidb
Date: Sat Dec  4 10:55:09 2010
New Revision: 1042137

URL: http://svn.apache.org/viewvc?rev=1042137&view=rev
Log:
Work in Progress - applied ASM weaving to insert method calls that store, fix up and restore the TCCL.

Added:
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Util.java
Modified:
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/META-INF/MANIFEST.MF
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Activator.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/MyWeavingHook.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/TCCLSetterVisitor.java

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/META-INF/MANIFEST.MF?rev=1042137&r1=1042136&r2=1042137&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/META-INF/MANIFEST.MF (original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/META-INF/MANIFEST.MF Sat Dec  4 10:55:09 2010
@@ -10,3 +10,4 @@ Import-Package: org.objectweb.asm;versio
  org.osgi.framework;version="1.3.0",
  org.osgi.framework.hooks.weaving;version="1.0.0",
  org.osgi.framework.wiring;version="1.0.0"
+Export-Package: testweavinghook

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Activator.java?rev=1042137&r1=1042136&r2=1042137&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Activator.java (original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Activator.java Sat Dec  4 10:55:09 2010
@@ -24,18 +24,14 @@ import org.osgi.framework.ServiceRegistr
 import org.osgi.framework.hooks.weaving.WeavingHook;
 
 public class Activator implements BundleActivator {
-	private ServiceRegistration<WeavingHook> reg;
+	private ServiceRegistration<WeavingHook> weavingHookService;
 
-	public void start(BundleContext context) throws Exception {
-		System.out.println("Hello World!!");
-		
+	public synchronized void start(BundleContext context) throws Exception {
 		WeavingHook wh = new MyWeavingHook();
-		reg = context.registerService(WeavingHook.class, wh, null);
+		weavingHookService = context.registerService(WeavingHook.class, wh, null);
 	}
 	
-	public void stop(BundleContext context) throws Exception {
-		reg.unregister();
-		
-		System.out.println("Goodbye World!!");
+	public synchronized void stop(BundleContext context) throws Exception {
+		weavingHookService.unregister();
 	}
 }

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/MyWeavingHook.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/MyWeavingHook.java?rev=1042137&r1=1042136&r2=1042137&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/MyWeavingHook.java (original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/MyWeavingHook.java Sat Dec  4 10:55:09 2010
@@ -18,18 +18,12 @@
  */
 package testweavinghook;
 
-import java.io.IOException;
-import java.net.URL;
-import java.util.List;
-
 import org.objectweb.asm.ClassReader;
 import org.objectweb.asm.ClassWriter;
 import org.osgi.framework.hooks.weaving.WeavingHook;
 import org.osgi.framework.hooks.weaving.WovenClass;
-import org.osgi.framework.wiring.BundleWiring;
 
 public class MyWeavingHook implements WeavingHook {
-
 	@Override
 	public void weave(WovenClass wovenClass) {
 	    if (wovenClass.getBundleWiring().getBundle().getSymbolicName().equals("MyTestBundle"))
@@ -41,24 +35,6 @@ public class MyWeavingHook implements We
 	        TCCLSetterVisitor tsv = new TCCLSetterVisitor(cw);
 	        cr.accept(tsv, 0);
 	        wovenClass.setBytes(cw.toByteArray());
-	    }
-	    /*
-		if (wovenClass.getClassName().startsWith("mytestbundle")) {
-			BundleWiring bw = wovenClass.getBundleWiring();
-			String fileName = wovenClass.getClassName().replace('.', '/') + ".class";
-			URL res = bw.getBundle().getResource("/altclasses/" + fileName);
-			if (res != null) {
-				System.out.println("*** Found an alternative class: " + res);
-				try {
-					wovenClass.setBytes(Streams.suck(res.openStream()));
-					List<String> imports = wovenClass.getDynamicImports();
-					imports.add("org.apache.aries.spifly.util");
-					imports.add("org.osgi.util.tracker");
-					imports.add("org.osgi.framework.wiring");
-				} catch (IOException e) {
-					e.printStackTrace();
-				}
-			}				
-		}*/			
+	    }			
 	}
 }

Modified: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/TCCLSetterVisitor.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/TCCLSetterVisitor.java?rev=1042137&r1=1042136&r2=1042137&view=diff
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/TCCLSetterVisitor.java (original)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/TCCLSetterVisitor.java Sat Dec  4 10:55:09 2010
@@ -57,12 +57,18 @@ public class TCCLSetterVisitor extends C
                 "load".equals(name)) {
                 System.out.println("+++ Gotcha!");
                 
-                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
-                mv.visitLdcInsn("Bleeeeeh");
-                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
+                mv.visitMethodInsn(INVOKESTATIC, "testweavinghook/Util",
+                        "storeContextClassloader", "()V");
+                mv.visitMethodInsn(INVOKESTATIC, "testweavinghook/Util",
+                        "fixContextClassloader", "()V");
+
+                super.visitMethodInsn(opcode, owner, name, desc);
+
+                mv.visitMethodInsn(INVOKESTATIC, "testweavinghook/Util",
+                        "restoreContextClassloader", "()V");
+            } else {                
+                super.visitMethodInsn(opcode, owner, name, desc);
             }
-                
-            super.visitMethodInsn(opcode, owner, name, desc);
         }
     }
 }

Added: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Util.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Util.java?rev=1042137&view=auto
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Util.java (added)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/testweavinghook/Util.java Sat Dec  4 10:55:09 2010
@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package testweavinghook;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleReference;
+import org.osgi.framework.wiring.BundleWiring;
+
+public class Util {
+    static ThreadLocal<ClassLoader> classLoaders = new ThreadLocal<ClassLoader>();
+    
+    public static void storeContextClassloader() {
+        classLoaders.set(Thread.currentThread().getContextClassLoader());
+    }
+    
+    public static void restoreContextClassloader() {
+        Thread.currentThread().setContextClassLoader(classLoaders.get());
+        classLoaders.set(null);
+    }
+    
+    public static void fixContextClassloader() {
+        Thread.currentThread().setContextClassLoader(findClassLoader());
+    }
+    
+    private static ClassLoader findClassLoader() {
+        ClassLoader cl = Activator.class.getClassLoader();
+        if (!(cl instanceof BundleReference)) {
+            return null;
+        }
+        
+        BundleReference br = (BundleReference) cl;      
+        for (Bundle b : br.getBundle().getBundleContext().getBundles()) {
+            // TODO find the appropriate bundle
+            if ("MyServiceImpl".equals(b.getSymbolicName())) {
+                BundleWiring bw = b.adapt(BundleWiring.class);
+                if (bw != null)
+                    return bw.getClassLoader();
+            }           
+        }
+        return null;
+    }
+}