1 18 19 package org.apache.tools.ant.types.selectors.modifiedselector; 20 21 22 import java.util.Comparator ; 24 import java.util.Vector ; 25 import java.util.Iterator ; 26 import java.io.File ; 27 28 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.IntrospectionHelper; 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.BuildListener; 33 import org.apache.tools.ant.BuildEvent; 34 import org.apache.tools.ant.types.EnumeratedAttribute; 35 import org.apache.tools.ant.types.Parameter; 36 import org.apache.tools.ant.types.Path; 37 import org.apache.tools.ant.types.Resource; 38 import org.apache.tools.ant.types.resources.FileResource; 39 import org.apache.tools.ant.types.resources.selectors.ResourceSelector; 40 import org.apache.tools.ant.types.selectors.BaseExtendSelector; 41 import org.apache.tools.ant.util.FileUtils; 42 import org.apache.tools.ant.util.ResourceUtils; 43 44 45 145 public class ModifiedSelector extends BaseExtendSelector 146 implements BuildListener, ResourceSelector { 147 148 149 151 152 153 private CacheName cacheName = null; 154 155 156 private String cacheClass; 157 158 159 private AlgorithmName algoName = null; 160 161 162 private String algorithmClass; 163 164 165 private ComparatorName compName = null; 166 167 168 private String comparatorClass; 169 170 171 private boolean update = true; 172 173 174 private boolean selectDirectories = true; 175 176 180 private boolean selectResourcesWithoutInputStream = true; 181 182 183 private boolean delayUpdate = true; 184 185 186 188 189 190 private Comparator comparator = null; 191 192 193 private Algorithm algorithm = null; 194 195 196 private Cache cache = null; 197 198 199 private int modified = 0; 200 201 202 private boolean isConfigured = false; 203 204 208 private Vector configParameter = new Vector (); 209 210 216 private Vector specialParameter = new Vector (); 217 218 219 private ClassLoader myClassLoader = null; 220 221 222 private Path classpath = null; 223 224 225 227 228 229 public ModifiedSelector() { 230 } 231 232 233 235 236 237 public void verifySettings() { 238 configure(); 239 if (cache == null) { 240 setError("Cache must be set."); 241 } else if (algorithm == null) { 242 setError("Algorithm must be set."); 243 } else if (!cache.isValid()) { 244 setError("Cache must be proper configured."); 245 } else if (!algorithm.isValid()) { 246 setError("Algorithm must be proper configured."); 247 } 248 } 249 250 251 268 public void configure() { 269 if (isConfigured) { 273 return; 274 } 275 isConfigured = true; 276 277 Project p = getProject(); 281 String filename = "cache.properties"; 282 File cachefile = null; 283 if (p != null) { 284 cachefile = new File (p.getBaseDir(), filename); 286 287 getProject().addBuildListener(this); 289 } else { 290 cachefile = new File (filename); 292 setDelayUpdate(false); 293 } 294 Cache defaultCache = new PropertiesfileCache(cachefile); 295 Algorithm defaultAlgorithm = new DigestAlgorithm(); 296 Comparator defaultComparator = new EqualComparator(); 297 update = true; 298 selectDirectories = true; 299 300 for (Iterator itConfig = configParameter.iterator(); itConfig.hasNext();) { 304 Parameter par = (Parameter) itConfig.next(); 305 if (par.getName().indexOf(".") > 0) { 306 specialParameter.add(par); 308 } else { 309 useParameter(par); 310 } 311 } 312 configParameter = new Vector (); 313 314 if (algoName != null) { 316 if ("hashvalue".equals(algoName.getValue())) { 318 algorithm = new HashvalueAlgorithm(); 319 } else if ("digest".equals(algoName.getValue())) { 320 algorithm = new DigestAlgorithm(); 321 } else if ("checksum".equals(algoName.getValue())) { 322 algorithm = new ChecksumAlgorithm(); 323 } 324 } else { 325 if (algorithmClass != null) { 326 algorithm = (Algorithm) loadClass( 328 algorithmClass, 329 "is not an Algorithm.", 330 Algorithm.class); 331 } else { 332 algorithm = defaultAlgorithm; 334 } 335 } 336 337 if (cacheName != null) { 339 if ("propertyfile".equals(cacheName.getValue())) { 341 cache = new PropertiesfileCache(); 342 } 343 } else { 344 if (cacheClass != null) { 345 cache = (Cache) loadClass(cacheClass, "is not a Cache.", Cache.class); 347 } else { 348 cache = defaultCache; 350 } 351 } 352 353 if (compName != null) { 355 if ("equal".equals(compName.getValue())) { 357 comparator = new EqualComparator(); 358 } else if ("rule".equals(compName.getValue())) { 359 throw new BuildException("RuleBasedCollator not yet supported."); 363 } 366 } else { 367 if (comparatorClass != null) { 368 comparator = (Comparator ) loadClass( 370 comparatorClass, 371 "is not a Comparator.", 372 Comparator .class); 373 } else { 374 comparator = defaultComparator; 376 } 377 } 378 379 for (Iterator itSpecial = specialParameter.iterator(); itSpecial.hasNext();) { 383 Parameter par = (Parameter) itSpecial.next(); 384 useParameter(par); 385 } 386 specialParameter = new Vector (); 387 } 388 389 390 399 protected Object loadClass(String classname, String msg, Class type) { 400 try { 401 ClassLoader cl = getClassLoader(); 403 Class clazz = null; 404 if (cl != null) { 405 clazz = cl.loadClass(classname); 406 } else { 407 clazz = Class.forName(classname); 408 } 409 410 Object rv = clazz.newInstance(); 411 412 if (!type.isInstance(rv)) { 413 throw new BuildException("Specified class (" + classname + ") " + msg); 414 } 415 return rv; 416 } catch (ClassNotFoundException e) { 417 throw new BuildException("Specified class (" + classname + ") not found."); 418 } catch (Exception e) { 419 throw new BuildException(e); 420 } 421 } 422 423 424 426 427 434 public boolean isSelected(Resource resource) { 435 if (resource.isFilesystemOnly()) { 436 FileResource fileResource = (FileResource) resource; 439 File file = fileResource.getFile(); 440 String filename = fileResource.getName(); 441 File basedir = fileResource.getBaseDir(); 442 return isSelected(basedir, filename, file); 443 } else { 444 try { 445 FileUtils fu = FileUtils.getFileUtils(); 448 File tmpFile = fu.createTempFile("modified-", ".tmp", null); 449 Resource tmpResource = new FileResource(tmpFile); 450 ResourceUtils.copyResource(resource, tmpResource); 451 boolean isSelected = isSelected(tmpFile.getParentFile(), 452 tmpFile.getName(), 453 resource.toLongString()); 454 tmpFile.delete(); 455 return isSelected; 456 } catch (UnsupportedOperationException uoe) { 457 log("The resource '" 458 + resource.getName() 459 + "' does not provide an InputStream, so it is not checked. " 460 + "Akkording to 'selres' attribute value it is " 461 + ((selectResourcesWithoutInputStream) ? "" : " not") 462 + "selected.", Project.MSG_INFO); 463 return selectResourcesWithoutInputStream; 464 } catch (Exception e) { 465 throw new BuildException(e); 466 } 467 } 468 } 469 470 471 479 public boolean isSelected(File basedir, String filename, File file) { 480 return isSelected(basedir, filename, file.getAbsolutePath()); 481 } 482 483 484 493 private boolean isSelected(File basedir, String filename, String cacheKey) { 494 validate(); 495 File f = new File (basedir, filename); 496 497 if (f.isDirectory()) { 499 return selectDirectories; 500 } 501 502 String cachedValue = String.valueOf(cache.get(f.getAbsolutePath())); 504 String newValue = algorithm.getValue(f); 505 506 boolean rv = (comparator.compare(cachedValue, newValue) != 0); 507 508 if (update && rv) { 510 cache.put(f.getAbsolutePath(), newValue); 511 setModified(getModified() + 1); 512 if (!getDelayUpdate()) { 513 saveCache(); 514 } 515 } 516 517 return rv; 518 } 519 520 521 524 protected void saveCache() { 525 if (getModified() > 1) { 526 cache.save(); 527 setModified(0); 528 } 529 } 530 531 532 534 535 539 public void setAlgorithmClass(String classname) { 540 algorithmClass = classname; 541 } 542 543 544 548 public void setComparatorClass(String classname) { 549 comparatorClass = classname; 550 } 551 552 553 557 public void setCacheClass(String classname) { 558 cacheClass = classname; 559 } 560 561 562 566 public void setUpdate(boolean update) { 567 this.update = update; 568 } 569 570 571 575 public void setSeldirs(boolean seldirs) { 576 selectDirectories = seldirs; 577 } 578 579 580 584 public void setSelres(boolean newValue) { 585 this.selectResourcesWithoutInputStream = newValue; 586 } 587 588 589 593 public int getModified() { 594 return modified; 595 } 596 597 598 602 public void setModified(int modified) { 603 this.modified = modified; 604 } 605 606 607 611 public boolean getDelayUpdate() { 612 return delayUpdate; 613 } 614 615 616 620 public void setDelayUpdate(boolean delayUpdate) { 621 this.delayUpdate = delayUpdate; 622 } 623 624 625 629 public void addClasspath(Path path) { 630 if (classpath != null) { 631 throw new BuildException("<classpath> can be set only once."); 632 } 633 classpath = path; 634 } 635 636 637 641 public ClassLoader getClassLoader() { 642 if (myClassLoader == null) { 643 myClassLoader = (classpath == null) 644 ? getClass().getClassLoader() 646 : getProject().createClassLoader(classpath); 648 } 649 return myClassLoader; 650 } 651 652 653 661 public void setClassLoader(ClassLoader loader) { 662 myClassLoader = loader; 663 } 664 665 666 671 public void addParam(String key, Object value) { 672 Parameter par = new Parameter(); 673 par.setName(key); 674 par.setValue(String.valueOf(value)); 675 configParameter.add(par); 676 } 677 678 679 683 public void addParam(Parameter parameter) { 684 configParameter.add(parameter); 685 } 686 687 688 695 public void setParameters(Parameter[] parameters) { 696 if (parameters != null) { 697 for (int i = 0; i < parameters.length; i++) { 698 configParameter.add(parameters[i]); 699 } 700 } 701 } 702 703 704 717 public void useParameter(Parameter parameter) { 718 String key = parameter.getName(); 719 String value = parameter.getValue(); 720 if ("cache".equals(key)) { 721 CacheName cn = new CacheName(); 722 cn.setValue(value); 723 setCache(cn); 724 } else if ("algorithm".equals(key)) { 725 AlgorithmName an = new AlgorithmName(); 726 an.setValue(value); 727 setAlgorithm(an); 728 } else if ("comparator".equals(key)) { 729 ComparatorName cn = new ComparatorName(); 730 cn.setValue(value); 731 setComparator(cn); 732 } else if ("update".equals(key)) { 733 boolean updateValue = 734 ("true".equalsIgnoreCase(value)) 735 ? true 736 : false; 737 setUpdate(updateValue); 738 } else if ("delayupdate".equals(key)) { 739 boolean updateValue = 740 ("true".equalsIgnoreCase(value)) 741 ? true 742 : false; 743 setDelayUpdate(updateValue); 744 } else if ("seldirs".equals(key)) { 745 boolean sdValue = 746 ("true".equalsIgnoreCase(value)) 747 ? true 748 : false; 749 setSeldirs(sdValue); 750 } else if (key.startsWith("cache.")) { 751 String name = key.substring(6); 752 tryToSetAParameter(cache, name, value); 753 } else if (key.startsWith("algorithm.")) { 754 String name = key.substring(10); 755 tryToSetAParameter(algorithm, name, value); 756 } else if (key.startsWith("comparator.")) { 757 String name = key.substring(11); 758 tryToSetAParameter(comparator, name, value); 759 } else { 760 setError("Invalid parameter " + key); 761 } 762 } 763 764 765 772 protected void tryToSetAParameter(Object obj, String name, String value) { 773 Project prj = (getProject() != null) ? getProject() : new Project(); 774 IntrospectionHelper iHelper 775 = IntrospectionHelper.getHelper(prj, obj.getClass()); 776 try { 777 iHelper.setAttribute(prj, obj, name, value); 778 } catch (org.apache.tools.ant.BuildException e) { 779 } 781 } 782 783 784 786 787 791 public String toString() { 792 StringBuffer buf = new StringBuffer ("{modifiedselector"); 793 buf.append(" update=").append(update); 794 buf.append(" seldirs=").append(selectDirectories); 795 buf.append(" cache=").append(cache); 796 buf.append(" algorithm=").append(algorithm); 797 buf.append(" comparator=").append(comparator); 798 buf.append("}"); 799 return buf.toString(); 800 } 801 802 803 805 806 810 public void buildFinished(BuildEvent event) { 811 if (getDelayUpdate()) { 812 saveCache(); 813 } 814 } 815 816 817 821 public void targetFinished(BuildEvent event) { 822 if (getDelayUpdate()) { 823 saveCache(); 824 } 825 } 826 827 828 832 public void taskFinished(BuildEvent event) { 833 if (getDelayUpdate()) { 834 saveCache(); 835 } 836 } 837 838 839 843 public void buildStarted(BuildEvent event) { 844 } 846 847 848 852 public void targetStarted(BuildEvent event) { 853 } 855 856 857 858 862 public void taskStarted(BuildEvent event) { 863 } 865 866 867 871 public void messageLogged(BuildEvent event) { 872 } 874 875 876 879 880 884 public Cache getCache() { return cache; } 885 886 890 public void setCache(CacheName name) { 891 cacheName = name; 892 } 893 894 898 public static class CacheName extends EnumeratedAttribute { 899 900 901 public String [] getValues() { 902 return new String [] {"propertyfile" }; 903 } 904 } 905 906 910 public Algorithm getAlgorithm() { return algorithm; } 911 912 916 public void setAlgorithm(AlgorithmName name) { 917 algoName = name; 918 } 919 920 924 public static class AlgorithmName extends EnumeratedAttribute { 925 926 927 public String [] getValues() { 928 return new String [] {"hashvalue", "digest", "checksum" }; 929 } 930 } 931 932 936 public Comparator getComparator() { return comparator; } 937 938 942 public void setComparator(ComparatorName name) { 943 compName = name; 944 } 945 946 950 public static class ComparatorName extends EnumeratedAttribute { 951 952 953 public String [] getValues() { 954 return new String [] {"equal", "rule" }; 955 } 956 } 957 958 } 959 | Popular Tags |