You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by gn...@apache.org on 2011/11/21 16:42:13 UTC

svn commit: r1204546 - in /aries/trunk/proxy: proxy-bundle/ proxy-impl/src/main/java/org/apache/aries/proxy/impl/ proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/ proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/ proxy-impl/src/te...

Author: gnodet
Date: Mon Nov 21 15:42:12 2011
New Revision: 1204546

URL: http://svn.apache.org/viewvc?rev=1204546&view=rev
Log:
[ARIES-786] Ability to control the list of woven classes using system properties

Added:
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/proxy/
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/proxy/impl/
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/proxy/impl/weaving/
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHookTest.java
Modified:
    aries/trunk/proxy/proxy-bundle/pom.xml
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/ProxyManagerActivator.java
    aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHook.java
    aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java

Modified: aries/trunk/proxy/proxy-bundle/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-bundle/pom.xml?rev=1204546&r1=1204545&r2=1204546&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-bundle/pom.xml (original)
+++ aries/trunk/proxy/proxy-bundle/pom.xml Mon Nov 21 15:42:12 2011
@@ -74,7 +74,7 @@
         <dependency>
         	<groupId>org.apache.aries.proxy</groupId>
         	<artifactId>org.apache.aries.proxy.impl</artifactId>
-            <version>0.4</version>
+            <version>0.4.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/ProxyManagerActivator.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/ProxyManagerActivator.java?rev=1204546&r1=1204545&r2=1204546&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/ProxyManagerActivator.java (original)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/ProxyManagerActivator.java Mon Nov 21 15:42:12 2011
@@ -50,7 +50,7 @@ public class ProxyManagerActivator imple
         //if ASM is available then we should also try weaving
         Class<?> cls = Class.forName("org.apache.aries.proxy.impl.weaving.ProxyWeavingHook");
         context.registerService("org.osgi.framework.hooks.weaving.WeavingHook",
-            cls.newInstance(), null);
+            cls.getConstructor(BundleContext.class).newInstance(context), null);
       } catch (Throwable t) {
         //We don't care about this, we just won't have registered the hook
       }

Modified: aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHook.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHook.java?rev=1204546&r1=1204545&r2=1204546&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHook.java (original)
+++ aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHook.java Mon Nov 21 15:42:12 2011
@@ -18,11 +18,14 @@
  */
 package org.apache.aries.proxy.impl.weaving;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.regex.Pattern;
 
 import org.apache.aries.proxy.UnableToProxyException;
 import org.apache.aries.proxy.impl.NLS;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.hooks.weaving.WeavingException;
 import org.osgi.framework.hooks.weaving.WeavingHook;
 import org.osgi.framework.hooks.weaving.WovenClass;
@@ -31,6 +34,12 @@ import org.slf4j.LoggerFactory;
 
 public final class ProxyWeavingHook implements WeavingHook {
 
+  public static final String WEAVING_ENABLED_CLASSES = "org.apache.aries.proxy.weaving.enabled";
+  public static final String WEAVING_DISABLED_CLASSES = "org.apache.aries.proxy.weaving.disabled";
+
+  public static final String WEAVING_ENABLED_CLASSES_DEFAULT = "*";
+  public static final String WEAVING_DISABLED_CLASSES_DEFAULT = "org.objectweb.asm.*,org.slf4j.*,org.apache.log4j.*,javax.*";
+
   private static final Logger LOGGER = LoggerFactory.getLogger(ProxyWeavingHook.class);
   /** An import of the WovenProxy package */
   private static final String IMPORT_A = "org.apache.aries.proxy.weaving";
@@ -40,7 +49,15 @@ public final class ProxyWeavingHook impl
    * on the impl.weaving package
    */
   private static final String IMPORT_B = "org.apache.aries.proxy";
-  
+
+  private final List<Pattern> enabled;
+  private final List<Pattern> disabled;
+
+  public ProxyWeavingHook(BundleContext context) {
+    enabled = parseMatchers(context != null ? context.getProperty(WEAVING_ENABLED_CLASSES) : null, WEAVING_ENABLED_CLASSES_DEFAULT);
+    disabled = parseMatchers(context != null ? context.getProperty(WEAVING_DISABLED_CLASSES) : null, WEAVING_DISABLED_CLASSES_DEFAULT);
+  }
+
   public final void weave(WovenClass wovenClass) {
     
     Bundle b = wovenClass.getBundleWiring().getBundle();
@@ -50,13 +67,12 @@ public final class ProxyWeavingHook impl
         b.getSymbolicName().startsWith("org.apache.aries.util")) {
       return;
     }
-    
-    if(wovenClass.getClassName().startsWith("org.objectweb.asm") || 
-        wovenClass.getClassName().startsWith("org.slf4j") || 
-        wovenClass.getClassName().startsWith("org.apache.log4j") ||
-        wovenClass.getClassName().startsWith("javax."))
-      return;
-    
+
+
+    if (!isEnabled(wovenClass.getClassName()) || isDisabled(wovenClass.getClassName())) {
+        return;
+    }
+
     byte[] bytes = null;
     
     try {
@@ -84,4 +100,37 @@ public final class ProxyWeavingHook impl
       imports.add(IMPORT_B);
     }
   }
