1 18 19 package org.apache.tools.ant.types.resources; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.net.HttpURLConnection ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 import java.net.MalformedURLException ; 29 import java.net.JarURLConnection ; 30 import java.util.jar.JarFile ; 31 32 import org.apache.tools.ant.Project; 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.types.Resource; 35 import org.apache.tools.ant.types.Reference; 36 import org.apache.tools.ant.util.FileUtils; 37 38 42 public class URLResource extends Resource { 43 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 44 private static final int NULL_URL 45 = Resource.getMagicNumber("null URL".getBytes()); 46 47 private URL url; 48 private URLConnection conn; 49 50 53 public URLResource() { 54 } 55 56 60 public URLResource(URL u) { 61 setURL(u); 62 } 63 64 68 public URLResource(File f) { 69 setFile(f); 70 } 71 72 77 public URLResource(String u) { 78 this(newURL(u)); 79 } 80 81 85 public synchronized void setURL(URL u) { 86 checkAttributesAllowed(); 87 url = u; 88 } 89 90 94 public synchronized void setFile(File f) { 95 try { 96 setURL(FILE_UTILS.getFileURL(f)); 97 } catch (MalformedURLException e) { 98 throw new BuildException(e); 99 } 100 } 101 102 106 public synchronized URL getURL() { 107 if (isReference()) { 108 return ((URLResource) getCheckedRef()).getURL(); 109 } 110 return url; 111 } 112 113 117 public synchronized void setRefid(Reference r) { 118 if (url != null) { 120 throw tooManyAttributes(); 121 } 122 super.setRefid(r); 123 } 124 125 130 public synchronized String getName() { 131 return isReference() ? ((Resource) getCheckedRef()).getName() 132 : getURL().getFile().substring(1); 133 } 134 135 139 public synchronized String toString() { 140 return isReference() 141 ? getCheckedRef().toString() : String.valueOf(getURL()); 142 } 143 144 148 public synchronized boolean isExists() { 149 if (isReference()) { 150 return ((Resource) getCheckedRef()).isExists(); 151 } 152 return isExists(false); 153 } 154 155 173 private synchronized boolean isExists(boolean closeConnection) { 174 if (getURL() == null) { 175 return false; 176 } 177 try { 178 connect(); 179 return true; 180 } catch (IOException e) { 181 return false; 182 } finally { 183 if (closeConnection) { 184 close(); 185 } 186 } 187 } 188 189 190 196 public synchronized long getLastModified() { 197 if (isReference()) { 198 return ((Resource) getCheckedRef()).getLastModified(); 199 } 200 if (!isExists(false)) { 201 return 0L; 202 } 203 return conn.getLastModified(); 204 } 205 206 210 public synchronized boolean isDirectory() { 211 return isReference() 212 ? ((Resource) getCheckedRef()).isDirectory() 213 : getName().endsWith("/"); 214 } 215 216 221 public synchronized long getSize() { 222 if (isReference()) { 223 return ((Resource) getCheckedRef()).getSize(); 224 } 225 if (!isExists(false)) { 226 return 0L; 227 } 228 try { 229 connect(); 230 long contentlength = conn.getContentLength(); 231 close(); 232 return contentlength; 233 } catch (IOException e) { 234 return UNKNOWN_SIZE; 235 } 236 } 237 238 243 public synchronized boolean equals(Object another) { 244 if (this == another) { 245 return true; 246 } 247 if (isReference()) { 248 return getCheckedRef().equals(another); 249 } 250 if (!(another.getClass().equals(getClass()))) { 251 return false; 252 } 253 URLResource otheru = (URLResource) another; 254 return getURL() == null 255 ? otheru.getURL() == null 256 : getURL().equals(otheru.getURL()); 257 } 258 259 263 public synchronized int hashCode() { 264 if (isReference()) { 265 return getCheckedRef().hashCode(); 266 } 267 return MAGIC * ((getURL() == null) ? NULL_URL : getURL().hashCode()); 268 } 269 270 278 public synchronized InputStream getInputStream() throws IOException { 279 if (isReference()) { 280 return ((Resource) getCheckedRef()).getInputStream(); 281 } 282 connect(); 283 try { 284 return conn.getInputStream(); 285 } finally { 286 conn = null; 287 } 288 } 289 290 299 public synchronized OutputStream getOutputStream() throws IOException { 300 if (isReference()) { 301 return ((Resource) getCheckedRef()).getOutputStream(); 302 } 303 connect(); 304 try { 305 return conn.getOutputStream(); 306 } finally { 307 conn = null; 308 } 309 } 310 311 315 protected synchronized void connect() throws IOException { 316 URL u = getURL(); 317 if (u == null) { 318 throw new BuildException("URL not set"); 319 } 320 if (conn == null) { 321 try { 322 conn = u.openConnection(); 323 conn.connect(); 324 } catch (IOException e) { 325 log(e.toString(), Project.MSG_ERR); 326 conn = null; 327 throw e; 328 } 329 } 330 } 331 332 340 private synchronized void close() { 341 if (conn != null) { 342 try { 343 if (conn instanceof JarURLConnection ) { 344 JarURLConnection juc = (JarURLConnection ) conn; 345 JarFile jf = juc.getJarFile(); 346 jf.close(); 347 jf = null; 348 } else if (conn instanceof HttpURLConnection ) { 349 ((HttpURLConnection ) conn).disconnect(); 350 } 351 } catch (IOException exc) { 352 } finally { 354 conn = null; 355 } 356 } 357 } 358 359 private static URL newURL(String u) { 360 try { 361 return new URL (u); 362 } catch (MalformedURLException e) { 363 throw new BuildException(e); 364 } 365 } 366 367 } 368 | Popular Tags |