01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
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 partition;
37: private String 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 fs = new File(partition);
45: ReflectWrapper w = new ReflectWrapper(fs);
46: long free = ((Long) w.invoke("getFreeSpace"))
47: .longValue();
48: return free >= StringUtils.parseHumanSizes(needed);
49: } else {
50: throw new BuildException(
51: "HasFreeSpace condition not supported on Java5 or less.");
52: }
53: } catch (Exception e) {
54: throw new BuildException(e);
55: }
56: }
57:
58: private void validate() throws BuildException {
59: if (null == partition) {
60: throw new BuildException(
61: "Please set the partition attribute.");
62: }
63: if (null == needed) {
64: throw new BuildException("Please set the needed attribute.");
65: }
66: }
67:
68: /**
69: * The partition/device to check
70: * @return
71: */
72: public String getPartition() {
73: return partition;
74: }
75:
76: public void setPartition(String partition) {
77: this .partition = partition;
78: }
79:
80: /**
81: * The amount of free space required
82: * @return the amount required
83: */
84: public String getNeeded() {
85: return needed;
86: }
87:
88: public void setNeeded(String needed) {
89: this.needed = needed;
90: }
91: }
|