1 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 57 public class MethodMatcher 58 { 59 private class StoredPattern 60 { 61 String _methodPattern; 62 63 MethodFilter _filter; 64 65 Object _patternValue; 66 67 StoredPattern(String pattern, Object value) 68 { 69 _methodPattern = pattern; 70 _patternValue = value; 71 } 72 73 boolean match(MethodSignature sig) 74 { 75 if (_filter == null) 76 { 77 78 try 79 { 80 _filter = parseMethodPattern(_methodPattern); 81 } 82 catch (RuntimeException ex) 83 { 84 Location l = HiveMind.findLocation(new Object [] 85 { _patternValue, ex }); 86 87 if (l == null) 88 throw ex; 89 90 throw new ApplicationRuntimeException(MethodMatchMessages.exceptionAtLocation( 91 l, 92 ex), ex); 93 } 94 } 95 96 return _filter.matchMethod(sig); 97 } 98 } 99 100 private MethodPatternParser _parser = new MethodPatternParser(); 101 102 private List _methodInfos; 103 104 private Object _defaultValue; 105 106 112 public MethodMatcher(Object defaultValue) 113 { 114 _defaultValue = defaultValue; 115 } 116 117 public MethodMatcher() 118 { 119 this(null); 120 } 121 122 private MethodFilter parseMethodPattern(String pattern) 123 { 124 return _parser.parseMethodPattern(pattern); 125 } 126 127 136 public synchronized void put(String methodPattern, Object patternValue) 137 { 138 if (_methodInfos == null) 139 _methodInfos = new ArrayList (); 140 141 StoredPattern sp = new StoredPattern(methodPattern, patternValue); 142 143 _methodInfos.add(sp); 144 } 145 146 155 public synchronized Object get(MethodSignature sig) 156 { 157 if (_methodInfos == null) 158 return _defaultValue; 159 160 Iterator i = _methodInfos.iterator(); 161 while (i.hasNext()) 162 { 163 StoredPattern sp = (StoredPattern) i.next(); 164 165 if (sp.match(sig)) 166 return sp._patternValue; 167 } 168 169 171 return _defaultValue; 172 } 173 } | Popular Tags |