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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 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
25 package hudson.tools;
26
27 import hudson.Extension;
28 import hudson.FilePath;
29 import hudson.FilePath.FileCallable;
30 import hudson.ProxyConfiguration;
31 import hudson.Util;
32 import hudson.Functions;
33 import hudson.os.PosixAPI;
34 import hudson.model.Node;
35 import hudson.model.TaskListener;
36 import hudson.remoting.VirtualChannel;
37 import hudson.util.FormValidation;
38 import hudson.util.jna.GNUCLibrary;
39
40 import java.io.File;
41 import java.io.IOException;
42 import java.net.HttpURLConnection;
43 import java.net.MalformedURLException;
44 import java.net.URL;
45 import java.net.URLConnection;
46 import org.kohsuke.stapler.DataBoundConstructor;
47 import org.kohsuke.stapler.QueryParameter;
48 import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
49
50 /**
51 * Installs a tool into the Hudson working area by downloading and unpacking a ZIP file.
52 * @since 1.305
53 */
54 public class ZipExtractionInstaller extends ToolInstaller {
55
56 /**
57 * URL of a ZIP file which should be downloaded in case the tool is missing.
58 */
59 private final String url;
60 /**
61 * Optional subdir to extract.
62 */
63 private final String subdir;
64
65 @DataBoundConstructor
66 public ZipExtractionInstaller(String label, String url, String subdir) {
67 super(label);
68 this.url = url;
69 this.subdir = Util.fixEmptyAndTrim(subdir);
70 }
71
72 public String getUrl() {
73 return url;
74 }
75
76 public String getSubdir() {
77 return subdir;
78 }
79
80 public FilePath performInstallation(ToolInstallation tool, Node node, TaskListener log) throws IOException, InterruptedException {
81 FilePath dir = preferredLocation(tool, node);
82 if (dir.installIfNecessaryFrom(new URL(url), log, "Unpacking " + url + " to " + dir + " on " + node.getDisplayName())) {
83 dir.act(new ChmodRecAPlusX());
84 }
85 if (subdir == null) {
86 return dir;
87 } else {
88 return dir.child(subdir);
89 }
90 }
91
92 @Extension
93 public static class DescriptorImpl extends ToolInstallerDescriptor<ZipExtractionInstaller> {
94
95 public String getDisplayName() {
96 return Messages.ZipExtractionInstaller_DescriptorImpl_displayName();
97 }
98
99 public FormValidation doCheckUrl(@QueryParameter String value) {
100 try {
101 URLConnection conn = ProxyConfiguration.open(new URL(value));
102 conn.connect();
103 if (conn instanceof HttpURLConnection) {
104 if (((HttpURLConnection) conn).getResponseCode() != HttpURLConnection.HTTP_OK) {
105 return FormValidation.error(Messages.ZipExtractionInstaller_bad_connection());
106 }
107 }
108 return FormValidation.ok();
109 } catch (MalformedURLException x) {
110 return FormValidation.error(Messages.ZipExtractionInstaller_malformed_url());
111 } catch (IOException x) {
112 return FormValidation.error(x,Messages.ZipExtractionInstaller_could_not_connect());
113 }
114 }
115
116 }
117
118 /**
119 * Sets execute permission on all files, since unzip etc. might not do this.
120 * Hackish, is there a better way?
121 */
122 static class ChmodRecAPlusX implements FileCallable<Void> {
123 private static final long serialVersionUID = 1L;
124 public Void invoke(File d, VirtualChannel channel) throws IOException {
125 if(!Functions.isWindows())
126 process(d);
127 return null;
128 }
129 @IgnoreJRERequirement
130 private void process(File f) {
131 if (f.isFile()) {
132 if(Functions.isMustangOrAbove())
133 f.setExecutable(true, false);
134 else {
135 try {
136 GNUCLibrary.LIBC.chmod(f.getAbsolutePath(),0755);
137 } catch (LinkageError e) {
138 // if JNA is unavailable, fall back.
139 // we still prefer to try JNA first as PosixAPI supports even smaller platforms.
140 PosixAPI.get().chmod(f.getAbsolutePath(),0755);
141 }
142 }
143 } else {
144 File[] kids = f.listFiles();
145 if (kids != null) {
146 for (File kid : kids) {
147 process(kid);
148 }
149 }
150 }
151 }
152 }
153
154 }
155