Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* If you wish your version of this file to be governed by only the CDDL |
25 |
* or only the GPL Version 2, indicate your decision by adding |
26 |
* "[Contributor] elects to include this software in this distribution |
27 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
28 |
* single choice of license, a recipient has the option to distribute |
29 |
* your version of this file under either the CDDL, the GPL Version 2 or |
30 |
* to extend the choice of license to its licensees as provided above. |
31 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
32 |
* Version 2 license, then the option applies only if the new code is |
33 |
* made subject to such option by the copyright holder. |
34 |
* |
35 |
* Contributor(s): |
36 |
* |
37 |
* Portions Copyrighted 2007 Sun Microsystems, Inc. |
38 |
*/ |
39 |
|
40 |
package org.netbeans.nbbuild.extlibs; |
41 |
|
42 |
import java.io.BufferedReader; |
43 |
import java.io.File; |
44 |
import java.io.FileInputStream; |
45 |
import java.io.FileOutputStream; |
46 |
import java.io.IOException; |
47 |
import java.io.InputStream; |
48 |
import java.io.InputStreamReader; |
49 |
import java.io.OutputStream; |
50 |
import java.math.BigInteger; |
51 |
import java.net.HttpURLConnection; |
52 |
import java.net.MalformedURLException; |
53 |
import java.net.URL; |
54 |
import java.net.URLConnection; |
55 |
import java.nio.channels.FileChannel; |
56 |
import java.security.MessageDigest; |
57 |
import java.security.NoSuchAlgorithmException; |
58 |
import java.util.ArrayList; |
59 |
import java.util.List; |
60 |
import org.apache.tools.ant.BuildException; |
61 |
import org.apache.tools.ant.DirectoryScanner; |
62 |
import org.apache.tools.ant.Project; |
63 |
import org.apache.tools.ant.Task; |
64 |
import org.apache.tools.ant.types.FileSet; |
65 |
import org.apache.tools.ant.util.FileUtils; |
66 |
|
67 |
/** |
68 |
* Task to retrieve named files (generally large binaries such as ZIPs) from a repository. |
69 |
* Similar to a very simplified version of Ivy, but correctly handles binaries |
70 |
* with missing or irrelevant version numbers, since it is based on a hash of contents. |
71 |
* You keep one or more manifests under version control which enumerate files and their SHA-1 hashes. |
72 |
* Then just run this task to download any missing files. |
73 |
* Remember to specify the binaries as "ignorable" to your version control system. |
74 |
* You can also run it in a clean mode which will remove the binaries. |
75 |
* At the end of this source file is a sample CGI script and matching form which you can run on the server |
76 |
* to permit people to upload files to the correct repository paths. |
77 |
* Motivation: http://wiki.netbeans.org/wiki/view/HgMigration#section-HgMigration-Binaries |
78 |
*/ |
79 |
public class DownloadBinaries extends Task { |
80 |
|
81 |
private File cache; |
82 |
/** |
83 |
* Location of per-user cache of already downloaded binaries. |
84 |
* Optional; no cache will be used if unset. |
85 |
* The directory will be created if it does not yet exist. |
86 |
*/ |
87 |
public void setCache(File cache) { |
88 |
this.cache = cache; |
89 |
} |
90 |
|
91 |
private String server; |
92 |
/** |
93 |
* URL prefix for the server repository. |
94 |
* Should generally include a trailing slash. |
95 |
* You may include multiple server URLs separated by spaces |
96 |
* in which case they will be tried in order. |
97 |
* To use a local repository, simply specify e.g. <code>file:/repo/</code> as the URL. |
98 |
*/ |
99 |
public void setServer(String server) { |
100 |
this.server = server; |
101 |
} |
102 |
|
103 |
private final List<FileSet> manifests = new ArrayList<FileSet>(); |
104 |
/** |
105 |
* Add one or more manifests of files to download. |
106 |
* Each manifest is a text file; lines beginning with # are ignored. |
107 |
* All other lines must be entries of the form |
108 |
* <pre> |
109 |
* 0123456789ABCDEF something-1.0.jar |
110 |
* </pre> |
111 |
* consisting of an SHA-1 hash followed by a filename. |
112 |
* The filename is relative to the manifest, usually a simple basename. |
113 |
* If the file exists and has the specified hash, nothing is done. |
114 |
* If it has the wrong hash, the task aborts with an error message. |
115 |
* If it is missing, it is downloaded from the server (or copied from cache) |
116 |
* using a filename derived from the basename of the file in the manifest and its hash. |
117 |
* For example, the above line with a server of <code>http://nowhere.net/repo/</code> |
118 |
* would try to download |
119 |
* <pre> |
120 |
* http://nowhere.net/repo/0123456789ABCDEF-something-1.0.jar |
121 |
* </pre> |
122 |
* Any version number etc. in the filename is purely informational; |
123 |
* the "up to date" check is entirely based on the hash. |
124 |
*/ |
125 |
public void addManifest(FileSet manifest) { |
126 |
manifests.add(manifest); |
127 |
} |
128 |
|
129 |
private boolean clean; |
130 |
/** |
131 |
* If true, rather than creating binary files, will delete them. |
132 |
* Any cache is ignored in this case. |
133 |
* If a binary does not match its hash, the build is aborted: |
134 |
* the file might be a precious customized version and should not be blindly deleted. |
135 |
*/ |
136 |
public void setClean(boolean clean) { |
137 |
this.clean = clean; |
138 |
} |
139 |
|
140 |
@Override |
141 |
public void execute() throws BuildException { |
142 |
for (FileSet fs : manifests) { |
143 |
DirectoryScanner scanner = fs.getDirectoryScanner(getProject()); |
144 |
File basedir = scanner.getBasedir(); |
145 |
for (String include : scanner.getIncludedFiles()) { |
146 |
File manifest = new File(basedir, include); |
147 |
log("Scanning: " + manifest, Project.MSG_VERBOSE); |
148 |
try { |
149 |
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(manifest), "UTF-8")); |
150 |
String line; |
151 |
while ((line = r.readLine()) != null) { |
152 |
if (line.startsWith("#")) { |
153 |
continue; |
154 |
} |
155 |
if (line.trim().length() == 0) { |
156 |
continue; |
157 |
} |
158 |
String[] hashAndFile = line.split(" ", 2); |
159 |
if (hashAndFile.length < 2) { |
160 |
throw new BuildException("Bad line '" + line + "' in " + manifest, getLocation()); |
161 |
} |
162 |
File f = new File(manifest.getParentFile(), hashAndFile[1]); |
163 |
if (!clean) { |
164 |
if (!f.exists()) { |
165 |
log("Creating " + f); |
166 |
String cacheName = hashAndFile[0] + "-" + f.getName(); |
167 |
if (cache != null) { |
168 |
cache.mkdirs(); |
169 |
File cacheFile = new File(cache, cacheName); |
170 |
if (!cacheFile.exists()) { |
171 |
download(cacheName, cacheFile); |
172 |
} |
173 |
try { |
174 |
FileUtils.getFileUtils().copyFile(cacheFile, f); |
175 |
} catch (IOException x) { |
176 |
throw new BuildException("Could not copy " + cacheFile + " to " + f + ": " + x, x, getLocation()); |
177 |
} |
178 |
} else { |
179 |
download(cacheName, f); |
180 |
} |
181 |
} |
182 |
String actualHash = hash(f); |
183 |
if (!actualHash.equals(hashAndFile[0])) { |
184 |
throw new BuildException("File " + f + " requested by " + manifest + " to have hash " + |
185 |
hashAndFile[0] + " actually had hash " + actualHash, getLocation()); |
186 |
} |
187 |
log("Have " + f + " with expected hash", Project.MSG_VERBOSE); |
188 |
} else { |
189 |
if (f.exists()) { |
190 |
String actualHash = hash(f); |
191 |
if (!actualHash.equals(hashAndFile[0])) { |
192 |
throw new BuildException("File " + f + " requested by " + manifest + " to have hash " + |
193 |
hashAndFile[0] + " actually had hash " + actualHash, getLocation()); |
194 |
} |
195 |
log("Deleting " + f); |
196 |
f.delete(); |
197 |
} |
198 |
} |
199 |
} |
200 |
} catch (IOException x) { |
201 |
throw new BuildException("Could not open " + manifest + ": " + x, x, getLocation()); |
202 |
} |
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
private void download(String cacheName, File destination) { |
208 |
if (server == null) { |
209 |
throw new BuildException("Must specify a server to download files from", getLocation()); |
210 |
} |
211 |
for (String prefix : server.split(" ")) { |
212 |
URL url; |
213 |
try { |
214 |
url = new URL(prefix + cacheName); |
215 |
} catch (MalformedURLException x) { |
216 |
throw new BuildException(x, getLocation()); |
217 |
} |
218 |
try { |
219 |
URLConnection conn = url.openConnection(); |
220 |
conn.connect(); |
221 |
int code = HttpURLConnection.HTTP_OK; |
222 |
if (conn instanceof HttpURLConnection) { |
223 |
code = ((HttpURLConnection) conn).getResponseCode(); |
224 |
} |
225 |
if (code != HttpURLConnection.HTTP_OK) { |
226 |
log("Skipping download from " + url + " due to response code " + code, Project.MSG_VERBOSE); |
227 |
} |
228 |
log("Downloading: " + url); |
229 |
InputStream is = conn.getInputStream(); |
230 |
try { |
231 |
OutputStream os = new FileOutputStream(destination); |
232 |
try { |
233 |
byte[] buf = new byte[4096]; |
234 |
int read; |
235 |
while ((read = is.read(buf)) != -1) { |
236 |
os.write(buf, 0, read); |
237 |
} |
238 |
os.close(); |
239 |
} catch (IOException x) { |
240 |
destination.delete(); |
241 |
throw x; |
242 |
} |
243 |
} finally { |
244 |
is.close(); |
245 |
} |
246 |
return; |
247 |
} catch (IOException x) { |
248 |
log("Could not download " + url + " to " + destination + ": " + x, Project.MSG_WARN); |
249 |
} |
250 |
} |
251 |
throw new BuildException("Could not download " + cacheName + " from " + server, getLocation()); |
252 |
} |
253 |
|
254 |
private String hash(File f) { |
255 |
MessageDigest digest; |
256 |
try { |
257 |
digest = MessageDigest.getInstance("SHA-1"); |
258 |
} catch (NoSuchAlgorithmException x) { |
259 |
throw new BuildException(x, getLocation()); |
260 |
} |
261 |
try { |
262 |
FileInputStream is = new FileInputStream(f); |
263 |
try { |
264 |
digest.update(is.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, f.length())); |
265 |
} finally { |
266 |
is.close(); |
267 |
} |
268 |
} catch (IOException x) { |
269 |
throw new BuildException("Could not get hash for " + f + ": " + x, x, getLocation()); |
270 |
} |
271 |
return String.format("%040X", new BigInteger(1, digest.digest())); |
272 |
} |
273 |
|
274 |
} |
275 |
|
276 |
/* |
277 |
|
278 |
Sample upload script (edit repository location as needed): |
279 |
|
280 |
#!/usr/bin/env ruby |
281 |
repository = '/tmp/repository' |
282 |
require 'cgi' |
283 |
require 'digest/sha1' |
284 |
require 'date' |
285 |
cgi = CGI.new |
286 |
begin |
287 |
if cgi.request_method == 'POST' |
288 |
value = cgi['file'] |
289 |
content = value.read |
290 |
name = value.original_filename.gsub(/\.\.|[^a-zA-Z0-9._+-]/, '_') |
291 |
sha1 = Digest::SHA1.hexdigest(content).upcase |
292 |
open("#{repository}/#{sha1}-#{name}", "w") do |f| |
293 |
f.write content |
294 |
end |
295 |
open("#{repository}/log", "a") do |f| |
296 |
f << "#{DateTime.now.to_s} #{sha1}-#{name} #{cgi.remote_user}\n" |
297 |
end |
298 |
cgi.out do <<RESPONSE |
299 |
<html> |
300 |
<head> |
301 |
<title>Uploaded #{name}</title> |
302 |
</head> |
303 |
<body> |
304 |
<p>Uploaded. Add to your manifest:</p> |
305 |
<pre>#{sha1} #{name}</pre> |
306 |
</body> |
307 |
</html> |
308 |
RESPONSE |
309 |
end |
310 |
else |
311 |
cgi.out do <<FORM |
312 |
<html> |
313 |
<head> |
314 |
<title>Upload a Binary</title> |
315 |
</head> |
316 |
<body> |
317 |
<form method="POST" action="" enctype="multipart/form-data"> |
318 |
<input type="file" name="file"> |
319 |
<input type="submit" value="Upload"> |
320 |
</form> |
321 |
</body> |
322 |
</html> |
323 |
FORM |
324 |
end |
325 |
end |
326 |
rescue |
327 |
cgi.out do |
328 |
"Caught an exception: #{$!}" |
329 |
end |
330 |
end |
331 |
|
332 |
*/ |