KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > localfilesystem > LocalFileSystem


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.localfilesystem;
20
21 import java.io.*;
22 import java.net.*;
23 import java.util.*;
24
25 import org.openharmonise.vfs.*;
26 import org.openharmonise.vfs.authentication.*;
27 import org.openharmonise.vfs.metadata.*;
28 import org.openharmonise.vfs.search.*;
29 import org.openharmonise.vfs.status.*;
30
31
32 /**
33  * @author Matthew Large
34  *
35  */

36 public class LocalFileSystem extends AbstractVirtualFileSystem {
37
38     private VirtualFileCache m_cache = null;
39
40     private VirtualFileSystemView m_vfView = null;
41
42     /**
43      * @param uri
44      * @param sPathSeparator
45      */

46     public LocalFileSystem(URI uri) {
47         super(uri);
48         this.setup();
49     }
50
51     /**
52      * @param uri
53      * @param authInfo
54      * @param sPathSeparator
55      */

56     public LocalFileSystem(URI uri, AuthInfo authInfo) {
57         super(uri, authInfo);
58         this.setup();
59     }
60
61     /**
62      * @param uri
63      * @param authStore
64      * @param sPathSeparator
65      */

66     public LocalFileSystem(
67         URI uri,
68         AbstractAuthenticationStore authStore) {
69         super(uri, authStore);
70         this.setup();
71     }
72
73     private void setup() {
74         this.m_sInitialPath = this.m_sInitialPath.substring(1, this.m_sInitialPath.length());
75         
76         this.m_sInitialPath = this.m_sInitialPath.replace('/', '\\');
77         this.m_cache = new VirtualFileCache();
78     }
79
80     /* (non-Javadoc)
81      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getOptions()
82      */

83     public List getOptions() {
84         return null;
85     }
86
87     /* (non-Javadoc)
88      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getVirtualFile(java.lang.String)
89      */

90     public ResourceStatusWrapper getVirtualFile(String JavaDoc sFullPath) {
91         VirtualFile vfFile = null;
92
93         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
94         sRealPath = sRealPath.replace('/', File.separatorChar);
95
96         if (this.m_cache.hasFile(sFullPath)) {
97             vfFile = this.m_cache.getFile(sFullPath);
98         } else {
99             vfFile = new VirtualFile(sFullPath);
100             vfFile.setVFS(this);
101             File fRealFile = new File(sRealPath);
102             this.populateVirtualFile(fRealFile, vfFile);
103             this.m_cache.addFile(vfFile);
104         }
105
106         if (vfFile == null) {
107             this.fireErrorEvent(
108                 "FileNotFound",
109                 "The file " + sFullPath + " cannot be found.");
110         }
111
112         return new ResourceStatusWrapper(vfFile, new VFSStatus());
113     }
114
115     private void populateVirtualFile(File fRealFile, VirtualFile vfFile) {
116         vfFile.setIsDirectory(fRealFile.isDirectory());
117
118         File[] children = fRealFile.listFiles();
119         if(children!=null) {
120             for (int i = 0; i < children.length; i++) {
121                 vfFile.addChild(
122                     vfFile.getFullPath()
123                         + "/"
124                         + children[i].getName());
125             }
126         }
127         this.setFileChildrenPopulated(vfFile, true);
128
129         this.setFileMetadataPopulated(vfFile, true);
130     }
131
132     /* (non-Javadoc)
133      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#addVirtualFile(java.lang.String, com.simulacramedia.vfs.VirtualFile)
134      */

135     public ResourceStatusWrapper addVirtualFile(String JavaDoc sPath, VirtualFile content) {
136         VirtualFile vfFile = null;
137
138         String JavaDoc sFullPath =
139             sPath + "/" + content.getFileName();
140
141         String JavaDoc sRealPath =
142             this.getInitialPath()
143                 + sPath
144                 + "/"
145                 + content.getFileName();
146         sRealPath = sRealPath.replace('/', File.separatorChar);
147
148         if (this.m_cache.hasFile(sFullPath)) {
149             // NO-OP
150
} else {
151             File fRealFile = new File(sRealPath);
152             try {
153                 FileOutputStream fos = new FileOutputStream(fRealFile);
154                 fos.write(content.getContent());
155             } catch (FileNotFoundException e) {
156                 this.fireErrorEvent(
157                     "FileNotAdded",
158                     "The file "
159                         + sFullPath
160                         + " cannot be added here, this might be because a file already exists here with the same name.");
161                 e.printStackTrace();
162             } catch (IOException e) {
163                 this.fireErrorEvent(
164                     "FileNotAdded",
165                     "The file "
166                         + sFullPath
167                         + " cannot be added here, this might be because a file already exists here with the same name.");
168                 e.printStackTrace();
169             }
170         }
171
172         if (vfFile == null) {
173             this.fireErrorEvent(
174                 "FileNotAdded",
175                 "The file "
176                     + sFullPath
177                     + " cannot be added here, this might be because a file already exists here with the same name.");
178         }
179
180         return new ResourceStatusWrapper(vfFile, new VFSStatus());
181     }
182
183     /* (non-Javadoc)
184      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#moveVirtualFile(java.lang.String, java.lang.String)
185      */

186     public StatusData moveVirtualFile(String JavaDoc sFromFullPath, String JavaDoc sToFullPath) {
187         VFSStatus retnStatus = new VFSStatus();
188         
189         boolean bError = false;
190         
191         String JavaDoc sRealFromPath = this.getInitialPath() + sFromFullPath;
192         sRealFromPath = sRealFromPath.replace('/', File.separatorChar);
193         String JavaDoc sRealToPath = this.getInitialPath() + sToFullPath;
194         sRealToPath = sRealToPath.replace('/', File.separatorChar);
195         
196         VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource();
197         if( !vfFromFile.isDirectory() ) {
198             byte[] content = vfFromFile.getContent();
199             File from = new File(sRealFromPath);
200             File to = new File(sRealToPath);
201             
202             FileInputStream fi = null;
203             FileOutputStream fo = null;
204
205             try {
206                 Long JavaDoc lLength = new Long JavaDoc(from.length());
207                 byte[] bytes = new byte[lLength.intValue()];
208                 fi = new FileInputStream(from);
209                 fo = new FileOutputStream(to);
210
211                 fi.read(bytes);
212                 fo.write(bytes);
213
214             } catch (Exception JavaDoc e) {
215                 e.printStackTrace(System.out);
216                 this.fireErrorEvent("FileNotMoved", "The original file " + sFromFullPath + " cannot be removed.");
217                 bError = true;
218             } finally {
219                 try {
220                     fo.close();
221                     fi.close();
222                 } catch (IOException ioe) {
223                     this.fireErrorEvent("FileNotMoved", "The original file " + sFromFullPath + " cannot be removed.");
224                     bError = true;
225                     ioe.printStackTrace(System.out);
226                 }
227             }
228             
229             if( !bError ) {
230                 from.delete();
231             }
232         }
233         
234         if(bError) {
235             retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
236             retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
237         }
238
239         return retnStatus;
240     }
241
242     /* (non-Javadoc)
243      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#copyVirtualFile(java.lang.String, java.lang.String)
244      */

245     public StatusData copyVirtualFile(String JavaDoc sFromFullPath, String JavaDoc sToFullPath) {
246         VFSStatus retnStatus = new VFSStatus();
247         
248         boolean bError = false;
249         
250         String JavaDoc sRealFromPath = this.getInitialPath() + sFromFullPath;
251         sRealFromPath = sRealFromPath.replace('/', File.separatorChar);
252         String JavaDoc sRealToPath = this.getInitialPath() + sToFullPath;
253         sRealToPath = sRealToPath.replace('/', File.separatorChar);
254         
255         VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource();
256         if( !vfFromFile.isDirectory() ) {
257             byte[] content = vfFromFile.getContent();
258             File from = new File(sRealFromPath);
259             File to = new File(sRealToPath);
260             
261             FileInputStream fi = null;
262             FileOutputStream fo = null;
263
264             try {
265                 Long JavaDoc lLength = new Long JavaDoc(from.length());
266                 byte[] bytes = new byte[lLength.intValue()];
267                 fi = new FileInputStream(from);
268                 fo = new FileOutputStream(to);
269
270                 fi.read(bytes);
271                 fo.write(bytes);
272
273             } catch (Exception JavaDoc e) {
274                 e.printStackTrace(System.out);
275                 this.fireErrorEvent("FileNotCopied", "The file " + sFromFullPath + " cannot be copied here, this might be because a file already exists here with the same name.");
276                 bError = true;
277             } finally {
278                 try {
279                     fo.close();
280                     fi.close();
281                 } catch (IOException ioe) {
282                     this.fireErrorEvent("FileNotCopied", "The file " + sFromFullPath + " cannot be copied here, this might be because a file already exists here with the same name.");
283                     bError = true;
284                     ioe.printStackTrace(System.out);
285                 }
286             }
287         }
288         
289         if(bError) {
290             retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
291             retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
292         }
293
294         return retnStatus;
295     }
296
297     /* (non-Javadoc)
298      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#deleteVirtualFile(java.lang.String)
299      */

300     public StatusData deleteVirtualFile(String JavaDoc sFullPath) {
301         VFSStatus retnStatus = new VFSStatus();
302         
303         boolean bError = false;
304         
305         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
306         sRealPath = sRealPath.replace('/', File.separatorChar);
307         
308         File fRealFile = new File(sRealPath);
309         
310         bError = !fRealFile.delete();
311         
312         if(bError) {
313             this.fireErrorEvent("FileNotDeleted", "The file " + sFullPath + " cannot be deleted.");
314         }
315         
316         if(bError) {
317             retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
318             retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
319         }
320
321         return retnStatus;
322     }
323
324     /* (non-Javadoc)
325      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#lockVirtualFile(java.lang.String)
326      */

327     public StatusData lockVirtualFile(String JavaDoc sFullPath) {
328         VFSStatus retnStatus = new VFSStatus();
329         
330         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
331         sRealPath = sRealPath.replace('/', File.separatorChar);
332         this.fireErrorEvent("FileNotLocked", "This server does not support the locking of files.");
333
334
335         return retnStatus;
336     }
337
338     /* (non-Javadoc)
339      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#unlockVirtualFile(java.lang.String)
340      */

341     public StatusData unlockVirtualFile(String JavaDoc sFullPath) {
342         VFSStatus retnStatus = new VFSStatus();
343         
344         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
345         sRealPath = sRealPath.replace('/', File.separatorChar);
346         this.fireErrorEvent("FileNotUnlocked", "This server does not support the locking of files.");
347
348         return retnStatus;
349     }
350
351     /* (non-Javadoc)
352      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#createVirtualDirectory(java.lang.String)
353      */

354     public StatusData createVirtualDirectory(String JavaDoc sFullPath) {
355         VFSStatus retnStatus = new VFSStatus();
356         
357         boolean bError = false;
358         
359         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
360         sRealPath = sRealPath.replace('/', File.separatorChar);
361         
362         File fRealDir = new File(sRealPath);
363         
364         bError = fRealDir.mkdirs();
365         
366         if(!bError) {
367             this.fireErrorEvent("DirectoryNotCreated", "The directory " + sFullPath + " cannot be created.");
368         }
369         
370         if(bError) {
371             retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
372             retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
373         }
374
375         return retnStatus;
376     }
377
378     /* (non-Javadoc)
379      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#createShortcut(java.lang.String, java.lang.String)
380      */

381     public StatusData createShortcut(String JavaDoc sFullPath, String JavaDoc sToFullPath) {
382         VFSStatus retnStatus = new VFSStatus();
383         
384         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
385         sRealPath = sRealPath.replace('/', File.separatorChar);
386         String JavaDoc sRealToPath = this.getInitialPath() + sToFullPath;
387         sRealToPath = sRealToPath.replace('/', File.separatorChar);
388         this.fireErrorEvent("ShortcutNotCreted", "This server does not support shortcuts.");
389
390         return retnStatus;
391     }
392
393     /* (non-Javadoc)
394      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#search(com.simulacramedia.vfs.search.Query)
395      */

396     public ResourceListStatusWrapper search(Query query) {
397         return null;
398     }
399
400     /* (non-Javadoc)
401      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getVirtualFileSystemView()
402      */

403     public VirtualFileSystemView getVirtualFileSystemView() {
404         if( this.m_vfView==null ) {
405             this.m_vfView = new LocalFileSystemView();
406         }
407
408         return this.m_vfView;
409     }
410
411     /* (non-Javadoc)
412      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getVirtualFileContent(java.lang.String)
413      */

414     public byte[] getVirtualFileContent(String JavaDoc sFullPath) {
415         boolean bError = false;
416         
417         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
418         sRealPath = sRealPath.replace('/', File.separatorChar);
419         
420         byte[] content = null;
421         
422         File from = new File(sRealPath);
423         FileInputStream fi = null;
424         
425         try {
426             Long JavaDoc lLength = new Long JavaDoc(from.length());
427             content = new byte[lLength.intValue()];
428             fi = new FileInputStream(from);
429
430             fi.read(content);
431             this.setFileContentPopulated( this.m_cache.getFile(sFullPath), true );
432
433         } catch (Exception JavaDoc e) {
434             e.printStackTrace(System.out);
435             this.fireErrorEvent("ContentNotRetrieved", "The content for " + sFullPath + " could not be retrieved.");
436             bError = true;
437         } finally {
438             try {
439                 fi.close();
440             } catch (IOException ioe) {
441                 this.fireErrorEvent("ContentNotRetrieved", "The content for " + sFullPath + " could not be retrieved.");
442                 bError = true;
443                 ioe.printStackTrace(System.out);
444             }
445         }
446
447         return content;
448     }
449
450     /* (non-Javadoc)
451      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#fullyPopulateFileMetadata(com.simulacramedia.vfs.VirtualFile)
452      */

453     protected void fullyPopulateFileMetadata(VirtualFile vfFile) {
454         // NO-OP
455
}
456
457     /* (non-Javadoc)
458      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#fullyPopulateFileChildren(com.simulacramedia.vfs.VirtualFile)
459      */

460     protected void fullyPopulateFileChildren(VirtualFile vfFile) {
461         // NO-OP
462
}
463
464     /* (non-Javadoc)
465      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#synchroniseFile(com.simulacramedia.vfs.VirtualFile)
466      */

467     public StatusData synchroniseFile(VirtualFile vfFile) {
468         VFSStatus retnStatus = new VFSStatus();
469         
470         boolean bError = false;
471         
472         if(vfFile.isChanged()) {
473             String JavaDoc sFullPath = vfFile.getFullPath();
474         
475             String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
476             sRealPath = sRealPath.replace('/', File.separatorChar);
477         
478             FileOutputStream fos = null;
479         
480             try {
481                 fos = new FileOutputStream(sRealPath);
482                 fos.write(vfFile.getContent());
483             } catch (FileNotFoundException e) {
484                 bError = true;
485                 e.printStackTrace();
486             } catch (IOException e) {
487                 bError = true;
488                 e.printStackTrace();
489             } finally {
490                 try {
491                     fos.close();
492                 } catch (IOException e1) {
493                     e1.printStackTrace();
494                 }
495             }
496         }
497         
498         if(bError) {
499             retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
500             retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
501         }
502
503         return retnStatus;
504     }
505
506     /* (non-Javadoc)
507      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#sunchroniseAllFiles()
508      */

509     public StatusData synchroniseAllFiles() {
510         VFSStatus retnStatus = new VFSStatus();
511         
512         boolean bError = false;
513         
514         Iterator itor = this.m_cache.getPaths().iterator();
515         while(itor.hasNext()) {
516             String JavaDoc sFullPath = (String JavaDoc)itor.next();
517             VirtualFile vfFile = this.m_cache.getFile(sFullPath);
518             if(vfFile.isChanged()) {
519                 StatusData status = this.synchroniseFile(vfFile);
520                 retnStatus.addStatusData(status);
521                 if( !status.isOK() ) {
522                     bError = true;
523                 }
524             }
525         }
526         
527         if(bError) {
528             retnStatus.setStatusLevel(StatusData.LEVEL_ERROR);
529             retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST);
530         }
531
532         return retnStatus;
533     }
534
535     /* (non-Javadoc)
536      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#exists(java.lang.String)
537      */

