You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by an...@apache.org on 2008/10/30 15:28:59 UTC

svn commit: r709171 - in /tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation: JDKProxyFactory.java SCAProxy.java

Author: antelder
Date: Thu Oct 30 07:28:59 2008
New Revision: 709171

URL: http://svn.apache.org/viewvc?rev=709171&view=rev
Log:
TUSCANY-2635: apply patch from  Greg Dritschler to Pool stateless-scoped Java impl instances

Added:
    tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/SCAProxy.java
Modified:
    tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyFactory.java

Modified: tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyFactory.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyFactory.java?rev=709171&r1=709170&r2=709171&view=diff
==============================================================================
--- tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyFactory.java (original)
+++ tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/JDKProxyFactory.java Thu Oct 30 07:28:59 2008
@@ -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 @@
         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 @@
         // 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 @@
         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 @@
      * @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);
     }
 }

Added: tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/SCAProxy.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/SCAProxy.java?rev=709171&view=auto
==============================================================================
--- tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/SCAProxy.java (added)
+++ tuscany/java/sca/modules/core/src/main/java/org/apache/tuscany/sca/core/invocation/SCAProxy.java Thu Oct 30 07:28:59 2008
@@ -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