Clover coverage report - Code Coverage for hivemind release 1.0-beta-1
Coverage timestamp: Sat Jul 3 2004 09:41:37 EDT
file stats: LOC: 153   Methods: 5
NCLOC: 69   Classes: 2
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%
coverage
 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.HiveMindMessages;
 24   
 import org.apache.hivemind.Location;
 25   
 import org.apache.hivemind.service.MethodSignature;
 26   
 
 27   
 /**
 28   
  * A utility class used for matching a {@link org.apache.hivemind.service.MethodSignature} against
 29   
  * a method pattern (this is primarily used by {@link org.apache.hivemind.ServiceInterceptorFactory
 30   
  * interceptor factories}). A method pattern consists of a <em>name pattern</em> and an optional
 31   
  * <em>parameters pattern</em>.
 32   
  * 
 33   
  * <p>
 34   
  * The name pattern matches against the method name, and can be one of the following:
 35   
  * <ul>
 36   
  * <li>A single name - which requires an exact match.  Example: <code>perform</code>
 37   
  * <li>A name suffix, indicated with a leading '*'.  Example: <code>*form</code>
 38   
  * <li>A name prefix, indicated with a trailing '*'.  Example: <code>per*</code>
 39   
  * <li>A name substring, indicated with leading and trailing '*'s.  Example: <code>*erfo*</code>.
 40   
  * <li>A match any, indicated with a single '*'.  Example: <code>*</code>
 41   
  * </ul>
 42   
  * 
 43   
  * <p>
 44   
  * The parameters pattern follows the name pattern and is optional. It is used to check the number
 45   
  * of parameters, or their types. When the parameters pattern is omitted, then the number and types
 46   
  * of parameters are not considred when matching methods.
 47   
  * 
 48   
  * <p>
 49   
  * The parameters pattern, when present, is contained within open and closed parenthis after the
 50   
  * method pattern.  Inside the parenthesis may be a number, indicating the exact number of
 51   
  * method parameters to match against. Alternately, a comma-seperated list of Java types is used,
 52   
  * which matches against a method that takes the exact set of parameters.  Examples:
 53   
  * <ul>
 54   
  * <li><code>perform()</code> -- method with no parameters
 55   
  * <li><code>perform(2)</code> -- method with two parameters
 56   
  * <li><code>perform(java.util.List, int)</code> - method taking a List and an int parameter
 57   
  * </ul> 
 58   
  *
 59   
  * @author Howard Lewis Ship
 60   
  */
 61   
 public class MethodMatcher
 62   
 {
 63   
     private class StoredPattern
 64   
     {
 65   
         String _methodPattern;
 66   
         MethodFilter _filter;
 67   
         Object _patternValue;
 68   
 
 69  8
         StoredPattern(String pattern, Object value)
 70   
         {
 71  8
             _methodPattern = pattern;
 72  8
             _patternValue = value;
 73   
         }
 74   
 
 75  10
         boolean match(MethodSignature sig)
 76   
         {
 77  10
             if (_filter == null)
 78   
             {
 79   
 
 80  7
                 try
 81   
                 {
 82  7
                     _filter = parseMethodPattern(_methodPattern);
 83   
                 }
 84   
                 catch (RuntimeException ex)
 85   
                 {
 86  2
                     Location l = HiveMind.findLocation(new Object[] { _patternValue, ex });
 87   
 
 88  2
                     if (l == null)
 89  1
                         throw ex;
 90   
 
 91  1
                     throw new ApplicationRuntimeException(
 92   
                         MethodMatchMessages.exceptionAtLocation(l, ex),
 93   
                         ex);
 94   
                 }
 95   
             }
 96   
 
 97  8
             return _filter.matchMethod(sig);
 98   
         }
 99   
     }
 100   
 
 101   
     private MethodPatternParser _parser = new MethodPatternParser();
 102   
     private List _methodInfos;
 103   
 
 104  7
     private MethodFilter parseMethodPattern(String pattern)
 105   
     {
 106  7
         return _parser.parseMethodPattern(pattern);
 107   
     }
 108   
 
 109   
     /**
 110   
      * Stores a pattern and an associated value. Values can later be
 111   
      * accessed via {@link #get(Method)}.
 112   
      * 
 113   
      * @param methodPattern a pattern that is used to recognize methods
 114   
      * @param patternValue a value associated with the pattern
 115   
      */
 116  8
     public synchronized void put(String methodPattern, Object patternValue)
 117   
     {
 118  8
         if (_methodInfos == null)
 119  6
             _methodInfos = new ArrayList();
 120   
 
 121  8
         StoredPattern sp = new StoredPattern(methodPattern, patternValue);
 122   
 
 123  8
         _methodInfos.add(sp);
 124   
     }
 125   
 
 126   
     /**
 127   
      * Returns a pattern value prevoiusly stored via {@link #put(String, Object)}.
 128   
      * Iterates over the patterns stored, in the order in which they were stored,
 129   
      * until a match is found.
 130   
      * 
 131   
      * @param sig the MethodSignature to find a matching pattern for
 132   
      * @returns the pattern value for the matching pattern, or null if not found.
 133   
      */
 134  9
     public synchronized Object get(MethodSignature sig)
 135   
     {
 136  9
         if (_methodInfos == null)
 137  1
             return null;
 138   
 
 139  8
         Iterator i = _methodInfos.iterator();
 140  8
         while (i.hasNext())
 141   
         {
 142  10
             StoredPattern sp = (StoredPattern) i.next();
 143   
 
 144  10
             if (sp.match(sig))
 145  4
                 return sp._patternValue;
 146   
         }
 147   
 
 148   
         // Not found.
 149   
         
 150  2
         return null;
 151   
     }
 152   
 }
 153