+
+    private List<Pattern> parseMatchers(String matchers, String def) {
+        String[] strings = (matchers != null ? matchers : def).split(",");
+        List<Pattern> patterns = new ArrayList<Pattern>();
+        for (String str : strings) {
+            str = str.trim();
+            if (str.length() != 0) {
+                str = str.replaceAll("\\.", "\\\\.");
+                str = str.replaceAll("\\*", ".*");
+                Pattern p = Pattern.compile(str);
+                patterns.add(p);
+            }
+        }
+        return patterns;
+    }
+
+    boolean isEnabled(String className) {
+        return matches(enabled, className);
+    }
+
+    boolean isDisabled(String className) {
+        return matches(disabled, className);
+    }
+
+    private boolean matches(List<Pattern> patterns, String className) {
+        for (Pattern p : patterns) {
+            if (p.matcher(className).matches()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
 }

Modified: aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java?rev=1204546&r1=1204545&r2=1204546&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java (original)
+++ aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/blueprint/proxy/WovenProxyGeneratorTest.java Mon Nov 21 15:42:12 2011
@@ -414,7 +414,7 @@ public class WovenProxyGeneratorTest ext
     Class<?> parent = weavingLoader.loadClass(ProxyTestClassStaticInitOfChildParent.class.getName());
     parent.getMethod("doStuff").invoke(null);
   }
-  
+
   @Override
   protected Object getProxyInstance(Class<?> proxyClass) {
     try {

Added: aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHookTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHookTest.java?rev=1204546&view=auto
==============================================================================
--- aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHookTest.java (added)
+++ aries/trunk/proxy/proxy-impl/src/test/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHookTest.java Mon Nov 21 15:42:12 2011
@@ -0,0 +1,69 @@
+/*
+ * 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.proxy.impl.weaving;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+
+import org.junit.Test;
+import org.osgi.framework.BundleContext;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class ProxyWeavingHookTest {
+
+    @Test
+    public void tesDefault() {
+        BundleContext ctx = (BundleContext) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { BundleContext.class },
+                new InvocationHandler() {
+                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+                        return null;
+                    }
+                });
+        ProxyWeavingHook hook = new ProxyWeavingHook(ctx);
+        assertTrue(hook.isEnabled("org.apache.foo.Bar"));
+        assertTrue(hook.isDisabled("javax.foo.Bar"));
+    }
+
+    @Test
+    public void testFilters() {
+        BundleContext ctx = (BundleContext) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { BundleContext.class },
+                new InvocationHandler() {
+                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+                        if (method.getName().equals("getProperty")) {
+                            if (ProxyWeavingHook.WEAVING_ENABLED_CLASSES.equals(args[0])) {
+                                return "";
+                            }
+                            if (ProxyWeavingHook.WEAVING_DISABLED_CLASSES.equals(args[0])) {
+                                return "org.apache.foo.*";
+                            }
+                        }
+                        return null;
+                    }
+                });
+        ProxyWeavingHook hook = new ProxyWeavingHook(ctx);
+        assertFalse(hook.isEnabled("org.apache.foo.Bar"));
+        assertTrue(hook.isDisabled("org.apache.foo.Bar"));
+        assertTrue(hook.isDisabled("org.apache.foo.bar.Bar"));
+        assertFalse(hook.isDisabled("org.apache.fooBar"));
+        assertFalse(hook.isDisabled("orgXapache.foo.Bar"));
+    }
+}