KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > file > FileRepository


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.file;
8
9 import java.io.File JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.List JavaDoc;
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 JavaDoc _baseDir;
23     private boolean _local = true;
24
25     public FileRepository() {
26         _baseDir = null;
27     }
28
29     public FileRepository(File JavaDoc basedir) {
30         _baseDir = basedir;
31     }
32
33     public Resource getResource(String JavaDoc source) throws IOException JavaDoc {
34         return new FileResource(this, getFile(source));
35     }
36
37     public void get(String JavaDoc source, File JavaDoc destination) throws IOException JavaDoc {
38         fireTransferInitiated(getResource(source), TransferEvent.REQUEST_GET);
39         copy(getFile(source), destination, true);
40     }
41
42     public void put(File JavaDoc source, String JavaDoc destination, boolean overwrite) throws IOException JavaDoc {
43         fireTransferInitiated(getResource(destination), TransferEvent.REQUEST_PUT);
44         copy(source, getFile(destination), overwrite);
45     }
46
47     private void copy(File JavaDoc src, File JavaDoc destination, boolean overwrite) throws IOException JavaDoc {
48         try {
49             _progress.setTotalLength(new Long JavaDoc(src.length()));
50             FileUtil.copy(src, destination, _progress, overwrite);
51         } catch (IOException JavaDoc ex) {
52             fireTransferError(ex);
53             throw ex;
54         } catch (RuntimeException JavaDoc ex) {
55             fireTransferError(ex);
56             throw ex;
57         } finally {
58             _progress.setTotalLength(null);
59         }
60     }
61
62     public List JavaDoc list(String JavaDoc parent) throws IOException JavaDoc {
63         File JavaDoc dir = getFile(parent);
64         if (dir.exists() && dir.isDirectory()) {
65             String JavaDoc[] names = dir.list();
66             if (names != null) {
67                 List JavaDoc ret = new ArrayList JavaDoc(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 JavaDoc getFile(String JavaDoc source) {
78         if (_baseDir != null) {
79             return new File JavaDoc(_baseDir, source);
80         } else {
81             return new File JavaDoc(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