KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > support > RegexpMethodPointcutAdvisor


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.support;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.aopalliance.aop.Advice;
22
23 import org.springframework.aop.Pointcut;
24 import org.springframework.core.JdkVersion;
25 import org.springframework.util.ObjectUtils;
26
27 /**
28  * Convenient class for regexp method pointcuts that hold an Advice,
29  * making them an {@link org.springframework.aop.Advisor}.
30  *
31  * <p>Configure this class using the "pattern" and "patterns"
32  * pass-through properties. These are analogous to the pattern
33  * and patterns properties of {@link AbstractRegexpMethodPointcut}.
34  *
35  * <p>Can delegate to any {@link AbstractRegexpMethodPointcut} subclass,
36  * like {@link Perl5RegexpMethodPointcut} or {@link JdkRegexpMethodPointcut}.
37  * To choose a specific one, either override the {@link #createPointcut}
38  * method or set the "perl5" flag accordingly.
39  *
40  * <p>By default, {@link JdkRegexpMethodPointcut} will be used on JDK 1.4+,
41  * falling back to {@link Perl5RegexpMethodPointcut} on JDK 1.3 (requiring
42  * Jakarta ORO on the classpath). The use of Perl5RegexpMethodPointcut
43  * can be enforced through specifying the "perl5" property.
44  *
45  * @author Rod Johnson
46  * @author Juergen Hoeller
47  * @see #setPattern
48  * @see #setPatterns
49  * @see #setPerl5
50  * @see #createPointcut
51  * @see Perl5RegexpMethodPointcut
52  * @see JdkRegexpMethodPointcut
53  */

54 public class RegexpMethodPointcutAdvisor extends AbstractGenericPointcutAdvisor {
55
56     private String JavaDoc[] patterns;
57
58     private boolean perl5 = false;
59
60     private AbstractRegexpMethodPointcut pointcut;
61
62     private final Object JavaDoc pointcutMonitor = new SerializableMonitor();
63
64
65     /**
66      * Create an empty RegexpMethodPointcutAdvisor.
67      * @see #setPattern
68      * @see #setPatterns
69      * @see #setPerl5
70      * @see #setAdvice
71      */

72     public RegexpMethodPointcutAdvisor() {
73     }
74
75     /**
76      * Create a RegexpMethodPointcutAdvisor for the given advice.
77      * The pattern still needs to be specified afterwards.
78      * @param advice the advice to use
79      * @see #setPattern
80      * @see #setPatterns
81      * @see #setPerl5
82      */

83     public RegexpMethodPointcutAdvisor(Advice advice) {
84         setAdvice(advice);
85     }
86
87     /**
88      * Create a RegexpMethodPointcutAdvisor for the given advice.
89      * @param pattern the pattern to use
90      * @param advice the advice to use
91      * @see #setPerl5
92      */

93     public RegexpMethodPointcutAdvisor(String JavaDoc pattern, Advice advice) {
94         setPattern(pattern);
95         setAdvice(advice);
96     }
97
98     /**
99      * Create a RegexpMethodPointcutAdvisor for the given advice.
100      * @param patterns the patterns to use
101      * @param advice the advice to use
102      * @see #setPerl5
103      */

104     public RegexpMethodPointcutAdvisor(String JavaDoc[] patterns, Advice advice) {
105         setPatterns(patterns);
106         setAdvice(advice);
107     }
108
109
110     /**
111      * Set the regular expression defining methods to match.
112      * <p>Use either this method or {@link #setPatterns}, not both.
113      * @see #setPatterns
114      */

115     public void setPattern(String JavaDoc pattern) {
116         setPatterns(new String JavaDoc[] {pattern});
117     }
118
119     /**
120      * Set the regular expressions defining methods to match.
121      * To be passed through to the pointcut implementation.
122      * <p>Matching will be the union of all these; if any of the
123      * patterns matches, the pointcut matches.
124      * @see AbstractRegexpMethodPointcut#setPatterns
125      */

126     public void setPatterns(String JavaDoc[] patterns) {
127         this.patterns = patterns;
128     }
129
130     /**
131      * Set whether to enforce Perl5 regexp syntax. Default is "false".
132      * <p>Turn this flag on to use {@link Perl5RegexpMethodPointcut}
133      * (delegating to Jakarta ORO). Else, {@link JdkRegexpMethodPointcut} will
134      * be used on JDK 1.4+, falling back to Perl5RegexpMethodPointcut on JDK 1.3.
135      * <p>Alternatively, override the {@link #createPointcut} method.
136      * @see #createPointcut
137      * @see Perl5RegexpMethodPointcut
138      * @see JdkRegexpMethodPointcut
139      */

140     public void setPerl5(boolean perl5) {
141         this.perl5 = perl5;
142     }
143
144
145     /**
146      * Initialize the singleton Pointcut held within this Advisor.
147      */

148     public Pointcut getPointcut() {
149         synchronized (this.pointcutMonitor) {
150             if (this.pointcut == null) {
151                 this.pointcut = createPointcut();
152                 this.pointcut.setPatterns(this.patterns);
153             }
154             return pointcut;
155         }
156     }
157
158     /**
159      * Create the actual pointcut: By default, a {@link Perl5RegexpMethodPointcut}
160      * will be created if Perl5 syntax is enforced or when running on JDK 1.3.
161      * Else, a {@link JdkRegexpMethodPointcut} (JDK 1.4+) will be used.
162      * @return the Pointcut instance (never <code>null</code>)
163      * @see #setPerl5
164      * @see Perl5RegexpMethodPointcut
165      * @see JdkRegexpMethodPointcut
166      */

167     protected AbstractRegexpMethodPointcut createPointcut() {
168         if (this.perl5 || JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_14) {
169             // needs Jakarta ORO on the classpath
170
return Perl5RegexpPointcutFactory.createPerl5RegexpPointcut();
171         }
172         else {
173             // needs to run on JDK >= 1.4
174
return new JdkRegexpMethodPointcut();
175         }
176     }
177
178     public String JavaDoc toString() {
179         return getClass().getName() + ": advice [" + getAdvice() +
180                 "], pointcut patterns " + ObjectUtils.nullSafeToString(this.patterns);
181     }
182
183
184     /**
185      * Inner factory class used to just introduce an ORO dependency
186      * when actually creating a Perl5 regexp pointcut.
187      */

188     private static class Perl5RegexpPointcutFactory {
189
190         public static AbstractRegexpMethodPointcut createPerl5RegexpPointcut() {
191             return new Perl5RegexpMethodPointcut();
192         }
193     }
194
195
196     /**
197      * Empty class used for a serializable monitor object.
198      */

199     private static class SerializableMonitor implements Serializable JavaDoc {
200     }
201
202 }
203
Popular Tags