01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.aspectj;
18:
19: import java.util.Iterator;
20: import java.util.List;
21:
22: import org.springframework.aop.Advisor;
23: import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
24:
25: /**
26: * Utility methods for working with AspectJ proxies.
27: *
28: * @author Rod Johnson
29: * @since 2.0
30: */
31: public abstract class AspectJProxyUtils {
32:
33: /**
34: * Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors.
35: * This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut matching)
36: * and make available the current AspectJ JoinPoint. The call will have no effect if there are no
37: * AspectJ advisors in the advisor chain.
38: * @param advisors Advisors available
39: * @return <code>true</code> if any special {@link Advisor Advisors} were added, otherwise <code>false</code>.
40: */
41: public static boolean makeAdvisorChainAspectJCapableIfNecessary(
42: List advisors) {
43: // Don't add advisors to an empty list; may indicate that proxying is just not required
44: if (!advisors.isEmpty()) {
45: boolean foundAspectJAdvice = false;
46: for (Iterator it = advisors.iterator(); it.hasNext()
47: && !foundAspectJAdvice;) {
48: Advisor advisor = (Advisor) it.next();
49: // Be careful not to get the Advice without a guard, as
50: // this might eagerly instantiate a non-singleton AspectJ aspect
51: if (advisor instanceof InstantiationModelAwarePointcutAdvisor
52: || advisor.getAdvice() instanceof AbstractAspectJAdvice) {
53: foundAspectJAdvice = true;
54: }
55: }
56: if (foundAspectJAdvice
57: && !advisors
58: .contains(ExposeInvocationInterceptor.ADVISOR)) {
59: advisors.add(0, ExposeInvocationInterceptor.ADVISOR);
60: return true;
61: }
62: }
63: return false;
64: }
65:
66: }
|