diff options
2 files changed, 82 insertions, 7 deletions
diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyFactory.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyFactory.java index 0992638318..a2cbd1365b 100644 --- a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyFactory.java +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyFactory.java @@ -19,11 +19,12 @@ package org.apache.tuscany.sca.core.invocation; import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Proxy; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.List; +import java.util.HashMap; +import org.apache.tuscany.sca.core.invocation.SCAProxy; import org.apache.tuscany.sca.core.context.CallableReferenceImpl; import org.apache.tuscany.sca.core.context.ServiceReferenceImpl; import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper; @@ -54,7 +55,7 @@ public class JDKProxyFactory implements ProxyFactory { ServiceReference<T> serviceReference = new ServiceReferenceImpl(interfaze, wire, this); return createProxy(serviceReference); } - + public <T> T createProxy(CallableReference<T> callableReference) throws ProxyCreationException { assert callableReference != null; final Class<T> interfaze = callableReference.getBusinessInterface(); @@ -62,10 +63,10 @@ public class JDKProxyFactory implements ProxyFactory { // Allow privileged access to class loader. Requires RuntimePermission in security policy. ClassLoader cl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { public ClassLoader run() { - return interfaze.getClassLoader(); + return interfaze.getClassLoader(); } }); - Object proxy = Proxy.newProxyInstance(cl, new Class[] {interfaze}, handler); + Object proxy = SCAProxy.newProxyInstance(cl, new Class[] {interfaze}, handler); ((CallableReferenceImpl)callableReference).setProxy(proxy); return interfaze.cast(proxy); } @@ -80,13 +81,13 @@ public class JDKProxyFactory implements ProxyFactory { Class<T> interfaze = callbackReference.getBusinessInterface(); InvocationHandler handler = new JDKCallbackInvocationHandler(messageFactory, callbackReference); ClassLoader cl = interfaze.getClassLoader(); - Object proxy = Proxy.newProxyInstance(cl, new Class[] {interfaze}, handler); + Object proxy = SCAProxy.newProxyInstance(cl, new Class[] {interfaze}, handler); callbackReference.setProxy(proxy); return interfaze.cast(proxy); } public <B, R extends CallableReference<B>> R cast(B target) throws IllegalArgumentException { - InvocationHandler handler = Proxy.getInvocationHandler(target); + InvocationHandler handler = SCAProxy.getInvocationHandler(target); if (handler instanceof JDKInvocationHandler) { return (R)((JDKInvocationHandler)handler).getCallableReference(); } else { @@ -98,6 +99,6 @@ public class JDKProxyFactory implements ProxyFactory { * @see org.apache.tuscany.sca.core.invocation.ProxyFactory#isProxyClass(java.lang.Class) */ public boolean isProxyClass(Class<?> clazz) { - return Proxy.isProxyClass(clazz); + return SCAProxy.isProxyClass(clazz); } } diff --git a/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/SCAProxy.java b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/SCAProxy.java new file mode 100644 index 0000000000..1112d02b7e --- /dev/null +++ b/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/SCAProxy.java @@ -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.tuscany.sca.core.invocation;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.Constructor;
+import java.util.WeakHashMap;
+
+public class SCAProxy extends Proxy
+{
+ protected SCAProxy (InvocationHandler handler) {
+ super(handler);
+ }
+
+ // This is a cache containing the proxy class constructor for each business interface.
+ // This improves performance compared to calling Proxy.newProxyInstance()
+ // every time that a proxy is needed.
+ private static WeakHashMap cache = new WeakHashMap<Class, Object>();
+
+ public static Object newProxyInstance(ClassLoader classloader, Class aclass[], InvocationHandler invocationhandler)
+ throws IllegalArgumentException
+ {
+ try {
+ if(invocationhandler == null)
+ throw new NullPointerException();
+ // Lookup cached constructor. aclass[0] is the reference's business interface.
+ Constructor proxyCTOR;
+ synchronized(cache) {
+ proxyCTOR = (Constructor) cache.get(aclass[0]);
+ }
+ if(proxyCTOR == null) {
+ Class proxyClass = getProxyClass(classloader, aclass);
+ proxyCTOR = proxyClass.getConstructor(constructorParams);
+ synchronized(cache){
+ cache.put(aclass[0],proxyCTOR);
+ }
+ }
+ return proxyCTOR.newInstance(new Object[] { invocationhandler });
+ }
+ catch(NoSuchMethodException e) {
+ throw new InternalError(e.toString());
+ }
+ catch(IllegalAccessException e) {
+ throw new InternalError(e.toString());
+ }
+ catch (InstantiationException e) {
+ throw new InternalError(e.toString());
+ }
+ catch (InvocationTargetException e) {
+ throw new InternalError(e.toString());
+ }
+ }
+
+ private static final Class constructorParams[] = { InvocationHandler.class };
+
+}
\ No newline at end of file |