commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jcar...@apache.org
Subject svn commit: r631336 - in /commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory: cglib/CglibProxyFactory.java util/AbstractSubclassingProxyFactory.java
Date Tue, 26 Feb 2008 19:04:03 GMT
Author: jcarman
Date: Tue Feb 26 11:03:49 2008
New Revision: 631336

URL: http://svn.apache.org/viewvc?rev=631336&view=rev
Log:
Added in proxy class caching

Modified:
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java
    commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/util/AbstractSubclassingProxyFactory.java

Modified: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java?rev=631336&r1=631335&r2=631336&view=diff
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java
(original)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/cglib/CglibProxyFactory.java
Tue Feb 26 11:03:49 2008
@@ -21,6 +21,7 @@
 import net.sf.cglib.proxy.CallbackFilter;
 import net.sf.cglib.proxy.Dispatcher;
 import net.sf.cglib.proxy.Enhancer;
+import net.sf.cglib.proxy.Factory;
 import net.sf.cglib.proxy.MethodProxy;
 import net.sf.cglib.proxy.NoOp;
 import org.apache.commons.proxy.Interceptor;
@@ -28,6 +29,8 @@
 import org.apache.commons.proxy.Invoker;
 import org.apache.commons.proxy.ObjectProvider;
 import org.apache.commons.proxy.factory.util.AbstractSubclassingProxyFactory;
