KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
22
23 import org.openharmonise.vfs.event.*;
24 import org.openharmonise.vfs.metadata.*;
25 import org.openharmonise.vfs.status.*;
26
27
28 /**
29  * A representation of a file in a Virtual File System.
30  *
31  * @author Matthew Large
32  * @version $Revision: 1.1 $
33  *
34  */

35 public class VirtualFile {
36
37     /**
38      * The virtual file system that this virtual file is attached to.
39      */

40     protected AbstractVirtualFileSystem m_vfs = null;
41     
42     /**
43      * The children of this virtual file, if it is a collection.
44      */

45     private ArrayList m_children = new ArrayList(5);
46     
47     /**
48      * The property instances attached to this virtual file.
49      */

50     private HashMap m_properties = new HashMap(5);
51
52     /**
53      * The path to the parent collection of this virtual file, e.g. if the full path is '/webdav/Documents/doc1' then this will be '/webdav/Documents'.
54      */

55     private String JavaDoc m_sFilePath;
56     
57     /**
58      * The name of this virtual file, e.g. if the full path is '/webdav/Documents/doc1' then this will be 'doc1'.
59      */

60     private String JavaDoc m_sFileName;
61     
62     /**
63      * The complete path of this virtual file.
64      */

65     private String JavaDoc m_sFullPath;
66     
67     /**
68      * The owner of the exclusive lock on this virtual file, if there is one. This should be the full path to the virtual file for the principal representing the user, if the underlying file system supports this.
69      */

70     private String JavaDoc m_sLockOwner = null;
71     
72     /**
73      * The unique lock token for the exclusive lock on this virtual file, if there is one.
74      */

75     private String JavaDoc m_sLockToken = null;
76     
77     /**
78      * true if the content for this virtual file is populated.
79      */

80     private boolean m_bContentPopulated = false;
81     
82     /**
83      * true if the children list for this virtual file, if it is a collection, is populated.
84      */

85     private boolean m_bChildrenPopulated = false;
86     
87     /**
88      * true if the metadata for this virtual file is populated.
89      */

90     private boolean m_bMetadataPopulated = false;
91     
92     /**
93      * true if the content for this virtual file has changed since it was populated.
94      */

95     private boolean m_bContentChanged = false;
96     
97     /**
98      * true if the metedata for this virtual file has changed since it was populated.
99      */

100     private boolean m_bMetadataChanged = false;
101
102     /**
103      * true if this virtual file is a collection.
104      */

105     private boolean m_bIsDirectory = false;
106     
107     /**
108      * The content for this virtual file.
109      */

110     private byte[] m_content = null;
111     
112     /**
113      * The list of {@link VirtualFileListener} objects attached to this virtual file.
114      */

115     private ArrayList m_listeners = new ArrayList();
116     
117     /**
118      * The state of a pending virtual file.
119      */

120     public static String JavaDoc STATE_PENDING = "PENDING";
121     
122     /**
123      * The state of a live virtual file.
124      */

125     public static String JavaDoc STATE_LIVE = "LIVE";
126     
127     /**
128      * The state of a historical virtual file.
129      */

130     public static String JavaDoc STATE_HISTORICAL = "HISTORICAL";
131     
132     /**
133      * The current state of this virtual file.
134      */

135     private String JavaDoc m_sState = VirtualFile.STATE_LIVE;
136
137     /**
138      * true if this virtual file is versionable.
139      */

140     private boolean m_bVersionable = false;
141     
142     /**
143      * true if this virtual file is orderable.
144      */

145     private boolean m_bOrderableDirectory = false;
146     
147     /**
148      * List of methods allowed on this virtual file.
149      */

150     private ArrayList m_allowedMethods = new ArrayList();
151     
152     /**
153      * Timestamp of the last time the allow methods list was populated.
154      */

155     private Date m_dtAllowedMethodsTimeStamp = null;
156     
157     /**
158      * Submit method identifier for allowed methods.
159      */

160     public static String JavaDoc METHOD_SYNC = "SYNC";
161     
162     /**
163      * Delete method identifier for allowed methods.
164      */

165     public static String JavaDoc METHOD_DELETE = "DELETE";
166     
167     /**
168      * Move method identifier for allowed methods.
169      */

170     public static String JavaDoc METHOD_MOVE = "MOVE";
171     
172     /**
173      * Copy method identifier for allowed methods.
174      */

175     public static String JavaDoc METHOD_COPY = "COPY";
176     
177     /**
178      * Create collection method identifier for allowed methods.
179      */

180     public static String JavaDoc METHOD_MKDIR = "MKDIR";
181     
182     /**
183      * Lock method identifier for allowed methods.
184      */

185     public static String JavaDoc METHOD_LOCK = "LOCK";
186     
187     /**
188      * Unlock method identifier for allowed methods.
189      */

190     public static String JavaDoc METHOD_UNLOCK = "UNLOCK";
191     
192     /**
193      * Create shortcut method identifier for allowed methods.
194      */

195     public static String JavaDoc METHOD_SHORTCUT = "SHORTCUT";
196     
197     /**
198      * Set children order method identifier for allowed methods.
199      */

200     public static String JavaDoc METHOD_SET_CHILD_ORDER = "SET_CHILD_ORDER";
201     
202     /**
203      * Checkin method identifier for allowed methods.
204      */

205     public static String JavaDoc METHOD_CHECKIN = "CHECKIN";
206     
207     /**
208      * Tag method identifier for allowed methods.
209      */

210     public static String JavaDoc METHOD_TAG = "TAG";
211     
212     /**
213      * Order method identifier for allowed methods.
214      */

215     public static String JavaDoc METHOD_ORDER = "ORDER";
216     
217     /**
218      * Reject method identifier for allowed methods.
219      */

220     public static String JavaDoc METHOD_REJECT = "REJECT";
221     
222     /**
223      * Checkout method identifier for allowed methods.
224      */

225     public static String JavaDoc METHOD_CHECKOUT = "CHECKOUT";
226     
227     /**
228      * Checkout method identifier for allowed methods.
229      */

230     public static String JavaDoc METHOD_RENAME = "RENAME";
231     
232     /**
233      * Add method identifier for allowed methods.
234      */

235     public static String JavaDoc METHOD_ADD = "ADD";
236     
237     /**
238      * Get method identifier for allowed methods.
239      */

240     public static String JavaDoc METHOD_GET = "GET";
241     
242     /**
243      * Search method identifier for allowed methods.
244      */

245     public static String JavaDoc METHOD_SEARCH = "SEARCH";
246     
247     public static int EVENT_NOTHING = 0;
248     
249     public static int EVENT_ADDITION = 1;
250     
251     public static int EVENT_REMOVAL = 2;
252     
253     /**
254      * true if the lock information for this virtual file is populated.
255      */

256     private boolean m_bPopulateLocked = false;
257
258     private boolean m_bIsVirtualDirectory = false;
259
260     /**
261      * Constructs a Virtual File.
262      *
263      * @param sFullPath FullPath of the file
264      */

265     public VirtualFile(String JavaDoc sFullPath) {
266         super();
267         this.setFullPath(sFullPath);
268     }
269
270     /**
271      * Constructs a Virtual File.
272      */

273     public VirtualFile() {
274         super();
275     }
276     
277     /**
278      * Discards any changes that may have been made to this virtual file
279      * since it was populate. The any information subsequently requested
280      * will be repopulated from the underlying file system.
281      *
282      */

283     protected void discardChanges() {
284         if(this.isChanged()) {
285             this.clearFile();
286         }
287     }
288     
289     /**
290      * Clears all of the information about this virtual file and resets
291      * all of the populated booleans to false.
292      *
293      */

294     public void clearFile() {
295         m_bContentPopulated = false;
296         m_bChildrenPopulated = false;
297         m_bMetadataPopulated = false;
298         this.m_children.clear();
299         this.m_content = null;
300         this.m_properties.clear();
301         this.m_bContentChanged=false;
302         this.m_bMetadataChanged=false;
303         this.fireVirtualFileEvent(VirtualFileEvent.FILE_CHANGES_DISCARDED);
304     }
305     
306     /**
307      * Checks if this virtual file's lock information has been populated.
308      *
309      * @return true if this virtual file's lock information has been populated
310      */

311     public boolean isPopulateLocked() {
312         return this.m_bPopulateLocked;
313     }
314     
315     /**
316      * Sets the lock populated information for this virtual file.
317      *
318      * @param bPopulateLocked Set to true to set that this virtual file's lock information has been populated.
319      */

320     public void setPopulateLocked(boolean bPopulateLocked) {
321         this.m_bPopulateLocked = bPopulateLocked;
322     }
323     
324     /**
325      * Sets the full path of this virtual file.
326      *
327      * @param sFullPath The full path for this virtual file
328      */

329     public void setFullPath(String JavaDoc sFullPath) {
330         String JavaDoc sFilePath = null;
331         String JavaDoc sFileName = null;
332         if( sFullPath.endsWith( "/" ) ) {
333             String JavaDoc sTemp = sFullPath;
334             sTemp = sTemp.substring(0, sTemp.length()-1);
335             sFilePath = sTemp.substring(0, sTemp.lastIndexOf( "/".charAt(0) ));
336             sFileName = sTemp.substring( sTemp.lastIndexOf( "/".charAt(0)) );
337         } else {
338             String JavaDoc sTemp = sFullPath;
339             sFilePath = sTemp.substring(0, sTemp.lastIndexOf( "/".charAt(0) ));
340             sFileName = sTemp.substring( sTemp.lastIndexOf( "/".charAt(0)) );
341         }
342         this.m_sFilePath = sFilePath;
343         if( sFullPath.endsWith("/")) {
344             sFullPath = sFullPath.substring(0, sFullPath.length()-1);
345         }
346         this.m_sFullPath = sFullPath;
347         if( sFileName.endsWith("/")) {
348             sFileName = sFileName.substring(0, sFileName.length()-1);
349         }
350         if( sFileName.startsWith("/") ) {
351             sFileName = sFileName.substring(1, sFileName.length());
352         }
353         this.m_sFileName = sFileName;
354     }
355     
356     /**
357      * Adds a path to this virtual file's children list.
358      *
359      * @param sURI Path to add to this virtual file's children list
360      */

361     public void addChild(String JavaDoc sURI) {
362         this.m_children.add(sURI);
363         //this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
364
}
365     
366     /**
367      * Removes a path from this virtual file's children list.
368      *
369      * @param sURI Path to remove from this virtual file's children list
370      */

371     public void removeChild(String JavaDoc sURI) {
372         this.m_children.remove(sURI);
373     }
374     
375     /**
376      * Checks if this virtual file has a path as one of its children.
377      *
378      * @param sURI Path to check
379      * @return true if the given path is a child of this virtual file
380      */

381     public boolean hasChild(String JavaDoc sURI) {
382         return this.m_children.contains(sURI);
383     }
384     
385     /**
386      * Returns a list of the paths that are children of this virtual file.
387      *
388      * @return List of virtual file paths
389      */

390     public List getChildren() {
391         if( this.isDirectory() && !this.m_bChildrenPopulated) {
392             this.m_vfs.fullyPopulateFileChildren(this);
393         }
394         return (List)this.m_children.clone();
395     }
396     
397     /**
398      * Sets the order of the children of this virtual file. This method
399      * will fail if this is not a collection which is orderable or if the
400      * contents of the suplied list of paths does not match the list of
401      * child paths for this collection.
402      *
403      * @param children List of paths in the order to be set
404      * @return true if the method was successful
405      */

406     public StatusData setChildrenOrder(List children) {
407         VFSStatus retnStatus = new VFSStatus();
408         retnStatus.setMethodName(METHOD_ORDER);
409         
410         if(this.isDirectory() && this.isOrderableDirectory() && this.m_children.containsAll(children) && children.containsAll(this.m_children)) {
411             this.m_children = (ArrayList) children;
412             StatusData vfsStatus = this.m_vfs.orderVirtualFileChildren(children, this);
413             retnStatus.addStatusData(vfsStatus);
414             this.m_children.clear();
415             this.m_bChildrenPopulated = false;
416             this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
417         }
418         
419         return retnStatus;
420     }
421     
422     /**
423      * Adds a {@link PropertyInstance} to the metadata for this vitual
424      * file.
425      *
426      * @param prop {@link PropertyInstance} to add to this virtual file
427      */

428     public void addProperty(PropertyInstance prop) {
429         this.m_properties.put( prop.getNamespaceURI() + "#" + prop.getName(), prop );
430         prop.setVirtualFile(this);
431     }
432     
433     /**
434      * Clears all the metadata for this virtual file.
435      *
436      */

437     protected void clearAllProperties() {
438         this.m_properties.clear();
439     }
440     
441     /**
442      * Removes a {@link PropertyInstance} from the metadata for this
443      * virtual file.
444      *
445      * @param sNamespaceURI Namespace of the {@link PropertyInstance} to be removed
446      * @param sName Name of the {@link PropertyInstance} to be removed
447      */

448     public void removeProperty(String JavaDoc sNamespaceURI, String JavaDoc sName) {
449         this.m_properties.remove(sNamespaceURI + "#" + sName);
450     }
451     
452     /**
453      * Returns a {@link PropertyInstance} from the metadata for this
454      * virtual file which matches the given namespace and name.
455      *
456      * @param sNamespaceURI Namespace of the {@link PropertyInstance} to be returned
457      * @param sName Name of the {@link PropertyInstance} to be returned
458      * @return null if no {@link PropertyInstance} was found in this virtual file's metadata matching the given namespace and name
459      */

460     public PropertyInstance getProperty(String JavaDoc sNamespaceURI, String JavaDoc sName) {
461         PropertyInstance prop = (PropertyInstance)this.m_properties.get(sNamespaceURI + "#" + sName);
462         
463         if( !this.isMetadataPopulated() && prop==null && this.m_vfs!=null ) {
464             this.m_vfs.fullyPopulateFileMetadata(this);
465             prop = (PropertyInstance)this.m_properties.get(sNamespaceURI + "#" + sName);
466         }
467          
468         return prop;
469     }
470     
471     /**
472      * Returns a list of all the {@link PropertyInstance} objects in this
473      * virtual file's metadata.
474      *
475      * @return List of {@link PropertyInstance} objects
476      */

477     public List getProperties() {
478         if(!this.m_bMetadataPopulated) {
479             this.m_vfs.fullyPopulateFileMetadata(this);
480         }
481         return new ArrayList(this.m_properties.values());
482     }
483     
484     /**
485      * Gives the file a hook to the file system it belongs to.
486      *
487      * @param vfs Virtual file system that the file belongs to
488      */

489     public void setVFS(AbstractVirtualFileSystem vfs) {
490         this.m_vfs = vfs;
491     }
492     
493     /**
494      * Returns the {@link AbstractVirtualFileSystem} that this virtual file
495      * belongs to.
496      *
497      * @return Virtual file system that this virtual file belongs to
498      */

499     public AbstractVirtualFileSystem getVFS() {
500         return this.m_vfs;
501     }
502
503     /**
504      * Checks if the file has any listeners.
505      *
506      * @return true if the file has any listeners
507      */

508     public boolean hasVirtualFileListeners() {
509         return this.m_listeners.size()>0;
510     }
511
512     /**
513      * Checks if the in memory file has changed in anyway since it either came from
514      * the Virtual File System or since it was last synchronized.
515      *
516      * @return true if the file has changed
517      */

518     public boolean isChanged() {
519         return this.m_bContentChanged || this.m_bMetadataChanged;
520     }
521     
522     /**
523      * Checks if the content for this virtual file has changes since it
524      * was last populated.
525      *
526      * @return true if the content has changed
527      */

528     public boolean isContentChanged() {
529         return this.m_bContentChanged;
530     }
531     
532     /**
533      * Checks if the metadata for this virtual file has changed since it
534      * was last populated.
535      *
536      * @return true if the metadata has changed
537      */

538     public boolean isMetadataChanged() {
539         return this.m_bMetadataChanged;
540     }
541
542     /**
543      * Checks if the file is fully populated, to allow for lazy partial instantiation.
544      *
545      * @return true if the file is fully populated
546      */

547     public boolean isFullyPopulated() {
548         return this.m_bChildrenPopulated && this.m_bContentPopulated && this.m_bMetadataPopulated;
549     }
550     
551     /**
552      * Checks if the list of children for this virtual file has been
553      * populated.
554      *
555      * @return true if the list of children has been populated
556      */

557     protected boolean isChildrenPopulated() {
558         return this.m_bChildrenPopulated;
559     }
560     
561     /**
562      * Sets that the list of children for this virtual file has been
563      * populated.
564      *
565      * @param bChildrenPopulated Set to true to set that the list of children has been populated
566      */

567     protected void setChildrenPopulated(boolean bChildrenPopulated) {
568         this.m_bChildrenPopulated = bChildrenPopulated;
569     }
570     
571     /**
572      * Checks if the content for this virtual file has been populated.
573      *
574      * @return true if the content has been populated
575      */

576     protected boolean isContentPopulated() {
577         return this.m_bContentPopulated;
578     }
579     
580     /**
581      * Sets that the content for this virtual file has been populated.
582      *
583      * @param bContentPopulated Set to true to set that the content has been populated
584      */

585     protected void setContentPopulated(boolean bContentPopulated) {
586         this.m_bContentPopulated = bContentPopulated;
587     }
588     
589     /**
590      * Checks if the metadata for this virtual file has been populated.
591      *
592      * @return true if the metadata has been populated
593      */

594     protected boolean isMetadataPopulated() {
595         return this.m_bMetadataPopulated;
596     }
597     
598     /**
599      * Sets that the metadata for this virtual file has been populated.
600      *
601      * @param bMetadataPopulated Set to true to set that the metadata has been populated
602      */

603     protected void setMetadataPopulated(boolean bMetadataPopulated) {
604         this.m_bMetadataPopulated = bMetadataPopulated;
605     }
606
607     /**
608      * Gets the full path of the file.
609      *
610      * @return Fullpath of the file
611      */

612     public String JavaDoc getFullPath() {
613         return this.m_sFullPath;
614     }
615     
616     /**
617      * Copies the file to a new path with a new filename.
618      *
619      * @param sNewPath Path to copy to
620      * @param sNewFilename Filename to give the new copy
621      * @return true if the method was successful
622      */

623     public StatusData copy(String JavaDoc sNewPath, String JavaDoc sNewFilename) {
624         VFSStatus retnStatus = new VFSStatus();
625         retnStatus.setMethodName(METHOD_COPY);
626         
627         String JavaDoc sOldPath = this.getFullPath();
628         VirtualFile vfOldParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
629         VirtualFile vfNewParent = this.m_vfs.getVirtualFile(sNewPath).getResource();
630         StatusData vfsStatus = this.m_vfs.copyVirtualFile(this.getFullPath(), sNewPath + "/" + sNewFilename);
631         retnStatus.addStatusData(vfsStatus);
632         if(retnStatus.isOK()) {
633             String JavaDoc sNewFilePath = sNewPath + "/" + this.getFileName();
634             this.fireVirtualFileEvent(VirtualFileEvent.FILE_COPIED);
635             if(!sNewPath.equals(this.getFilePath())) {
636                 vfOldParent.removeChild(sOldPath);
637                 vfOldParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
638             }
639             vfNewParent.addChild(sNewFilePath);
640             vfNewParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
641         }
642         
643         return retnStatus;
644     }
645     
646     /**
647      * Copies the file to a new path, giving the copy the same name as the current
648      * version of the file.
649      *
650      * @param sNewPath Path to copy to
651      * @return true if the method was successful
652      */

653     public StatusData copy(String JavaDoc sNewPath) {
654         VFSStatus retnStatus = new VFSStatus();
655         retnStatus.setMethodName(METHOD_COPY);
656         
657         String JavaDoc sOldPath = this.getFullPath();
658         VirtualFile vfOldParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
659         VirtualFile vfNewParent = this.m_vfs.getVirtualFile(sNewPath).getResource();
660         StatusData vfsStatus = this.m_vfs.copyVirtualFile(this.getFullPath(), sNewPath + "/" + this.getFileName());
661         retnStatus.addStatusData(vfsStatus);
662         if(retnStatus.isOK()) {
663             sNewPath = sNewPath + "/" + this.getFileName();
664             vfOldParent.removeChild(sOldPath);
665             vfNewParent.addChild(sNewPath);
666             this.fireVirtualFileEvent(VirtualFileEvent.FILE_COPIED);
667             vfOldParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
668             vfNewParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
669         }
670         
671         return retnStatus;
672     }
673     
674     /**
675      * Moves the file to a new path with a new filename.
676      *
677      * @param sNewPath Path to move to
678      * @param sNewFilename Filename to give the file
679      * @return true if the methods was successful
680      */

681     public StatusData move(String JavaDoc sNewPath, String JavaDoc sNewFilename) {
682         VFSStatus retnStatus = new VFSStatus();
683         retnStatus.setMethodName(METHOD_MOVE);
684         
685         String JavaDoc sOldPath = this.getFullPath();
686         VirtualFile vfOldParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
687         VirtualFile vfNewParent = this.m_vfs.getVirtualFile(sNewPath).getResource();
688         StatusData vfsStatus = this.m_vfs.moveVirtualFile(this.getFullPath(), sNewPath + "/" + sNewFilename);
689         retnStatus.addStatusData(vfsStatus);
690         if(retnStatus.isOK()) {
691             sNewPath = sNewPath + "/" + sNewFilename;
692             vfOldParent.removeChild(sOldPath);
693             vfNewParent.addChild(sNewPath);
694             this.fireVirtualFileEvent(VirtualFileEvent.FILE_MOVED);
695             vfOldParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
696             vfNewParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
697         }
698         
699         return retnStatus;
700     }
701     
702     /**
703      * Renames the virtual file.
704      *
705      * @param sNewFilename New name
706      * @return true if the methods was successful
707      */

708     public StatusData rename(String JavaDoc sNewFilename) {
709         VFSStatus retnStatus = new VFSStatus();
710         retnStatus.setMethodName(METHOD_RENAME);
711         
712         String JavaDoc sOldPath = this.getFullPath();
713         StatusData vfsStatus = this.m_vfs.moveVirtualFile(this.getFullPath(), this.m_sFilePath + "/" + sNewFilename);
714         retnStatus.addStatusData(vfsStatus);
715         if(retnStatus.isOK()) {
716             String JavaDoc sNewPath = this.getFilePath() + "/" + sNewFilename;
717             VirtualFile vfParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
718             vfParent.removeChild(sOldPath);
719             vfParent.addChild(sNewPath);
720             this.fireVirtualFileEvent(VirtualFileEvent.FILE_RENAMED);
721             vfParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
722         }
723         
724         return retnStatus;
725     }
726     
727     /**
728      * Moves the file to a new path.
729      *
730      * @param sNewPath Path to move to
731      * @return true if the methods was successful
732      */

733     public StatusData move(String JavaDoc sNewPath) {
734         VFSStatus retnStatus = new VFSStatus();
735         retnStatus.setMethodName(METHOD_MOVE);
736         
737         String JavaDoc sOldPath = this.getFullPath();
738         VirtualFile vfOldParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
739         VirtualFile vfNewParent = this.m_vfs.getVirtualFile(sNewPath).getResource();
740         StatusData vfsStatus = this.m_vfs.moveVirtualFile(this.getFullPath(), sNewPath + "/" + this.getFileName());
741         retnStatus.addStatusData(vfsStatus);
742         if(retnStatus.isOK()) {
743             sNewPath = sNewPath + "/" + this.getFileName();
744             vfOldParent.removeChild(sOldPath);
745             vfNewParent.addChild(sNewPath);
746             this.fireVirtualFileEvent(VirtualFileEvent.FILE_MOVED);
747             vfOldParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
748             vfNewParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
749         }
750         
751         return retnStatus;
752     }
753     
754     /**
755      * Deletes the file.
756      * @return true if the methods was successful
757      */

758     public StatusData delete() {
759         VFSStatus retnStatus = new VFSStatus();
760         retnStatus.setMethodName(METHOD_DELETE);
761         
762         VirtualFile vfParent = this.m_vfs.getVirtualFile( this.m_sFilePath).getResource();
763         StatusData vfsStatus = this.m_vfs.deleteVirtualFile(this.getFullPath());
764         retnStatus.addStatusData(vfsStatus);
765         if(retnStatus.isOK()) {
766             this.fireVirtualFileEvent(VirtualFileEvent.FILE_DELETED);
767             if(vfParent!=null && !((this instanceof VersionedVirtualFile) && ((VersionedVirtualFile)this).getLiveVersionPath()!=null)) {
768                 vfParent.removeChild(this.getFullPath());
769                 vfParent.refreshChildren();
770                 vfParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
771             }
772         }
773         
774         return retnStatus;
775     }
776     
777     /**
778      * Locks the file to the logged in user.
779      * @return true if the methods was successful
780      */

781     public StatusData lock() {
782         VFSStatus retnStatus = new VFSStatus();
783         retnStatus.setMethodName(METHOD_LOCK);
784         
785         StatusData vfsStatus = this.m_vfs.lockVirtualFile(this.getFullPath());
786         retnStatus.addStatusData(vfsStatus);
787         if(retnStatus.isOK()) {
788             this.fireVirtualFileEvent(VirtualFileEvent.FILE_LOCKED);
789         }
790         
791         return retnStatus;
792     }
793     
794     /**
795      * Unlocks the file.
796      * @return true if the methods was successful
797      */

798     public StatusData unlock() {
799         VFSStatus retnStatus = new VFSStatus();
800         retnStatus.setMethodName(METHOD_UNLOCK);
801         
802         StatusData vfsStatus = this.m_vfs.unlockVirtualFile(this.getFullPath());
803         retnStatus.addStatusData(vfsStatus);
804         if(retnStatus.isOK()) {
805             this.fireVirtualFileEvent(VirtualFileEvent.FILE_UNLOCKED);
806         }
807         
808         return retnStatus;
809     }
810     
811     /**
812      * Creates a new directory with the current one. Only works on Virtual Files that
813      * are directories.
814      *
815      * @param sDirname Name to give to new directory
816      * @return true if the methods was successful
817      */

818     public StatusData mkDir(String JavaDoc sDirname) {
819         VFSStatus retnStatus = new VFSStatus();
820         retnStatus.setMethodName(METHOD_MKDIR);
821         
822         StatusData vfsStatus = this.m_vfs.createVirtualDirectory( this.getFullPath() + "/" + sDirname);
823         retnStatus.addStatusData(vfsStatus);
824         if(retnStatus.isOK()) {
825             this.refreshChildren();
826             this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
827         }
828         
829         return retnStatus;
830     }
831     
832     /**
833      * Creates a new shortcut to a specified file with a specified name within
834      * the current directory, only works on Virtual Files that are directories.
835      *
836      * @param sShortcutName Name to give to new shortcut
837      * @param sFromFullPath Path that the new shortcut points to
838      * @return true if the methods was successful
839      */

840     public StatusData createShortcut(String JavaDoc sShortcutName, String JavaDoc sFromFullPath) {
841         VFSStatus retnStatus = new VFSStatus();
842         retnStatus.setMethodName(METHOD_SHORTCUT);
843         
844         StatusData vfsStatus = this.m_vfs.createShortcut(sFromFullPath, this.getFullPath() + "/" + sShortcutName);
845         retnStatus.addStatusData(vfsStatus);
846         if(retnStatus.isOK()) {
847             this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
848         }
849         
850         return retnStatus;
851     }
852     
853     /**
854      * Returns the owner of the lock on the file, if there is one.
855      *
856      * @return Username of the lock owner, null if there is no lock
857      */

858     public String JavaDoc getLockOwner() {
859         return this.m_sLockOwner;
860     }
861     
862     /**
863      * Sets the owner of the lock on the file. This should be the path to
864      * the virtual file for the principal representing the user who owns
865      * the lock.
866      *
867      * @param sLockOwner Username of the lock owner
868      */

869     public void setLockOwner(String JavaDoc sLockOwner) {
870         this.m_sLockOwner = sLockOwner;
871     }
872     
873     /**
874      * Returns the lock token for the lock on the file, if there is one.
875      *
876      * @return Lock token of the lock, null if there is no lock
877      */

878     public String JavaDoc getLockToken() {
879         return this.m_sLockToken;
880     }
881     
882     /**
883      * Sets the lock token for the lock on the file.
884      *
885      * @param sLockToken Lock token for the lock on the file
886      */

887     public void setLockToken(String JavaDoc sLockToken) {
888         this.m_sLockToken = sLockToken;
889     }
890     
891     /**
892      * Checks if this virtual file is locked.
893      *
894      * @return True if the file is locked
895      */

896     public boolean isLocked() {
897         return (this.m_sLockOwner!=null || this.m_sLockToken!=null);
898     }
899
900     /**
901      * Checks if this virtual file is a collection.
902      *
903      * @return true if this virtual file is a collection
904      */

905     public boolean isDirectory() {
906         return this.m_bIsDirectory;
907     }
908     
909     /**
910      * Sets if this virtual file is a collection.
911      *
912      * @param bIsDirectory True to set that this virtual file is a collection
913      */

914     public void setIsDirectory(boolean bIsDirectory) {
915         this.m_bIsDirectory = bIsDirectory;
916     }
917     
918     /**
919      * Returns the contents for this virtual file.
920      *
921      * @return Content
922      */

923     public byte[] getContent() {
924         byte[] dataRetn = null;
925         
926         if( !this.m_bContentChanged && !this.isContentPopulated() ) {
927             this.m_content = this.m_vfs.getVirtualFileContent(this.getFullPath());
928         }
929
930         dataRetn = this.m_content;
931         
932         return dataRetn;
933     }
934
935     /**
936      * Sets the content for this virtual file.
937      *
938      * @param bs Content
939      */

940     public void setContent(byte[] bs) {
941         this.m_bContentChanged=true;
942         m_content = bs;
943         if(bs!=null) {
944             this.fireVirtualFileEvent(VirtualFileEvent.FILE_CONTENT_CHANGED);
945         }
946     }
947
948     /**
949      * Returns the name of this virtual file.
950      *
951      * @return Name of this virtual file
952      */

953     public String JavaDoc getFileName() {
954         return m_sFileName;
955     }
956
957     /**
958      * Sets the name of this virtual file.
959      *
960      * @param string Name for this virtual file
961      */

962     public void setFileName(String JavaDoc string) {
963         m_sFileName = string;
964     }
965
966     /**
967      * Returns the path to the parent collection of this virtual file.
968      *
969      * @return Path to parent collection
970      */

971     public String JavaDoc getFilePath() {
972         return m_sFilePath;
973     }
974
975     /**
976      * Sets the path to the parent collection for this virtual file.
977      *
978      * @param string Path to parent collection
979      */

980     public void setFilePath(String JavaDoc string) {
981         m_sFilePath = string;
982     }
983     
984     /**
985      * Checks if this virtual file exists within the virtual file system
986      * that this virtual file belongs to.
987      *
988      * @return true if this virtual file exists
989      */

990     public boolean exists() {
991         return this.m_vfs.exists(this.m_sFullPath);
992     }
993     
994     /**
995      * Adds a {@link VirtualFileListener}.
996      *
997      * @param listener Listener to add
998      */

999     public void addVirtualFileListener(VirtualFileListener listener) {
1000        if(!this.m_listeners.contains(listener)) {
1001            this.m_listeners.add(listener);
1002        }
1003    }
1004    
1005    /**
1006     * Removes a {@link VirtualFileListener}.
1007     *
1008     * @param listener Listener to remove
1009     */

1010    public void removeVirtualFileListener(VirtualFileListener listener) {
1011        this.m_listeners.remove(listener);
1012    }
1013    
1014    /**
1015     * Creates and fires a {@link VirtualFileEvent} to all the listeners.
1016     *
1017     * @param sEventType Event type
1018     */

1019    public void fireVirtualFileEvent(String JavaDoc sEventType) {
1020        this.fireVirtualFileEvent(sEventType, EVENT_NOTHING, null);
1021    }
1022    
1023    /**
1024     * Creates and fires a {@link VirtualFileEvent} to all the listeners.
1025     *
1026     * @param sEventType Event type
1027     */

1028    public void fireVirtualFileEvent(String JavaDoc sEventType, int nEventAction, String JavaDoc sPath) {
1029        
1030        if(sEventType.equals(VirtualFileEvent.FILE_METADATA_CHANGED)) {
1031            this.m_bMetadataChanged=true;
1032        } else if(sEventType.equals(VirtualFileEvent.FILE_CONTENT_CHANGED)) {
1033            this.m_bContentChanged=true;
1034        }
1035        
1036        VirtualFileEvent vfe = new VirtualFileEvent(this.m_sFullPath, this.m_vfs, sEventType);
1037        if(nEventAction>0) {
1038            vfe.setSubType(nEventAction);
1039            vfe.setChildPath(sPath);
1040        }
1041        
1042        Iterator itor = ((ArrayList)this.m_listeners.clone()).iterator();
1043        while(itor.hasNext()) {
1044            VirtualFileListener listener = ((VirtualFileListener)itor.next());
1045            listener.virtualFileChanged(vfe);
1046        }
1047        
1048        if(!this.isLocked() && (sEventType.equals(VirtualFileEvent.FILE_METADATA_CHANGED)
1049            || sEventType.equals(VirtualFileEvent.FILE_CONTENT_CHANGED))) {
1050            if(this.m_vfs!=null) {
1051                this.lock();
1052            }
1053        }
1054    }
1055    
1056    /**
1057     * Refreshes the list of children for this virtual file.
1058     *
1059     */

1060    public void refreshChildren() {
1061        this.m_vfs.refreshChildren(this);
1062        this.m_bChildrenPopulated=false;
1063        this.m_children.clear();
1064        this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
1065    }
1066    
1067    /**
1068     * Refreshes the list of children for this virtual file.
1069     *
1070     */

1071    public void refreshChildren(int nAction, String JavaDoc sChildPath) {
1072        this.m_vfs.refreshChildren(this);
1073        this.m_bChildrenPopulated=false;
1074        this.m_children.clear();
1075        this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED, nAction, sChildPath);
1076    }
1077    
1078    /**
1079     * Checks to see if the logged in user will be able to lock this VirtualFile.
1080     * Will return false if the current user does not have permission,
1081     * if the VirtualFileSystem will not support locking for this VirtualFile or
1082     * if the VirtualFile is already locked by another user.
1083     *
1084     * @return true if the current user will be able to lock this VirtualFile
1085     */

