KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > AbstractVirtualFileSystem


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.vfs;
20
21 import java.net.URI JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26
27 import org.openharmonise.vfs.authentication.*;
28 import org.openharmonise.vfs.metadata.*;
29 import org.openharmonise.vfs.search.*;
30 import org.openharmonise.vfs.status.*;
31
32
33 /**
34  * This is the abstract class from which all Virtual File Systems
35  * implementations should extend.
36  *
37  * @author Matthew Large
38  * @version $Revision: 1.1 $
39  *
40  */

41 public abstract class AbstractVirtualFileSystem {
42
43     /**
44      * Path separator used by the underlying file system, e.g. '\' for windows file system.
45      */

46     private static String JavaDoc PATH_SEPATATOR = "\\";
47
48     /**
49      * Full URI for the underlying file system.
50      */

51     private URI JavaDoc m_uri = null;
52     
53     /**
54      * The initial path for the underlying file system, e.g. for 'http://localhost/temp/root' this would be '/temp/root/'.
55      */

56     protected String JavaDoc m_sInitialPath = null;
57     /**
58      * The root path segment for the underlying file system, e.g. for 'http://localhost/temp/root' this would be 'root'.
59      */

60     protected String JavaDoc m_sRootPathSegment = null;
61     
62     /**
63      * Authentication information to use to gain access to the underlying file system.
64      */

65     private AuthInfo m_authInfo = null;
66     
67     /**
68      * An store of authentication information from which the authentication information to use to gain access to the underlying file system.
69      */

70     private AbstractAuthenticationStore m_authStore = null;
71     
72     /**
73      * The error listeners for this virtual file system.
74      */

75     private ArrayList JavaDoc m_errorListeners = new ArrayList JavaDoc(3);
76
77     /**
78      * @param uri URI to location file system to be connected to
79      */

80     public AbstractVirtualFileSystem(URI JavaDoc uri) {
81         super();
82         m_uri = uri;
83         this.pathSetup(uri.getPath());
84     }
85
86     /**
87      * @param uri URI to location file system to be connected to
88      * @param authInfo Authentication information
89      */

90     public AbstractVirtualFileSystem(URI JavaDoc uri, AuthInfo authInfo) {
91         super();
92         m_uri = uri;
93         m_authInfo = authInfo;
94         this.pathSetup(uri.getPath());
95     }
96
97     /**
98      * @param uri URI to location file system to be connected to
99      * @param authStore Authentication Store from which to lookup authentication information
100      */

101     public AbstractVirtualFileSystem(URI JavaDoc uri, AbstractAuthenticationStore authStore) {
102         super();
103         m_uri = uri;
104         m_authStore = authStore;
105         this.pathSetup(uri.getPath());
106     }
107     
108     /**
109      * Utility method to work out the parent path of passed in path
110      *
111      * @param sFullPath
112      * @return
113      */

114     protected String JavaDoc getParentPath(String JavaDoc sFullPath) {
115         return sFullPath.substring(0, sFullPath.lastIndexOf('/'));
116     }
117     
118     /**
119      * Returns the initial path for the virtual file system, e.g.
120      * if the uri for the virtual file system was http://localhost/temp/root
121      * the returned path would be '/temp/root'
122      *
123      * @return Initial path
124      */

125     public String JavaDoc getInitialPath() {
126         return this.m_sInitialPath;
127     }
128     
129     /**
130      * Returns the root path segment for the virtual file system, e.g.
131      * if the uri for the virtual file system was http://localhost/temp/root
132      * the returned path would be 'root'
133      *
134      * @return
135      */

136     public String JavaDoc getRootPathSegment() {
137         return this.m_sRootPathSegment;
138     }
139     
140     /**
141      * Internal method to take the path from the setup uri
142      * and pull out all the required information, e.g. initial path and
143      * root segment
144      *
145      * @param sPath
146      */

147     private void pathSetup(String JavaDoc sPath) {
148         
149         StringTokenizer JavaDoc sTok = new StringTokenizer JavaDoc(sPath, "/", false);
150         
151         String JavaDoc sInitialPath = "";
152         ArrayList JavaDoc aPathSegments = new ArrayList JavaDoc(5);
153         
154         while(sTok.hasMoreElements()) {
155             aPathSegments.add(sTok.nextElement());
156         }
157         Iterator JavaDoc itor = aPathSegments.iterator();
158         String JavaDoc sPathSegment = null;
159         while(itor.hasNext()) {
160             sPathSegment = (String JavaDoc)itor.next();
161             if( itor.hasNext()) {
162                 sInitialPath = sInitialPath + "/" + sPathSegment;
163             }
164         }
165         this.m_sInitialPath = sInitialPath;
166         this.m_sRootPathSegment = sPathSegment;
167     }
168     
169     /**
170      * Returns authentication information for the current user for this file system.
171      *
172      * @return Authentication information, null if no authentication information can be found
173      */

174     public AuthInfo getAuthentication() {
175         AuthInfo authInfo = null;
176         
177         if( this.m_authInfo!=null ) {
178             authInfo = this.m_authInfo;
179         } else if(this.m_authStore!=null) {
180             this.m_authInfo = this.m_authStore.getAuthentication(this.m_uri);
181             authInfo = this.m_authInfo;
182         } else {
183             authInfo = new AuthInfo();
184             authInfo.setUsername("simulacra");
185             this.m_authInfo = authInfo;
186         }
187         
188         return authInfo;
189     }
190     
191     /**
192      * Adds an error listener to the virtual file system
193      *
194      * @param error listener to be added
195      */

196     public void addErrorListener( VirtualFileSystemErrorListener listener) {
197         this.m_errorListeners.add(listener);
198     }
199     
200     /**
201      * Creates and fires an error event to all the error listeners that
202      * have been added to this virtual file system
203      *
204      * @param sMessage Message for the error event
205      * @param sDetails Details of the error event
206      */

207     protected void fireErrorEvent(String JavaDoc sMessage, String JavaDoc sDetails) {
208         ErrorEvent error = new ErrorEvent(sMessage, sDetails);
209         Iterator JavaDoc itor = this.m_errorListeners.iterator();
210         while(itor.hasNext()) {
211             ((VirtualFileSystemErrorListener)itor.next()).error(error);
212         }
213     }
214     
215     /**
216      * Utility method to return a List object of path segements
217      * for a given path
218      *
219      * @param sPath Path to split into segments
220      * @param sPathSeparator The separator for the path, e.g. '/'
221      * @return List of path segment Strings
222      */

223     public static List JavaDoc getPathSegments(String JavaDoc sPath, String JavaDoc sPathSeparator) {
224         ArrayList JavaDoc aSegments = new ArrayList JavaDoc(7);
225         
226         StringTokenizer JavaDoc sTok = new StringTokenizer JavaDoc(sPath, sPathSeparator, false);
227         while(sTok.hasMoreElements()) {
228             String JavaDoc sTemp = (String JavaDoc)sTok.nextElement();
229             aSegments.add(sTemp);
230         }
231         
232         return aSegments;
233     }
234     
235     /**
236      * Returns the URI of the file system connected to
237      *
238      * @return URI of the file system
239      */

240     public URI JavaDoc getURI() {
241         return this.m_uri;
242     }
243     
244     /**
245      * Returns the methods that are supported by an implementation
246      *
247      * @return Comma separated list of methods that are supported by an implementation
248      */

249     public abstract List JavaDoc getOptions();
250     
251     /**
252      * This method will return either the Virtual File that was requested by the path
253      * or a new blank file initialised to that path
254      *
255      * @param sFullPath Full path to the requested file
256      * @return VirtualFile
257      */

258     public abstract ResourceStatusWrapper getVirtualFile(String JavaDoc sFullPath);
259
260     /**
261      * Adds a new virtual file to the virtual file system
262      *
263      * @param sPath Full path for the new virtual file file
264      * @param vfFile Virtual file to be added to the virtual file system
265      * @return The virtual file that was added to the virtual file system
266      */

267     public abstract ResourceStatusWrapper addVirtualFile(String JavaDoc sPath, VirtualFile vfFile);
268     
269     /**
270      * Clears the children of a collection
271      *
272      * @param vfFile Collection to have its children cleared
273      */

274     protected void clearVirtualFileChildren(VirtualFile vfFile) {
275         vfFile.clearChildren();
276     }
277     
278     /**
279      * Sets the order of the children of a collection
280      *
281      * @param aPaths Ordered list of paths of the children of a collection
282      * @param vfDir Collection to have its children ordered
283      * @return true if the method was successful
284      */

285     public abstract StatusData orderVirtualFileChildren(List JavaDoc aPaths, VirtualFile vfDir);
286     
287     /**
288      * Moves a Virtual File specified by the first path to the second path
289      *
290      * @param sFromFullPath Path of file to move
291      * @param sToFullPath Path to move file to
292      * @return true if the method was successful
293      */

294     public abstract StatusData moveVirtualFile(String JavaDoc sFromFullPath, String JavaDoc sToFullPath);
295     
296     /**
297      * Copies a Virtual File specified by the first path to the second path
298      *
299      * @param sFromFullPath Path of file to copy
300      * @param sToFullPath Path to copy file to
301      * @return true if the method was successful
302      */

303     public abstract StatusData copyVirtualFile(String JavaDoc sFromFullPath, String JavaDoc sToFullPath);
304     
305     /**
306      * Deletes a Virtual File
307      *
308      * @param sFullPath Path of file to delete
309      * @return true if the method was successful
310      */

311     public abstract StatusData deleteVirtualFile(String JavaDoc sFullPath);
312     
313     /**
314      * Locks a Virtual File
315      *
316      * @param sFullPath Path of file to lock
317      * @return true if the method was successful
318      */

319     public abstract StatusData lockVirtualFile(String JavaDoc sFullPath);
320     
321     /**
322      * Unlocks a Virtual File
323      *
324      * @param sFullPath Path of file to unlock
325      * @return true if the method was successful
326      */

327     public abstract StatusData unlockVirtualFile(String JavaDoc sFullPath);
328     
329     /**
330      * Creates a new Virtual Directory
331      *
332      * @param sFullPath Path of directory to be created
333      * @return true if the method was successful
334      */

335     public abstract StatusData createVirtualDirectory(String JavaDoc sFullPath);
336     
337     /**
338      * Creates a shortcut to a Virtual File
339      *
340      * @param sFullPath Path of shortcut to be created
341      * @param sToFullPath Path of file that the shortcut is to
342      * @return true if the method was successful
343      */

344     public abstract StatusData createShortcut(String JavaDoc sFullPath, String JavaDoc sToFullPath);
345     
346     /**
347      * Performs a search of the file system using a given query, returning a List of
348      * Virtual File objects
349      *
350      * @param query Query to use as conditions on the search
351      * @return List of Virtual File objects, empty List is none where found
352      */

353     public abstract ResourceListStatusWrapper search( Query query);
354     
355     /**
356      * Checks if the given path actually exists within this virtual file
357      * system
358      *
359      * @param sFullPath Path to check
360      * @return true if the path exists within this virtual file system
361      */

362     public abstract boolean exists(String JavaDoc sFullPath);
363     
364     /**
365      * Access to handler for GUI elements that view the file system, e.g. file icons,
366      * display names etc
367      *
368      * @return View of file system
369      */

370     public abstract VirtualFileSystemView getVirtualFileSystemView();
371     
372     /**
373      * Return the content a path in a byte array
374      *
375      * @param sFullPath Path to get content for
376      * @return byte array of content for the path
377      */

378     public abstract byte[] getVirtualFileContent(String JavaDoc sFullPath);
379     
380     /**
381      * Populates all the metadata for a given virtual file, use if a
382      * virtual file was only partially populated for instance on its
383      * initial fetch
384      *
385      * @param vfFile Virtual file to have its metadata fully populated
386      */

387     protected abstract void fullyPopulateFileMetadata(VirtualFile vfFile);
388     
389     /**
390      * Populates the complete list of children for a collection, use if
391      * only the collection itself was fetched and not its children
392      *
393      * @param vfFile Collection to have its children fully populated
394      */

395     protected abstract void fullyPopulateFileChildren(VirtualFile vfFile);
396     
397     /**
398      * Submits a changed virtual file back to the virtual file system,
399      * the changes might be in the content or the metadata
400      *
401      * @param vfFile Virtual file to have its changes submitted
402      * @return true if the method was successful
403      */

404     public abstract StatusData synchroniseFile(VirtualFile vfFile);
405     
406     /**
407      * Submits all the changed virtual files in the cache back to the
408      * virtual file system, the changes might be in the content or the
409      * metadata
410      *
411      * @return true if the method was successful
412      */

413     public abstract StatusData synchroniseAllFiles();
414     
415     /**
416      * Rejects all the changes to virtual files in the cache. These
417      * virtual files will be removed from the cache to be replaced with
418      * the current versions the next time they are requested using the
419      * {@link #getVirtualFile(String)} method
420      *
421      * @return true if the method was successful
422      */

423     public abstract boolean rejectAllChanges();
424     
425     /**
426      * This method allows access to the VirtualFile {@link VirtualFile#isContentPopulated()} method.
427      *
428      * @param vfFile Virtual file to check
429      * @return true if the file's content is fully populated
430      */

431     protected boolean isFileContentPopulated(VirtualFile vfFile) {
432         return vfFile.isContentPopulated();
433     }
434     
435     /**
436      * This method allows access to the VirtualFile {@link VirtualFile#setContentPopulated(boolean)} method
437      *
438      * @param vfFile Virtual file to check
439      * @param bContentPopulated Set to true to set that the file's content is fully populated
440      */

441     protected void setFileContentPopulated(VirtualFile vfFile, boolean bContentPopulated) {
442         vfFile.setContentPopulated(bContentPopulated);
443     }
444     
445     /**
446      * This method allows access to the VirtualFile {@link VirtualFile#isChildrenPopulated()} method
447      *
448      * @param vfFile Virtual file to check
449      * @return true if the file's children are fully populated
450      */

451     protected boolean isFileChildrenPopulated(VirtualFile vfFile) {
452         return vfFile.isChildrenPopulated();
453     }
454     
455     /**
456      * This method allows access to the VirtualFile {@link VirtualFile#setChildrenPopulated(boolean)} method
457      *
458      * @param vfFile Virtual file to check
459      * @param bChildrenPopulated Set to true to set that the file's children are fully populated
460      */

461     protected void setFileChildrenPopulated(VirtualFile vfFile, boolean bChildrenPopulated) {
462         vfFile.setChildrenPopulated(bChildrenPopulated);
463     }
464     
465     /**
466      * This method allows access to the VirtualFile {@link VirtualFile#isMetadataPopulated()} method
467      *
468      * @param vfFile Virtual file to check
469      * @return true if the file's metadata is fully populated
470      */

471     protected boolean isFileMetadataPopulated(VirtualFile vfFile) {
472         return vfFile.isMetadataPopulated();
473     }
474     
475     /**
476      * This method allows access to the VirtualFile {@link VirtualFile#setMetadataPopulated(boolean)} method
477      *
478      * @param vfFile Virtual file to check
479      * @param bMetadataPopulated Set to true to set that the file's metadata is fully populated
480      */

481     protected void setFileMetadataPopulated(VirtualFile vfFile, boolean bMetadataPopulated) {
482         vfFile.setMetadataPopulated(bMetadataPopulated);
483     }
484
485     /**
486      * This method allows access to the VirtualFile {@link VirtualFile#setVersionable(boolean)} method
487      *
488      * @param vfFile Virtual file to check
489      * @param bVersionable Set to true to set that the file is versionable
490      */

491     protected void setFileIsVersionable(VirtualFile vfFile, boolean bVersionable) {
492         vfFile.setVersionable(bVersionable);
493     }
494
495     /**
496      * This method allows access to the VirtualFile {@link VirtualFile#setState(String)} method
497      *
498      * @param vfFile Virtual file to check
499      * @param sState Stage to set on the virtual file
500      */

501     protected void setFileState(VirtualFile vfFile, String JavaDoc sState) {
502         vfFile.setState(sState);
503     }
504
505     /**
506      * This method allows access to the VirtualFile {@link VirtualFile#addAllowedMethods(String)} method
507      *
508      * @param vfFile Virtual file to check
509      * @param sMethod Allowed method to add to the virtual file
510      */

511     protected void addFileAllowedMethod(VirtualFile vfFile, String JavaDoc sMethod) {
512         vfFile.addAllowedMethods(sMethod);
513     }
514     
515     /**
516      * Populates the allowed method list for a given virtual file
517      *
518      * @param vfFile Virtual file to have its allowed methods populated
519      */

520     protected abstract void fullyPopulateFileAllowedMethods(VirtualFile vfFile);
521     
522     /**
523      * This method allows access to the VirtualFile {@link VirtualFile#clearAllowedMethods()} method
524      *
525      * @param vfFile Virtual file to check
526      */

527     protected void clearFileAllowedMethods(VirtualFile vfFile) {
528         vfFile.clearAllowedMethods();
529     }
530
531     /**
532      * Refreshes the children of a given collection, any children that do
533      * not have current changes will be cleared from the cache and
534      * property definitions for all children will be updated if required
535      *
536      * @param vfFile Collection to have its children refreshed
537      */

538     protected abstract void refreshChildren(VirtualFile vfFile);
539
540     /**
541      * This method allows access to the VirtualFile {@link VirtualFile#discardChanges()} method
542      *
543      * @param vfFile Path of the virtual file to check
544      */

545     public void discardFileChanges(String JavaDoc sPath) {
546         VirtualFile vfFile = this.getVirtualFile(sPath).getResource();
547         vfFile.discardChanges();
548     }
549     
550     /**
551      * This method allows access to the VirtualFile {@link VirtualFile#setOrderableDirectory(boolean)} method
552      * for a collection
553      *
554      * @param vfFile Path of the virtual file to check
555      * @param bOrderableDirectory True to set that the collection is orderable
556      */

557     protected void setOrderableDirectory(VirtualFile vfFile, boolean bOrderableDirectory) {
558         vfFile.setOrderableDirectory(bOrderableDirectory);
559     }
560     
561     /**
562      * This method allows access to the VirtualFile {@link VirtualFile#clearAllProperties()} method
563      *
564      * @param vfFile Virtual file to check
565      */

566     protected void clearAllFileProperties(VirtualFile vfFile) {
567         vfFile.clearAllProperties();
568     }
569     
570     /**
571      * Gets a new {@link ValueInstance}, of the correct concrete type, for a given
572      * {@link PropertyInstance}
573      *
574      * @param propInst Property instance to get a new value for
575      * @return New value
576      */

577     public abstract ValueInstance getNewValueInstance(PropertyInstance propInst);
578
579     /**
580      * Returns a path to the principal's virtual file for the user in the
581      * {@link AuthInfo} object
582      *
583      * @param authInfo Authentication information to use to find the user
584      * @return Path to the principal's virtual file
585      */

586     public abstract String JavaDoc currentUserResourcePath(AuthInfo authInfo);
587
588     public abstract List JavaDoc getChangedVirtualFiles();
589     
590     public abstract VirtualFile getPropertyVirtualFile(String JavaDoc sPropPath);
591
592 }
593
Popular Tags