1 13 package com.tonbeller.wcf.controller; 14 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.LinkedList ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 34 public class DispatcherSupport implements Dispatcher { 35 36 private HashMap map = new HashMap (); 38 39 private HashMap inverseMap = new HashMap (); 41 42 51 public void addRequestListener(String name, String value, RequestListener listener) { 52 removeRequestListener(listener); 53 String key = getKey(name, value); 54 map.put(listener, key); 55 List list = (List ) inverseMap.get(key); 56 if (list == null) { 57 list = new LinkedList (); 58 inverseMap.put(key, list); 59 } 60 list.add(listener); 61 } 62 63 67 public void removeRequestListener(RequestListener listener) { 68 String key = (String ) map.get(listener); 69 if (key != null) { 70 map.remove(listener); 71 List list = (List ) inverseMap.get(key); 72 if (list != null) 73 list.remove(listener); 74 } 75 } 76 77 80 public void clear() { 81 map.clear(); 82 inverseMap.clear(); 83 } 84 85 String getKey(String name, String value) { 86 if ((name != null) && (value != null)) { 87 return name + "=" + value; 88 } else if (name != null) { 89 return name + "="; 90 } else if (value != null) { 91 return "=" + value; 92 } 93 return "="; 94 } 95 96 102 List findAll(Map requestParameters) { 103 List match = new LinkedList (); 104 Iterator it = requestParameters.keySet().iterator(); 105 106 while (it.hasNext()) { 107 String name = (String ) it.next(); 108 String [] values = (String []) requestParameters.get(name); 109 110 if (values != null) { 111 for (int i = 0; i < values.length; i++) { 112 113 if (values[i] == null || values[i].length() == 0) 115 continue; 116 117 Object obj = inverseMap.get(name + "=" + values[i]); 119 120 if (obj != null) { 121 match.addAll((List ) obj); 122 } 123 124 obj = inverseMap.get("=" + values[i]); 126 127 if (obj != null) { 128 match.addAll((List ) obj); 129 } 130 } 131 } 132 133 Object obj = inverseMap.get(name + "="); 135 if (obj != null) 136 match.addAll((List ) obj); 137 138 if (name.endsWith(".x")) { 140 name = name.substring(0, name.length() - 2); 141 obj = inverseMap.get(name + "="); 142 if (obj != null) 143 match.addAll((List ) obj); 144 } 145 } 146 147 Object obj = inverseMap.get("="); 149 150 if (obj != null) { 151 match.addAll((List ) obj); 152 } 153 154 return match; 155 } 156 157 162 public void request(RequestContext context) throws Exception { 163 Iterator it = findAll(context.getRequest().getParameterMap()).iterator(); 164 165 while (it.hasNext()) { 166 RequestListener listener = (RequestListener) it.next(); 167 listener.request(context); 168 } 169 } 170 171 175 public List findMatchingListeners(Map httpParams) { 176 List candidates = findAll(httpParams); 177 List result = new ArrayList (); 178 for (Iterator it = candidates.iterator(); it.hasNext();) { 179 Object obj = it.next(); 180 if (obj instanceof Dispatcher) { 181 Dispatcher d = (Dispatcher) obj; 182 result.addAll(d.findMatchingListeners(httpParams)); 183 } else { 184 result.add(obj); 185 } 186 } 187 return result; 188 } 189 190 } | Popular Tags |