KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > HasFreeSpace


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.condition;
20
21 import java.io.File JavaDoc;
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 /**
29  * <hasfreespace>
30  * <p>Condition returns true if selected partition
31  * has the requested space, false otherwise.</p>
32  * @since Ant 1.7
33  */

34 public class HasFreeSpace implements Condition {
35
36     private String JavaDoc partition;
37     private String JavaDoc needed;
38
39     public boolean eval() throws BuildException {
40         validate();
41         try {
42             if (JavaEnvUtils.isAtLeastJavaVersion("1.6")) {
43                 //reflection to avoid bootstrap/build problems
44
File JavaDoc fs = new File JavaDoc(partition);
45                 ReflectWrapper w = new ReflectWrapper(fs);
46                 long free = ((Long JavaDoc)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 JavaDoc 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     /**
66      * The partition/device to check
67      * @return
68      */

69     public String JavaDoc getPartition() {
70         return partition;
71     }
72
73     public void setPartition(String JavaDoc partition) {
74         this.partition = partition;
75     }
76
77     /**
78      * The amount of free space required
79      * @return the amount required
80      */

81     public String JavaDoc getNeeded() {
82         return needed;
83     }
84
85     public void setNeeded(String JavaDoc needed) {
86         this.needed = needed;
87     }
88 }
89
Popular Tags