01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.ivy.util.url;
19:
20: import java.io.File;
21: import java.io.IOException;
22: import java.io.InputStream;
23: import java.net.URL;
24: import java.util.HashMap;
25: import java.util.Map;
26:
27: import org.apache.ivy.util.CopyProgressListener;
28:
29: /**
30: * This class is used to dispatch downloading requests
31: */
32: public class URLHandlerDispatcher implements URLHandler {
33: private Map handlers = new HashMap();
34:
35: private URLHandler defaultHandler = new BasicURLHandler();
36:
37: public URLHandlerDispatcher() {
38: }
39:
40: public boolean isReachable(URL url) {
41: return getHandler(url.getProtocol()).isReachable(url);
42: }
43:
44: public boolean isReachable(URL url, int timeout) {
45: return getHandler(url.getProtocol()).isReachable(url, timeout);
46: }
47:
48: public long getContentLength(URL url) {
49: return getHandler(url.getProtocol()).getContentLength(url);
50: }
51:
52: public long getContentLength(URL url, int timeout) {
53: return getHandler(url.getProtocol()).getContentLength(url,
54: timeout);
55: }
56:
57: public long getLastModified(URL url) {
58: return getHandler(url.getProtocol()).getLastModified(url);
59: }
60:
61: public long getLastModified(URL url, int timeout) {
62: return getHandler(url.getProtocol()).getLastModified(url,
63: timeout);
64: }
65:
66: public URLInfo getURLInfo(URL url) {
67: return getHandler(url.getProtocol()).getURLInfo(url);
68: }
69:
70: public URLInfo getURLInfo(URL url, int timeout) {
71: return getHandler(url.getProtocol()).getURLInfo(url, timeout);
72: }
73:
74: public InputStream openStream(URL url) throws IOException {
75: return getHandler(url.getProtocol()).openStream(url);
76: }
77:
78: public void download(URL src, File dest, CopyProgressListener l)
79: throws IOException {
80: getHandler(src.getProtocol()).download(src, dest, l);
81: }
82:
83: public void setDownloader(String protocol, URLHandler downloader) {
84: handlers.put(protocol, downloader);
85: }
86:
87: public URLHandler getHandler(String protocol) {
88: URLHandler downloader = (URLHandler) handlers.get(protocol);
89: return downloader == null ? defaultHandler : downloader;
90: }
91:
92: public URLHandler getDefault() {
93: return defaultHandler;
94: }
95:
96: public void setDefault(URLHandler default1) {
97: defaultHandler = default1;
98: }
99: }
|