KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > build > Grabber


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.build;
16
17 import java.io.BufferedOutputStream JavaDoc;
18 import java.io.File JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.Task;
29
30 /**
31  * Ant task for conditionally downloading a file and checking its md5 sum. MD5 checking occurs only
32  * if a download occurs. This is very similar to Ant's <code>Get</code> task, with a few
33  * differences:
34  * <ul>
35  * <li>Always does a timestamp comparison, where possible
36  * <li>Has additional parameter for specifying where the MD5 sum is stored
37  * <li>Less verbose --- no output if the file isn't downloaded
38  * </ul>
39  *
40  * @author Howard Lewis Ship
41  */

42 public class Grabber extends Task
43 {
44     private URL JavaDoc _src;
45
46     private File JavaDoc _dest;
47
48     public void execute() throws BuildException
49     {
50         if (_src == null)
51             throw new BuildException("src is required", getLocation());
52
53         if (_dest == null)
54             throw new BuildException("dest is required", getLocation());
55
56         if (_dest.exists())
57         {
58
59             if (_dest.isDirectory())
60                 throw new BuildException("The specified destination is a directory", getLocation());
61
62             if (isNonZeroLength(_dest))
63                 return;
64
65             if (!_dest.canWrite())
66                 throw new BuildException("Can't write to " + _dest.getAbsolutePath(), getLocation());
67         }
68
69         try
70         {
71
72             URLConnection JavaDoc connection = _src.openConnection();
73
74             connection.connect();
75
76             copyConnectionToFile(connection);
77         }
78         catch (IOException JavaDoc ex)
79         {
80             log("Failure accessing " + _src + ": " + ex.getMessage(), Project.MSG_ERR);
81         }
82     }
83
84     private boolean isNonZeroLength(File JavaDoc file)
85     {
86         return file.length() > 0;
87     }
88
89     private void copyConnectionToFile(URLConnection JavaDoc connection) throws IOException JavaDoc
90     {
91         log("Downloading " + _src + " to " + _dest);
92
93         InputStream JavaDoc is = open(connection);
94
95         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(_dest);
96         OutputStream JavaDoc os = new BufferedOutputStream JavaDoc(fos);
97
98         byte[] buffer = new byte[100 * 1024];
99
100         while (true)
101         {
102             int bytesRead = is.read(buffer);
103
104             if (bytesRead < 0)
105                 break;
106
107             os.write(buffer, 0, bytesRead);
108         }
109
110         is.close();
111         os.close();
112     }
113
114     private InputStream JavaDoc open(URLConnection JavaDoc connection)
115     {
116         InputStream JavaDoc result = null;
117
118         int i = 0;
119         while (true)
120         {
121             try
122             {
123                 result = connection.getInputStream();
124                 break;
125             }
126             catch (IOException JavaDoc ex)
127             {
128                 if (i++ == 3)
129                     throw new BuildException("Unable to open " + _src + ": " + ex.getMessage(), ex,
130                             getLocation());
131             }
132         }
133
134         if (result == null)
135             throw new BuildException("Unable to open " + _src, getLocation());
136
137         return result;
138     }
139
140     public void setDest(File JavaDoc file)
141     {
142         _dest = file;
143     }
144
145     public void setSrc(URL JavaDoc url)
146     {
147         _src = url;
148     }
149
150 }
Popular Tags