001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.aop.support;
018:
019: import java.util.regex.Matcher;
020: import java.util.regex.Pattern;
021: import java.util.regex.PatternSyntaxException;
022:
023: /**
024: * Regular expression pointcut based on the <code>java.util.regex</code> package.
025: * Supports the following JavaBean properties:
026: * <ul>
027: * <li>pattern: regular expression for the fully-qualified method names to match
028: * <li>patterns: alternative property taking a String array of patterns. The result will
029: * be the union of these patterns.
030: * </ul>
031: *
032: * <p>Note: the regular expressions must be a match. For example,
033: * <code>.*get.*</code> will match com.mycom.Foo.getBar().
034: * <code>get.*</code> will not.
035: *
036: * @author Dmitriy Kopylenko
037: * @author Rob Harrop
038: * @since 1.1
039: */
040: public class JdkRegexpMethodPointcut extends
041: AbstractRegexpMethodPointcut {
042:
043: /**
044: * Compiled form of the patterns.
045: */
046: private transient Pattern[] compiledPatterns = new Pattern[0];
047:
048: /**
049: * Compiled form of the exclusion patterns.
050: */
051: private transient Pattern[] compiledExclusionPatterns = new Pattern[0];
052:
053: /**
054: * Initialize {@link Pattern Patterns} from the supplied <code>String[]</code>.
055: */
056: protected void initPatternRepresentation(String[] patterns)
057: throws PatternSyntaxException {
058: this .compiledPatterns = compilePatterns(patterns);
059: }
060:
061: /**
062: * Returns <code>true</code> if the {@link Pattern} at index <code>patternIndex</code>
063: * matches the supplied candidate <code>String</code>.
064: */
065: protected boolean matches(String pattern, int patternIndex) {
066: Matcher matcher = this .compiledPatterns[patternIndex]
067: .matcher(pattern);
068: return matcher.matches();
069: }
070:
071: /**
072: * Initialize exclusion {@link Pattern Patterns} from the supplied <code>String[]</code>.
073: */
074: protected void initExcludedPatternRepresentation(
075: String[] excludedPatterns) throws IllegalArgumentException {
076: this .compiledExclusionPatterns = compilePatterns(excludedPatterns);
077: }
078:
079: /**
080: * Returns <code>true</code> if the exclusion {@link Pattern} at index <code>patternIndex</code>
081: * matches the supplied candidate <code>String</code>.
082: */
083: protected boolean matchesExclusion(String candidate,
084: int patternIndex) {
085: Matcher matcher = this .compiledExclusionPatterns[patternIndex]
086: .matcher(candidate);
087: return matcher.matches();
088: }
089:
090: /**
091: * Compiles the supplied <code>String[]</code> into an array of
092: * {@link Pattern} objects and returns that array.
093: */
094: private Pattern[] compilePatterns(String[] source) {
095: Pattern[] destination = new Pattern[source.length];
096: for (int i = 0; i < source.length; i++) {
097: destination[i] = Pattern.compile(source[i]);
098: }
099: return destination;
100: }
101:
102: }
|