KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > url > URLResource


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.repository.url;
8
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13
14 import fr.jayasoft.ivy.repository.Resource;
15 import fr.jayasoft.ivy.url.URLHandlerRegistry;
16 import fr.jayasoft.ivy.url.URLHandler.URLInfo;
17
18 public class URLResource implements Resource {
19     private URL JavaDoc _url;
20     private boolean _init = false;
21     private long _lastModified;
22     private long _contentLength;
23     private boolean _exists;
24
25     public URLResource(URL JavaDoc url) {
26         _url = url;
27     }
28
29     public String JavaDoc getName() {
30         return _url.toExternalForm();
31     }
32     
33     public Resource clone(String JavaDoc cloneName) {
34         try {
35             return new URLResource(new URL JavaDoc(cloneName));
36         } catch (MalformedURLException JavaDoc e) {
37             throw new IllegalArgumentException JavaDoc("bad clone name provided: not suitable for an URLResource: "+cloneName);
38         }
39     }
40
41     public long getLastModified() {
42         if (!_init) {
43             init();
44         }
45         return _lastModified;
46     }
47
48     private void init() {
49         URLInfo info = URLHandlerRegistry.getDefault().getURLInfo(_url);
50         _contentLength = info.getContentLength();
51         _lastModified = info.getLastModified();
52         _exists = info.isReachable();
53         _init = true;
54     }
55
56     public long getContentLength() {
57         if (!_init) {
58             init();
59         }
60         return _contentLength;
61     }
62
63     public boolean exists() {
64         if (!_init) {
65             init();
66         }
67         return _exists;
68     }
69
70     public URL JavaDoc getURL() {
71         return _url;
72     }
73     public String JavaDoc toString() {
74         return getName();
75     }
76     
77     public boolean isLocal() {
78         return false;
79     }
80
81     public InputStream JavaDoc openStream() throws IOException JavaDoc {
82         return _url.openStream();
83     }
84 }
85
Popular Tags