1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.util.Enumeration ; 27 import java.util.Properties ; 28 import java.util.Stack ; 29 import java.util.Vector ; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.PropertyHelper; 34 import org.apache.tools.ant.Task; 35 import org.apache.tools.ant.types.Path; 36 import org.apache.tools.ant.types.Reference; 37 38 71 public class Property extends Task { 72 73 protected String name; 75 protected String value; 76 protected File file; 77 protected URL url; 78 protected String resource; 79 protected Path classpath; 80 protected String env; 81 protected Reference ref; 82 protected String prefix; 83 private Project fallback; 84 85 protected boolean userProperty; 88 91 public Property() { 92 this(false); 93 } 94 95 100 protected Property(boolean userProperty) { 101 this(userProperty, null); 102 } 103 104 111 protected Property(boolean userProperty, Project fallback) { 112 this.userProperty = userProperty; 113 this.fallback = fallback; 114 } 115 116 120 public void setName(String name) { 121 this.name = name; 122 } 123 124 128 public String getName() { 129 return name; 130 } 131 132 142 public void setLocation(File location) { 143 setValue(location.getAbsolutePath()); 144 } 145 146 152 public void setValue(String value) { 153 this.value = value; 154 } 155 156 160 public String getValue() { 161 return value; 162 } 163 164 170 public void setFile(File file) { 171 this.file = file; 172 } 173 174 178 public File getFile() { 179 return file; 180 } 181 182 188 public void setUrl(URL url) { 189 this.url = url; 190 } 191 192 196 public URL getUrl() { 197 return url; 198 } 199 200 207 public void setPrefix(String prefix) { 208 this.prefix = prefix; 209 if (!prefix.endsWith(".")) { 210 this.prefix += "."; 211 } 212 } 213 214 219 public String getPrefix() { 220 return prefix; 221 } 222 223 232 public void setRefid(Reference ref) { 233 this.ref = ref; 234 } 235 236 240 public Reference getRefid() { 241 return ref; 242 } 243 244 250 public void setResource(String resource) { 251 this.resource = resource; 252 } 253 254 258 public String getResource() { 259 return resource; 260 } 261 262 282 public void setEnvironment(String env) { 283 this.env = env; 284 } 285 286 291 public String getEnvironment() { 292 return env; 293 } 294 295 299 public void setClasspath(Path classpath) { 300 if (this.classpath == null) { 301 this.classpath = classpath; 302 } else { 303 this.classpath.append(classpath); 304 } 305 } 306 307 311 public Path createClasspath() { 312 if (this.classpath == null) { 313 this.classpath = new Path(getProject()); 314 } 315 return this.classpath.createPath(); 316 } 317 318 323 public void setClasspathRef(Reference r) { 324 createClasspath().setRefid(r); 325 } 326 327 332 public Path getClasspath() { 333 return classpath; 334 } 335 336 343 public void setUserProperty(boolean userProperty) { 344 log("DEPRECATED: Ignoring request to set user property in Property" 345 + " task.", Project.MSG_WARN); 346 } 347 348 352 public String toString() { 353 return value == null ? "" : value; 354 } 355 356 362 public void execute() throws BuildException { 363 if (getProject() == null) { 364 throw new IllegalStateException ("project has not been set"); 365 } 366 367 if (name != null) { 368 if (value == null && ref == null) { 369 throw new BuildException("You must specify value, location or " 370 + "refid with the name attribute", 371 getLocation()); 372 } 373 } else { 374 if (url == null && file == null && resource == null && env == null) { 375 throw new BuildException("You must specify url, file, resource or " 376 + "environment when not using the " 377 + "name attribute", getLocation()); 378 } 379 } 380 381 if (url == null && file == null && resource == null && prefix != null) { 382 throw new BuildException("Prefix is only valid when loading from " 383 + "a url, file or resource", getLocation()); 384 } 385 386 if ((name != null) && (value != null)) { 387 addProperty(name, value); 388 } 389 390 if (file != null) { 391 loadFile(file); 392 } 393 394 if (url != null) { 395 loadUrl(url); 396 } 397 398 if (resource != null) { 399 loadResource(resource); 400 } 401 402 if (env != null) { 403 loadEnvironment(env); 404 } 405 406 if ((name != null) && (ref != null)) { 407 try { 408 addProperty(name, 409 ref.getReferencedObject(getProject()).toString()); 410 } catch (BuildException be) { 411 if (fallback != null) { 412 addProperty(name, 413 ref.getReferencedObject(fallback).toString()); 414 } else { 415 throw be; 416 } 417 } 418 } 419 } 420 421 426 protected void loadUrl(URL url) throws BuildException { 427 Properties props = new Properties (); 428 log("Loading " + url, Project.MSG_VERBOSE); 429 try { 430 InputStream is = url.openStream(); 431 try { 432 props.load(is); 433 } finally { 434 if (is != null) { 435 is.close(); 436 } 437 } 438 addProperties(props); 439 } catch (IOException ex) { 440 throw new BuildException(ex, getLocation()); 441 } 442 } 443 444 445 450 protected void loadFile(File file) throws BuildException { 451 Properties props = new Properties (); 452 log("Loading " + file.getAbsolutePath(), Project.MSG_VERBOSE); 453 try { 454 if (file.exists()) { 455 FileInputStream fis = new FileInputStream (file); 456 try { 457 props.load(fis); 458 } finally { 459 if (fis != null) { 460 fis.close(); 461 } 462 } 463 addProperties(props); 464 } else { 465 log("Unable to find property file: " + file.getAbsolutePath(), 466 Project.MSG_VERBOSE); 467 } 468 } catch (IOException ex) { 469 throw new BuildException(ex, getLocation()); 470 } 471 } 472 473 477 protected void loadResource(String name) { 478 Properties props = new Properties (); 479 log("Resource Loading " + name, Project.MSG_VERBOSE); 480 InputStream is = null; 481 try { 482 ClassLoader cL = null; 483 484 if (classpath != null) { 485 cL = getProject().createClassLoader(classpath); 486 } else { 487 cL = this.getClass().getClassLoader(); 488 } 489 490 if (cL == null) { 491 is = ClassLoader.getSystemResourceAsStream(name); 492 } else { 493 is = cL.getResourceAsStream(name); 494 } 495 496 if (is != null) { 497 props.load(is); 498 addProperties(props); 499 } else { 500 log("Unable to find resource " + name, Project.MSG_WARN); 501 } 502 } catch (IOException ex) { 503 throw new BuildException(ex, getLocation()); 504 } finally { 505 if (is != null) { 506 try { 507 is.close(); 508 } catch (IOException e) { 509 } 511 } 512 } 513 514 } 515 516 520 protected void loadEnvironment(String prefix) { 521 Properties props = new Properties (); 522 if (!prefix.endsWith(".")) { 523 prefix += "."; 524 } 525 log("Loading Environment " + prefix, Project.MSG_VERBOSE); 526 Vector osEnv = Execute.getProcEnvironment(); 527 for (Enumeration e = osEnv.elements(); e.hasMoreElements();) { 528 String entry = (String ) e.nextElement(); 529 int pos = entry.indexOf('='); 530 if (pos == -1) { 531 log("Ignoring: " + entry, Project.MSG_WARN); 532 } else { 533 props.put(prefix + entry.substring(0, pos), 534 entry.substring(pos + 1)); 535 } 536 } 537 addProperties(props); 538 } 539 540 545 protected void addProperties(Properties props) { 546 resolveAllProperties(props); 547 Enumeration e = props.keys(); 548 while (e.hasMoreElements()) { 549 String propertyName = (String ) e.nextElement(); 550 String propertyValue = props.getProperty(propertyName); 551 552 String v = getProject().replaceProperties(propertyValue); 553 554 if (prefix != null) { 555 propertyName = prefix + propertyName; 556 } 557 558 addProperty(propertyName, v); 559 } 560 } 561 562 567 protected void addProperty(String n, String v) { 568 if (userProperty) { 569 if (getProject().getUserProperty(n) == null) { 570 getProject().setInheritedProperty(n, v); 571 } else { 572 log("Override ignored for " + n, Project.MSG_VERBOSE); 573 } 574 } else { 575 getProject().setNewProperty(n, v); 576 } 577 } 578 579 583 private void resolveAllProperties(Properties props) throws BuildException { 584 for (Enumeration e = props.keys(); e.hasMoreElements();) { 585 String propertyName = (String ) e.nextElement(); 586 Stack referencesSeen = new Stack (); 587 resolve(props, propertyName, referencesSeen); 588 } 589 } 590 591 601 private void resolve(Properties props, String name, Stack referencesSeen) 602 throws BuildException { 603 if (referencesSeen.contains(name)) { 604 throw new BuildException("Property " + name + " was circularly " 605 + "defined."); 606 } 607 608 String propertyValue = props.getProperty(name); 609 Vector fragments = new Vector (); 610 Vector propertyRefs = new Vector (); 611 PropertyHelper.getPropertyHelper( 612 this.getProject()).parsePropertyString( 613 propertyValue, fragments, propertyRefs); 614 615 if (propertyRefs.size() != 0) { 616 referencesSeen.push(name); 617 StringBuffer sb = new StringBuffer (); 618 Enumeration i = fragments.elements(); 619 Enumeration j = propertyRefs.elements(); 620 while (i.hasMoreElements()) { 621 String fragment = (String ) i.nextElement(); 622 if (fragment == null) { 623 String propertyName = (String ) j.nextElement(); 624 fragment = getProject().getProperty(propertyName); 625 if (fragment == null) { 626 if (props.containsKey(propertyName)) { 627 resolve(props, propertyName, referencesSeen); 628 fragment = props.getProperty(propertyName); 629 } else { 630 fragment = "${" + propertyName + "}"; 631 } 632 } 633 } 634 sb.append(fragment); 635 } 636 propertyValue = sb.toString(); 637 props.put(name, propertyValue); 638 referencesSeen.pop(); 639 } 640 } 641 } 642 | Popular Tags |