|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MethodMatcher.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.methodmatch;
|
|
16 |
|
|
17 |
import java.util.ArrayList;
|
|
18 |
import java.util.Iterator;
|
|
19 |
import java.util.List;
|
|
20 |
|
|
21 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
22 |
import org.apache.hivemind.HiveMind;
|
|
23 |
import org.apache.hivemind.Location;
|
|
24 |
import org.apache.hivemind.service.MethodSignature;
|
|
25 |
|
|
26 |
/**
|
|
27 |
* A utility class used for matching a {@link org.apache.hivemind.service.MethodSignature} against
|
|
28 |
* a method pattern (this is primarily used by {@link org.apache.hivemind.ServiceInterceptorFactory
|
|
29 |
* interceptor factories}). A method pattern consists of a <em>name pattern</em> and an optional
|
|
30 |
* <em>parameters pattern</em>.
|
|
31 |
*
|
|
32 |
* <p>
|
|
33 |
* The name pattern matches against the method name, and can be one of the following:
|
|
34 |
* <ul>
|
|
35 |
* <li>A single name - which requires an exact match. Example: <code>perform</code>
|
|
36 |
* <li>A name suffix, indicated with a leading '*'. Example: <code>*form</code>
|
|
37 |
* <li>A name prefix, indicated with a trailing '*'. Example: <code>per*</code>
|
|
38 |
* <li>A name substring, indicated with leading and trailing '*'s. Example: <code>*erfo*</code>.
|
|
39 |
* <li>A match any, indicated with a single '*'. Example: <code>*</code>
|
|
40 |
* </ul>
|
|
41 |
*
|
|
42 |
* <p>
|
|
43 |
* The parameters pattern follows the name pattern and is optional. It is used to check the number
|
|
44 |
* of parameters, or their types. When the parameters pattern is omitted, then the number and types
|
|
45 |
* of parameters are not considred when matching methods.
|
|
46 |
*
|
|
47 |
* <p>
|
|
48 |
* The parameters pattern, when present, is contained within open and closed parenthis after the
|
|
49 |
* method pattern. Inside the parenthesis may be a number, indicating the exact number of
|
|
50 |
* method parameters to match against. Alternately, a comma-seperated list of Java types is used,
|
|
51 |
* which matches against a method that takes the exact set of parameters. Examples:
|
|
52 |
* <ul>
|
|
53 |
* <li><code>perform()</code> -- method with no parameters
|
|
54 |
* <li><code>perform(2)</code> -- method with two parameters
|
|
55 |
* <li><code>perform(java.util.List, int)</code> - method taking a List and an int parameter
|
|
56 |
* </ul>
|
|
57 |
*
|
|
58 |
* @author Howard Lewis Ship
|
|
59 |
*/
|
|
60 |
public class MethodMatcher |
|
61 |
{ |
|
62 |
private class StoredPattern |
|
63 |
{ |
|
64 |
String _methodPattern; |
|
65 |
MethodFilter _filter; |
|
66 |
Object _patternValue; |
|
67 |
|
|
68 | 8 |
StoredPattern(String pattern, Object value) |
69 |
{ |
|
70 | 8 |
_methodPattern = pattern; |
71 | 8 |
_patternValue = value; |
72 |
} |
|
73 |
|
|
74 | 10 |
boolean match(MethodSignature sig)
|
75 |
{ |
|
76 | 10 |
if (_filter == null) |
77 |
{ |
|
78 |
|
|
79 | 7 |
try
|
80 |
{ |
|
81 | 7 |
_filter = parseMethodPattern(_methodPattern); |
82 |
} |
|
83 |
catch (RuntimeException ex)
|
|
84 |
{ |
|
85 | 2 |
Location l = HiveMind.findLocation(new Object[] { _patternValue, ex });
|
86 |
|
|
87 | 2 |
if (l == null) |
88 | 1 |
throw ex;
|
89 |
|
|
90 | 1 |
throw new ApplicationRuntimeException( |
91 |
MethodMatchMessages.exceptionAtLocation(l, ex), |
|
92 |
ex); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 | 8 |
return _filter.matchMethod(sig);
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
private MethodPatternParser _parser = new MethodPatternParser(); |
|
101 |
private List _methodInfos;
|
|
102 |
|
|
103 | 7 |
private MethodFilter parseMethodPattern(String pattern)
|
104 |
{ |
|
105 | 7 |
return _parser.parseMethodPattern(pattern);
|
106 |
} |
|
107 |
|
|
108 |
/**
|
|
109 |
* Stores a pattern and an associated value. Values can later be
|
|
110 |
* accessed via {@link #get(Method)}.
|
|
111 |
*
|
|
112 |
* @param methodPattern a pattern that is used to recognize methods
|
|
113 |
* @param patternValue a value associated with the pattern
|
|
114 |
*/
|
|
115 | 8 |
public synchronized void put(String methodPattern, Object patternValue) |
116 |
{ |
|
117 | 8 |
if (_methodInfos == null) |
118 | 6 |
_methodInfos = new ArrayList();
|
119 |
|
|
120 | 8 |
StoredPattern sp = new StoredPattern(methodPattern, patternValue);
|
121 |
|
|
122 | 8 |
_methodInfos.add(sp); |
123 |
} |
|
124 |
|
|
125 |
/**
|
|
126 |
* Returns a pattern value prevoiusly stored via {@link #put(String, Object)}.
|
|
127 |
* Iterates over the patterns stored, in the order in which they were stored,
|
|
128 |
* until a match is found.
|
|
129 |
*
|
|
130 |
* @param sig the MethodSignature to find a matching pattern for
|
|
131 |
* @returns the pattern value for the matching pattern, or null if not found.
|
|
132 |
*/
|
|
133 | 9 |
public synchronized Object get(MethodSignature sig) |
134 |
{ |
|
135 | 9 |
if (_methodInfos == null) |
136 | 1 |
return null; |
137 |
|
|
138 | 8 |
Iterator i = _methodInfos.iterator(); |
139 | 8 |
while (i.hasNext())
|
140 |
{ |
|
141 | 10 |
StoredPattern sp = (StoredPattern) i.next(); |
142 |
|
|
143 | 10 |
if (sp.match(sig))
|
144 | 4 |
return sp._patternValue;
|
145 |
} |
|
146 |
|
|
147 |
// Not found.
|
|
148 |
|
|
149 | 2 |
return null; |
150 |
} |
|
151 |
} |
|
152 |
|
|