1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.proxy2.impl;
19
20 import org.apache.commons.proxy2.Interceptor;
21 import org.apache.commons.proxy2.Invoker;
22 import org.apache.commons.proxy2.ObjectProvider;
23 import org.apache.commons.proxy2.ProxyFactory;
24
25 /**
26 * Base abstract {@link ProxyFactory} implementation, primarily providing implementations of the interface methods that
27 * are typically convenience constructs over the other methods.
28 */
29 public abstract class AbstractProxyFactory implements ProxyFactory
30 {
31 /**
32 * Returns true if all <code>proxyClasses</code> are interfaces.
33 *
34 * @param proxyClasses
35 * the proxy classes
36 * @return true if all <code>proxyClasses</code> are interfaces
37 */
38 @Override
39 public boolean canProxy(Class<?>... proxyClasses)
40 {
41 for (Class<?> proxyClass : proxyClasses)
42 {
43 if (!proxyClass.isInterface())
44 {
45 return false;
46 }
47 }
48 return true;
49 }
50
51 /**
52 * Creates a proxy which delegates to the object provided by <code>delegateProvider</code>. The proxy will be
53 * generated using the current thread's "context class loader."
54 *
55 * @param delegateProvider
56 * the delegate provider
57 * @param proxyClasses
58 * the interfaces that the proxy should implement
59 * @return a proxy which delegates to the object provided by the target object provider
60 */
61 @Override
62 public <T> T createDelegatorProxy(ObjectProvider<?> delegateProvider, Class<?>... proxyClasses)
63 {
64 return createDelegatorProxy(Thread.currentThread().getContextClassLoader(), delegateProvider, proxyClasses);
65 }
66
67 /**
68 * Creates a proxy which passes through a {@link Interceptor interceptor} before eventually reaching the
69 * <code>target</code> object. The proxy will be generated using the current thread's "context class loader."
70 *
71 * @param target
72 * the target object
73 * @param interceptor
74 * the method interceptor
75 * @param proxyClasses
76 * the interfaces that the proxy should implement
77 * @return a proxy which passes through a {@link Interceptor interceptor} before eventually reaching the
78 * <code>target</code> object.
79 */
80 @Override
81 public <T> T createInterceptorProxy(Object target, Interceptor interceptor, Class<?>... proxyClasses)
82 {
83 return createInterceptorProxy(Thread.currentThread().getContextClassLoader(), target, interceptor,
84 proxyClasses);
85 }
86
87 /**
88 * Creates a proxy which uses the provided {@link Invoker} to handle all method invocations. The proxy will be
89 * generated using the current thread's "context class loader."
90 *
91 * @param invoker
92 * the invoker
93 * @param proxyClasses
94 * the interfaces that the proxy should implement
95 * @return a proxy which uses the provided {@link Invoker} to handle all method invocations
96 */
97 @Override
98 public <T> T createInvokerProxy(Invoker invoker, Class<?>... proxyClasses)
99 {
100 return createInvokerProxy(Thread.currentThread().getContextClassLoader(), invoker, proxyClasses);
101 }
102
103 }