1 23 24 package com.sun.enterprise.tools.guiframework.event.handlers; 25 26 import com.iplanet.jato.RequestContext; 27 28 import com.sun.enterprise.tools.guiframework.view.HandlerContext; 29 30 import java.lang.reflect.Array ; 31 import java.lang.reflect.Method ; 32 import java.util.ArrayList ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 37 40 public class AttributeHandlers { 41 42 51 public void getAttribute(RequestContext reqCtx, HandlerContext handlerCtx) { 52 Object key = handlerCtx.getInputValue(KEY); 53 if (key == null) { 54 throw new IllegalArgumentException ( 55 "The parameter map did not contain a key!"); 56 } 57 handlerCtx.setOutputValue( 58 VALUE, reqCtx.getRequest().getAttribute(key.toString())); 59 } 60 61 62 70 public void setAttribute(RequestContext reqCtx, HandlerContext handlerCtx) { 71 Object key = handlerCtx.getInputValue(KEY); 72 Object value = handlerCtx.getInputValue(VALUE); 73 if (key == null) { 74 throw new IllegalArgumentException ( 75 "The parameter map did not contain a key!"); 76 } 77 reqCtx.getRequest().setAttribute(key.toString(), value); 78 } 79 80 81 92 public void getListElement(RequestContext reqCtx, HandlerContext handlerCtx) { 93 Object key = handlerCtx.getInputValue(KEY); 95 if (key == null) { 96 throw new IllegalArgumentException ( 97 "The parameter map did not contain 'key'!"); 98 } 99 100 int index = getIndex((Integer )handlerCtx.getInputValue(INDEX)); 102 103 Object attr = reqCtx.getRequest().getAttribute(key.toString()); 105 if (attr == null) { 106 throw new IllegalArgumentException ( 107 "No request attribute is stored under the key: '"+key+"'."); 108 } 109 110 if (attr instanceof List ) { 112 handlerCtx.setOutputValue(VALUE, ((List )attr).get(index)); 114 return; 115 } 116 117 if (attr.getClass().isArray()) { 119 handlerCtx.setOutputValue(VALUE, Array.get(attr, index)); 121 return; 122 } 123 124 throw new IllegalArgumentException ( 126 "Value stored under request attribute '"+key+ 127 "' is not an array or List!"); 128 } 129 130 131 142 public void setListElement(RequestContext reqCtx, HandlerContext handlerCtx) { 143 Object value = handlerCtx.getInputValue(VALUE); 144 Object key = handlerCtx.getInputValue(KEY); 146 if (key == null) { 147 throw new IllegalArgumentException ( 148 "The parameter map did not contain 'key'!"); 149 } 150 151 int index = getIndex((Integer )handlerCtx.getInputValue(INDEX)); 153 154 Object list = reqCtx.getRequest().getAttribute(key.toString()); 156 if (list == null) { 157 list = new ArrayList (index); 158 } 159 160 if (list instanceof List ) { 162 int len = ((List )list).size(); 163 if (index >= len) { 164 Object val = null; 167 while (index > len) { 168 try { 169 try { 170 ((List )list).add(val); 171 } catch (NullPointerException ex) { 172 val = ""; 174 ((List )list).add(val); 175 } 176 } catch (ClassCastException ex2) { 177 val = value; 180 ((List )list).add(val); 181 } 182 len++; 183 } 184 185 ((List )list).add(value); 187 } else { 188 ((List )list).set(index, value); 190 } 191 192 } else if (list.getClass().isArray()) { 194 int len = Array.getLength(list); 196 Class type = list.getClass().getComponentType(); 197 if (index >= len) { 198 Object newList = Array.newInstance(type, index+1); 200 201 for (int count=0; count<len; count++) { 203 Array.set(newList, count, Array.get(list, count)); 204 } 205 206 list = newList; 208 } 209 210 try { 212 Array.set(list, index, value); 213 } catch (Exception ex) { 214 throw new RuntimeException ( 215 "Unable to set '"+value+"' to array!", ex); 216 } 217 } else { 218 throw new IllegalArgumentException ( 219 "Value stored in request attribute '"+key+ 220 "' is not an array or List: '"+list.getClass()+"'."); 221 } 222 223 reqCtx.getRequest().setAttribute(key.toString(), list); 225 } 226 227 228 237 private int getIndex(Integer idx) { 238 if (idx == null) { 239 throw new IllegalArgumentException ( 240 "The parameter map did not contain 'index'!"); 241 } 242 int index = ((Integer )idx).intValue(); 243 244 if (index < 0) { 246 throw new IllegalArgumentException ( 247 "'index' must be non-negative (received '"+index+"')."); 248 } 249 250 return index; 252 } 253 254 261 public String callMethod(RequestContext reqCtx, HandlerContext handlerCtx) { 262 Object obj = handlerCtx.getInputValue(OBJECT); 263 String methodName = (String )handlerCtx.getInputValue(METHOD); 264 if (obj == null || methodName == null) { 265 throw new IllegalArgumentException ("Null parameter(s) sent to 'callMethod'."); 266 } 267 Method method = null; 268 try { 269 method = obj.getClass().getMethod(methodName, (Class [])null); 270 } catch (Exception ex) { 271 throw new RuntimeException ("Method not found: " + methodName, ex); 272 } 273 Object value = obj; 274 try { 275 value = method.invoke(obj, (Object [])null); 276 } catch (Exception ex) { 277 throw new RuntimeException ("Error calling: " + methodName, ex); 278 } 279 handlerCtx.setOutputValue(VALUE, value); 280 if (value != null) 281 return value.toString(); 282 else 283 return ""; 284 } 285 286 289 public static final String KEY = "key"; 290 public static final String VALUE = "value"; 291 public static final String INDEX = "index"; 292 public static final String OBJECT = "object"; 293 public static final String METHOD = "method"; 294 } 295 | Popular Tags |