1 7 package fr.jayasoft.ivy.repository.url; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.net.URL ; 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.ListIterator ; 17 import java.util.Map ; 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 _resourcesCache = new HashMap (); 29 30 public Resource getResource(String source) throws IOException { 31 Resource res = (Resource)_resourcesCache.get(source); 32 if (res == null) { 33 res = new URLResource(new URL (source)); 34 _resourcesCache.put(source, res); 35 } 36 return res; 37 } 38 39 public void get(String source, File destination) throws IOException { 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 (totalLength)); 46 } 47 FileUtil.copy(new URL (source), destination, _progress); 48 } catch (IOException ex) { 49 fireTransferError(ex); 50 throw ex; 51 } catch (RuntimeException ex) { 52 fireTransferError(ex); 53 throw ex; 54 } finally { 55 _progress.setTotalLength(null); 56 } 57 } 58 59 public void put(File source, String destination, boolean overwrite) throws IOException { 60 throw new UnsupportedOperationException ("URL repository is not able to put files for the moment"); 61 } 62 63 private ApacheURLLister _lister = new ApacheURLLister(); 64 public List list(String parent) throws IOException { 65 if (parent.startsWith("http")) { 66 List urls = _lister.listAll(new URL (parent)); 67 if (urls != null) { 68 List ret = new ArrayList (urls.size()); 69 for (ListIterator iter = urls.listIterator(); iter.hasNext();) { 70 URL url = (URL )iter.next(); 71 ret.add(url.toExternalForm()); 72 } 73 return ret; 74 } 75 } else if (parent.startsWith("file")) { 76 String path = new URL (parent).getPath(); 77 File file = new File (path); 78 if (file.exists() && file.isDirectory()) { 79 String [] files = file.list(); 80 List ret = new ArrayList (files.length); 81 URL context = path.endsWith("/") ? new URL (parent) : new URL (parent+"/"); 82 for (int i = 0; i < files.length; i++) { 83 ret.add(new URL (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 |