You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by no...@apache.org on 2012/02/12 17:50:13 UTC
svn commit: r1243277 - in /aries/trunk/proxy: proxy-api/
proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/ proxy-bundle/
proxy-impl/ proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/
proxy-itests/src/test/java/org/apache/aries/pro...
Author: not
Date: Sun Feb 12 16:50:12 2012
New Revision: 1243277
URL: http://svn.apache.org/viewvc?rev=1243277&view=rev
Log:
ARIES-826 Add the ability to write a service to control the way proxy weaving works.
Added:
aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/
aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/ProxyWeavingController.java
aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/WeavingHelper.java
aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/packageinfo
Modified:
aries/trunk/proxy/proxy-api/pom.xml
aries/trunk/proxy/proxy-bundle/pom.xml
aries/trunk/proxy/proxy-impl/pom.xml
aries/trunk/proxy/proxy-impl/src/main/java/org/apache/aries/proxy/impl/weaving/ProxyWeavingHook.java
aries/trunk/proxy/proxy-itests/src/test/java/org/apache/aries/proxy/itests/WeavingProxyTest.java
Modified: aries/trunk/proxy/proxy-api/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-api/pom.xml?rev=1243277&r1=1243276&r2=1243277&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-api/pom.xml (original)
+++ aries/trunk/proxy/proxy-api/pom.xml Sun Feb 12 16:50:12 2012
@@ -45,7 +45,7 @@
<!-- Export package versions are maintained in packageinfo files -->
<aries.osgi.export.pkg>
- org.apache.aries.proxy
+ org.apache.aries.proxy*
</aries.osgi.export.pkg>
<!--
We use the range macro to get a 0.x to 0.x+1 version range while Aries is still
@@ -54,6 +54,7 @@
-->
<aries.osgi.import.pkg>
org.apache.aries.*;version="$<range;[==,=+)>",
+ org.osgi.framework.hooks.weaving;resolution:=optional,
*
</aries.osgi.import.pkg>
<aries.osgi.private.pkg>
@@ -64,6 +65,7 @@
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
+ <version>4.3.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Added: aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/ProxyWeavingController.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/ProxyWeavingController.java?rev=1243277&view=auto
==============================================================================
--- aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/ProxyWeavingController.java (added)
+++ aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/ProxyWeavingController.java Sun Feb 12 16:50:12 2012
@@ -0,0 +1,45 @@
+/*
+ * 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 WARRANTIESOR 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.weavinghook;
+
+import org.osgi.framework.hooks.weaving.WovenClass;
+
+/**
+ * Services of this interface are used by the ProxyManager's weaving implementation to
+ * decide if a specific bundle should be subject to weaving.
+ *
+ * <p>If multiple ProxyWeavingController are registered all will be consulted to decide
+ * whether to weave or not. As soon as one service says to weave a class then
+ * it will be woven and following services may not be consulted.
+ * </p>
+ */
+public interface ProxyWeavingController
+{
+ /**
+ * Returns true if the class should be subject to proxy weaving. If it returns
+ * false then the class will not be weaved. The result of this method is immutable
+ * for a given bundle. That means repeated calls given the same bundle MUST
+ * return the same response.
+ *
+ * @param classToWeave the class that is a candidate to be weaved.
+ * @param helper a helper calss to allow the implementation to make intelligent weaving decisions.
+ * @return true if it should be woven, false otherwise.
+ */
+ public boolean shouldWeave(WovenClass classToWeave, WeavingHelper helper);
+}
\ No newline at end of file
Added: aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/WeavingHelper.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/WeavingHelper.java?rev=1243277&view=auto
==============================================================================
--- aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/WeavingHelper.java (added)
+++ aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/WeavingHelper.java Sun Feb 12 16:50:12 2012
@@ -0,0 +1,47 @@
+/*
+ * 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 WARRANTIESOR 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.weavinghook;
+
+import org.osgi.framework.hooks.weaving.WovenClass;
+
+/**
+ * This provides helper methods to allow a ProxyWeavingController to make
+ * sensible decisions without needing to know how the ProxyManager has implemented
+ * the weaving support.
+ *
+ * @noimplement
+ */
+public interface WeavingHelper
+{
+ /**
+ * Tests to see if the provided class has been woven for proxying.
+ *
+ * @param c the class to test
+ * @return true if it is woven, false otherwise.
+ */
+ public boolean isWoven(Class<?> c);
+ /**
+ * Tests to see if the super class of the provided WovenClass has
+ * been woven to support proxying.
+ *
+ * @param wovenClass the class whose parent should be tested.
+ * @return true if it is woven, false otherwise.
+ */
+ public boolean isSuperClassWoven(WovenClass wovenClass);
+}
\ No newline at end of file
Added: aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/packageinfo
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/packageinfo?rev=1243277&view=auto
==============================================================================
--- aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/packageinfo (added)
+++ aries/trunk/proxy/proxy-api/src/main/java/org/apache/aries/proxy/weavinghook/packageinfo Sun Feb 12 16:50:12 2012
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+version 0.1
Modified: aries/trunk/proxy/proxy-bundle/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-bundle/pom.xml?rev=1243277&r1=1243276&r2=1243277&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-bundle/pom.xml (original)
+++ aries/trunk/proxy/proxy-bundle/pom.xml Sun Feb 12 16:50:12 2012
@@ -48,7 +48,8 @@
</aries.osgi.activator>
<aries.osgi.export.pkg>
org.apache.aries.proxy;,
- org.apache.aries.proxy.weaving;
+ org.apache.aries.proxy.weaving;,
+ org.apache.aries.proxy.weavinghook;
</aries.osgi.export.pkg>
<aries.osgi.import.pkg>
org.apache.aries.*;version="$<range;[==,=+)>",
Modified: aries/trunk/proxy/proxy-impl/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-impl/pom.xml?rev=1243277&r1=1243276&r2=1243277&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-impl/pom.xml (original)
+++ aries/trunk/proxy/proxy-impl/pom.xml Sun Feb 12 16:50:12 2012
@@ -94,6 +94,12 @@
</dependency>
<dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
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=1243277&r1=1243276&r2=1243277&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 Sun Feb 12 16:50:12 2012
@@ -24,15 +24,20 @@ import java.util.regex.Pattern;
import org.apache.aries.proxy.UnableToProxyException;
import org.apache.aries.proxy.impl.NLS;
+import org.apache.aries.proxy.weaving.WovenProxy;
+import org.apache.aries.proxy.weavinghook.ProxyWeavingController;
+import org.apache.aries.proxy.weavinghook.WeavingHelper;
+import org.objectweb.asm.ClassReader;
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;
+import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public final class ProxyWeavingHook implements WeavingHook {
+public final class ProxyWeavingHook implements WeavingHook, WeavingHelper {
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";
@@ -52,10 +57,13 @@ public final class ProxyWeavingHook impl
private final List<Pattern> enabled;
private final List<Pattern> disabled;
+ private final ServiceTracker controllers;
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);
+ controllers = new ServiceTracker(context, ProxyWeavingController.class.getName(), null);
+ controllers.open();
}
public final void weave(WovenClass wovenClass) {
@@ -68,36 +76,37 @@ public final class ProxyWeavingHook impl
return;
}
-
if (!isEnabled(wovenClass.getClassName()) || isDisabled(wovenClass.getClassName())) {
return;
}
- byte[] bytes = null;
-
- try {
- bytes = WovenProxyGenerator.getWovenProxy(wovenClass.getBytes(),
- wovenClass.getBundleWiring().getClassLoader());
+ if (shouldWeave(wovenClass)) {
+ byte[] bytes = null;
- } catch (Exception e) {
- if(e instanceof RuntimeException &&
- e.getCause() instanceof UnableToProxyException){
- //This is a weaving failure that should be logged, but the class
- //can still be loaded
- LOGGER.trace(NLS.MESSAGES.getMessage("cannot.weave", wovenClass.getClassName()), e);
- } else {
- String failureMessage = NLS.MESSAGES.getMessage("fatal.weaving.failure", wovenClass.getClassName());
- //This is a failure that should stop the class loading!
- LOGGER.error(failureMessage, e);
- throw new WeavingException(failureMessage, e);
+ try {
+ bytes = WovenProxyGenerator.getWovenProxy(wovenClass.getBytes(),
+ wovenClass.getBundleWiring().getClassLoader());
+
+ } catch (Exception e) {
+ if(e instanceof RuntimeException &&
+ e.getCause() instanceof UnableToProxyException){
+ //This is a weaving failure that should be logged, but the class
+ //can still be loaded
+ LOGGER.trace(NLS.MESSAGES.getMessage("cannot.weave", wovenClass.getClassName()), e);
+ } else {
+ String failureMessage = NLS.MESSAGES.getMessage("fatal.weaving.failure", wovenClass.getClassName());
+ //This is a failure that should stop the class loading!
+ LOGGER.error(failureMessage, e);
+ throw new WeavingException(failureMessage, e);
+ }
+ }
+
+ if(bytes != null && bytes.length != 0) {
+ wovenClass.setBytes(bytes);
+ List<String> imports = wovenClass.getDynamicImports();
+ imports.add(IMPORT_A);
+ imports.add(IMPORT_B);
}
- }
-
- if(bytes != null && bytes.length != 0) {
- wovenClass.setBytes(bytes);
- List<String> imports = wovenClass.getDynamicImports();
- imports.add(IMPORT_A);
- imports.add(IMPORT_B);
}
}
@@ -132,5 +141,47 @@ public final class ProxyWeavingHook impl
}
return false;
}
+
+ public boolean isWoven(Class<?> clazz)
+ {
+ return WovenProxy.class.isAssignableFrom(clazz);
+ }
+
+ public boolean isSuperClassWoven(WovenClass wovenClass)
+ {
+ ClassReader cReader = new ClassReader(wovenClass.getBytes());
+ try {
+ Class<?> superClass = Class.forName(cReader.getSuperName().replace('/', '.'), false,
+ wovenClass.getBundleWiring().getClassLoader());
+ return WovenProxy.class.isAssignableFrom(superClass);
+ } catch (ClassNotFoundException e) {
+ String failureMessage = NLS.MESSAGES.getMessage("fatal.weaving.failure", wovenClass.getClassName());
+ //This is a failure that should stop the class loading!
+ LOGGER.error(failureMessage, e);
+ throw new WeavingException(failureMessage, e);
+ }
+ }
+
+ private boolean shouldWeave(WovenClass wovenClass)
+ {
+ // assume we weave
+ boolean result = true;
+ Object[] cs = controllers.getServices();
+
+ // if we have at least 1 weaving controller
+ if (cs != null && cs.length > 0) {
+ // first of all set to false.
+ result = false;
+ for (Object obj : cs) {
+ ProxyWeavingController c = (ProxyWeavingController) obj;
+ if (c.shouldWeave(wovenClass, this)) {
+ // exit as soon as we get told to weave, otherwise keep going.
+ return true;
+ }
+ }
+ }
+
+ return result;
+ }
-}
+}
\ No newline at end of file
Modified: aries/trunk/proxy/proxy-itests/src/test/java/org/apache/aries/proxy/itests/WeavingProxyTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/proxy/proxy-itests/src/test/java/org/apache/aries/proxy/itests/WeavingProxyTest.java?rev=1243277&r1=1243276&r2=1243277&view=diff
==============================================================================
--- aries/trunk/proxy/proxy-itests/src/test/java/org/apache/aries/proxy/itests/WeavingProxyTest.java (original)
+++ aries/trunk/proxy/proxy-itests/src/test/java/org/apache/aries/proxy/itests/WeavingProxyTest.java Sun Feb 12 16:50:12 2012
@@ -27,8 +27,11 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.Callable;
+import org.apache.aries.proxy.FinalModifierException;
import org.apache.aries.proxy.ProxyManager;
import org.apache.aries.proxy.weaving.WovenProxy;
+import org.apache.aries.proxy.weavinghook.ProxyWeavingController;
+import org.apache.aries.proxy.weavinghook.WeavingHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
@@ -36,6 +39,7 @@ import org.ops4j.pax.exam.container.def.
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.hooks.weaving.WovenClass;
@RunWith(JUnit4TestRunner.class)
public class WeavingProxyTest extends AbstractProxyTest
@@ -86,6 +90,31 @@ public class WeavingProxyTest extends Ab
if(!!!(o instanceof WovenProxy))
fail("Proxy should be woven!");
}
+
+ @Test(expected = FinalModifierException.class)
+ public void checkProxyController() throws Exception
+ {
+ context().registerService(ProxyWeavingController.class.getName(), new ProxyWeavingController() {
+
+ public boolean shouldWeave(WovenClass arg0, WeavingHelper arg1)
+ {
+ return false;
+ }
+ }, null);
+
+ ProxyManager mgr = context().getService(ProxyManager.class);
+ Bundle b = FrameworkUtil.getBundle(this.getClass());
+ Callable<Object> c = new TestCallable();
+ Collection<Class<?>> classes = new ArrayList<Class<?>>();
+ Runnable r = new Runnable() {
+ public final void run() {
+ }
+ };
+ classes.add(r.getClass());
+ Object o = mgr.createDelegatingProxy(b, classes, c, r);
+ if(o instanceof WovenProxy)
+ fail("Proxy should not have been woven!");
+ }
@org.ops4j.pax.exam.junit.Configuration
public static Option[] configuration() {