KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > url > URLHandlerDispatcher


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.url;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import fr.jayasoft.ivy.util.CopyProgressListener;
16
17 /**
18  * This class is used to dispatch downloading requests
19  *
20  * @author Xavier Hanin
21  *
22  */

23 public class URLHandlerDispatcher implements URLHandler {
24     protected Map JavaDoc _handlers = new HashMap JavaDoc();
25     protected URLHandler _default = new BasicURLHandler();
26
27     public URLHandlerDispatcher() {
28     }
29     
30     public boolean isReachable(URL JavaDoc url) {
31         return getHandler(url.getProtocol()).isReachable(url);
32     }
33     
34     public boolean isReachable(URL JavaDoc url, int timeout) {
35         return getHandler(url.getProtocol()).isReachable(url, timeout);
36     }
37     
38     public long getContentLength(URL JavaDoc url) {
39         return getHandler(url.getProtocol()).getContentLength(url);
40     }
41     
42     public long getContentLength(URL JavaDoc url, int timeout) {
43         return getHandler(url.getProtocol()).getContentLength(url, timeout);
44     }
45     
46
47     public long getLastModified(URL JavaDoc url) {
48         return getHandler(url.getProtocol()).getLastModified(url);
49     }
50
51     public long getLastModified(URL JavaDoc url, int timeout) {
52         return getHandler(url.getProtocol()).getLastModified(url, timeout);
53     }
54
55     public URLInfo getURLInfo(URL JavaDoc url) {
56         return getHandler(url.getProtocol()).getURLInfo(url);
57     }
58
59     public URLInfo getURLInfo(URL JavaDoc url, int timeout) {
60         return getHandler(url.getProtocol()).getURLInfo(url, timeout);
61     }
62     public InputStream JavaDoc openStream(URL JavaDoc url) throws IOException JavaDoc {
63         return getHandler(url.getProtocol()).openStream(url);
64     }
65     
66     public void download(URL JavaDoc src, File JavaDoc dest, CopyProgressListener l) throws IOException JavaDoc {
67         getHandler(src.getProtocol()).download(src, dest, l);
68     }
69     
70     public void setDownloader(String JavaDoc protocol, URLHandler downloader) {
71         _handlers.put(protocol, downloader);
72     }
73     
74     public URLHandler getHandler(String JavaDoc protocol) {
75         URLHandler downloader = (URLHandler)_handlers.get(protocol);
76         return downloader == null ? _default : downloader;
77     }
78     
79     public URLHandler getDefault() {
80         return _default;
81     }
82     public void setDefault(URLHandler default1) {
83         _default = default1;
84     }
85 }
86
Popular Tags