01: /*
02: * Copyright 2002-2007 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.PointcutAdvisor;
24: import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
25:
26: /**
27: * Utility methods for working with AspectJ proxies.
28: *
29: * @author Rod Johnson
30: * @author Ramnivas Laddad
31: * @since 2.0
32: */
33: public abstract class AspectJProxyUtils {
34:
35: /**
36: * Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors.
37: * This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut matching)
38: * and make available the current AspectJ JoinPoint. The call will have no effect if there are no
39: * AspectJ advisors in the advisor chain.
40: * @param advisors Advisors available
41: * @return <code>true</code> if any special {@link Advisor Advisors} were added, otherwise <code>false</code>.
42: */
43: public static boolean makeAdvisorChainAspectJCapableIfNecessary(
44: List advisors) {
45: // Don't add advisors to an empty list; may indicate that proxying is just not required
46: if (!advisors.isEmpty()) {
47: boolean foundAspectJAdvice = false;
48: for (Iterator it = advisors.iterator(); it.hasNext()
49: && !foundAspectJAdvice;) {
50: Advisor advisor = (Advisor) it.next();
51: // Be careful not to get the Advice without a guard, as
52: // this might eagerly instantiate a non-singleton AspectJ aspect
53: if (isAspectJAdvice(advisor)) {
54: foundAspectJAdvice = true;
55: }
56: }
57: if (foundAspectJAdvice
58: && !advisors
59: .contains(ExposeInvocationInterceptor.ADVISOR)) {
60: advisors.add(0, ExposeInvocationInterceptor.ADVISOR);
61: return true;
62: }
63: }
64: return false;
65: }
66:
67: /**
68: * Determine whether the given Advisor contains an AspectJ advice.
69: * @param advisor the Advisor to check
70: */
71: private static boolean isAspectJAdvice(Advisor advisor) {
72: return (advisor instanceof InstantiationModelAwarePointcutAdvisor
73: || advisor.getAdvice() instanceof AbstractAspectJAdvice || (advisor instanceof PointcutAdvisor && ((PointcutAdvisor) advisor)
74: .getPointcut() instanceof AspectJExpressionPointcut));
75: }
76:
77: }
|