1 28 29 package com.idaremedia.antx.feedback; 30 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.Project; 36 import com.idaremedia.apis.DiagnosticsEmitter; 37 38 import com.idaremedia.antx.AntX; 39 import com.idaremedia.antx.AntXFixture; 40 import com.idaremedia.antx.ErrorSnapshot; 41 import com.idaremedia.antx.NoiseLevel; 42 import com.idaremedia.antx.helpers.Setting; 43 import com.idaremedia.antx.helpers.Strings; 44 import com.idaremedia.antx.helpers.Tk; 45 import com.idaremedia.antx.starters.MsgTask; 46 47 69 70 public class EmitTask extends MsgTask implements EmitConfigurable 71 { 72 75 public EmitTask() 76 { 77 super(AntX.feedback); 78 } 79 80 81 84 public EmitTask(String iam) 85 { 86 super(iam); 87 } 88 89 90 97 public void addConfiguredInclude(EmitInclude include) 98 { 99 require_(include!=null,"addIncl- nonzro incl"); 100 getNestedInclusions().add(include); 101 } 102 103 104 108 protected final List getNestedInclusions() 109 { 110 return m_nestedIncludes; 111 } 112 113 114 120 public void setEcho(String wantIt) 121 { 122 require_(wantIt!=null,"setEcho- nonzro arg"); 123 m_echoPref= Setting.from(wantIt, Setting.INHERITED); } 125 126 127 131 public final Setting getPreferEcho() 132 { 133 return m_echoPref; 134 } 135 136 137 141 protected final boolean shouldEcho(EmitConfiguration defaults) 142 { 143 switch (getPreferEcho().getIndex()) { 144 case Setting.ON_INDEX: { 145 return true; 146 } 147 case Setting.OFF_INDEX: { 148 return false; 149 } 150 default: { 151 return defaults.shouldEcho(); 152 } 153 } 154 } 155 156 157 161 protected final NoiseLevel getNoiseLevel(EmitConfiguration defaults) 162 { 163 NoiseLevel nl = getPreferredMsgLevel(); 164 if (nl==null) { 165 nl= defaults.getNoiseLevel(); 166 } 167 if (nl==null) { 168 nl= getDefaultMsgLevel(); 169 ensure_(nl!=null,"getEffPri- nonzro lvl"); 170 } 171 return nl; 172 } 173 174 175 182 public void setProperties(String nameList) 183 { 184 require_(nameList!=null, "setProps- nonzro lst"); 185 m_propertyNames= nameList; 186 } 187 188 189 194 public final String getPropertyNamesList() 195 { 196 return m_propertyNames; 197 } 198 199 200 207 private boolean captureNamedProperties(ErrorSnapshot es, String nameList) 208 { 209 boolean all=false; 210 211 if (nameList!=null) { 212 String normalized = Tk.lowercaseFrom(nameList.trim()); 213 if (Strings.USER.equals(normalized)) { 214 es.captureUserProperties(); 215 } else if (Strings.ALL.equals(normalized)) { 216 es.captureAllProperties(); 217 all=true; 218 } else { 219 es.captureProperties(nameList); 220 } 221 } 222 return all; 223 } 224 225 226 233 private void captureProperties(EmitConfiguration defaults, 234 StringBuffer defaultProperties, 235 ErrorSnapshot es) 236 { 237 boolean ignore = false; 238 239 if (defaultProperties.length()>0) { 240 ignore = captureNamedProperties(es,defaultProperties.substring(0)); 241 } 242 243 if (!ignore) { 244 String nameList = getPropertyNamesList(); 245 if (nameList!=null) { 246 ignore= captureNamedProperties(es,nameList); 247 } 248 } 249 250 if (!getNestedInclusions().isEmpty()) { 251 Iterator itr= getNestedInclusions().iterator(); 252 while (itr.hasNext()) { 253 ((EmitInclude)itr.next()).apply(es); 254 } 255 } 256 } 257 258 259 266 public void setThrownRefId(String refId) 267 { 268 require_(refId!=null,"setThrownRefId- nonzro refid"); 269 m_thrownRefId= refId; 270 } 271 272 273 277 public final void setThrown(String refId) 278 { 279 setThrownRefId(refId); 280 } 281 282 283 287 public final String getThrownRefId() 288 { 289 return m_thrownRefId; 290 } 291 292 293 299 private Exception getThrown() 300 { 301 String refid = getThrownRefId(); 302 if (refid!=null) { 303 Object ox= getProject().getReference(refid); 304 if (ox instanceof BuildException) { 305 return (BuildException)ox; 306 } else if (ox instanceof ErrorSnapshot) { 307 return ((ErrorSnapshot)ox).getThrown(); 308 } 309 String ermsg = uistrs().get("task.echo.bad.refid", refid); 310 log(ermsg, Project.MSG_WARN); 311 } 312 return null; 313 } 314 315 316 321 public void setTimestamp(String setin) 322 { 323 require_(setin!=null,"setTm- nonzro setin"); 324 m_timestampPref = Setting.from(setin, Setting.INHERITED); } 326 327 328 332 public final Setting getPreferTimestamp() 333 { 334 return m_timestampPref; 335 } 336 337 338 343 protected final String getTimestampLiteral(EmitConfiguration defaults, final long NOW) 344 { 345 String tsl = ""; 346 switch (getPreferTimestamp().getIndex()) { 347 case Setting.OFF_INDEX: { 348 break; 349 } 350 case Setting.ON_INDEX: { 351 tsl = defaults.stampify(NOW); 352 break; 353 } 354 default: { 355 if (defaults.wantTimestamp()) { 356 tsl = defaults.stampify(NOW); 357 } 358 } 359 } 360 return tsl; 361 } 362 363 364 370 private String getLocalComment(EmitConfiguration defaults, final long NOW) 371 { 372 String tsl= getTimestampLiteral(defaults,NOW); 373 String msg= null; 374 375 if (getMsgId()!=null) { 376 377 382 String arg3 = getMsgArg1(); 383 String arg4 = getMsgArg2(); 384 385 if (arg3!=null || arg4!=null) { 386 if (arg3!=null) { 387 if (arg4!=null) { 388 msg = getMsg(newMsgGetter(tsl,arg3,arg4)); 389 } else { 390 msg = getMsg(newMsgGetter(tsl,arg3)); 391 } 392 } else { 393 msg = getMsg(newMsgGetter(tsl,"",arg4)); 394 } 395 } 396 else { 397 msg = getMsg(newMsgGetter(tsl)); 398 } 399 } 400 401 405 if (Tk.isWhitespace(msg) && getDefaultMsg()!=null) { 406 msg = getDefaultMsg(); 407 } 408 409 412 if (Tk.isWhitespace(msg)) { 413 msg = tsl; 414 } 415 416 return msg; 417 } 418 419 420 425 public void setFrom(String categoryId) 426 { 427 require_(categoryId!=null,"setFrom- nonzro category"); 428 m_categoryId= categoryId; 429 } 430 431 432 437 public final String getFrom() 438 { 439 return m_categoryId; 440 } 441 442 443 446 public void setTask(String pseudoTaskName) 447 { 448 require_(pseudoTaskName!=null,"setTsk- nonzro pseudoNam"); 449 m_customTaskName= pseudoTaskName; 450 } 451 452 453 457 public final String getEmittedTaskName() 458 { 459 String name = getTaskName(); 460 if (m_customTaskName!=null) { 461 name = m_customTaskName; 462 } 463 return name; 464 } 465 466 467 470 protected final synchronized DiagnosticsEmitter getEmitter(EmitConfiguration defaults) 471 { 472 if (m_emitr!=null) { 473 return m_emitr; 474 } 475 String grpId = getFrom(); 476 if (grpId!=null) { 477 m_emitr = defaults.getCustomEmitter(grpId); 478 } else { 479 m_emitr = defaults.getEmitter(); 480 } 481 return m_emitr; 482 } 483 484 485 490 private ErrorSnapshot getSnapshot(EmitConfiguration defaults, 491 StringBuffer properties, 492 final long NOW) 493 { 494 ErrorSnapshot es =null; 495 496 Exception thrown= getThrown(); 498 if (thrown!=null) { 499 es = new ErrorSnapshot(this, thrown); 500 } else { 501 es = new ErrorSnapshot(this); 502 } 503 if (getThrownRefId()!=null) { 504 es.setName(getThrownRefId()); 505 } 506 507 es.setEffectiveTaskName(getEmittedTaskName()); 509 510 captureProperties(defaults,properties,es); 512 513 es.setComment(getLocalComment(defaults,NOW)); 515 516 return es; 517 } 518 519 520 526 private boolean needsSnapshot(EmitConfiguration defaults, 527 StringBuffer defaultProperties) 528 { 529 return (defaults.getPropertiesNameList(defaultProperties) || 530 defaultProperties.length()>0 || 531 getPropertyNamesList()!=null || 532 !getNestedInclusions().isEmpty() 533 ); 534 } 535 536 537 541 public EmitConfiguration getDefaults() 542 { 543 EmitConfiguration ec = EmitContext.getConfiguration(); 544 if (ec==null) { 545 ec= DefaultEmitConfiguration.INSTANCE; 546 } 547 return ec; 548 } 549 550 551 556 public void execute() throws BuildException 557 { 558 final long NOW = System.currentTimeMillis(); 559 560 verifyCanExecute_("execute"); 561 562 EmitConfiguration defaults = getDefaults(); 564 DiagnosticsEmitter emitter = getEmitter(defaults); 565 NoiseLevel nl = getNoiseLevel(defaults); 566 567 boolean isEchoed = shouldEcho(defaults); 568 boolean isEmitted = emitter.emits(nl.getIndex()); 569 570 if (isEmitted || isEchoed) { 571 StringBuffer defaultProperties = new StringBuffer (71); 572 573 if (needsSnapshot(defaults, defaultProperties)) { 574 ErrorSnapshot es = getSnapshot(defaults,defaultProperties,NOW); 575 576 if (isEmitted) { 577 Emit.broadcast(emitter, es, es.getThrown(), nl); 578 } 579 if (isEchoed) { 580 log(es.toString(), nl.getNativeIndex()); 581 } 582 } 583 else { 584 String msg = getLocalComment(defaults,NOW); 585 Exception thrown = getThrown(); 586 if (thrown!=null) { 587 msg += uistrs().get("emit.cause.msg.addon",thrown.getMessage()); 588 } 589 590 if (isEmitted) { 591 Emit.broadcast(emitter, msg, thrown, nl); 592 } 593 if (isEchoed) { 594 log(msg, nl.getNativeIndex()); 595 } 596 } 597 } 598 } 599 600 private List m_nestedIncludes = AntXFixture.newList(4); private String m_customTaskName; private String m_propertyNames; private String m_thrownRefId; private Setting m_timestampPref=Setting.INHERITED; private Setting m_echoPref= Setting.INHERITED; private String m_categoryId=null; private DiagnosticsEmitter m_emitr;} 609 610 611 | Popular Tags |