1086    public boolean canLock() {
1087        // TODO Must make this correct!!!!
1088
return false;
1089    }
1090    
1091    /**
1092     * Checks to see if the logged in user will be able to unlock this VirtualFile.
1093     * Will return false if the current user does not have permission,
1094     * if the VirtualFileSystem will not support unlocking for this VirtualFile or
1095     * if the VirtualFile is locked by another user.
1096     *
1097     * @return true if the current user will be able to unlock this VirtualFile
1098     */

1099    public boolean canUnlock() {
1100        // TODO Must make this correct!!!!
1101
return false;
1102    }
1103    
1104    /**
1105     * Provides a list of the methods which might work for this VirtualFile.
1106     * This list will be dependant on the VirtualFileSystem implementation, the
1107     * server and the currently logged in user's permissions.
1108     *
1109     * @return List of Strings of method names supported for this user by this VirtualFile.
1110     */

1111    public List getAllowedMethods() {
1112        Date dt = new Date();
1113        if(this.m_dtAllowedMethodsTimeStamp==null || dt.getTime()-this.m_dtAllowedMethodsTimeStamp.getTime()>60000*5/*5 mins*/) {
1114            this.m_allowedMethods.clear();
1115            this.m_vfs.fullyPopulateFileAllowedMethods(this);
1116            this.m_dtAllowedMethodsTimeStamp = dt;
1117        }
1118        return (List) this.m_allowedMethods.clone();
1119    }
1120    
1121    /**
1122     * Adds an allowed method to the list for this virtual file.
1123     *
1124     * @param sMethod Method type to add
1125     */