538     public boolean exists(String JavaDoc sFullPath) {
539         boolean bExists = true;
540         
541         String JavaDoc sRealPath = this.getInitialPath() + sFullPath;
542         sRealPath = sRealPath.replace('/', File.separatorChar);
543         
544         File file = new File(sRealPath);
545         bExists = file.exists();
546         
547         return bExists;
548     }
549
550     /* (non-Javadoc)
551      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#refreshChildren(com.simulacramedia.vfs.VirtualFile)
552      */

553     protected void refreshChildren(VirtualFile vfFile) {
554     }
555
556     /* (non-Javadoc)
557      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#orderVirtualFileChildren(java.util.List, com.simulacramedia.vfs.VirtualFile)
558      */

559     public StatusData orderVirtualFileChildren(List aPaths, VirtualFile vfDir) {
560         VFSStatus retnStatus = new VFSStatus();
561         return retnStatus;
562     }
563
564     /* (non-Javadoc)
565      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getNewValueInstance(com.simulacramedia.vfs.metadata.PropertyInstance)
566      */

567     public ValueInstance getNewValueInstance(PropertyInstance propInst) {
568         return null;
569     }
570
571     /* (non-Javadoc)
572      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#rejectAllChanges()
573      */

574     public boolean rejectAllChanges() {
575         return false;
576     }
577
578     /* (non-Javadoc)
579      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#fullyPopulateFileAllowedMethods(com.simulacramedia.vfs.VirtualFile)
580      */

581     protected void fullyPopulateFileAllowedMethods(VirtualFile vfFile) {
582     }
583
584     /* (non-Javadoc)
585      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#currentUserResourcePath()
586      */

587     public String JavaDoc currentUserResourcePath(AuthInfo authInfo) {
588         return null;
589     }
590
591     /* (non-Javadoc)
592      * @see com.simulacramedia.vfs.AbstractVirtualFileSystem#getChangedVirtualFiles()
593      */

594     public List getChangedVirtualFiles() {
595         return new ArrayList();
596     }
597
598     /* (non-Javadoc)
599      * @see org.openharmonise.vfs.AbstractVirtualFileSystem#getPropertyVirtualFile(java.lang.String)
600      */

601     public VirtualFile getPropertyVirtualFile(String JavaDoc sPropPath) {
602         return null;
603     }
604
605 }
606
Popular Tags