+import org.apache.commons.proxy.factory.util.ProxyClassCache;
+import org.apache.commons.proxy.factory.util.ProxyClassGenerator;
 
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
@@ -44,93 +47,124 @@
  */
 public class CglibProxyFactory extends AbstractSubclassingProxyFactory
 {
-//----------------------------------------------------------------------------------------------------------------------
+//**********************************************************************************************************************
 // Fields
-//----------------------------------------------------------------------------------------------------------------------
+//**********************************************************************************************************************
 
     private static CallbackFilter callbackFilter = new PublicCallbackFilter();
+    private static final ProxyClassCache invokerClassCache = new ProxyClassCache(new InvokerProxyClassGenerator());
+    private static final ProxyClassCache interceptorClassCache = new ProxyClassCache(new
InterceptorProxyClassGenerator());
+    private static final ProxyClassCache delegatorClassCache = new ProxyClassCache(new DelegatorProxyClassGenerator());
 
-//----------------------------------------------------------------------------------------------------------------------
-// ProxyFactory Implementation
-//----------------------------------------------------------------------------------------------------------------------
-
-    public Object createDelegatorProxy( ClassLoader classLoader, ObjectProvider targetProvider,
-                                        Class[] proxyClasses )
-    {
-        final Enhancer enhancer = new Enhancer();
-        enhancer.setClassLoader( classLoader );
-        enhancer.setInterfaces( toInterfaces( proxyClasses ) );
-        enhancer.setSuperclass( getSuperclass( proxyClasses ) );
-        enhancer.setCallbackFilter( callbackFilter );
-        enhancer.setCallbacks( new Callback[]{ new ObjectProviderDispatcher( targetProvider
), NoOp.INSTANCE } );
-        return enhancer.create();
-    }
-
-    public Object createInterceptorProxy( ClassLoader classLoader, Object target, Interceptor
interceptor,
-                                          Class[] proxyClasses )
-    {
-        final Enhancer enhancer = new Enhancer();
-        enhancer.setClassLoader( classLoader );
-        enhancer.setInterfaces( toInterfaces( proxyClasses ) );
-        enhancer.setSuperclass( getSuperclass( proxyClasses ) );
-        enhancer.setCallbackFilter( callbackFilter );
-        enhancer.setCallbacks( new Callback[]{ new InterceptorBridge( target, interceptor
), NoOp.INSTANCE } );
-        return enhancer.create();
-    }
-
-    public Object createInvokerProxy( ClassLoader classLoader, Invoker invoker,
-                                      Class[] proxyClasses )
-    {
-        final Enhancer enhancer = new Enhancer();
-        enhancer.setClassLoader( classLoader );
-        enhancer.setInterfaces( toInterfaces( proxyClasses ) );
-        enhancer.setSuperclass( getSuperclass( proxyClasses ) );
-        enhancer.setCallbackFilter( callbackFilter );
-        enhancer.setCallbacks( new Callback[]{ new InvokerBridge( invoker ), NoOp.INSTANCE
} );
-        return enhancer.create();
+//**********************************************************************************************************************
+// Other Methods
+//**********************************************************************************************************************
+
+    public Object createDelegatorProxy(ClassLoader classLoader, ObjectProvider targetProvider,
+                                       Class[] proxyClasses)
+    {
+        final Class proxyClass = delegatorClassCache.getProxyClass(classLoader, proxyClasses);
+        final Object proxy = instantiate(proxyClass);
+        ((Factory) proxy).setCallbacks(new Callback[]{new ObjectProviderDispatcher(targetProvider),
NoOp.INSTANCE});
+        return proxy;
+    }
+
+    public Object createInterceptorProxy(ClassLoader classLoader, Object target, Interceptor
interceptor,
+                                         Class[] proxyClasses)
+    {
+        final Class proxyClass = interceptorClassCache.getProxyClass(classLoader, proxyClasses);
+        final Object proxy = instantiate(proxyClass);
+        ((Factory) proxy).setCallbacks(new Callback[]{new InterceptorBridge(target, interceptor),
NoOp.INSTANCE});
+        return proxy;
     }
 
-//----------------------------------------------------------------------------------------------------------------------
+    public Object createInvokerProxy(ClassLoader classLoader, Invoker invoker,
+                                     Class[] proxyClasses)
+    {
+        final Class proxyClass = invokerClassCache.getProxyClass(classLoader, proxyClasses);
+        final Object proxy = instantiate(proxyClass);
+        ((Factory) proxy).setCallbacks(new Callback[]{new InvokerBridge(invoker), NoOp.INSTANCE});
+        return proxy;
+    }
+
+//**********************************************************************************************************************
 // Inner Classes
-//----------------------------------------------------------------------------------------------------------------------
+//**********************************************************************************************************************
 
-    private static class PublicCallbackFilter implements CallbackFilter
+    private static class DelegatorProxyClassGenerator implements ProxyClassGenerator
     {
-        public int accept( Method method )
+        public Class generateProxyClass(ClassLoader classLoader, Class[] proxyClasses)
         {
-            return Modifier.isPublic( method.getModifiers() ) ? 0 : 1;
+            final Enhancer enhancer = new Enhancer();
+            enhancer.setClassLoader(classLoader);
+            enhancer.setInterfaces(toInterfaces(proxyClasses));
+            enhancer.setSuperclass(getSuperclass(proxyClasses));
+            enhancer.setCallbackFilter(callbackFilter);
+            enhancer.setCallbackTypes(new Class[]{ObjectProviderDispatcher.class, NoOp.class});
+            enhancer.setUseFactory(true);
+            return enhancer.createClass();
         }
     }
 
-    private class InvokerBridge implements net.sf.cglib.proxy.InvocationHandler
+    private class InterceptorBridge implements net.sf.cglib.proxy.MethodInterceptor
     {
-        private final Invoker original;
+        private final Interceptor inner;
+        private final Object target;
 
-        public InvokerBridge( Invoker original )
+        public InterceptorBridge(Object target, Interceptor inner)
         {
-            this.original = original;
+            this.inner = inner;
+            this.target = target;
         }
 
-        public Object invoke( Object object, Method method, Object[] objects ) throws Throwable
+        public Object intercept(Object object, Method method, Object[] args, MethodProxy
methodProxy) throws Throwable
         {
-            return original.invoke( object, method, objects );
+            return inner.intercept(new MethodProxyInvocation(target, method, args, methodProxy));
         }
     }
 
-    private class InterceptorBridge implements net.sf.cglib.proxy.MethodInterceptor
+    private static class InterceptorProxyClassGenerator implements ProxyClassGenerator
     {
-        private final Interceptor inner;
-        private final Object target;
+        public Class generateProxyClass(ClassLoader classLoader, Class[] proxyClasses)
+        {
+            final Enhancer enhancer = new Enhancer();
+            enhancer.setClassLoader(classLoader);
+            enhancer.setInterfaces(toInterfaces(proxyClasses));
+            enhancer.setSuperclass(getSuperclass(proxyClasses));
+            enhancer.setCallbackFilter(callbackFilter);
+            enhancer.setCallbackTypes(new Class[]{InterceptorBridge.class, NoOp.class});
+            enhancer.setUseFactory(true);
+            return enhancer.createClass();
+        }
+    }
+
+    private class InvokerBridge implements net.sf.cglib.proxy.InvocationHandler
+    {
+        private final Invoker original;
 
-        public InterceptorBridge( Object target, Interceptor inner )
+        public InvokerBridge(Invoker original)
         {
-            this.inner = inner;
-            this.target = target;
+            this.original = original;
+        }
+
+        public Object invoke(Object object, Method method, Object[] objects) throws Throwable
+        {
+            return original.invoke(object, method, objects);
         }
+    }
 
-        public Object intercept( Object object, Method method, Object[] args, MethodProxy
methodProxy ) throws Throwable
+    private static class InvokerProxyClassGenerator implements ProxyClassGenerator
+    {
+        public Class generateProxyClass(ClassLoader classLoader, Class[] proxyClasses)
         {
-            return inner.intercept( new MethodProxyInvocation( target, method, args, methodProxy
) );
+            final Enhancer enhancer = new Enhancer();
+            enhancer.setClassLoader(classLoader);
+            enhancer.setInterfaces(toInterfaces(proxyClasses));
+            enhancer.setSuperclass(getSuperclass(proxyClasses));
+            enhancer.setCallbackFilter(callbackFilter);
+            enhancer.setCallbackTypes(new Class[]{InvokerBridge.class, NoOp.class});
+            enhancer.setUseFactory(true);
+            return enhancer.createClass();
         }
     }
 
@@ -141,7 +175,7 @@
         private final Object[] args;
         private final Object target;
 
-        public MethodProxyInvocation( Object target, Method method, Object[] args, MethodProxy
methodProxy )
+        public MethodProxyInvocation(Object target, Method method, Object[] args, MethodProxy
methodProxy)
         {
             this.target = target;
             this.method = method;
@@ -161,7 +195,7 @@
 
         public Object proceed() throws Throwable
         {
-            return methodProxy.invoke( target, args );
+            return methodProxy.invoke(target, args);
         }
 
         public Object getProxy()
@@ -174,7 +208,7 @@
     {
         private final ObjectProvider delegateProvider;
 
-        public ObjectProviderDispatcher( ObjectProvider delegateProvider )
+        public ObjectProviderDispatcher(ObjectProvider delegateProvider)
         {
             this.delegateProvider = delegateProvider;
         }
@@ -182,6 +216,14 @@
         public Object loadObject()
         {
             return delegateProvider.getObject();
+        }
+    }
+
+    private static class PublicCallbackFilter implements CallbackFilter
+    {
+        public int accept(Method method)
+        {
+            return Modifier.isPublic(method.getModifiers()) ? 0 : 1;
         }
     }
 }

Modified: commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/util/AbstractSubclassingProxyFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/util/AbstractSubclassingProxyFactory.java?rev=631336&r1=631335&r2=631336&view=diff
==============================================================================
--- commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/util/AbstractSubclassingProxyFactory.java
(original)
+++ commons/proper/proxy/trunk/src/main/java/org/apache/commons/proxy/factory/util/AbstractSubclassingProxyFactory.java
Tue Feb 26 11:03:49 2008
@@ -155,5 +155,26 @@
                 throw new ProxyFactoryException( errorMessage.toString() );
         }
     }
+
+    /**
+     * Helper method for instantiating a proxy object from its proxy class.  Uses the default
constructor.
+     * @param proxyClass the proxy class
+     * @return a proxy object
+     */
+    protected static Object instantiate(Class proxyClass)
+    {
+        try
+        {
+            return proxyClass.newInstance();
+        }
+        catch (InstantiationException e)
+        {
+            throw new ProxyFactoryException("Unable to instantiate proxy object from proxy
class.", e);
+        }
+        catch (IllegalAccessException e)
+        {
+            throw new ProxyFactoryException("Unable to instantiate proxy object from proxy
class.", e);
+        }
+    }
 }
 



Mime
View raw message