1 16 package org.directwebremoting.extend; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.LinkedList ; 21 import java.util.Map ; 22 23 import org.directwebremoting.dwrp.ProtocolConstants; 24 import org.directwebremoting.util.Logger; 25 26 32 public final class InboundContext 33 { 34 38 public void pushContext(TypeHintContext context) 39 { 40 contexts.addFirst(context); 41 } 42 43 46 public void popContext() 47 { 48 contexts.removeFirst(); 49 } 50 51 54 public TypeHintContext getCurrentTypeHintContext() 55 { 56 return (TypeHintContext) contexts.getFirst(); 57 } 58 59 69 public void createInboundVariable(int callNum, String key, String type, String value) 70 { 71 InboundVariable cte = new InboundVariable(this, key, type, value); 72 73 Object old = variables.put(key, cte); 74 if (old != null) 75 { 76 log.warn("Duplicate variable called: " + key); 77 } 78 79 String paramPrefix = ProtocolConstants.INBOUND_CALLNUM_PREFIX + callNum + 80 ProtocolConstants.INBOUND_CALLNUM_SUFFIX + 81 ProtocolConstants.INBOUND_KEY_PARAM; 82 83 if (key.startsWith(paramPrefix)) 84 { 85 int i = Integer.parseInt(key.substring(paramPrefix.length())) + 1; 86 if (i > paramCount) 87 { 88 paramCount = i; 89 } 90 } 91 } 92 93 98 public InboundVariable getInboundVariable(String name) 99 { 100 return (InboundVariable) variables.get(name); 101 } 102 103 109 public void clearConverted() 110 { 111 converted.clear(); 112 } 113 114 120 public void addConverted(InboundVariable iv, Class type, Object bean) 121 { 122 Conversion conversion = new Conversion(iv, type); 123 Object old = converted.put(conversion, bean); 124 if (old != null) 125 { 126 log.warn("Duplicate variable conversion called: " + conversion); 127 } 128 } 129 130 136 public Object getConverted(InboundVariable iv, Class type) 137 { 138 Conversion conversion = new Conversion(iv, type); 139 return converted.get(conversion); 140 } 141 142 146 public int getParameterCount() 147 { 148 return paramCount; 149 } 150 151 157 public int getParameterCount(int callNum) 158 { 159 int count = 0; 160 String prefix = ProtocolConstants.INBOUND_CALLNUM_PREFIX + callNum + ProtocolConstants.INBOUND_CALLNUM_SUFFIX + ProtocolConstants.INBOUND_KEY_PARAM; 161 for (Iterator it = variables.keySet().iterator(); it.hasNext();) 162 { 163 String key = (String ) it.next(); 164 if (key.startsWith(prefix)) 165 { 166 count++; 167 } 168 } 169 return count; 170 } 171 172 178 public InboundVariable getParameter(int callNum, int index) 179 { 180 String key = ProtocolConstants.INBOUND_CALLNUM_PREFIX + callNum + 181 ProtocolConstants.INBOUND_CALLNUM_SUFFIX + 182 ProtocolConstants.INBOUND_KEY_PARAM + index; 183 184 return (InboundVariable) variables.get(key); 185 } 186 187 191 public Iterator getInboundVariableNames() 192 { 193 return variables.keySet().iterator(); 194 } 195 196 199 protected static class Conversion 200 { 201 205 Conversion(InboundVariable inboundVariable, Class type) 206 { 207 this.inboundVariable = inboundVariable; 208 this.type = type; 209 } 210 211 214 public boolean equals(Object obj) 215 { 216 if (!(obj instanceof Conversion)) 217 { 218 return false; 219 } 220 221 Conversion that = (Conversion) obj; 222 223 if (!this.type.equals(that.type)) 224 { 225 return false; 226 } 227 228 return this.inboundVariable.equals(that.inboundVariable); 229 } 230 231 234 public int hashCode() 235 { 236 return inboundVariable.hashCode() + type.hashCode(); 237 } 238 239 242 public String toString() 243 { 244 return "Conversion[" + inboundVariable + "," + type.getName() + "]"; 245 } 246 247 private InboundVariable inboundVariable; 248 249 private Class type; 250 } 251 252 255 public String toString() 256 { 257 StringBuffer buffer = new StringBuffer (); 258 buffer.append("InboundContext["); 259 for (Iterator it = variables.entrySet().iterator(); it.hasNext();) 260 { 261 Map.Entry entry = (Map.Entry ) it.next(); 262 buffer.append(entry.getKey()); 263 buffer.append('='); 264 buffer.append(entry.getValue()); 265 buffer.append(','); 266 } 267 buffer.append("]"); 268 return buffer.toString(); 269 } 270 271 275 private LinkedList contexts = new LinkedList (); 276 277 282 private int paramCount = 0; 283 284 287 private final Map variables = new HashMap (); 288 289 292 private final Map converted = new HashMap (); 293 294 297 private static final Logger log = Logger.getLogger(InboundContext.class); 298 } 299 | Popular Tags |