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 12:06:58 UTC

svn commit: r1042142 - in /incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org: ./ apache/ apache/aries/ apache/aries/spifly/

Author: davidb
Date: Sat Dec  4 11:06:58 2010
New Revision: 1042142

URL: http://svn.apache.org/viewvc?rev=1042142&view=rev
Log: (empty)

Added:
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Activator.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/MyWeavingHook.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Streams.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/TCCLSetterVisitor.java
    incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Util.java

Added: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Activator.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Activator.java?rev=1042142&view=auto
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Activator.java (added)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Activator.java Sat Dec  4 11:06:58 2010
@@ -0,0 +1,37 @@
+/**
+ * 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 org.apache.aries.spifly;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.hooks.weaving.WeavingHook;
+
+public class Activator implements BundleActivator {
+	private ServiceRegistration<WeavingHook> weavingHookService;
+
+	public synchronized void start(BundleContext context) throws Exception {
+		WeavingHook wh = new MyWeavingHook();
+		weavingHookService = context.registerService(WeavingHook.class, wh, null);
+	}
+	
+	public synchronized void stop(BundleContext context) throws Exception {
+		weavingHookService.unregister();
+	}
+}

Added: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/MyWeavingHook.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/MyWeavingHook.java?rev=1042142&view=auto
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/MyWeavingHook.java (added)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/MyWeavingHook.java Sat Dec  4 11:06:58 2010
@@ -0,0 +1,40 @@
+/**
+ * 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 org.apache.aries.spifly;
+
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassWriter;
+import org.osgi.framework.hooks.weaving.WeavingHook;
+import org.osgi.framework.hooks.weaving.WovenClass;
+
+public class MyWeavingHook implements WeavingHook {
+	@Override
+	public void weave(WovenClass wovenClass) {
+	    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);
+	        cr.accept(tsv, 0);
+	        wovenClass.setBytes(cw.toByteArray());
+	    }			
+	}
+}

Added: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Streams.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Streams.java?rev=1042142&view=auto
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Streams.java (added)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Streams.java Sat Dec  4 11:06:58 2010
@@ -0,0 +1,57 @@
+/**
+ * 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 org.apache.aries.spifly;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class Streams {
+    private Streams() {}
+    
+    public static void pump(InputStream is, OutputStream os) throws IOException {        
+        byte[] bytes = new byte[8192];
+
+        int length = 0;
+        int offset = 0;
+
+        while ((length = is.read(bytes, offset, bytes.length - offset)) != -1) {
+            offset += length;
+
+            if (offset == bytes.length) {
+                os.write(bytes, 0, bytes.length);
+                offset = 0;
+            }
+        }
+        if (offset != 0) {
+            os.write(bytes, 0, offset);
+        }
+    }
+    
+    public static byte [] suck(InputStream is) throws IOException {
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try {
+            pump(is, baos);
+            return baos.toByteArray();
+        } finally {
+            is.close();
+        }
+    }
+}

Added: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/TCCLSetterVisitor.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/TCCLSetterVisitor.java?rev=1042142&view=auto
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/TCCLSetterVisitor.java (added)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/TCCLSetterVisitor.java Sat Dec  4 11:06:58 2010
@@ -0,0 +1,74 @@
+/**
+ * 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 org.apache.aries.spifly;
+
+import java.util.Arrays;
+
+import org.objectweb.asm.ClassAdapter;
+import org.objectweb.asm.ClassVisitor;
+import org.objectweb.asm.MethodAdapter;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+
+public class TCCLSetterVisitor extends ClassAdapter implements ClassVisitor, Opcodes {
+
+    public TCCLSetterVisitor(ClassVisitor cv) {
+        super(cv);
+    }
+
+    @Override
+    public MethodVisitor visitMethod(int access, String name, String desc,
+            String signature, String[] exceptions) {
+        System.out.println("@@@ " + access + ": " + name + "#" + desc + "#" + signature + "~" + Arrays.toString(exceptions));
+
+        MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
+        return new TCCLSetterMethodVisitor(mv);
+    }
+
+    private static class TCCLSetterMethodVisitor extends MethodAdapter implements MethodVisitor
+    {
+        public TCCLSetterMethodVisitor(MethodVisitor mv) {
+            super(mv);
+        }
+
+        @Override
+        public void visitMethodInsn(int opcode, String owner, String name,
+                String desc) {
+            System.out.println("### " + opcode + ": " + owner + "#" + name + "#" + desc);
+            
+            if (opcode == INVOKESTATIC &&
+                "java/util/ServiceLoader".equals(owner) &&
+                "load".equals(name)) {
+                System.out.println("+++ Gotcha!");
+                
+                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);
+            }
+        }
+    }
+}

Added: incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Util.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Util.java?rev=1042142&view=auto
==============================================================================
--- incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Util.java (added)
+++ incubator/aries/trunk/spi-fly/contrib/pilot_using_weavinghook/TestWeavingHook/src/org/apache/aries/spifly/Util.java Sat Dec  4 11:06:58 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 org.apache.aries.spifly;
+
+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;
+    }
+}