KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > repository > vfs > VfsRepository


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  * Implementation of a VFS repository
6  *
7  * @author glen
8  * @author Matt Inger
9  * @author Stephen Nesbitt
10  *
11  */

12
13 package fr.jayasoft.ivy.repository.vfs;
14
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.ArrayList JavaDoc;
21
22 import org.apache.commons.vfs.*;
23 import org.apache.commons.vfs.impl.StandardFileSystemManager;
24
25 import fr.jayasoft.ivy.repository.AbstractRepository;
26 import fr.jayasoft.ivy.repository.RepositoryCopyProgressListener;
27 import fr.jayasoft.ivy.repository.Resource;
28 import fr.jayasoft.ivy.repository.TransferEvent;
29 import fr.jayasoft.ivy.util.CopyProgressListener;
30 import fr.jayasoft.ivy.util.FileUtil;
31 import fr.jayasoft.ivy.util.Message;
32
33
34 public class VfsRepository extends AbstractRepository {
35     /**
36      * Name of the resource defining the Ivy VFS Repo configuration.
37      */

38     private static final String JavaDoc IVY_VFS_CONFIG = "ivy_vfs.xml";
39     private StandardFileSystemManager _manager = null;
40     private final CopyProgressListener _progress = new RepositoryCopyProgressListener(this);
41     
42
43     /**
44      * Create a new Ivy VFS Repository Instance
45      *
46      */

47     public VfsRepository() {
48     }
49     
50     private FileSystemManager getVFSManager() throws IOException JavaDoc {
51         synchronized (this) {
52             if (_manager == null) {
53                 _manager = createVFSManager();
54             }
55         }
56         return _manager;
57     }
58
59     private StandardFileSystemManager createVFSManager() throws IOException JavaDoc {
60         StandardFileSystemManager result = null;
61         try {
62             /*
63              * The DefaultFileSystemManager gets its configuration from the jakarta-vfs-common
64              * implementation which includes the res and tmp schemes which are of no use to use here.
65              * Using StandardFileSystemManager lets us specify which schemes to support as well as
66              * providing a mechanism to change this support without recompilation.
67              */

68             result = new StandardFileSystemManager();
69             result.setConfiguration(getClass().getResource(IVY_VFS_CONFIG));
70             result.init();
71
72             // Generate and print a list of available schemes
73
Message.verbose("Available VFS schemes...");
74             String JavaDoc[] schemes = result.getSchemes();
75             Arrays.sort(schemes);
76             for (int i = 0; i < schemes.length; i++) {
77                 Message.verbose("VFS Supported Scheme: " + schemes[i]);
78             }
79         } catch (FileSystemException e) {
80             /*
81              * If our attempt to initialize a VFS Repository fails we log the failure
82              * but continue on. Given that an Ivy instance may involve numerous
83              * different repository types, it seems overly cautious to throw a runtime
84              * exception on the initialization failure of just one repository type.
85              */

86             Message.error("Unable to initialize VFS repository manager!");
87             Message.error(e.getLocalizedMessage());
88             IOException JavaDoc error = new IOException JavaDoc(e.getLocalizedMessage());
89             error.initCause(e);
90             throw error;
91         }
92         
93         return result;
94     }
95
96     
97     protected void finalize() {
98         if (_manager != null) {
99             _manager.close();
100             _manager = null;
101         }
102     }
103     
104     /**
105      * Get a VfsResource
106      *
107      * @param source a <code>String</code> identifying a VFS Resource
108      * @throws <code>IOException</code> on failure
109      * @see "Supported File Systems in the jakarta-commons-vfs documentation"
110      */

111     public Resource getResource(String JavaDoc vfsURI) throws IOException JavaDoc {
112         return new VfsResource(vfsURI, getVFSManager());
113     }
114     
115     /**
116      * Transfer a VFS Resource from the repository to the local file system.
117      *
118      * @param srcVfsURI a <code>String</code> identifying the VFS resource to be fetched
119      * @param destination a <code>File</code> identifying the destination file
120      * @throws <code>IOException</code> on failure
121      * @see "Supported File Systems in the jakarta-commons-vfs documentation"
122      */

123     public void get(String JavaDoc srcVfsURI, File JavaDoc destination) throws IOException JavaDoc {
124         VfsResource src = new VfsResource(srcVfsURI, getVFSManager());
125         fireTransferInitiated(src, TransferEvent.REQUEST_GET);
126         try {
127             FileContent content = src.getContent();
128             if (content == null) {
129                 throw new IllegalArgumentException JavaDoc("invalid vfs uri "+srcVfsURI+": no content found");
130             }
131             FileUtil.copy(content.getInputStream(), destination, _progress);
132         } catch (IOException JavaDoc ex) {
133             fireTransferError(ex);
134             throw ex;
135         } catch (RuntimeException JavaDoc ex) {
136             fireTransferError(ex);
137             throw ex;
138         }
139     }
140     
141     /**
142      * Return a listing of the contents of a parent directory. Listing is a set
143      * of strings representing VFS URIs.
144      *
145      * @param vfsURI providing identifying a VFS provided resource
146      * @throws IOException on failure.
147      * @see "Supported File Systems in the jakarta-commons-vfs documentation"
148      */

149     public List JavaDoc list(String JavaDoc vfsURI) throws IOException JavaDoc {
150         ArrayList JavaDoc list = new ArrayList JavaDoc();
151         Message.debug("list called for URI" + vfsURI);
152         FileObject resourceImpl = getVFSManager().resolveFile(vfsURI);
153         Message.debug("resourceImpl=" + resourceImpl.toString());
154         Message.debug("resourceImpl.exists()" + resourceImpl.exists());
155         Message.debug("resourceImpl.getType()" + resourceImpl.getType());
156         Message.debug("FileType.FOLDER" + FileType.FOLDER);
157         if ((resourceImpl != null) && resourceImpl.exists() && (resourceImpl.getType() == FileType.FOLDER)) {
158             FileObject[] children = resourceImpl.getChildren();
159             for (int i = 0; i < children.length; i++) {
160                 FileObject child = children[i];
161                 Message.debug("child " + i + child.getName().getURI());
162                 list.add(VfsResource.normalize(child.getName().getURI()));
163             }
164         }
165         return list;
166     }
167     
168     
169     
170     /**
171      * Transfer an Ivy resource to a VFS repository
172      *
173      * @param source a <code>File</code> indentifying the local file to transfer to the repository
174      * @param vfsURI a <code>String</code> identifying the destination VFS Resource.
175      * @param overwrite whether to overwrite an existing resource.
176      * @throws <code>IOException</code> on failure.
177      * @see "Supported File Systems in the jakarta-commons-vfs documentation"
178      *
179      */

180     public void put(File JavaDoc source, String JavaDoc vfsURI, boolean overwrite) throws IOException JavaDoc {
181         VfsResource dest = new VfsResource(vfsURI, getVFSManager());
182         fireTransferInitiated(dest, TransferEvent.REQUEST_PUT);
183         if (dest.physicallyExists() && ! overwrite) {
184             throw new IOException JavaDoc("Cannot copy. Destination file: " + dest.getName() + " exists and overwrite not set.");
185         }
186         if (dest.getContent() == null) {
187             throw new IllegalArgumentException JavaDoc("invalid vfs uri "+vfsURI+" to put data to: resource has no content");
188         }
189         
190         FileUtil.copy(new FileInputStream JavaDoc(source),
191                        dest.getContent().getOutputStream(),
192                        _progress);
193     }
194     
195
196 }
197
Popular Tags