KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > config > AopNamespaceUtils


1 /*
2  * Copyright 2002-2006 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.config;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator;
23 import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
24 import org.springframework.beans.factory.config.BeanDefinition;
25 import org.springframework.beans.factory.parsing.BeanComponentDefinition;
26 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
27 import org.springframework.beans.factory.support.RootBeanDefinition;
28 import org.springframework.beans.factory.xml.ParserContext;
29 import org.springframework.core.Ordered;
30 import org.springframework.util.Assert;
31 import org.springframework.util.ClassUtils;
32
33 /**
34  * Utility class for handling registration of auto-proxy creators used internally
35  * by the '<code>aop</code>' namespace tags.
36  *
37  * <p>Only a single auto-proxy creator can be registered and multiple tags may wish
38  * to register different concrete implementations. As such this class wraps a simple
39  * escalation protocol, allowing clases to request a particular auto-proxy creator
40  * and know that class, <code>or a subclass thereof</code>, will eventually be resident
41  * in the application context.
42  *
43  * @author Rob Harrop
44  * @author Juergen Hoeller
45  * @since 2.0
46  */

47 public abstract class AopNamespaceUtils {
48
49     /**
50      * The bean name of the internally managed auto-proxy creator.
51      */

52     public static final String JavaDoc AUTO_PROXY_CREATOR_BEAN_NAME =
53                     "org.springframework.aop.config.internalAutoProxyCreator";
54
55     /**
56      * The class name of the '<code>AnnotationAwareAspectJAutoProxyCreator</code>' class.
57      * Only available with AspectJ and Java 5.
58      */

59     public static final String JavaDoc ASPECTJ_AUTO_PROXY_CREATOR_CLASS_NAME =
60                     "org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator";
61
62
63     /**
64      * Stores the auto proxy creator classes in escalation order.
65      */

66     private static final List JavaDoc APC_PRIORITY_LIST = new ArrayList JavaDoc();
67
68     /**
69      * Setup the escalation list.
70      */

71     static {
72         APC_PRIORITY_LIST.add(DefaultAdvisorAutoProxyCreator.class.getName());
73         APC_PRIORITY_LIST.add(AspectJAwareAdvisorAutoProxyCreator.class.getName());
74         APC_PRIORITY_LIST.add(ASPECTJ_AUTO_PROXY_CREATOR_CLASS_NAME);
75     }
76
77
78     public static void registerAutoProxyCreatorIfNecessary(ParserContext parserContext, Object JavaDoc sourceElement) {
79         registryOrEscalateApcAsRequired(DefaultAdvisorAutoProxyCreator.class, parserContext, sourceElement);
80     }
81
82     public static void registerAspectJAutoProxyCreatorIfNecessary(ParserContext parserContext, Object JavaDoc sourceElement) {
83         registryOrEscalateApcAsRequired(
84                 AspectJAwareAdvisorAutoProxyCreator.class, parserContext, sourceElement);
85     }
86
87     public static void registerAtAspectJAutoProxyCreatorIfNecessary(ParserContext parserContext, Object JavaDoc sourceElement) {
88         Class JavaDoc cls = getAspectJAutoProxyCreatorClassIfPossible();
89         registryOrEscalateApcAsRequired(cls, parserContext, sourceElement);
90     }
91
92     private static void registryOrEscalateApcAsRequired(Class JavaDoc cls, ParserContext parserContext, Object JavaDoc sourceElement) {
93         Assert.notNull(parserContext, "ParserContext must not be null");
94         BeanDefinitionRegistry registry = parserContext.getRegistry();
95
96         if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
97             BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
98             if (cls.getName().equals(apcDefinition.getBeanClassName())) {
99                 return;
100             }
101             int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
102             int requiredPriority = findPriorityForClass(cls.getName());
103             if (currentPriority < requiredPriority) {
104                 apcDefinition.setBeanClassName(cls.getName());
105             }
106         }
107
108         else {
109             RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
110             beanDefinition.setSource(parserContext.extractSource(sourceElement));
111             beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
112             registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
113             beanDefinition.getPropertyValues().addPropertyValue("order", new Integer JavaDoc(Ordered.HIGHEST_PRECEDENCE));
114             // Notify of bean registration.
115
BeanComponentDefinition componentDefinition =
116                     new BeanComponentDefinition(beanDefinition, AUTO_PROXY_CREATOR_BEAN_NAME);
117             parserContext.registerComponent(componentDefinition);
118         }
119     }
120
121     public static void forceAutoProxyCreatorToUseClassProxying(BeanDefinitionRegistry registry) {
122         if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
123             BeanDefinition definition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
124             definition.getPropertyValues().addPropertyValue("proxyTargetClass", Boolean.TRUE);
125         }
126     }
127
128     private static Class JavaDoc getAspectJAutoProxyCreatorClassIfPossible() {
129         try {
130             return ClassUtils.forName(ASPECTJ_AUTO_PROXY_CREATOR_CLASS_NAME);
131         }
132         catch (Throwable JavaDoc ex) {
133             throw new IllegalStateException JavaDoc(
134                     "Unable to load class [" + ASPECTJ_AUTO_PROXY_CREATOR_CLASS_NAME +
135                     "]. Are you running on Java 1.5+? Root cause: " + ex);
136         }
137     }
138
139     private static final int findPriorityForClass(String JavaDoc className) {
140         Assert.notNull(className, "Class name must not be null");
141         for (int i = 0; i < APC_PRIORITY_LIST.size(); i++) {
142             String JavaDoc str = (String JavaDoc) APC_PRIORITY_LIST.get(i);
143             if (className.equals(str)) {
144                 return i;
145             }
146         }
147         throw new IllegalArgumentException JavaDoc(
148                 "Class name [" + className + "] is not a known auto-proxy creator class");
149     }
150
151 }
152
Popular Tags