001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.proxy2.impl; 019 020import org.apache.commons.proxy2.Interceptor; 021import org.apache.commons.proxy2.Invoker; 022import org.apache.commons.proxy2.ObjectProvider; 023import org.apache.commons.proxy2.ProxyFactory; 024 025/** 026 * Base abstract {@link ProxyFactory} implementation, primarily providing implementations of the interface methods that 027 * are typically convenience constructs over the other methods. 028 */ 029public abstract class AbstractProxyFactory implements ProxyFactory 030{ 031 /** 032 * Returns true if all <code>proxyClasses</code> are interfaces. 033 * 034 * @param proxyClasses 035 * the proxy classes 036 * @return true if all <code>proxyClasses</code> are interfaces 037 */ 038 @Override 039 public boolean canProxy(Class<?>... proxyClasses) 040 { 041 for (Class<?> proxyClass : proxyClasses) 042 { 043 if (!proxyClass.isInterface()) 044 { 045 return false; 046 } 047 } 048 return true; 049 } 050 051 /** 052 * Creates a proxy which delegates to the object provided by <code>delegateProvider</code>. The proxy will be 053 * generated using the current thread's "context class loader." 054 * 055 * @param delegateProvider 056 * the delegate provider 057 * @param proxyClasses 058 * the interfaces that the proxy should implement 059 * @return a proxy which delegates to the object provided by the target object provider 060 */ 061 @Override 062 public <T> T createDelegatorProxy(ObjectProvider<?> delegateProvider, Class<?>... proxyClasses) 063 { 064 return createDelegatorProxy(Thread.currentThread().getContextClassLoader(), delegateProvider, proxyClasses); 065 } 066 067 /** 068 * Creates a proxy which passes through a {@link Interceptor interceptor} before eventually reaching the 069 * <code>target</code> object. The proxy will be generated using the current thread's "context class loader." 070 * 071 * @param target 072 * the target object 073 * @param interceptor 074 * the method interceptor 075 * @param proxyClasses 076 * the interfaces that the proxy should implement 077 * @return a proxy which passes through a {@link Interceptor interceptor} before eventually reaching the 078 * <code>target</code> object. 079 */ 080 @Override 081 public <T> T createInterceptorProxy(Object target, Interceptor interceptor, Class<?>... proxyClasses) 082 { 083 return createInterceptorProxy(Thread.currentThread().getContextClassLoader(), target, interceptor, 084 proxyClasses); 085 } 086 087 /** 088 * Creates a proxy which uses the provided {@link Invoker} to handle all method invocations. The proxy will be 089 * generated using the current thread's "context class loader." 090 * 091 * @param invoker 092 * the invoker 093 * @param proxyClasses 094 * the interfaces that the proxy should implement 095 * @return a proxy which uses the provided {@link Invoker} to handle all method invocations 096 */ 097 @Override 098 public <T> T createInvokerProxy(Invoker invoker, Class<?>... proxyClasses) 099 { 100 return createInvokerProxy(Thread.currentThread().getContextClassLoader(), invoker, proxyClasses); 101 } 102 103}