1126    protected void addAllowedMethods(String JavaDoc sMethod) {
1127        if(!this.m_allowedMethods.contains(sMethod)) {
1128            this.m_allowedMethods.add(sMethod);
1129        }
1130    }
1131    
1132    /**
1133     * Clears the list of allowed methods for this virtual file.
1134     *
1135     */

1136    protected void clearAllowedMethods() {
1137        this.m_allowedMethods = new ArrayList();
1138    }
1139    
1140    /**
1141     * Checks if this VirtualFile is under version control.
1142     *
1143     * @return true if VirtualFile is under version control
1144     */

1145    public boolean isVersionable() {
1146        return this.m_bVersionable;
1147    }
1148    
1149    /**
1150     * Sets if this VirtualFile is under version control.
1151     *
1152     * @return true to set that this VirtualFile is under version control
1153     */

1154    protected void setVersionable(boolean bVersionable) {
1155        this.m_bVersionable = bVersionable;
1156    }
1157    
1158    /**
1159     * Sets the current state of this virtual file.
1160     *
1161     * @param sState State to set
1162     */

1163    protected void setState(String JavaDoc sState) {
1164        this.m_sState = sState;
1165    }
1166    
1167    /**
1168     * Returns the current state of this virtual file.
1169     *
1170     * @return Current state
1171     */

