01: /*****************************************************************************
02: * Copyright (c) PicoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: * Idea by Rachel Davies, Original code by various *
09: *****************************************************************************/package org.nanocontainer.aop.defaults;
10:
11: import org.apache.oro.text.regex.MalformedPatternException;
12: import org.apache.oro.text.regex.Pattern;
13: import org.apache.oro.text.regex.Perl5Compiler;
14: import org.apache.oro.text.regex.Perl5Matcher;
15: import org.nanocontainer.aop.ComponentPointcut;
16: import org.nanocontainer.aop.MalformedRegularExpressionException;
17:
18: /**
19: * Component pointcut that matches the component name against a regular
20: * expression.
21: *
22: * @author Stephen Molitor
23: * @version $Revision: 3144 $
24: */
25: public class NameMatchesComponentPointcut implements ComponentPointcut {
26:
27: private final Pattern pattern;
28: private final Perl5Matcher matcher = new Perl5Matcher();
29:
30: /**
31: * Creates a new <code>NameMatchesComponentPointcut</code> that will match
32: * the component key against the given regular expression. The regular
33: * expression must be an <a
34: * href="http://jakarta.apache.org/oro/index.html">ORO </a> Perl5 regular
35: * expression.
36: *
37: * @param regex the regular expression to match against the component name.
38: * @throws org.nanocontainer.aop.MalformedRegularExpressionException
39: * if the regular expression is
40: * invalid.
41: */
42: public NameMatchesComponentPointcut(String regex)
43: throws MalformedRegularExpressionException {
44: Perl5Compiler compiler = new Perl5Compiler();
45: try {
46: pattern = compiler.compile(regex);
47: } catch (MalformedPatternException e) {
48: throw new MalformedRegularExpressionException(
49: "malformed component name regular expression", e);
50: }
51: }
52:
53: /**
54: * Tests to see if the component key's toString() value matches the regular expression passed
55: * to the constructor.
56: *
57: * @param componentKey the component key to match against.
58: * @return true if the regular expression passed to the constructor matches
59: * against <code>componentKey</code>, else false.
60: */
61: public boolean picks(Object componentKey) {
62: String componentName = (String) componentKey.toString();
63: return matcher.contains(componentName, pattern);
64: }
65:
66: }
|