001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.repository.url;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.net.URL;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.ListIterator;
028: import java.util.Map;
029:
030: import org.apache.ivy.plugins.repository.AbstractRepository;
031: import org.apache.ivy.plugins.repository.RepositoryCopyProgressListener;
032: import org.apache.ivy.plugins.repository.Resource;
033: import org.apache.ivy.plugins.repository.TransferEvent;
034: import org.apache.ivy.util.FileUtil;
035: import org.apache.ivy.util.url.ApacheURLLister;
036:
037: public class URLRepository extends AbstractRepository {
038: private RepositoryCopyProgressListener _progress = new RepositoryCopyProgressListener(
039: this );
040:
041: private Map _resourcesCache = new HashMap();
042:
043: public Resource getResource(String source) throws IOException {
044: Resource res = (Resource) _resourcesCache.get(source);
045: if (res == null) {
046: res = new URLResource(new URL(source));
047: _resourcesCache.put(source, res);
048: }
049: return res;
050: }
051:
052: public void get(String source, File destination) throws IOException {
053: fireTransferInitiated(getResource(source),
054: TransferEvent.REQUEST_GET);
055: try {
056: Resource res = getResource(source);
057: long totalLength = res.getContentLength();
058: if (totalLength > 0) {
059: _progress.setTotalLength(new Long(totalLength));
060: }
061: FileUtil.copy(new URL(source), destination, _progress);
062: } catch (IOException ex) {
063: fireTransferError(ex);
064: throw ex;
065: } catch (RuntimeException ex) {
066: fireTransferError(ex);
067: throw ex;
068: } finally {
069: _progress.setTotalLength(null);
070: }
071: }
072:
073: public void put(File source, String destination, boolean overwrite)
074: throws IOException {
075: throw new UnsupportedOperationException(
076: "URL repository is not able to put files for the moment");
077: }
078:
079: private ApacheURLLister _lister = new ApacheURLLister();
080:
081: public List list(String parent) throws IOException {
082: if (parent.startsWith("http")) {
083: List urls = _lister.listAll(new URL(parent));
084: if (urls != null) {
085: List ret = new ArrayList(urls.size());
086: for (ListIterator iter = urls.listIterator(); iter
087: .hasNext();) {
088: URL url = (URL) iter.next();
089: ret.add(url.toExternalForm());
090: }
091: return ret;
092: }
093: } else if (parent.startsWith("file")) {
094: String path = new URL(parent).getPath();
095: File file = new File(path);
096: if (file.exists() && file.isDirectory()) {
097: String[] files = file.list();
098: List ret = new ArrayList(files.length);
099: URL context = path.endsWith("/") ? new URL(parent)
100: : new URL(parent + "/");
101: for (int i = 0; i < files.length; i++) {
102: ret
103: .add(new URL(context, files[i])
104: .toExternalForm());
105: }
106: return ret;
107: } else {
108: return Collections.EMPTY_LIST;
109: }
110:
111: }
112: return null;
113: }
114:
115: }
|