1 18 19 package org.objectweb.jac.aspects.gui; 20 21 import java.util.Arrays ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import org.apache.log4j.Logger; 25 import org.objectweb.jac.core.Collaboration; 26 import org.objectweb.jac.core.rtti.AbstractMethodItem; 27 import org.objectweb.jac.core.rtti.ConstructorItem; 28 import org.objectweb.jac.core.rtti.MethodItem; 29 import org.objectweb.jac.util.Exceptions; 30 31 47 48 public class InvokeThread extends Thread { 49 static Logger logger = Logger.getLogger("gui.threads"); 50 static Logger loggerInput = Logger.getLogger("gui.input"); 51 52 InvokeEvent invoke; 53 Object returnValue; 54 String [] attrNames; 55 Object [] attrValues; 56 String [] lattrNames; 57 Object [] lattrValues; 58 Collaboration parentCollaboration; 59 boolean showResult = true; 60 61 73 public static InvokeThread run(InvokeEvent invoke, 74 String [] attrNames, Object [] attrValues, 75 String [] lattrNames, Object [] lattrValues) { 76 InvokeThread ret = new InvokeThread(invoke); 77 ret.parentCollaboration = Collaboration.get(); 78 ret.attrNames = attrNames; 79 ret.attrValues = attrValues; 80 ret.lattrNames = lattrNames; 81 ret.lattrValues = lattrValues; 82 ret.start(); 83 return ret; 84 } 85 86 98 public static 99 InvokeThread quietRun(InvokeEvent invoke, 100 String [] attrNames, Object [] attrValues, 101 String [] lattrNames, Object [] lattrValues) 102 { 103 InvokeThread ret = new InvokeThread(invoke); 104 ret.parentCollaboration = Collaboration.get(); 105 ret.attrNames = attrNames; 106 ret.attrValues = attrValues; 107 ret.lattrNames = lattrNames; 108 ret.lattrValues = lattrValues; 109 ret.start(); 111 return ret; 112 } 113 114 119 public static InvokeThread run(InvokeEvent invoke) { 120 InvokeThread ret = new InvokeThread(invoke); 121 ret.start(); 122 return ret; 123 } 124 125 133 public InvokeThread(InvokeEvent invoke) { 134 this.invoke = invoke; 135 } 136 137 151 public InvokeThread(InvokeEvent invoke, 152 String [] attrNames, Object [] attrValues, 153 String [] lattrNames, Object [] lattrValues) { 154 this.invoke = invoke; 155 this.parentCollaboration = Collaboration.get(); 156 this.attrNames = attrNames; 157 this.attrValues = attrValues; 158 this.lattrNames = lattrNames; 159 this.lattrValues = lattrValues; 160 this.showResult = false; 161 } 162 163 169 public void run() { 170 Collaboration collab = Collaboration.get(); 171 Object [] parameters = invoke.getParameters(); 172 AbstractMethodItem method = invoke.getMethod(); 173 Object substance = invoke.getSubstance(); 174 175 logger.debug("invokeThread "+this+": "+invoke); 176 if (parentCollaboration!=null) { 177 Iterator it = parentCollaboration.attributeNames().iterator(); 178 while (it.hasNext()) { 179 String attrName = (String )it.next(); 180 collab.addAttribute( 181 attrName,parentCollaboration.getAttribute(attrName)); 182 } 183 logger.debug("application = "+parentCollaboration.getCurApp()); 184 collab.setCurApp(parentCollaboration.getCurApp()); 185 } 186 if (attrNames != null) { 187 for (int i=0; i<attrNames.length; i++) { 188 logger.debug("setting attribute " + attrNames[i] + 189 " to " + attrValues[i]); 190 collab.addAttribute( 191 attrNames[i], attrValues[i]); 192 } 193 } 194 if (lattrNames != null) { 195 for (int i=0; i<lattrNames.length; i++) { 196 logger.debug("setting local attribute " + 197 lattrNames[i] + " to " + lattrValues[i]); 198 collab.addAttribute( 199 lattrNames[i], lattrValues[i]); 200 } 201 } 202 DisplayContext context = (DisplayContext)collab 203 .getAttribute(GuiAC.DISPLAY_CONTEXT); 204 205 CustomizedDisplay display = context.getDisplay(); 206 try { 207 logger.debug("InvokeThread " + invoke); 208 209 Class [] paramTypes = method.getParameterTypes(); 210 if (paramTypes.length != invoke.getParameters().length) 211 throw new RuntimeException ("Wrong number of parameters ("+ 212 parameters.length+") for "+method); 213 for (int i=0; i<parameters.length; i++) { 214 if (parameters[i]==null) { 215 if (paramTypes[i] == float.class) { 216 method.setParameter(parameters,i,new Float (0.0)); 217 } else if (paramTypes[i] == long.class) { 218 method.setParameter(parameters,i,new Long (0)); 219 } else if (paramTypes[i] == double.class) { 220 method.setParameter(parameters,i,new Double (0.0)); 221 } else if (paramTypes[i] == byte.class) { 222 method.setParameter(parameters,i,new Byte ((byte)0)); 223 } else if (paramTypes[i] == char.class) { 224 method.setParameter(parameters,i,new Character (' ')); 225 } else if (paramTypes[i] == short.class) { 226 method.setParameter(parameters,i,new Short ((short)0)); 227 } else if (paramTypes[i] == int.class) { 228 method.setParameter(parameters,i,new Integer (0)); 229 } else if (paramTypes[i] == boolean.class) { 230 method.setParameter(parameters,i,Boolean.FALSE); 231 } 232 } 233 } 234 235 if (method instanceof ConstructorItem) { 236 returnValue = ((ConstructorItem)method).newInstance(parameters); 237 } else { 238 returnValue = ((MethodItem)method).invoke(substance, parameters); 239 } 240 241 if (display != null) { 242 display.onInvocationReturn(substance,method); 243 } 244 List hooks = (List )method.getAttribute(GuiAC.POST_INVOKE_HOOKS); 245 if (hooks!=null) { 246 Iterator i = hooks.iterator(); 247 while (i.hasNext()) { 248 AbstractMethodItem hook = (AbstractMethodItem)i.next(); 249 try { 250 loggerInput.debug("Invoking post hook "+hook.getName()); 251 hook.invoke( 252 null, 253 new Object [] {invoke}); 254 } catch (Exception e) { 255 loggerInput.error("Post invoke hook for "+ 256 substance+"."+ 257 method.getFullName()+" failed",e); 258 } 259 } 260 } 261 262 if (method.getType() != void.class) { 263 if (display != null && showResult) { 264 265 if (collab.getAttribute(GuiAC.OPEN_VIEW)!=null) { 266 display.openView(returnValue); 267 } else { 268 if(method.getAttribute(GuiAC.SMALL_TARGET_CONTAINER)!=null) { 269 EventHandler.get().onSelection(context,method,returnValue,null,null,false); 270 } else if (returnValue instanceof HandlerResult) { 271 EventHandler.get().handleResult(context,(HandlerResult)returnValue); 272 } else { 273 display.show(returnValue); 274 } 275 } 276 } 277 } else { 278 if (display != null && showResult) 279 display.refresh(); 280 } 281 } catch (Exception e) { 282 Throwable te = Exceptions.getTargetException(e); 283 logger.debug(this+" TargetException is "+te); 284 if (te instanceof TimeoutException) { 285 logger.debug(this+" Timeout"); 286 DialogView dialog = ((TimeoutException)te).getDialog(); 287 display.closeWindow(dialog,false); 288 display.addTimedoutDialog(dialog); 289 } else if (display != null && showResult) { 290 if (!(te instanceof org.objectweb.jac.util.VoidException)) 291 display.showModal(te,"Error","",context.getWindow(),false,false,true); 292 else 293 display.refresh(); 294 } 295 } 296 logger.debug("invokeThread done "+this+": "+ 297 substance+"."+method+Arrays.asList(parameters)); 298 } 299 300 Object getReturnValue() { 301 return returnValue; 302 } 303 } 304 305 class NotAvailableException extends Exception {} 306 | Popular Tags |