File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\node_monitors\DiskSpaceMonitorDescriptor.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.node_monitors;
25
26 import hudson.FilePath.FileCallable;
27 import hudson.model.Computer;
28 import hudson.remoting.VirtualChannel;
29 import hudson.Util;
30 import hudson.slaves.OfflineCause;
31 import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace;
32 import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.Serializable;
37 import java.math.BigDecimal;
38 import java.text.ParseException;
39 import java.util.Locale;
40
41 import org.kohsuke.stapler.export.ExportedBean;
42 import org.kohsuke.stapler.export.Exported;
43
44 /**
45 * {@link AbstractNodeMonitorDescriptor} for {@link NodeMonitor} that checks a free disk space of some directory.
46 *
47 * @author Kohsuke Kawaguchi
48 */
49 /*package*/ abstract class DiskSpaceMonitorDescriptor extends AbstractNodeMonitorDescriptor<DiskSpace> {
50 /**
51 * Value object that represents the disk space.
52 */
53 @ExportedBean
54 public static final class DiskSpace extends OfflineCause implements Serializable {
55 private final String path;
56 @Exported
57 public final long size;
58
59 private boolean triggered;
60 private Class<? extends AbstractDiskSpaceMonitor> trigger;
61
62 /**
63 * @deprecated as of 1.467
64 */
65 public DiskSpace(long size) {
66 this(".",size);
67 }
68
69 /**
70 * @param path
71 * Specify the file path that was monitored.
72 */
73 public DiskSpace(String path, long size) {
74 this.path = path;
75 this.size = size;
76 }
77
78 @Override
79 public String toString() {
80 return String.valueOf(size);
81 }
82
83 /**
84 * The path that was checked
85 */
86 @Exported
87 public String getPath() {
88 return path;
89 }
90
91 /**
92 * Gets GB left.
93 */
94 public String getGbLeft() {
95 long space = size;
96 space/=1024L; // convert to KB
97 space/=1024L; // convert to MB
98
99 return new BigDecimal(space).scaleByPowerOfTen(-3).toPlainString();
100 }
101
102 /**
103 * Returns the HTML representation of the space.
104 */
105 public String toHtml() {
106 long space = size;
107 space/=1024L; // convert to KB
108 space/=1024L; // convert to MB
109 if(triggered) {
110 // less than a GB
111 return Util.wrapToErrorSpan(new BigDecimal(space).scaleByPowerOfTen(-3).toPlainString()+"GB");
112 }
113
114 return space/1024+"GB";
115 }
116
117 /**
118 * Sets whether this disk space amount should be treated as outside
119 * the acceptable conditions or not.
120 */
121 protected void setTriggered(boolean triggered) {
122 this.triggered = triggered;
123 }
124
125 /**
126 * Same as {@link DiskSpace#setTriggered(boolean)}, also sets the trigger class which made the decision
127 */
128 protected void setTriggered(Class<? extends AbstractDiskSpaceMonitor> trigger, boolean triggered) {
129 this.trigger = trigger;
130 this.triggered = triggered;
131 }
132
133 public Class<? extends AbstractDiskSpaceMonitor> getTrigger() {
134 return trigger;
135 }
136
137 /**
138 * Parses a human readable size description like "1GB", "0.5m", etc. into {@link DiskSpace}
139 *
140 * @throws ParseException
141 * If failed to parse.
142 */
143 public static DiskSpace parse(String size) throws ParseException {
144 size = size.toUpperCase(Locale.ENGLISH).trim();
145 if (size.endsWith("B")) // cut off 'B' from KB, MB, etc.
146 size = size.substring(0,size.length()-1);
147
148 long multiplier=1;
149
150 // look for the size suffix. The goal here isn't to detect all invalid size suffix,
151 // so we ignore double suffix like "10gkb" or anything like that.
152 String suffix = "KMGT";
153 for (int i=0; i<suffix.length(); i++) {
154 if (size.endsWith(suffix.substring(i,i+1))) {
155 multiplier = 1;
156 for (int j=0; j<=i; j++ )
157 multiplier*=1024;
158 size = size.substring(0,size.length()-1);
159 }
160 }
161
162 return new DiskSpace("", (long)(Double.parseDouble(size.trim())*multiplier));
163 }
164
165 private static final long serialVersionUID = 2L;
166 }
167
168 protected DiskSpace monitor(Computer c) throws IOException, InterruptedException {
169 return getFreeSpace(c);
170 }
171
172 /**
173 * Computes the free size.
174 */
175 protected abstract DiskSpace getFreeSpace(Computer c) throws IOException, InterruptedException;
176
177 protected static final class GetUsableSpace implements FileCallable<DiskSpace> {
178 @IgnoreJRERequirement
179 public DiskSpace invoke(File f, VirtualChannel channel) throws IOException {
180 try {
181 long s = f.getUsableSpace();
182 if(s<=0) return null;
183 return new DiskSpace(f.getCanonicalPath(), s);
184 } catch (LinkageError e) {
185 // pre-mustang
186 return null;
187 }
188 }
189 private static final long serialVersionUID = 1L;
190 }
191 }
192