1 7 package fr.jayasoft.ivy.repository.file; 8 9 import java.io.File ; 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 14 import fr.jayasoft.ivy.repository.AbstractRepository; 15 import fr.jayasoft.ivy.repository.RepositoryCopyProgressListener; 16 import fr.jayasoft.ivy.repository.Resource; 17 import fr.jayasoft.ivy.repository.TransferEvent; 18 import fr.jayasoft.ivy.util.FileUtil; 19 20 public class FileRepository extends AbstractRepository { 21 private RepositoryCopyProgressListener _progress = new RepositoryCopyProgressListener(this); 22 private File _baseDir; 23 private boolean _local = true; 24 25 public FileRepository() { 26 _baseDir = null; 27 } 28 29 public FileRepository(File basedir) { 30 _baseDir = basedir; 31 } 32 33 public Resource getResource(String source) throws IOException { 34 return new FileResource(this, getFile(source)); 35 } 36 37 public void get(String source, File destination) throws IOException { 38 fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET); 39 copy(getFile(source), destination, true); 40 } 41 42 public void put(File source, String destination, boolean overwrite) throws IOException { 43 fireTransferInitiated(getResource(destination), TransferEvent.REQUEST_PUT); 44 copy(source, getFile(destination), overwrite); 45 } 46 47 private void copy(File src, File destination, boolean overwrite) throws IOException { 48 try { 49 _progress.setTotalLength(new Long (src.length())); 50 FileUtil.copy(src, destination, _progress, overwrite); 51 } catch (IOException ex) { 52 fireTransferError(ex); 53 throw ex; 54 } catch (RuntimeException ex) { 55 fireTransferError(ex); 56 throw ex; 57 } finally { 58 _progress.setTotalLength(null); 59 } 60 } 61 62 public List list(String parent) throws IOException { 63 File dir = getFile(parent); 64 if (dir.exists() && dir.isDirectory()) { 65 String [] names = dir.list(); 66 if (names != null) { 67 List ret = new ArrayList (names.length); 68 for (int i = 0; i < names.length; i++) { 69 ret.add(parent+getFileSeparator()+names[i]); 70 } 71 return ret; 72 } 73 } 74 return null; 75 } 76 77 private File getFile(String source) { 78 if (_baseDir != null) { 79 return new File (_baseDir, source); 80 } else { 81 return new File (source); 82 } 83 } 84 85 public boolean isLocal() { 86 return _local; 87 } 88 89 public void setLocal(boolean local) { 90 _local = local; 91 } 92 93 } 94 | Popular Tags |