1172    public String JavaDoc getState() {
1173        return this.m_sState;
1174    }
1175    
1176    /**
1177     * Submits any changes that have been made to this virtual file back
1178     * to the underlying file system.
1179     *
1180     * @return true if the method was successful
1181     */

1182    public StatusData sync() {
1183        VFSStatus retnStatus = new VFSStatus();
1184        retnStatus.setMethodName(METHOD_SYNC);
1185        
1186        StatusData vfsStatus = this.m_vfs.synchroniseFile(this);
1187        retnStatus.addStatusData(vfsStatus);
1188
1189        VirtualFile vfParent = null;
1190        if(this.getState().equals(VirtualFile.STATE_PENDING) && ((VersionedVirtualFile)this).getLiveVersionPath()!=null) {
1191            VirtualFile vfLiveFile = this.m_vfs.getVirtualFile(((VersionedVirtualFile)this).getLiveVersionPath()).getResource();
1192            vfParent = this.m_vfs.getVirtualFile(vfLiveFile.getFilePath()).getResource();
1193        } else {
1194            vfParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
1195        }
1196        if(retnStatus.isOK()) {
1197            this.m_bContentChanged = false;
1198            this.m_bMetadataChanged = false;
1199            this.fireVirtualFileEvent(VirtualFileEvent.FILE_SYNCHED);
1200        }
1201        vfParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
1202        return retnStatus;
1203    }
1204    
1205    /**
1206     * Checks if this collection is able to have the order of its
1207     * children set.
1208     *
1209     * @return true if the order of children can be set
1210     */

1211    public boolean isOrderableDirectory() {
1212        return m_bOrderableDirectory;
1213    }
1214
1215    /**
1216     * Sets that the order of the children of this collection can be set.
1217     *
1218     * @param b true if the order of the children can be set
1219     */

1220    protected void setOrderableDirectory(boolean b) {
1221        m_bOrderableDirectory = b;
1222    }
1223    
1224    /**
1225     * Clears the list of paths of children of this collection.
1226     *
1227     */

1228    protected void clearChildren() {
1229        this.m_bChildrenPopulated=false;
1230        this.m_children.clear();
1231    }
1232
1233    /**
1234     * @param b
1235     */

1236    public void setIsVirtualDirectory(boolean bIsVirtual) {
1237        this.m_bIsVirtualDirectory = bIsVirtual;
1238    }
1239    
1240    public boolean isVirtualDirectory() {
1241        return this.m_bIsVirtualDirectory;
1242    }
1243
1244}
1245
Popular Tags