~ubuntu-branches/ubuntu/karmic/ivy/karmic

« back to all changes in this revision

Viewing changes to src/java/org/apache/ivy/plugins/repository/url/URLRepository.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2009-03-06 22:04:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090306220456-5v37luqiuqda8ewp
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 *  contributor license agreements.  See the NOTICE file distributed with
 
4
 *  this work for additional information regarding copyright ownership.
 
5
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 *  (the "License"); you may not use this file except in compliance with
 
7
 *  the License.  You may obtain a copy of the License at
 
8
 *
 
9
 *      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.plugins.repository.url;
 
19
 
 
20
import java.io.File;
 
21
import java.io.IOException;
 
22
import java.net.URI;
 
23
import java.net.URISyntaxException;
 
24
import java.net.URL;
 
25
import java.util.ArrayList;
 
26
import java.util.Collections;
 
27
import java.util.HashMap;
 
28
import java.util.List;
 
29
import java.util.ListIterator;
 
30
import java.util.Map;
 
31
 
 
32
import org.apache.ivy.plugins.repository.AbstractRepository;
 
33
import org.apache.ivy.plugins.repository.RepositoryCopyProgressListener;
 
34
import org.apache.ivy.plugins.repository.Resource;
 
35
import org.apache.ivy.plugins.repository.TransferEvent;
 
36
import org.apache.ivy.util.FileUtil;
 
37
import org.apache.ivy.util.url.ApacheURLLister;
 
38
 
 
39
public class URLRepository extends AbstractRepository {
 
40
    private RepositoryCopyProgressListener progress = new RepositoryCopyProgressListener(this);
 
41
 
 
42
    private Map resourcesCache = new HashMap();
 
43
 
 
44
    public Resource getResource(String source) throws IOException {
 
45
        Resource res = (Resource) resourcesCache.get(source);
 
46
        if (res == null) {
 
47
            res = new URLResource(new URL(source));
 
48
            resourcesCache.put(source, res);
 
49
        }
 
50
        return res;
 
51
    }
 
52
    
 
53
    public void get(String source, File destination) throws IOException {
 
54
        fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET);
 
55
        try {
 
56
            Resource res = getResource(source);
 
57
            long totalLength = res.getContentLength();
 
58
            if (totalLength > 0) {
 
59
                progress.setTotalLength(new Long(totalLength));
 
60
            }
 
61
            FileUtil.copy(new URL(source), destination, progress);
 
62
        } catch (IOException ex) {
 
63
            fireTransferError(ex);
 
64
            throw ex;
 
65
        } catch (RuntimeException ex) {
 
66
            fireTransferError(ex);
 
67
            throw ex;
 
68
        } finally {
 
69
            progress.setTotalLength(null);
 
70
        }
 
71
    }
 
72
 
 
73
    public void put(File source, String destination, boolean overwrite) throws IOException {
 
74
        if (!overwrite) {
 
75
            throw new UnsupportedOperationException(
 
76
                    "URL repository do not support append operations at the moment");
 
77
        }
 
78
 
 
79
        fireTransferInitiated(getResource(destination), TransferEvent.REQUEST_PUT);
 
80
        try {
 
81
            long totalLength = source.length();
 
82
            if (totalLength > 0) {
 
83
                progress.setTotalLength(new Long(totalLength));
 
84
            }
 
85
            FileUtil.copy(source, new URL(destination), progress);
 
86
        } catch (IOException ex) {
 
87
            fireTransferError(ex);
 
88
            throw ex;
 
89
        } catch (RuntimeException ex) {
 
90
            fireTransferError(ex);
 
91
            throw ex;
 
92
        } finally {
 
93
            progress.setTotalLength(null);
 
94
        }
 
95
    }
 
96
 
 
97
    private ApacheURLLister lister = new ApacheURLLister();
 
98
 
 
99
    public List list(String parent) throws IOException {
 
100
        if (parent.startsWith("http")) {
 
101
            List urls = lister.listAll(new URL(parent));
 
102
            if (urls != null) {
 
103
                List ret = new ArrayList(urls.size());
 
104
                for (ListIterator iter = urls.listIterator(); iter.hasNext();) {
 
105
                    URL url = (URL) iter.next();
 
106
                    ret.add(url.toExternalForm());
 
107
                }
 
108
                return ret;
 
109
            }
 
110
        } else if (parent.startsWith("file")) {
 
111
            String path;
 
112
            try {
 
113
                path = new URI(parent).getPath();
 
114
            } catch (URISyntaxException e) {
 
115
                IOException ioe = new IOException("Couldn't list content of '" + parent + "'");
 
116
                ioe.initCause(e);
 
117
                throw ioe;
 
118
            }
 
119
            
 
120
            File file = new File(path);
 
121
            if (file.exists() && file.isDirectory()) {
 
122
                String[] files = file.list();
 
123
                List ret = new ArrayList(files.length);
 
124
                URL context = path.endsWith("/") ? new URL(parent) : new URL(parent + "/");
 
125
                for (int i = 0; i < files.length; i++) {
 
126
                    ret.add(new URL(context, files[i]).toExternalForm());
 
127
                }
 
128
                return ret;
 
129
            } else {
 
130
                return Collections.EMPTY_LIST;
 
131
            }
 
132
 
 
133
        }
 
134
        return null;
 
135
    }
 
136
 
 
137
}