KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.ListIterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import fr.jayasoft.ivy.repository.AbstractRepository;
20 import fr.jayasoft.ivy.repository.RepositoryCopyProgressListener;
21 import fr.jayasoft.ivy.repository.Resource;
22 import fr.jayasoft.ivy.repository.TransferEvent;
23 import fr.jayasoft.ivy.url.ApacheURLLister;
24 import fr.jayasoft.ivy.util.FileUtil;
25
26 public class URLRepository extends AbstractRepository {
27     private RepositoryCopyProgressListener _progress = new RepositoryCopyProgressListener(this);
28     private Map JavaDoc _resourcesCache = new HashMap JavaDoc();
29
30     public Resource getResource(String JavaDoc source) throws IOException JavaDoc {
31         Resource res = (Resource)_resourcesCache.get(source);
32         if (res == null) {
33             res = new URLResource(new URL JavaDoc(source));
34             _resourcesCache.put(source, res);
35         }
36         return res;
37     }
38
39     public void get(String JavaDoc source, File JavaDoc destination) throws IOException JavaDoc {
40         fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET);
41         try {
42             Resource res = getResource(source);
43             long totalLength = res.getContentLength();
44             if (totalLength > 0) {
45                 _progress.setTotalLength(new Long JavaDoc(totalLength));
46             }
47             FileUtil.copy(new URL JavaDoc(source), destination, _progress);
48         } catch (IOException JavaDoc ex) {
49             fireTransferError(ex);
50             throw ex;
51         } catch (RuntimeException JavaDoc ex) {
52             fireTransferError(ex);
53             throw ex;
54         } finally {
55             _progress.setTotalLength(null);
56         }
57     }
58
59     public void put(File JavaDoc source, String JavaDoc destination, boolean overwrite) throws IOException JavaDoc {
60         throw new UnsupportedOperationException JavaDoc("URL repository is not able to put files for the moment");
61     }
62
63     private ApacheURLLister _lister = new ApacheURLLister();
64     public List JavaDoc list(String JavaDoc parent) throws IOException JavaDoc {
65         if (parent.startsWith("http")) {
66             List JavaDoc urls = _lister.listAll(new URL JavaDoc(parent));
67             if (urls != null) {
68                 List JavaDoc ret = new ArrayList JavaDoc(urls.size());
69                 for (ListIterator JavaDoc iter = urls.listIterator(); iter.hasNext();) {
70                     URL JavaDoc url = (URL JavaDoc)iter.next();
71                     ret.add(url.toExternalForm());
72                 }
73                 return ret;
74             }
75         } else if (parent.startsWith("file")) {
76             String JavaDoc path = new URL JavaDoc(parent).getPath();
77             File JavaDoc file = new File JavaDoc(path);
78             if (file.exists() && file.isDirectory()) {
79                 String JavaDoc[] files = file.list();
80                 List JavaDoc ret = new ArrayList JavaDoc(files.length);
81                 URL JavaDoc context = path.endsWith("/") ? new URL JavaDoc(parent) : new URL JavaDoc(parent+"/");
82                 for (int i = 0; i < files.length; i++) {
83                     ret.add(new URL JavaDoc(context, files[i]).toExternalForm());
84                 }
85                 return ret;
86             } else {
87                 return Collections.EMPTY_LIST;
88             }
89
90         }
91         return null;
92     }
93
94 }
95
Popular Tags