Author: mbenson Date: Thu Jul 22 18:14:42 2010 New Revision: 966779 URL: http://svn.apache.org/viewvc?rev=966779&view=rev Log: add ProxyFactory that delegates to discovered service providers
Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/DefaultProxyFactory.java Modified: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/ProxyUtils.java Added: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/DefaultProxyFactory.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/DefaultProxyFactory.java?rev=966779&view=auto ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/DefaultProxyFactory.java (added) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/DefaultProxyFactory.java Thu Jul 22 18:14:42 2010 @@ -0,0 +1,116 @@ +/* + * 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.commons.proxy2; + +import java.util.Arrays; +import java.util.ServiceLoader; + +/** + * {...@link ProxyFactory} implementation that delegates to the first discovered + * {...@link ProxyFactory} service provider that {...@link #canProxy(Class...)}. + * + * @author Matt Benson + */ +class DefaultProxyFactory implements ProxyFactory { + /** Shared instance */ + static final DefaultProxyFactory INSTANCE = new DefaultProxyFactory(); + + private static final ServiceLoader<ProxyFactory> SERVICES = ServiceLoader + .load(ProxyFactory.class); + + /** + * {...@inheritdoc} + */ + @Override + public boolean canProxy(Class<?>... proxyClasses) { + for (ProxyFactory proxyFactory : SERVICES) { + if (proxyFactory.canProxy(proxyClasses)) { + return true; + } + } + return false; + } + + /** + * {...@inheritdoc} + */ + @Override + public <T> T createDelegatorProxy(ObjectProvider<?> delegateProvider, + Class<?>... proxyClasses) { + return getCapableProxyFactory(proxyClasses).createDelegatorProxy( + delegateProvider, proxyClasses); + } + + /** + * {...@inheritdoc} + */ + @Override + public <T> T createDelegatorProxy(ClassLoader classLoader, + ObjectProvider<?> delegateProvider, Class<?>... proxyClasses) { + return getCapableProxyFactory(proxyClasses).createDelegatorProxy( + classLoader, delegateProvider, proxyClasses); + } + + /** + * {...@inheritdoc} + */ + @Override + public <T> T createInterceptorProxy(Object target, Interceptor interceptor, + Class<?>... proxyClasses) { + return getCapableProxyFactory(proxyClasses).createInterceptorProxy( + target, interceptor, proxyClasses); + } + + /** + * {...@inheritdoc} + */ + @Override + public <T> T createInterceptorProxy(ClassLoader classLoader, Object target, + Interceptor interceptor, Class<?>... proxyClasses) { + return getCapableProxyFactory(proxyClasses).createInterceptorProxy( + classLoader, target, interceptor, proxyClasses); + } + + /** + * {...@inheritdoc} + */ + @Override + public <T> T createInvokerProxy(Invoker invoker, Class<?>... proxyClasses) { + return getCapableProxyFactory(proxyClasses).createInvokerProxy(invoker, + proxyClasses); + } + + /** + * {...@inheritdoc} + */ + @Override + public <T> T createInvokerProxy(ClassLoader classLoader, Invoker invoker, + Class<?>... proxyClasses) { + return getCapableProxyFactory(proxyClasses).createInvokerProxy( + classLoader, invoker, proxyClasses); + } + + private ProxyFactory getCapableProxyFactory(Class<?>... proxyClasses) { + for (ProxyFactory proxyFactory : SERVICES) { + if (proxyFactory.canProxy(proxyClasses)) { + return proxyFactory; + } + } + throw new IllegalArgumentException("Could not proxy " + + Arrays.toString(proxyClasses)); + } +} Modified: commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/ProxyUtils.java URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/ProxyUtils.java?rev=966779&r1=966778&r2=966779&view=diff ============================================================================== --- commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/ProxyUtils.java (original) +++ commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/ProxyUtils.java Thu Jul 22 18:14:42 2010 @@ -79,7 +79,7 @@ public class ProxyUtils */ public static Object createNullObject(ProxyFactory proxyFactory, Class<?>[] proxyClasses) { - return proxyFactory.createInvokerProxy(new NullInvoker(), proxyClasses); + return proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, proxyClasses); } /** @@ -92,7 +92,7 @@ public class ProxyUtils */ public static Object createNullObject(ProxyFactory proxyFactory, ClassLoader classLoader, Class<?>[] proxyClasses) { - return proxyFactory.createInvokerProxy(classLoader, new NullInvoker(), proxyClasses); + return proxyFactory.createInvokerProxy(classLoader, NullInvoker.INSTANCE, proxyClasses); } /** @@ -204,4 +204,11 @@ public class ProxyUtils method.getParameterTypes().length == 0; } + /** + * Get a {...@link ProxyFactory} that delegates to discoverable {...@link ProxyFactory} service providers. + * @return {...@link ProxyFactory} + */ + public static ProxyFactory proxyFactory() { + return DefaultProxyFactory.INSTANCE; + } }