KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > ftp > client > FTPFileSystem


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.ftp.client;
20
21 import java.io.IOException JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.net.UnknownHostException JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.openharmonise.commons.xml.namespace.NamespaceType;
28 import org.openharmonise.vfs.*;
29 import org.openharmonise.vfs.authentication.*;
30 import org.openharmonise.vfs.metadata.*;
31 import org.openharmonise.vfs.search.*;
32 import org.openharmonise.vfs.status.*;
33
34 import com.enterprisedt.net.ftp.FTPClient;
35 import com.enterprisedt.net.ftp.FTPException;
36
37 /**
38  * Represents contents of an FTP server as a virtual file system.
39  *
40  * @author Matthew Large
41  *
42  */

43 public class FTPFileSystem extends org.openharmonise.vfs.AbstractVirtualFileSystem {
44     
45     private VirtualFileCache m_cache = null;
46     
47     private FTPClient m_conn = null;
48     
49     private VirtualFileSystemView m_vfView = null;
50     
51     /**
52      *
53      */

54     public FTPFileSystem(URI JavaDoc uri) {
55         super(uri);
56         this.setup();
57     }
58     
59     /**
60      *
61      */

62     public FTPFileSystem(URI JavaDoc uri, AuthInfo authInfo) {
63         super(uri, authInfo);
64         this.setup();
65     }
66     
67     /**
68      *
69      */

70     public FTPFileSystem(URI JavaDoc uri, AbstractAuthenticationStore authStore) {
71         super(uri, authStore);
72         this.setup();
73     }
74     
75     private void setup() {
76         InetAddress JavaDoc addr;
77         this.m_sInitialPath = this.m_sInitialPath.replace('\\', '/');
78         
79         try {
80             addr = InetAddress.getByName(this.getURI().getHost());
81             m_conn = new FTPClient(addr, this.getURI().getPort());
82         } catch (UnknownHostException JavaDoc e) {
83             e.printStackTrace();
84         } catch (IOException JavaDoc e) {
85             e.printStackTrace();
86         } catch (FTPException e) {
87             e.printStackTrace();
88         }
89         this.m_cache = new VirtualFileCache();
90     }
91
92     /* (non-Javadoc)
93      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getOptions()
94      */

95     public List JavaDoc getOptions() {
96         return null;
97     }
98
99     /* (non-Javadoc)
100      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getVirtualFile(java.lang.String)
101      */

102     public ResourceStatusWrapper getVirtualFile(String JavaDoc sFullPath) {
103         VirtualFile vfFile = null;
104         
105         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
106         
107         if( this.m_cache.hasFile(sFullPath) ) {
108             vfFile = this.m_cache.getFile(sFullPath);
109         } else {
110             try {
111                 vfFile = new VirtualFile(sFullPath);
112                 vfFile.setVFS(this);
113                 String JavaDoc[] sListing = this.m_conn.dir(sRealPath, true);
114                 this.populateVirtualFiles(sListing,sFullPath, vfFile);
115             } catch (IOException JavaDoc e) {
116                 e.printStackTrace();
117             } catch (FTPException e) {
118                 if( e.getMessage().trim().equals("Please login with USER and PASS.")) {
119                     AuthInfo auth = this.getAuthentication();
120                     try {
121                         this.m_conn.login(auth.getUsername(), auth.getPassword());
122                     } catch (IOException JavaDoc e1) {
123                         e1.printStackTrace();
124                     } catch (FTPException e1) {
125                         e1.printStackTrace();
126                     }
127                     vfFile = this.getVirtualFile(sFullPath).getResource();
128                 } else {
129                     e.printStackTrace();
130                 }
131             }
132         }
133         
134         if(vfFile==null) {
135             this.fireErrorEvent("FileNotFound", "The file " + sFullPath + " cannot be found.");
136         }
137
138         return new ResourceStatusWrapper(vfFile, new VFSStatus());
139     }
140     
141     private void populateVirtualFiles(String JavaDoc[] sListing, String JavaDoc sPath, VirtualFile vfFile) {
142
143         List JavaDoc aListing = this.getListParser(sListing).parse(sListing, sPath);
144         
145         
146         int nCount=1;
147         for(int i=0; i<aListing.size(); i++) {
148             if( nCount==1) {
149                 this.populateVirtualFile( (FTPListItem)aListing.get(i), vfFile );
150             } else if( nCount==2) {
151                 //NO-OP
152
} else {
153                 VirtualFile vfChild = null;
154                 String JavaDoc sTempPath = sPath + "/" + ((FTPListItem)aListing.get(i)).getFileName();
155                 if( this.m_cache.hasFile(sTempPath) ) {
156                     vfChild = this.m_cache.getFile(sTempPath);
157                 } else {
158                     vfChild = new VirtualFile(sTempPath);
159                     vfChild.setVFS(this);
160                 }
161                 this.populateVirtualFile((FTPListItem)aListing.get(i), vfChild);
162                 vfFile.addChild( vfChild.getFullPath() );
163             }
164             
165             nCount++;
166         }
167         if( nCount>2 ) {
168             this.setFileChildrenPopulated(vfFile, true);
169             this.setFileMetadataPopulated(vfFile, true);
170         }
171
172     }
173     
174     private void populateVirtualFile(FTPListItem item, VirtualFile vfFile) {
175         vfFile.setIsDirectory( item.isDirectory() );
176         
177         PropertyInstance prop = new PropertyInstance(NamespaceType.OHRM.getURI(), "filesize");
178         vfFile.addProperty(prop);
179         
180         prop = new PropertyInstance(NamespaceType.OHRM.getURI(), "modificationdate");
181         vfFile.addProperty(prop);
182     }
183     
184     private FTPListParseable getListParser(String JavaDoc[] sListing) {
185         FTPListParseable listParser = new FTPUNIXListParser();
186         return listParser;
187     }
188
189     /* (non-Javadoc)
190      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#addVirtualFile(java.lang.String, com.simulacramedia.vfs.VirtualFile)
191      */

192     public ResourceStatusWrapper addVirtualFile(String JavaDoc sPath, VirtualFile content) {
193         VirtualFile vfFile = null;
194         
195         String JavaDoc sFullPath = sPath + "/" + content.getFileName();
196         
197         sPath = this.getInitialPath() + sPath + "/" + content.getFileName();
198         
199         
200         
201         if( this.m_cache.hasFile(sFullPath) ) {
202             // NO-OP
203
} else {
204             try {
205                 this.m_conn.put(content.getContent(), sPath);
206             } catch (IOException JavaDoc e) {
207                 this.fireErrorEvent("FileNotAdded", "The file " + sFullPath + " cannot be added here, this might be because a file already exists here with the same name.");
208                 e.printStackTrace();
209             } catch (FTPException e) {
210                 this.fireErrorEvent("FileNotAdded", "The file " + sFullPath + " cannot be added here, this might be because a file already exists here with the same name.");
211                 e.printStackTrace();
212             }
213         }
214         
215         if(vfFile==null) {
216             this.fireErrorEvent("FileNotAdded", "The file " + sFullPath + " cannot be added here, this might be because a file already exists here with the same name.");
217         }
218
219         return new ResourceStatusWrapper(vfFile, new VFSStatus());
220     }
221
222     /* (non-Javadoc)
223      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#moveVirtualFile(java.lang.String, java.lang.String)
224      */

225     public StatusData moveVirtualFile(String JavaDoc sFromFullPath, String JavaDoc sToFullPath) {
226         VFSStatus retnStatus = new VFSStatus();
227         
228         boolean bError = false;
229         
230         String JavaDoc sRealFromPath = this.getInitialPath() + sFromFullPath;
231         String JavaDoc sRealToPath = this.getInitialPath() + sToFullPath;
232         
233         VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource();
234         if( !vfFromFile.isDirectory() ) {
235             byte[] content = vfFromFile.getContent();
236             try {
237                 this.m_conn.put(content, sRealToPath);
238             } catch (IOException JavaDoc e) {
239                 this.fireErrorEvent("FileNotMoved", "The file " + sFromFullPath + " cannot be moved here, this might be because a file already exists here with the same name.");
240                 bError = true;
241                 e.printStackTrace();
242             } catch (FTPException e) {
243                 this.fireErrorEvent("FileNotMoved", "The file " + sFromFullPath + " cannot be moved here, this might be because a file already exists here with the same name.");
244                 bError = true;
245                 e.printStackTrace();
246             }
247             
248             if( !bError ) {
249                 try {
250                     this.m_conn.delete(sRealFromPath);
251                 } catch (IOException JavaDoc e1) {
252                     this.fireErrorEvent("FileNotMoved", "The original file " + sFromFullPath + " cannot be removed.");
253                     bError = true;
254                     e1.printStackTrace();
255                 } catch (FTPException e1) {
256                     this.fireErrorEvent("FileNotMoved", "The original file " + sFromFullPath + " cannot be removed.");
257                     bError = true;
258                     e1.printStackTrace();
259                 }
260             } else {
261                 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
262                 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
263             }
264         }
265
266         return retnStatus;
267     }
268
269     /* (non-Javadoc)
270      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#copyVirtualFile(java.lang.String, java.lang.String)
271      */

272     public StatusData copyVirtualFile(String JavaDoc sFromFullPath, String JavaDoc sToFullPath) {
273         VFSStatus retnStatus = new VFSStatus();
274         
275         boolean bError = false;
276         
277         String JavaDoc sRealFromPath = this.getInitialPath() + sFromFullPath;
278         String JavaDoc sRealToPath = this.getInitialPath() + sToFullPath;
279         
280         VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource();
281         if( !vfFromFile.isDirectory() ) {
282             byte[] content = vfFromFile.getContent();
283             try {
284                 this.m_conn.put(content, sRealToPath);
285             } catch (IOException JavaDoc e) {
286                 this.fireErrorEvent("FileNotCopied", "The file " + sFromFullPath + " cannot be copied here, this might be because a file already exists here with the same name.");
287                 bError = true;
288                 e.printStackTrace();
289             } catch (FTPException e) {
290                 this.fireErrorEvent("FileNotCopied", "The file " + sFromFullPath + " cannot be copied here, this might be because a file already exists here with the same name.");
291                 bError = true;
292                 e.printStackTrace();
293             }
294         }
295         
296         if(bError) {
297             retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
298             retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
299         }
300
301         return retnStatus;
302     }
303
304     /* (non-Javadoc)
305      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#deleteVirtualFile(java.lang.String)
306      */

307     public StatusData deleteVirtualFile(String JavaDoc sFullPath) {
308         VFSStatus retnStatus = new VFSStatus();
309         
310         boolean bError = false;
311         
312         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
313         
314         try {
315             this.m_conn.delete(sRealPath);
316         } catch (IOException JavaDoc e) {
317             this.fireErrorEvent("FileNotDeleted", "The file " + sFullPath + " cannot be deleted.");
318             bError = true;
319             e.printStackTrace();
320         } catch (FTPException e) {
321             this.fireErrorEvent("FileNotDeleted", "The file " + sFullPath + " cannot be deleted.");
322             bError = true;
323             e.printStackTrace();
324         }
325         
326         if(bError) {
327             retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
328             retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
329         }
330
331         return retnStatus;
332     }
333
334     /* (non-Javadoc)
335      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#lockVirtualFile(java.lang.String)
336      */

337     public StatusData lockVirtualFile(String JavaDoc sFullPath) {
338         VFSStatus retnStatus = new VFSStatus();
339         
340         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
341         this.fireErrorEvent("FileNotLocked", "This server does not support the locking of files.");
342         return retnStatus;
343     }
344
345     /* (non-Javadoc)
346      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#unlockVirtualFile(java.lang.String)
347      */

348     public StatusData unlockVirtualFile(String JavaDoc sFullPath) {
349         VFSStatus retnStatus = new VFSStatus();
350         
351         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
352         this.fireErrorEvent("FileNotUnlocked", "This server does not support the locking of files.");
353         return retnStatus;
354     }
355
356     /* (non-Javadoc)
357      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#createVirtualDirectory(java.lang.String)
358      */

359     public StatusData createVirtualDirectory(String JavaDoc sFullPath) {
360         VFSStatus retnStatus = new VFSStatus();
361         
362         boolean bError = false;
363         
364         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
365         
366         try {
367             this.m_conn.mkdir(sRealPath);
368         } catch (IOException JavaDoc e) {
369             this.fireErrorEvent("DirectoryNotCreated", "The directory " + sFullPath + " cannot be created.");
370             bError = true;
371             e.printStackTrace();
372         } catch (FTPException e) {
373             this.fireErrorEvent("DirectoryNotCreated", "The directory " + sFullPath + " cannot be created.");
374             bError = true;
375             e.printStackTrace();
376         }
377         
378         if(bError) {
379             retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
380             retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
381         }
382
383         return retnStatus;
384     }
385
386     /* (non-Javadoc)
387      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#createShortcut(java.lang.String, java.lang.String)
388      */

389     public StatusData createShortcut(String JavaDoc sFullPath, String JavaDoc sToFullPath) {
390         VFSStatus retnStatus = new VFSStatus();
391         
392         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
393         String JavaDoc sRealToPath = this.getInitialPath() + sToFullPath;
394         this.fireErrorEvent("ShortcutNotCreted", "This server does not support shortcuts.");
395         return retnStatus;
396     }
397
398     /* (non-Javadoc)
399      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#search(com.simulacramedia.vfs.search.Query)
400      */

401     public ResourceListStatusWrapper search(Query query) {
402         return null;
403     }
404
405     /* (non-Javadoc)
406      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getVirtualFileSystemView()
407      */

408     public VirtualFileSystemView getVirtualFileSystemView() {
409         if( this.m_vfView==null ) {
410             this.m_vfView = new FTPFileSystemView();
411         }
412
413         return this.m_vfView;
414     }
415
416     /* (non-Javadoc)
417      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getVirtualFileContent(java.lang.String)
418      */

419     public byte[] getVirtualFileContent(String JavaDoc sFullPath) {
420         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
421         
422         byte[] content = null;
423         
424         try {
425             content = this.m_conn.get(sRealPath);
426         } catch (IOException JavaDoc e) {
427             this.fireErrorEvent("ContentNotRetrieved", "The content for " + sFullPath + " could not be retrieved.");
428             e.printStackTrace();
429         } catch (FTPException e) {
430             this.fireErrorEvent("ContentNotRetrieved", "The content for " + sFullPath + " could not be retrieved.");
431             e.printStackTrace();
432         }
433
434         return content;
435     }
436
437     /* (non-Javadoc)
438      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#fullyPopulateFileMetadata(com.simulacramedia.vfs.VirtualFile)
439      */

440     protected void fullyPopulateFileMetadata(VirtualFile vfFile) {
441         // NO-OP
442
}
443
444     /* (non-Javadoc)
445      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#fullyPopulateFileChildren(com.simulacramedia.vfs.VirtualFile)
446      */

447     protected void fullyPopulateFileChildren(VirtualFile vfFile) {
448         // NO-OP
449

450     }
451
452     /* (non-Javadoc)
453      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#synchroniseFile(com.simulacramedia.vfs.VirtualFile)
454      */

455     public StatusData synchroniseFile(VirtualFile vfFile) {
456         VFSStatus retnStatus = new VFSStatus();
457         return retnStatus;
458     }
459
460     /* (non-Javadoc)
461      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#sunchroniseAllFiles()
462      */

463     public StatusData synchroniseAllFiles() {
464         VFSStatus retnStatus = new VFSStatus();
465         return retnStatus;
466     }
467
468     /* (non-Javadoc)
469      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#exists(java.lang.String)
470      */

471     public boolean exists(String JavaDoc sFullPath) {
472         boolean bExists = true;
473         
474         return bExists;
475     }
476
477     /* (non-Javadoc)
478      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#refreshChildren(com.simulacramedia.vfs.VirtualFile)
479      */

480     protected void refreshChildren(VirtualFile vfFile) {
481     }
482
483     /* (non-Javadoc)
484      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#orderVirtualFileChildren(java.util.List, com.simulacramedia.vfs.VirtualFile)
485      */

486     public StatusData orderVirtualFileChildren(List JavaDoc aPaths, VirtualFile vfDir) {
487         VFSStatus retnStatus = new VFSStatus();
488         return retnStatus;
489     }
490
491     /* (non-Javadoc)
492      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getNewValueInstance(com.simulacramedia.vfs.metadata.PropertyInstance)
493      */

494     public ValueInstance getNewValueInstance(PropertyInstance propInst) {
495         return null;
496     }
497
498     /* (non-Javadoc)
499      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#rejectAllChanges()
500      */

501     public boolean rejectAllChanges() {
502         return false;
503     }
504
505     /* (non-Javadoc)
506      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#fullyPopulateFileAllowedMethods(com.simulacramedia.vfs.VirtualFile)
507      */

508     protected void fullyPopulateFileAllowedMethods(VirtualFile vfFile) {
509     }
510
511     /* (non-Javadoc)
512      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#currentUserResourcePath()
513      */

514     public String JavaDoc currentUserResourcePath(AuthInfo authInfo) {
515         return null;
516     }
517
518     /* (non-Javadoc)
519      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getChangedVirtualFiles()
520      */

521     public List JavaDoc getChangedVirtualFiles() {
522         return null;
523     }
524
525     /* (non-Javadoc)
526      * @see org.openharmonise.vfs.AbstractVirtualFileSystem#getPropertyVirtualFile(java.lang.String)
527      */

528     public VirtualFile getPropertyVirtualFile(String JavaDoc sPropPath) {
529         return null;
530     }
531
532 }
533
Popular Tags