1 28 29 package com.idaremedia.antx.ownhelpers; 30 31 import java.util.Collections ; 32 import java.util.Hashtable ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 36 import org.apache.tools.ant.BuildException; 37 import org.apache.tools.ant.Project; 38 import org.apache.tools.ant.PropertyHelper; 39 40 import com.idaremedia.antx.AntX; 41 import com.idaremedia.antx.AntXFixture; 42 import com.idaremedia.antx.FixtureExaminer; 43 import com.idaremedia.antx.apis.Nameable; 44 import com.idaremedia.antx.apis.ProblemHandler; 45 import com.idaremedia.antx.apis.ProjectDependent; 46 47 74 75 public final class ProjectPropertiesNet extends PropertyHelper 76 implements ProjectDependent, Nameable 77 { 78 private static final String IAM_ = AntX.utilities+"PropertiesNet"; 79 80 81 85 public static final String ANT_HELPER_REFID= "ant.PropertyHelper"; 86 87 88 89 93 public ProjectPropertiesNet() 94 { 95 super(); 96 } 97 98 99 100 107 public ProjectPropertiesNet(String name, Project project) 108 { 109 super(); 110 setProject(project); 111 if (name!=null) { 112 setName(name); 113 } 114 install(project); 115 } 116 117 118 119 130 public void seedProperties(Map properties, boolean control, boolean readonly) 131 { 132 AntX.require_(properties!=null,IAM_,"seed- nonzro property set"); 133 if (!properties.isEmpty()) { 134 if (control) { 135 m_controlProperties.putAll(properties); 136 m_cliProperties.putAll(properties); 137 } else if (readonly) { 138 m_cliProperties.putAll(properties); 139 } else { 140 Iterator itr= properties.entrySet().iterator(); 141 while (itr.hasNext()) { 142 Map.Entry e = (Map.Entry )itr.next(); 143 String value = (String )e.getValue(); 144 if (value==null || value.length()==0) { 145 e.setValue(EMPTY); 146 } 147 } 148 m_rwProperties = properties; 149 } 150 m_allProperties.putAll(properties); 151 } 152 } 153 154 155 156 161 public void setProject(Project project) 162 { 163 super.setProject(project); 164 m_project = project; 165 } 166 167 168 169 174 public Project getProject() 175 { 176 return m_project; 177 } 178 179 180 181 186 public final void setName(String name) 187 { 188 AntX.require_(name!=null,IAM_,"setName- nonzro name"); 189 m_nameImpl = name; 190 } 191 192 193 194 199 public final String getName() 200 { 201 return m_nameImpl; 202 } 203 204 205 206 210 private void verifyNotInChain_(PropertyHelper ph, Project P) 211 { 212 AntX.verify_(ph!=null,IAM_,"install- Ant env inited"); 213 do { 214 if (ph==this) { 215 String error = AntX.uistrs().get("fixture.ph.install.once",getName()); 216 P.log(error,Project.MSG_ERR); 217 throw new BuildException(error); 218 } 219 ph = ph.getNext(); 220 }while (ph!=null); 221 } 222 223 224 225 232 public boolean install(Project project) 233 { 234 AntX.require_(project!=null,IAM_,"install- nonzro project"); 235 setProject(project); 236 PropertyHelper current = PropertyHelper.getPropertyHelper(project); 237 238 verifyNotInChain_(current,project); 239 240 log("Installing...",Project.MSG_VERBOSE); 241 m_defImpl = current; 242 project.getReferences().put(ANT_HELPER_REFID,this); 243 244 AntX.verify_(PropertyHelper.getPropertyHelper(project)==this, 245 IAM_,"install- installed properly"); 246 return true; 247 } 248 249 250 251 258 public void uninstall(ProblemHandler notInstalled) 259 { 260 Project P = getProjectNoNull(); 261 PropertyHelper current = PropertyHelper.getPropertyHelper(P); 262 if (current==this) { 263 log("Uninstalling...",Project.MSG_VERBOSE); 264 P.getReferences().put(ANT_HELPER_REFID,m_defImpl); 265 m_defImpl = null; 266 } else if (current!=null) { 267 String error = AntX.uistrs().get("fixture.ph.not.instald",getName()); 268 if (notInstalled!=null) { 269 notInstalled.problem(error,Project.MSG_ERR); 270 } 271 throw new BuildException(error); 272 } 273 } 274 275 276 277 281 public synchronized boolean setProperty(String ns, String name, 282 Object value, boolean verbose) 283 { 284 AntX.require_(name!=null,IAM_,"setProperty- nonzro name"); 285 boolean written = true; 286 287 if (getUserProperty(ns,name)==null) { 288 if (setPropertyHook(ns,name,value,false,false,false)) { 289 if (verbose) { 290 log("Set local hook property '"+name+ 291 "' -> "+value, Project.MSG_DEBUG); 292 } 293 } else { 294 Object old = m_allProperties.put(name,value); 295 if (old!=null && old!=EMPTY) { 296 log("Overwrote script property '"+name+"'"); 297 } 298 else if (verbose) { 299 log("Set script property '"+name+ 300 "' -> "+value, Project.MSG_DEBUG); 301 } 302 } 303 } else { 304 written = false; 305 if (verbose) { 306 log("Ignored script definition of '"+name+"'"); 307 } 308 } 309 return written; 310 } 311 312 313 314 318 public synchronized void setNewProperty(String ns, String name, 319 Object value) 320 { 321 if (getProperty(ns,name)!=null) { 322 log("Ignored new script definition of '"+name+"'"); 323 } else { 324 m_allProperties.put(name,value); 325 log("Set script property '"+name+"' -> "+value, 326 Project.MSG_DEBUG); 327 } 328 } 329 330 331 332 337 public synchronized void setUserProperty(String ns, String name, 338 Object value) 339 { 340 if (setPropertyHook(ns,name,value,false,true,false)) { 341 log("(Re)Set local hook CLI property '"+name+"' -> "+value); 342 } 343 else { 344 m_cliProperties.put(name,value); 345 m_allProperties.put(name,value); 346 log("(Re)Set CLI property '"+name+"' -> "+value); 347 } 348 } 349 350 351 352 358 public synchronized void setInheritedProperty(String ns, String name, 359 Object value) 360 { 361 if (setPropertyHook(ns,name,value,true,false,false)) { 362 log("(Re)Set local hook CTRL property '"+name+"' -> "+value); 363 } 364 else { 365 m_controlProperties.put(name,value); 366 m_cliProperties.put(name,value); 367 m_allProperties.put(name,value); 368 log("(Re)Set CTRL property '"+name+"' -> "+value); 369 } 370 } 371 372 373 374 private Object readLocalURIs(String ns, String name, boolean userOnly) 375 { 376 Project p = getProjectNoNull(); 377 if (FixtureExaminer.valueURIInterpreterInstalled(p)) { 378 return null; 379 } 380 if ((ns==null || ns.length()==0) && !userOnly) { 381 return FixtureExaminer.findValue(p,name,null); 382 } 383 return null; 384 } 385 386 387 388 395 public Object getPropertyHook(String ns, String name, boolean userOnly) 396 { 397 Object o; 398 399 if (getOverride()!=null) { 400 o = getOverride().getPropertyHook(ns,name,userOnly); 401 if (o!=null) { 402 return o; 403 } 404 } 405 return readLocalURIs(ns,name,userOnly); 406 } 407 408 409 410 415 public synchronized Object getProperty(String ns, String name) 416 { 417 Object o = getPropertyHook(ns,name,false); 418 return o==null ? getProperty0(ns,name) : o; 419 } 420 421 422 423 427 public synchronized Object getUserProperty(String ns, String name) 428 { 429 Object o = getPropertyHook(ns,name,true); 430 return o==null ? getUserProperty0(ns,name) : o; 431 } 432 433 434 435 439 public synchronized Hashtable getProperties() 440 { 441 Hashtable copy = m_defImpl.getProperties(); 442 copy.putAll(m_allProperties); 443 return copy; 444 } 445 446 447 448 452 public synchronized Hashtable getUserProperties() 453 { 454 Hashtable copy = m_defImpl.getUserProperties(); 455 copy.putAll(m_cliProperties); 456 return copy; 457 } 458 459 460 461 464 public synchronized void copyInheritedProperties(Project other) 465 { 466 AntX.require_(other!=getProject(),IAM_,"copyInherited- not same"); 467 468 if (!m_controlProperties.isEmpty()) { 469 Iterator itr = m_controlProperties.entrySet().iterator(); 470 while (itr.hasNext()) { 471 Map.Entry mE = (Map.Entry )itr.next(); 472 String key = (String )mE.getKey(); 473 if (other.getUserProperty(key)==null) { 474 other.setInheritedProperty(key,(String )mE.getValue()); 475 } 476 } 477 } 478 m_defImpl.copyInheritedProperties(other); } 480 481 482 483 487 public synchronized void copyUserProperties(Project other) 488 { 489 m_defImpl.copyUserProperties(other); 490 Iterator itr= m_cliProperties.entrySet().iterator(); 491 while (itr.hasNext()) { 492 Map.Entry mE= (Map.Entry )itr.next(); 493 if (!m_controlProperties.containsKey(mE.getKey())) { 494 other.setUserProperty((String )mE.getKey(),(String )mE.getValue()); 495 } 496 } 497 } 498 499 500 501 511 public Map getFinalProperties(boolean filtered, boolean blocking) 512 { 513 Hashtable ht = new Hashtable (m_allProperties); 514 if (filtered) { 515 if (blocking) { 516 ht.keySet().removeAll(m_rwProperties.keySet()); 517 } else { 518 ht.keySet().retainAll(m_rwProperties.keySet()); 519 Iterator itr= ht.values().iterator(); 520 while (itr.hasNext()) { 521 if (itr.next()==EMPTY) { 522 itr.remove(); 523 } 524 } 525 } 526 } 527 return ht; 528 } 529 530 531 532 537 private Project getProjectNoNull() 538 { 539 Project P = getProject(); 540 AntX.verify_(P!=null,IAM_,"getProj- inited"); 541 return P; 542 } 543 544 545 549 private Object getUserProperty0(String ns, String name) 550 { 551 if (name==null) { 552 return null; 553 } 554 Object o = m_cliProperties.get(name); 555 if (o==null) { 556 o = m_defImpl.getUserProperty(ns,name); 557 } 558 return o; 559 } 560 561 562 566 private Object getProperty0(String ns, String name) 567 { 568 if (name==null) { 569 return null; 570 } 571 Object o = m_allProperties.get(name); 572 if (o==null) { 573 o = m_defImpl.getProperty(ns,name); 574 } else { 575 if (o==EMPTY) { o = null; 577 } 578 } 579 return o; 580 } 581 582 583 private void log(String msg, int noiselevel) 584 { 585 getProjectNoNull().log("PropertiesNet("+getName()+"): "+msg, 586 noiselevel); 587 } 588 589 590 private void log(String msg) 591 { 592 getProjectNoNull().log("PropertiesNet("+getName()+"): "+msg, 593 Project.MSG_VERBOSE); 594 } 595 596 597 private PropertyHelper getOverride() 598 { 599 return getNext(); } 601 602 603 private static final String EMPTY= new String (""); private Project m_project; 605 private PropertyHelper m_defImpl; 606 private String m_nameImpl="n/d"; 607 private Map m_allProperties = AntXFixture.newMap(); private Map m_cliProperties = AntXFixture.newMap(); 609 private Map m_controlProperties = AntXFixture.newMap(); 610 private Map m_rwProperties = Collections.EMPTY_MAP;} 612 613 614 615 | Popular Tags |