File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\node_monitors\TemporarySpaceMonitor.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.Extension;
27 import hudson.FilePath;
28 import hudson.FilePath.FileCallable;
29 import hudson.Functions;
30 import hudson.model.Computer;
31 import jenkins.model.Jenkins;
32 import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace;
33 import hudson.remoting.VirtualChannel;
34 import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
35 import org.kohsuke.stapler.DataBoundConstructor;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.text.ParseException;
40
41 /**
42 * Monitors the disk space of "/tmp".
43 *
44 * @author Kohsuke Kawaguchi
45 */
46 public class TemporarySpaceMonitor extends AbstractDiskSpaceMonitor {
47 @DataBoundConstructor
48 public TemporarySpaceMonitor(String freeSpaceThreshold) throws ParseException {
49 super(freeSpaceThreshold);
50 }
51
52 public TemporarySpaceMonitor() {}
53
54 public DiskSpace getFreeSpace(Computer c) {
55 return DESCRIPTOR.get(c);
56 }
57
58 @Override
59 public String getColumnCaption() {
60 // Hide this column from non-admins
61 return Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER) ? super.getColumnCaption() : null;
62 }
63
64 public static final DiskSpaceMonitorDescriptor DESCRIPTOR = new DiskSpaceMonitorDescriptor() {
65 public String getDisplayName() {
66 return Messages.TemporarySpaceMonitor_DisplayName();
67 }
68
69 protected DiskSpace getFreeSpace(Computer c) throws IOException, InterruptedException {
70 FilePath p = c.getNode().getRootPath();
71 if(p==null) return null;
72
73 return p.act(new GetTempSpace());
74 }
75 };
76
77 @Extension
78 public static DiskSpaceMonitorDescriptor install() {
79 if(Functions.isMustangOrAbove()) return DESCRIPTOR;
80 return null;
81 }
82
83 protected static final class GetTempSpace implements FileCallable<DiskSpace> {
84 @IgnoreJRERequirement
85 public DiskSpace invoke(File f, VirtualChannel channel) throws IOException {
86 try {
87 // if the disk is really filled up we can't even create a single file,
88 // so calling File.createTempFile and figuring out the directory won't reliably work.
89 f = new File(System.getProperty("java.io.tmpdir"));
90 long s = f.getUsableSpace();
91 if(s<=0) return null;
92 return new DiskSpace(f.getCanonicalPath(), s);
93 } catch (LinkageError e) {
94 // pre-mustang
95 return null;
96 }
97 }
98 private static final long serialVersionUID = 1L;
99 }
100 }
101