KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > DefaultAopProxyFactory


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.framework;
18
19 import org.springframework.aop.SpringProxy;
20 import org.springframework.util.ClassUtils;
21
22 /**
23  * Default {@link AopProxyFactory} implementation,
24  * creating either a CGLIB proxy or a JDK dynamic proxy.
25  *
26  * <p>Creates a CGLIB proxy if one the following is true
27  * for a given {@link AdvisedSupport} instance:
28  * <ul>
29  * <li>the "optimize" flag is set
30  * <li>the "proxyTargetClass" flag is set
31  * <li>no proxy interfaces have been specified
32  * </ul>
33  *
34  * <p>Note that the CGLIB library classes have to be present on
35  * the class path if an actual CGLIB proxy needs to be created.
36  *
37  * <p>In general, specify "proxyTargetClass" to enforce a CGLIB proxy,
38  * or specify one or more interfaces to use a JDK dynamic proxy.
39  *
40  * @author Rod Johnson
41  * @author Juergen Hoeller
42  * @since 12.03.2004
43  * @see AdvisedSupport#setOptimize
44  * @see AdvisedSupport#setProxyTargetClass
45  * @see AdvisedSupport#setInterfaces
46  */

47 public class DefaultAopProxyFactory implements AopProxyFactory {
48
49     /** Whether the CGLIB2 library is present on the classpath */
50     private static final boolean cglibAvailable =
51             ClassUtils.isPresent("net.sf.cglib.proxy.Enhancer", DefaultAopProxyFactory.class.getClassLoader());
52
53
54     public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
55         if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
56             Class JavaDoc targetClass = config.getTargetClass();
57             if (targetClass == null) {
58                 throw new AopConfigException("TargetSource cannot determine target class: " +
59                         "Either an interface or a target is required for proxy creation.");
60             }
61             if (targetClass.isInterface()) {
62                 return new JdkDynamicAopProxy(config);
63             }
64             if (!cglibAvailable) {
65                 throw new AopConfigException(
66                         "Cannot proxy target class because CGLIB2 is not available. " +
67                         "Add CGLIB to the class path or specify proxy interfaces.");
68             }
69             return CglibProxyFactory.createCglibProxy(config);
70         }
71         else {
72             return new JdkDynamicAopProxy(config);
73         }
74     }
75
76     /**
77      * Determine whether the supplied {@link AdvisedSupport} has only the
78      * {@link org.springframework.aop.SpringProxy} interface specified
79      * (or no proxy interfaces specified at all).
80      */

81     private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
82         Class JavaDoc[] interfaces = config.getProxiedInterfaces();
83         return (interfaces.length == 0 || (interfaces.length == 1 && SpringProxy.class.equals(interfaces[0])));
84     }
85
86
87     /**
88      * Inner factory class used to just introduce a CGLIB2 dependency
89      * when actually creating a CGLIB proxy.
90      */

91     private static class CglibProxyFactory {
92
93         public static AopProxy createCglibProxy(AdvisedSupport advisedSupport) {
94             return new Cglib2AopProxy(advisedSupport);
95         }
96     }
97
98 }
99
Popular Tags