/*
* The contents of this file are subject to the
* Mozilla Public License Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights and
* limitations under the License.
*
* The Initial Developer of the Original Code is Simulacra Media Ltd.
* Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
*
* All Rights Reserved.
*
* Contributor(s):
*/
package org.openharmonise.vfs;
import java.util.*;
import org.openharmonise.vfs.event.*;
import org.openharmonise.vfs.metadata.*;
import org.openharmonise.vfs.status.*;
/**
* A representation of a file in a Virtual File System.
*
* @author Matthew Large
* @version $Revision: 1.1 $
*
*/
public class VirtualFile {
/**
* The virtual file system that this virtual file is attached to.
*/
protected AbstractVirtualFileSystem m_vfs = null;
/**
* The children of this virtual file, if it is a collection.
*/
private ArrayList m_children = new ArrayList(5);
/**
* The property instances attached to this virtual file.
*/
private HashMap m_properties = new HashMap(5);
/**
* 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'.
*/
private String m_sFilePath;
/**
* The name of this virtual file, e.g. if the full path is '/webdav/Documents/doc1' then this will be 'doc1'.
*/
private String m_sFileName;
/**
* The complete path of this virtual file.
*/
private String m_sFullPath;
/**
* 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.
*/
private String m_sLockOwner = null;
/**
* The unique lock token for the exclusive lock on this virtual file, if there is one.
*/
private String m_sLockToken = null;
/**
* true if the content for this virtual file is populated.
*/
private boolean m_bContentPopulated = false;
/**
* true if the children list for this virtual file, if it is a collection, is populated.
*/
private boolean m_bChildrenPopulated = false;
/**
* true if the metadata for this virtual file is populated.
*/
private boolean m_bMetadataPopulated = false;
/**
* true if the content for this virtual file has changed since it was populated.
*/
private boolean m_bContentChanged = false;
/**
* true if the metedata for this virtual file has changed since it was populated.
*/
private boolean m_bMetadataChanged = false;
/**
* true if this virtual file is a collection.
*/
private boolean m_bIsDirectory = false;
/**
* The content for this virtual file.
*/
private byte[] m_content = null;
/**
* The list of {@link VirtualFileListener} objects attached to this virtual file.
*/
private ArrayList m_listeners = new ArrayList();
/**
* The state of a pending virtual file.
*/
public static String STATE_PENDING = "PENDING";
/**
* The state of a live virtual file.
*/
public static String STATE_LIVE = "LIVE";
/**
* The state of a historical virtual file.
*/
public static String STATE_HISTORICAL = "HISTORICAL";
/**
* The current state of this virtual file.
*/
private String m_sState = VirtualFile.STATE_LIVE;
/**
* true if this virtual file is versionable.
*/
private boolean m_bVersionable = false;
/**
* true if this virtual file is orderable.
*/
private boolean m_bOrderableDirectory = false;
/**
* List of methods allowed on this virtual file.
*/
private ArrayList m_allowedMethods = new ArrayList();
/**
* Timestamp of the last time the allow methods list was populated.
*/
private Date m_dtAllowedMethodsTimeStamp = null;
/**
* Submit method identifier for allowed methods.
*/
public static String METHOD_SYNC = "SYNC";
/**
* Delete method identifier for allowed methods.
*/
public static String METHOD_DELETE = "DELETE";
/**
* Move method identifier for allowed methods.
*/
public static String METHOD_MOVE = "MOVE";
/**
* Copy method identifier for allowed methods.
*/
public static String METHOD_COPY = "COPY";
/**
* Create collection method identifier for allowed methods.
*/
public static String METHOD_MKDIR = "MKDIR";
/**
* Lock method identifier for allowed methods.
*/
public static String METHOD_LOCK = "LOCK";
/**
* Unlock method identifier for allowed methods.
*/
public static String METHOD_UNLOCK = "UNLOCK";
/**
* Create shortcut method identifier for allowed methods.
*/
public static String METHOD_SHORTCUT = "SHORTCUT";
/**
* Set children order method identifier for allowed methods.
*/
public static String METHOD_SET_CHILD_ORDER = "SET_CHILD_ORDER";
/**
* Checkin method identifier for allowed methods.
*/
public static String METHOD_CHECKIN = "CHECKIN";
/**
* Tag method identifier for allowed methods.
*/
public static String METHOD_TAG = "TAG";
/**
* Order method identifier for allowed methods.
*/
public static String METHOD_ORDER = "ORDER";
/**
* Reject method identifier for allowed methods.
*/
public static String METHOD_REJECT = "REJECT";
/**
* Checkout method identifier for allowed methods.
*/
public static String METHOD_CHECKOUT = "CHECKOUT";
/**
* Checkout method identifier for allowed methods.
*/
public static String METHOD_RENAME = "RENAME";
/**
* Add method identifier for allowed methods.
*/
public static String METHOD_ADD = "ADD";
/**
* Get method identifier for allowed methods.
*/
public static String METHOD_GET = "GET";
/**
* Search method identifier for allowed methods.
*/
public static String METHOD_SEARCH = "SEARCH";
public static int EVENT_NOTHING = 0;
public static int EVENT_ADDITION = 1;
public static int EVENT_REMOVAL = 2;
/**
* true if the lock information for this virtual file is populated.
*/
private boolean m_bPopulateLocked = false;
private boolean m_bIsVirtualDirectory = false;
/**
* Constructs a Virtual File.
*
* @param sFullPath FullPath of the file
*/
public VirtualFile(String sFullPath) {
super();
this.setFullPath(sFullPath);
}
/**
* Constructs a Virtual File.
*/
public VirtualFile() {
super();
}
/**
* Discards any changes that may have been made to this virtual file
* since it was populate. The any information subsequently requested
* will be repopulated from the underlying file system.
*
*/
protected void discardChanges() {
if(this.isChanged()) {
this.clearFile();
}
}
/**
* Clears all of the information about this virtual file and resets
* all of the populated booleans to false.
*
*/
public void clearFile() {
m_bContentPopulated = false;
m_bChildrenPopulated = false;
m_bMetadataPopulated = false;
this.m_children.clear();
this.m_content = null;
this.m_properties.clear();
this.m_bContentChanged=false;
this.m_bMetadataChanged=false;
this.fireVirtualFileEvent(VirtualFileEvent.FILE_CHANGES_DISCARDED);
}
/**
* Checks if this virtual file's lock information has been populated.
*
* @return true if this virtual file's lock information has been populated
*/
public boolean isPopulateLocked() {
return this.m_bPopulateLocked;
}
/**
* Sets the lock populated information for this virtual file.
*
* @param bPopulateLocked Set to true to set that this virtual file's lock information has been populated.
*/
public void setPopulateLocked(boolean bPopulateLocked) {
this.m_bPopulateLocked = bPopulateLocked;
}
/**
* Sets the full path of this virtual file.
*
* @param sFullPath The full path for this virtual file
*/
public void setFullPath(String sFullPath) {
String sFilePath = null;
String sFileName = null;
if( sFullPath.endsWith( "/" ) ) {
String sTemp = sFullPath;
sTemp = sTemp.substring(0, sTemp.length()-1);
sFilePath = sTemp.substring(0, sTemp.lastIndexOf( "/".charAt(0) ));
sFileName = sTemp.substring( sTemp.lastIndexOf( "/".charAt(0)) );
} else {
String sTemp = sFullPath;
sFilePath = sTemp.substring(0, sTemp.lastIndexOf( "/".charAt(0) ));
sFileName = sTemp.substring( sTemp.lastIndexOf( "/".charAt(0)) );
}
this.m_sFilePath = sFilePath;
if( sFullPath.endsWith("/")) {
sFullPath = sFullPath.substring(0, sFullPath.length()-1);
}
this.m_sFullPath = sFullPath;
if( sFileName.endsWith("/")) {
sFileName = sFileName.substring(0, sFileName.length()-1);
}
if( sFileName.startsWith("/") ) {
sFileName = sFileName.substring(1, sFileName.length());
}
this.m_sFileName = sFileName;
}
/**
* Adds a path to this virtual file's children list.
*
* @param sURI Path to add to this virtual file's children list
*/
public void addChild(String sURI) {
this.m_children.add(sURI);
//this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
/**
* Removes a path from this virtual file's children list.
*
* @param sURI Path to remove from this virtual file's children list
*/
public void removeChild(String sURI) {
this.m_children.remove(sURI);
}
/**
* Checks if this virtual file has a path as one of its children.
*
* @param sURI Path to check
* @return true if the given path is a child of this virtual file
*/
public boolean hasChild(String sURI) {
return this.m_children.contains(sURI);
}
/**
* Returns a list of the paths that are children of this virtual file.
*
* @return List of virtual file paths
*/
public List getChildren() {
if( this.isDirectory() && !this.m_bChildrenPopulated) {
this.m_vfs.fullyPopulateFileChildren(this);
}
return (List)this.m_children.clone();
}
/**
* Sets the order of the children of this virtual file. This method
* will fail if this is not a collection which is orderable or if the
* contents of the suplied list of paths does not match the list of
* child paths for this collection.
*
* @param children List of paths in the order to be set
* @return true if the method was successful
*/
public StatusData setChildrenOrder(List children) {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_ORDER);
if(this.isDirectory() && this.isOrderableDirectory() && this.m_children.containsAll(children) && children.containsAll(this.m_children)) {
this.m_children = (ArrayList) children;
StatusData vfsStatus = this.m_vfs.orderVirtualFileChildren(children, this);
retnStatus.addStatusData(vfsStatus);
this.m_children.clear();
this.m_bChildrenPopulated = false;
this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
return retnStatus;
}
/**
* Adds a {@link PropertyInstance} to the metadata for this vitual
* file.
*
* @param prop {@link PropertyInstance} to add to this virtual file
*/
public void addProperty(PropertyInstance prop) {
this.m_properties.put( prop.getNamespaceURI() + "#" + prop.getName(), prop );
prop.setVirtualFile(this);
}
/**
* Clears all the metadata for this virtual file.
*
*/
protected void clearAllProperties() {
this.m_properties.clear();
}
/**
* Removes a {@link PropertyInstance} from the metadata for this
* virtual file.
*
* @param sNamespaceURI Namespace of the {@link PropertyInstance} to be removed
* @param sName Name of the {@link PropertyInstance} to be removed
*/
public void removeProperty(String sNamespaceURI, String sName) {
this.m_properties.remove(sNamespaceURI + "#" + sName);
}
/**
* Returns a {@link PropertyInstance} from the metadata for this
* virtual file which matches the given namespace and name.
*
* @param sNamespaceURI Namespace of the {@link PropertyInstance} to be returned
* @param sName Name of the {@link PropertyInstance} to be returned
* @return null if no {@link PropertyInstance} was found in this virtual file's metadata matching the given namespace and name
*/
public PropertyInstance getProperty(String sNamespaceURI, String sName) {
PropertyInstance prop = (PropertyInstance)this.m_properties.get(sNamespaceURI + "#" + sName);
if( !this.isMetadataPopulated() && prop==null && this.m_vfs!=null ) {
this.m_vfs.fullyPopulateFileMetadata(this);
prop = (PropertyInstance)this.m_properties.get(sNamespaceURI + "#" + sName);
}
return prop;
}
/**
* Returns a list of all the {@link PropertyInstance} objects in this
* virtual file's metadata.
*
* @return List of {@link PropertyInstance} objects
*/
public List getProperties() {
if(!this.m_bMetadataPopulated) {
this.m_vfs.fullyPopulateFileMetadata(this);
}
return new ArrayList(this.m_properties.values());
}
/**
* Gives the file a hook to the file system it belongs to.
*
* @param vfs Virtual file system that the file belongs to
*/
public void setVFS(AbstractVirtualFileSystem vfs) {
this.m_vfs = vfs;
}
/**
* Returns the {@link AbstractVirtualFileSystem} that this virtual file
* belongs to.
*
* @return Virtual file system that this virtual file belongs to
*/
public AbstractVirtualFileSystem getVFS() {
return this.m_vfs;
}
/**
* Checks if the file has any listeners.
*
* @return true if the file has any listeners
*/
public boolean hasVirtualFileListeners() {
return this.m_listeners.size()>0;
}
/**
* Checks if the in memory file has changed in anyway since it either came from
* the Virtual File System or since it was last synchronized.
*
* @return true if the file has changed
*/
public boolean isChanged() {
return this.m_bContentChanged || this.m_bMetadataChanged;
}
/**
* Checks if the content for this virtual file has changes since it
* was last populated.
*
* @return true if the content has changed
*/
public boolean isContentChanged() {
return this.m_bContentChanged;
}
/**
* Checks if the metadata for this virtual file has changed since it
* was last populated.
*
* @return true if the metadata has changed
*/
public boolean isMetadataChanged() {
return this.m_bMetadataChanged;
}
/**
* Checks if the file is fully populated, to allow for lazy partial instantiation.
*
* @return true if the file is fully populated
*/
public boolean isFullyPopulated() {
return this.m_bChildrenPopulated && this.m_bContentPopulated && this.m_bMetadataPopulated;
}
/**
* Checks if the list of children for this virtual file has been
* populated.
*
* @return true if the list of children has been populated
*/
protected boolean isChildrenPopulated() {
return this.m_bChildrenPopulated;
}
/**
* Sets that the list of children for this virtual file has been
* populated.
*
* @param bChildrenPopulated Set to true to set that the list of children has been populated
*/
protected void setChildrenPopulated(boolean bChildrenPopulated) {
this.m_bChildrenPopulated = bChildrenPopulated;
}
/**
* Checks if the content for this virtual file has been populated.
*
* @return true if the content has been populated
*/
protected boolean isContentPopulated() {
return this.m_bContentPopulated;
}
/**
* Sets that the content for this virtual file has been populated.
*
* @param bContentPopulated Set to true to set that the content has been populated
*/
protected void setContentPopulated(boolean bContentPopulated) {
this.m_bContentPopulated = bContentPopulated;
}
/**
* Checks if the metadata for this virtual file has been populated.
*
* @return true if the metadata has been populated
*/
protected boolean isMetadataPopulated() {
return this.m_bMetadataPopulated;
}
/**
* Sets that the metadata for this virtual file has been populated.
*
* @param bMetadataPopulated Set to true to set that the metadata has been populated
*/
protected void setMetadataPopulated(boolean bMetadataPopulated) {
this.m_bMetadataPopulated = bMetadataPopulated;
}
/**
* Gets the full path of the file.
*
* @return Fullpath of the file
*/
public String getFullPath() {
return this.m_sFullPath;
}
/**
* Copies the file to a new path with a new filename.
*
* @param sNewPath Path to copy to
* @param sNewFilename Filename to give the new copy
* @return true if the method was successful
*/
public StatusData copy(String sNewPath, String sNewFilename) {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_COPY);
String sOldPath = this.getFullPath();
VirtualFile vfOldParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
VirtualFile vfNewParent = this.m_vfs.getVirtualFile(sNewPath).getResource();
StatusData vfsStatus = this.m_vfs.copyVirtualFile(this.getFullPath(), sNewPath + "/" + sNewFilename);
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
String sNewFilePath = sNewPath + "/" + this.getFileName();
this.fireVirtualFileEvent(VirtualFileEvent.FILE_COPIED);
if(!sNewPath.equals(this.getFilePath())) {
vfOldParent.removeChild(sOldPath);
vfOldParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
vfNewParent.addChild(sNewFilePath);
vfNewParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
return retnStatus;
}
/**
* Copies the file to a new path, giving the copy the same name as the current
* version of the file.
*
* @param sNewPath Path to copy to
* @return true if the method was successful
*/
public StatusData copy(String sNewPath) {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_COPY);
String sOldPath = this.getFullPath();
VirtualFile vfOldParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
VirtualFile vfNewParent = this.m_vfs.getVirtualFile(sNewPath).getResource();
StatusData vfsStatus = this.m_vfs.copyVirtualFile(this.getFullPath(), sNewPath + "/" + this.getFileName());
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
sNewPath = sNewPath + "/" + this.getFileName();
vfOldParent.removeChild(sOldPath);
vfNewParent.addChild(sNewPath);
this.fireVirtualFileEvent(VirtualFileEvent.FILE_COPIED);
vfOldParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
vfNewParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
return retnStatus;
}
/**
* Moves the file to a new path with a new filename.
*
* @param sNewPath Path to move to
* @param sNewFilename Filename to give the file
* @return true if the methods was successful
*/
public StatusData move(String sNewPath, String sNewFilename) {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_MOVE);
String sOldPath = this.getFullPath();
VirtualFile vfOldParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
VirtualFile vfNewParent = this.m_vfs.getVirtualFile(sNewPath).getResource();
StatusData vfsStatus = this.m_vfs.moveVirtualFile(this.getFullPath(), sNewPath + "/" + sNewFilename);
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
sNewPath = sNewPath + "/" + sNewFilename;
vfOldParent.removeChild(sOldPath);
vfNewParent.addChild(sNewPath);
this.fireVirtualFileEvent(VirtualFileEvent.FILE_MOVED);
vfOldParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
vfNewParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
return retnStatus;
}
/**
* Renames the virtual file.
*
* @param sNewFilename New name
* @return true if the methods was successful
*/
public StatusData rename(String sNewFilename) {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_RENAME);
String sOldPath = this.getFullPath();
StatusData vfsStatus = this.m_vfs.moveVirtualFile(this.getFullPath(), this.m_sFilePath + "/" + sNewFilename);
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
String sNewPath = this.getFilePath() + "/" + sNewFilename;
VirtualFile vfParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
vfParent.removeChild(sOldPath);
vfParent.addChild(sNewPath);
this.fireVirtualFileEvent(VirtualFileEvent.FILE_RENAMED);
vfParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
return retnStatus;
}
/**
* Moves the file to a new path.
*
* @param sNewPath Path to move to
* @return true if the methods was successful
*/
public StatusData move(String sNewPath) {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_MOVE);
String sOldPath = this.getFullPath();
VirtualFile vfOldParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
VirtualFile vfNewParent = this.m_vfs.getVirtualFile(sNewPath).getResource();
StatusData vfsStatus = this.m_vfs.moveVirtualFile(this.getFullPath(), sNewPath + "/" + this.getFileName());
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
sNewPath = sNewPath + "/" + this.getFileName();
vfOldParent.removeChild(sOldPath);
vfNewParent.addChild(sNewPath);
this.fireVirtualFileEvent(VirtualFileEvent.FILE_MOVED);
vfOldParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
vfNewParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
return retnStatus;
}
/**
* Deletes the file.
* @return true if the methods was successful
*/
public StatusData delete() {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_DELETE);
VirtualFile vfParent = this.m_vfs.getVirtualFile( this.m_sFilePath).getResource();
StatusData vfsStatus = this.m_vfs.deleteVirtualFile(this.getFullPath());
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
this.fireVirtualFileEvent(VirtualFileEvent.FILE_DELETED);
if(vfParent!=null && !((this instanceof VersionedVirtualFile) && ((VersionedVirtualFile)this).getLiveVersionPath()!=null)) {
vfParent.removeChild(this.getFullPath());
vfParent.refreshChildren();
vfParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
}
return retnStatus;
}
/**
* Locks the file to the logged in user.
* @return true if the methods was successful
*/
public StatusData lock() {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_LOCK);
StatusData vfsStatus = this.m_vfs.lockVirtualFile(this.getFullPath());
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
this.fireVirtualFileEvent(VirtualFileEvent.FILE_LOCKED);
}
return retnStatus;
}
/**
* Unlocks the file.
* @return true if the methods was successful
*/
public StatusData unlock() {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_UNLOCK);
StatusData vfsStatus = this.m_vfs.unlockVirtualFile(this.getFullPath());
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
this.fireVirtualFileEvent(VirtualFileEvent.FILE_UNLOCKED);
}
return retnStatus;
}
/**
* Creates a new directory with the current one. Only works on Virtual Files that
* are directories.
*
* @param sDirname Name to give to new directory
* @return true if the methods was successful
*/
public StatusData mkDir(String sDirname) {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_MKDIR);
StatusData vfsStatus = this.m_vfs.createVirtualDirectory( this.getFullPath() + "/" + sDirname);
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
this.refreshChildren();
this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
return retnStatus;
}
/**
* Creates a new shortcut to a specified file with a specified name within
* the current directory, only works on Virtual Files that are directories.
*
* @param sShortcutName Name to give to new shortcut
* @param sFromFullPath Path that the new shortcut points to
* @return true if the methods was successful
*/
public StatusData createShortcut(String sShortcutName, String sFromFullPath) {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_SHORTCUT);
StatusData vfsStatus = this.m_vfs.createShortcut(sFromFullPath, this.getFullPath() + "/" + sShortcutName);
retnStatus.addStatusData(vfsStatus);
if(retnStatus.isOK()) {
this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
return retnStatus;
}
/**
* Returns the owner of the lock on the file, if there is one.
*
* @return Username of the lock owner, null if there is no lock
*/
public String getLockOwner() {
return this.m_sLockOwner;
}
/**
* Sets the owner of the lock on the file. This should be the path to
* the virtual file for the principal representing the user who owns
* the lock.
*
* @param sLockOwner Username of the lock owner
*/
public void setLockOwner(String sLockOwner) {
this.m_sLockOwner = sLockOwner;
}
/**
* Returns the lock token for the lock on the file, if there is one.
*
* @return Lock token of the lock, null if there is no lock
*/
public String getLockToken() {
return this.m_sLockToken;
}
/**
* Sets the lock token for the lock on the file.
*
* @param sLockToken Lock token for the lock on the file
*/
public void setLockToken(String sLockToken) {
this.m_sLockToken = sLockToken;
}
/**
* Checks if this virtual file is locked.
*
* @return True if the file is locked
*/
public boolean isLocked() {
return (this.m_sLockOwner!=null || this.m_sLockToken!=null);
}
/**
* Checks if this virtual file is a collection.
*
* @return true if this virtual file is a collection
*/
public boolean isDirectory() {
return this.m_bIsDirectory;
}
/**
* Sets if this virtual file is a collection.
*
* @param bIsDirectory True to set that this virtual file is a collection
*/
public void setIsDirectory(boolean bIsDirectory) {
this.m_bIsDirectory = bIsDirectory;
}
/**
* Returns the contents for this virtual file.
*
* @return Content
*/
public byte[] getContent() {
byte[] dataRetn = null;
if( !this.m_bContentChanged && !this.isContentPopulated() ) {
this.m_content = this.m_vfs.getVirtualFileContent(this.getFullPath());
}
dataRetn = this.m_content;
return dataRetn;
}
/**
* Sets the content for this virtual file.
*
* @param bs Content
*/
public void setContent(byte[] bs) {
this.m_bContentChanged=true;
m_content = bs;
if(bs!=null) {
this.fireVirtualFileEvent(VirtualFileEvent.FILE_CONTENT_CHANGED);
}
}
/**
* Returns the name of this virtual file.
*
* @return Name of this virtual file
*/
public String getFileName() {
return m_sFileName;
}
/**
* Sets the name of this virtual file.
*
* @param string Name for this virtual file
*/
public void setFileName(String string) {
m_sFileName = string;
}
/**
* Returns the path to the parent collection of this virtual file.
*
* @return Path to parent collection
*/
public String getFilePath() {
return m_sFilePath;
}
/**
* Sets the path to the parent collection for this virtual file.
*
* @param string Path to parent collection
*/
public void setFilePath(String string) {
m_sFilePath = string;
}
/**
* Checks if this virtual file exists within the virtual file system
* that this virtual file belongs to.
*
* @return true if this virtual file exists
*/
public boolean exists() {
return this.m_vfs.exists(this.m_sFullPath);
}
/**
* Adds a {@link VirtualFileListener}.
*
* @param listener Listener to add
*/
public void addVirtualFileListener(VirtualFileListener listener) {
if(!this.m_listeners.contains(listener)) {
this.m_listeners.add(listener);
}
}
/**
* Removes a {@link VirtualFileListener}.
*
* @param listener Listener to remove
*/
public void removeVirtualFileListener(VirtualFileListener listener) {
this.m_listeners.remove(listener);
}
/**
* Creates and fires a {@link VirtualFileEvent} to all the listeners.
*
* @param sEventType Event type
*/
public void fireVirtualFileEvent(String sEventType) {
this.fireVirtualFileEvent(sEventType, EVENT_NOTHING, null);
}
/**
* Creates and fires a {@link VirtualFileEvent} to all the listeners.
*
* @param sEventType Event type
*/
public void fireVirtualFileEvent(String sEventType, int nEventAction, String sPath) {
if(sEventType.equals(VirtualFileEvent.FILE_METADATA_CHANGED)) {
this.m_bMetadataChanged=true;
} else if(sEventType.equals(VirtualFileEvent.FILE_CONTENT_CHANGED)) {
this.m_bContentChanged=true;
}
VirtualFileEvent vfe = new VirtualFileEvent(this.m_sFullPath, this.m_vfs, sEventType);
if(nEventAction>0) {
vfe.setSubType(nEventAction);
vfe.setChildPath(sPath);
}
Iterator itor = ((ArrayList)this.m_listeners.clone()).iterator();
while(itor.hasNext()) {
VirtualFileListener listener = ((VirtualFileListener)itor.next());
listener.virtualFileChanged(vfe);
}
if(!this.isLocked() && (sEventType.equals(VirtualFileEvent.FILE_METADATA_CHANGED)
|| sEventType.equals(VirtualFileEvent.FILE_CONTENT_CHANGED))) {
if(this.m_vfs!=null) {
this.lock();
}
}
}
/**
* Refreshes the list of children for this virtual file.
*
*/
public void refreshChildren() {
this.m_vfs.refreshChildren(this);
this.m_bChildrenPopulated=false;
this.m_children.clear();
this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
}
/**
* Refreshes the list of children for this virtual file.
*
*/
public void refreshChildren(int nAction, String sChildPath) {
this.m_vfs.refreshChildren(this);
this.m_bChildrenPopulated=false;
this.m_children.clear();
this.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED, nAction, sChildPath);
}
/**
* Checks to see if the logged in user will be able to lock this VirtualFile.
* Will return false if the current user does not have permission,
* if the VirtualFileSystem will not support locking for this VirtualFile or
* if the VirtualFile is already locked by another user.
*
* @return true if the current user will be able to lock this VirtualFile
*/
public boolean canLock() {
// TODO Must make this correct!!!!
return false;
}
/**
* Checks to see if the logged in user will be able to unlock this VirtualFile.
* Will return false if the current user does not have permission,
* if the VirtualFileSystem will not support unlocking for this VirtualFile or
* if the VirtualFile is locked by another user.
*
* @return true if the current user will be able to unlock this VirtualFile
*/
public boolean canUnlock() {
// TODO Must make this correct!!!!
return false;
}
/**
* Provides a list of the methods which might work for this VirtualFile.
* This list will be dependant on the VirtualFileSystem implementation, the
* server and the currently logged in user's permissions.
*
* @return List of Strings of method names supported for this user by this VirtualFile.
*/
public List getAllowedMethods() {
Date dt = new Date();
if(this.m_dtAllowedMethodsTimeStamp==null || dt.getTime()-this.m_dtAllowedMethodsTimeStamp.getTime()>60000*5/*5 mins*/) {
this.m_allowedMethods.clear();
this.m_vfs.fullyPopulateFileAllowedMethods(this);
this.m_dtAllowedMethodsTimeStamp = dt;
}
return (List) this.m_allowedMethods.clone();
}
/**
* Adds an allowed method to the list for this virtual file.
*
* @param sMethod Method type to add
*/
protected void addAllowedMethods(String sMethod) {
if(!this.m_allowedMethods.contains(sMethod)) {
this.m_allowedMethods.add(sMethod);
}
}
/**
* Clears the list of allowed methods for this virtual file.
*
*/
protected void clearAllowedMethods() {
this.m_allowedMethods = new ArrayList();
}
/**
* Checks if this VirtualFile is under version control.
*
* @return true if VirtualFile is under version control
*/
public boolean isVersionable() {
return this.m_bVersionable;
}
/**
* Sets if this VirtualFile is under version control.
*
* @return true to set that this VirtualFile is under version control
*/
protected void setVersionable(boolean bVersionable) {
this.m_bVersionable = bVersionable;
}
/**
* Sets the current state of this virtual file.
*
* @param sState State to set
*/
protected void setState(String sState) {
this.m_sState = sState;
}
/**
* Returns the current state of this virtual file.
*
* @return Current state
*/
public String getState() {
return this.m_sState;
}
/**
* Submits any changes that have been made to this virtual file back
* to the underlying file system.
*
* @return true if the method was successful
*/
public StatusData sync() {
VFSStatus retnStatus = new VFSStatus();
retnStatus.setMethodName(METHOD_SYNC);
StatusData vfsStatus = this.m_vfs.synchroniseFile(this);
retnStatus.addStatusData(vfsStatus);
VirtualFile vfParent = null;
if(this.getState().equals(VirtualFile.STATE_PENDING) && ((VersionedVirtualFile)this).getLiveVersionPath()!=null) {
VirtualFile vfLiveFile = this.m_vfs.getVirtualFile(((VersionedVirtualFile)this).getLiveVersionPath()).getResource();
vfParent = this.m_vfs.getVirtualFile(vfLiveFile.getFilePath()).getResource();
} else {
vfParent = this.m_vfs.getVirtualFile(this.getFilePath()).getResource();
}
if(retnStatus.isOK()) {
this.m_bContentChanged = false;
this.m_bMetadataChanged = false;
this.fireVirtualFileEvent(VirtualFileEvent.FILE_SYNCHED);
}
vfParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
return retnStatus;
}
/**
* Checks if this collection is able to have the order of its
* children set.
*
* @return true if the order of children can be set
*/
public boolean isOrderableDirectory() {
return m_bOrderableDirectory;
}
/**
* Sets that the order of the children of this collection can be set.
*
* @param b true if the order of the children can be set
*/
protected void setOrderableDirectory(boolean b) {
m_bOrderableDirectory = b;
}
/**
* Clears the list of paths of children of this collection.
*
*/
protected void clearChildren() {
this.m_bChildrenPopulated=false;
this.m_children.clear();
}
/**
* @param b
*/
public void setIsVirtualDirectory(boolean bIsVirtual) {
this.m_bIsVirtualDirectory = bIsVirtual;
}
public boolean isVirtualDirectory() {
return this.m_bIsVirtualDirectory;
}
}
|