1 23 package com.sun.enterprise.tools.jsfext.event.handlers; 24 25 import com.sun.enterprise.tools.jsfext.component.ComponentUtil; 26 import com.sun.enterprise.tools.jsfext.event.UIComponentHolder; 27 import com.sun.enterprise.tools.jsfext.util.TypeConverter; 28 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 import java.util.EventObject ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import javax.faces.component.UIComponent; 36 37 38 60 public class Handler implements java.io.Serializable { 61 62 65 public Handler(HandlerDefinition handlerDef) { 66 setHandlerDefinition(handlerDef); 67 } 68 69 72 public HandlerDefinition getHandlerDefinition() { 73 return _handlerDef; 74 } 75 76 79 protected void setHandlerDefinition(HandlerDefinition handler) { 80 _handlerDef = handler; 81 } 82 83 86 public void setInputValue(String name, Object value) { 87 _inputs.put(name, value); 88 } 89 90 94 protected Map getInputMap() { 95 return _inputs; 96 } 97 98 108 public Object getInputValue(String name) { 109 return _inputs.get(name); 110 } 111 112 127 public Object getInputValue(HandlerContext ctx, String name) { 128 IODescriptor inDesc = getHandlerDefinition().getInputDef(name); 130 if (inDesc == null) { 131 throw new RuntimeException ("Attempted to get input value '" 132 + name + "', however, this is not a declared input " 133 + "parameter in handler definition '" 134 + getHandlerDefinition().getId() + "'! Check your handler " 135 + " and/or the XML (near LayoutElement '" 136 + ctx.getLayoutElement().getId(ctx.getFacesContext(), null) 137 + "')"); 138 } 139 140 Object value = getInputValue(name); 142 if (value == null) { 143 if (inDesc.isRequired()) { 144 throw new RuntimeException ("'" + name 145 + "' is required for handler '" 146 + getHandlerDefinition().getId() + "'!"); 147 } 148 value = inDesc.getDefault(); 149 } 150 151 EventObject event = ctx.getEventObject(); 153 UIComponent component = null; 154 if (event instanceof UIComponentHolder) { 155 component = ((UIComponentHolder) event).getUIComponent(); 156 } 157 if ((value != null) && (value instanceof String )) { 158 value = ComponentUtil.resolveValue(ctx.getFacesContext(), 159 ctx.getLayoutElement(), component, "" + value); 160 } 161 162 value = TypeConverter.asType(inDesc.getType(), value); 164 165 return value; 166 } 167 168 177 public Object getOutputValue(HandlerContext context, String name) { 178 HandlerDefinition handlerDef = getHandlerDefinition(); 180 IODescriptor outIODesc = handlerDef.getOutputDef(name); 181 if (outIODesc == null) { 182 throw new RuntimeException ("Attempted to get output value '" 183 + name + "' from handler '" + handlerDef.getId() 184 + "', however, this is not a declared output parameter! " 185 + "Check your handler and/or the XML."); 186 } 187 188 OutputMapping outputDesc = getOutput(name); 190 191 return outputDesc.getOutputType(). 193 getValue(context, outIODesc, outputDesc.getOutputKey()); 194 } 195 196 204 public void setOutputValue(HandlerContext context, String name, Object value) { 205 HandlerDefinition handlerDef = getHandlerDefinition(); 207 IODescriptor outIODesc = handlerDef.getOutputDef(name); 208 if (outIODesc == null) { 209 throw new RuntimeException ("Attempted to set output value '" 210 + name + "' from handler '" + handlerDef.getId() 211 + "', however, this is not a declared output parameter! " 212 + "Check your handler and/or the XML."); 213 } 214 215 OutputMapping outputMapping = getOutput(name); 217 if (outputMapping == null) { 218 return; 220 } 221 222 value = TypeConverter.asType(outIODesc.getType(), value); 224 225 EventObject event = context.getEventObject(); 227 UIComponent component = null; 228 if (event instanceof UIComponentHolder) { 229 component = ((UIComponentHolder) event).getUIComponent(); 230 } 231 outputMapping.getOutputType().setValue( 232 context, outIODesc, "" + ComponentUtil.resolveValue( 233 context.getFacesContext(), 234 context.getLayoutElement(), 235 component, 236 outputMapping.getOutputKey()), value); 237 } 238 239 249 public void setOutputMapping(String outputName, String targetKey, String targetType) { 250 if (targetKey != null) { 252 targetKey = targetKey.trim(); 253 if (targetKey.length() == 0) { 254 targetKey = null; 255 } 256 } 257 targetType = targetType.trim(); 258 259 try { 260 _outputs.put(outputName, new OutputMapping( 261 outputName, targetKey, targetType)); 262 } catch (IllegalArgumentException ex) { 263 throw new RuntimeException ( 264 "Unable to create OutputMapping with given information: " 265 + "outputName='" + outputName 266 + "', targetKey='" + targetKey 267 + "', targetType=" + targetType + "'", ex); 268 } 269 } 270 271 274 public OutputMapping getOutput(String name) { 275 return (OutputMapping) _outputs.get(name); 276 } 277 278 281 public boolean isStatic() { 282 return getHandlerDefinition().isStatic(); 283 } 284 285 288 public Object invoke(HandlerContext handlerContext) throws InstantiationException , IllegalAccessException , InvocationTargetException { 289 Object retVal = null; 290 HandlerDefinition handlerDef = getHandlerDefinition(); 291 Method method = handlerDef.getHandlerMethod(); 292 293 Object result = handlerContext.getLayoutElement().dispatchHandlers( 297 new HandlerContextImpl(handlerContext), 298 handlerDef.getChildHandlers()); 299 300 if (method != null) { 303 Object instance = null; 304 if (!isStatic()) { 305 instance = method.getDeclaringClass().newInstance(); 307 } 308 309 retVal = method.invoke(instance, new Object [] {handlerContext}); 311 if (retVal != null) { 312 result = retVal; 313 } 314 } 315 316 return result; 318 } 319 320 321 private HandlerDefinition _handlerDef = null; 322 private Map _inputs = new HashMap (); 323 private Map _outputs = new HashMap (); 324 } 325 | Popular Tags |