1 28 29 package com.idaremedia.antx; 30 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 37 import org.apache.tools.ant.BuildException; 38 import org.apache.tools.ant.Location; 39 import org.apache.tools.ant.Project; 40 import org.apache.tools.ant.Task; 41 import org.apache.tools.ant.types.PropertySet; 42 43 import com.idaremedia.antx.apis.FlexStringFriendly; 44 import com.idaremedia.antx.apis.Nameable; 45 import com.idaremedia.antx.helpers.Strings; 46 import com.idaremedia.antx.helpers.Tk; 47 48 59 60 public final class ErrorSnapshot extends AssertableProjectComponent 61 implements Cloneable , Nameable, FlexStringFriendly 62 { 63 66 private void init(Task source) 67 { 68 require_(source!=null,"ctor- nonzro task"); 69 setProject(source.getProject()); 70 m_taskName = source.getTaskName(); 71 m_targetName = source.getOwningTarget().getName(); 72 setLocation(source.getLocation()); 73 m_Id = "ErrorSnapshot@"+System.identityHashCode(this); 74 } 75 76 77 81 public ErrorSnapshot() 82 { 83 super(AntX.flow+"protect"); 84 } 85 86 87 91 public ErrorSnapshot(Task source) 92 { 93 super(AntX.flow+"protect"); 94 init(source); 95 } 96 97 98 101 public ErrorSnapshot(Task source, Exception thrown) 102 { 103 super(AntX.flow+"protect"); 104 init(source); 105 setThrown(thrown); 106 } 107 108 109 114 public String getName() 115 { 116 return m_Id; 117 } 118 119 120 124 public void setName(String name) 125 { 126 if (name!=null) { 127 m_Id = name; 128 } 129 } 130 131 132 136 public String getTaskName() 137 { 138 return m_taskName; 139 } 140 141 142 146 public void setEffectiveTaskName(String taskName) 147 { 148 require_(taskName!=null,"setTskNam- nonzro nam"); 149 m_taskName = taskName; 150 } 151 152 153 158 public String getTargetName() 159 { 160 return m_targetName; 161 } 162 163 164 168 public void setEffectiveTargetName(String targetName) 169 { 170 require_(targetName!=null,"setTrgtNam- nonzro nam"); 171 m_targetName = targetName; 172 } 173 174 175 179 public synchronized void setThrown(Exception thrown) 180 { 181 m_thrown = thrown; 182 if (getLocation()==null && (thrown instanceof BuildException)) { 183 setLocation(((BuildException)thrown).getLocation()); 184 } 185 } 186 187 188 195 public synchronized void setThrown(Exception thrown, 196 boolean updateLoc) 197 { 198 m_thrown = thrown; 199 if (updateLoc && (thrown instanceof BuildException)) { 200 setLocation(((BuildException)thrown).getLocation()); 201 } 202 } 203 204 205 209 public Exception getThrown() 210 { 211 return m_thrown; 212 } 213 214 215 218 public void setLocation(Location location) 219 { 220 m_location = location; 221 } 222 223 224 228 public Location getLocation() 229 { 230 return m_location; 231 } 232 233 234 237 public void setComment(String comment) 238 { 239 m_comment = (comment==null) ? "" : comment; 240 } 241 242 243 247 public final String getComment() 248 { 249 return m_comment; 250 } 251 252 253 258 public final void addConfiguredPropertySet(PropertySet ps) 259 { 260 require_(ps!=null,"addPropSet- nonzro propset"); 261 try { 262 Properties p= ps.getProperties(); 263 synchronized(this) { 264 if (m_env==null) { 265 m_env = p; 266 } else { 267 getEnv().putAll(p); 268 p.clear(); 269 } 270 } 271 p=null; 272 } catch(RuntimeException rtX) { 273 log(uistrs().get("task.bad.propset",rtX.getMessage()), 274 Project.MSG_ERR); 275 } 276 } 277 278 279 283 protected synchronized Hashtable getEnv() 284 { 285 if (m_env==null) { 286 m_env = new Hashtable (31,0.8f); 287 } 288 return m_env; 289 } 290 291 292 297 public void setProperty(String name, String value) 298 { 299 if (value==null) { 300 value=Strings.NULL; 301 } 302 getEnv().put(name,value); 303 } 304 305 306 310 public void captureProperty(String name) 311 { 312 String value = getProject().getProperty(name); 313 if (value==null) { 314 value= Strings.NULL; 315 } 316 getEnv().put(name,value); 317 } 318 319 320 324 public void captureProperties(String namesList) 325 { 326 List keys = Tk.splitList(namesList,","); 327 for (int i=0,N=keys.size();i<N;i++) { 328 captureProperty((String )keys.get(i)); 329 } 330 } 331 332 333 334 341 public void captureProperties(Map properties) 342 { 343 getEnv().putAll(properties); 344 } 345 346 347 352 private void assumeProperties(Hashtable fullP) 353 { 354 if (m_env==null) { 355 m_env= fullP; 356 } else { 357 Hashtable byhand= m_env; 358 m_env= fullP; 359 m_env.putAll(byhand); } 361 } 362 363 364 367 public synchronized void captureUserProperties() { 369 assumeProperties(getProject().getUserProperties()); 370 } 371 372 373 376 public synchronized void captureAllProperties() { 378 assumeProperties(getProject().getProperties()); 379 } 380 381 382 386 public synchronized Properties copyOfProperties() 387 { 388 Hashtable myP = getEnv(); 389 synchronized(myP) { 390 if (myP instanceof Properties ) { 391 return (Properties )myP.clone(); 392 } 393 Properties copiedP= new Properties (); 394 if (!myP.isEmpty()) { 395 Iterator itr= myP.entrySet().iterator(); 396 while (itr.hasNext()) { 397 Map.Entry mE= (Map.Entry )itr.next(); 398 copiedP.setProperty(mE.getKey().toString(), 399 mE.getValue().toString()); 400 } 401 } 402 return copiedP; 403 } 404 } 405 406 407 411 public synchronized Object clone() 412 { 413 try { 414 ErrorSnapshot copy= (ErrorSnapshot)super.clone(); 415 copy.m_env = copyOfProperties(); return copy; 417 } catch(CloneNotSupportedException clnx) { 418 throw new InternalError (uistrs().get(AntX.CLONE_BROKEN_MSGID)); 419 } 420 } 421 422 423 428 public String toString() 429 { 430 String comment = getComment(); 431 432 if (getThrown()!=null) { 433 String what = getThrown().getMessage(); 434 return (comment!=null) ? (comment+" CAUSE: '"+what+"'") : what; 435 436 } else if (comment!=null && comment.length()>0) { 437 return comment; 438 439 } else if (getLocation()!=null) { 440 return getLocation().toString(); 441 } 442 443 return uistrs().get("snapshot.fallback.msg", 444 getTargetName(),getTaskName()); 445 } 446 447 448 452 public String stringFrom(Project p) 453 { 454 if (getThrown()!=null) { 455 return getThrown().getMessage(); 456 } 457 return getComment(); 458 } 459 460 461 private String m_Id; 462 private String m_targetName="", m_taskName=""; 463 private Location m_location=Location.UNKNOWN_LOCATION; 464 private Hashtable m_env; 465 private String m_comment=""; 466 private Exception m_thrown; 467 } 468 469 470 | Popular Tags |