1 18 19 package org.apache.tools.ant.taskdefs.condition; 20 21 import java.io.File ; 22 23 import org.apache.tools.ant.BuildException; 24 import org.apache.tools.ant.util.JavaEnvUtils; 25 import org.apache.tools.ant.util.ReflectWrapper; 26 import org.apache.tools.ant.util.StringUtils; 27 28 34 public class HasFreeSpace implements Condition { 35 36 private String partition; 37 private String needed; 38 39 public boolean eval() throws BuildException { 40 validate(); 41 try { 42 if (JavaEnvUtils.isAtLeastJavaVersion("1.6")) { 43 File fs = new File (partition); 45 ReflectWrapper w = new ReflectWrapper(fs); 46 long free = ((Long )w.invoke("getFreeSpace")).longValue(); 47 return free >= StringUtils.parseHumanSizes(needed); 48 } else { 49 throw new BuildException("HasFreeSpace condition not supported on Java5 or less."); 50 } 51 } catch (Exception e) { 52 throw new BuildException(e); 53 } 54 } 55 56 private void validate() throws BuildException { 57 if(null == partition) { 58 throw new BuildException("Please set the partition attribute."); 59 } 60 if(null == needed) { 61 throw new BuildException("Please set the needed attribute."); 62 } 63 } 64 65 69 public String getPartition() { 70 return partition; 71 } 72 73 public void setPartition(String partition) { 74 this.partition = partition; 75 } 76 77 81 public String getNeeded() { 82 return needed; 83 } 84 85 public void setNeeded(String needed) { 86 this.needed = needed; 87 } 88 } 89 | Popular Tags |