org.entityfs.impl.impl
Class DirectoryImpl

java.lang.Object
  extended by org.entityfs.lock.AbstractLockable
      extended by org.entityfs.impl.impl.AbstractEntityImpl
          extended by org.entityfs.impl.impl.AbstractEntityAcceptingEntityImpl<DirectoryView>
              extended by org.entityfs.impl.impl.DirectoryImpl
All Implemented Interfaces:
Comparable<EntityView>, Iterable<EntityView>, Deletable, Directory, DirectoryView, Entity, EntityAcceptingEntity<DirectoryView>, EntityAcceptingEntityView<DirectoryView>, EntityHolder, EntityListable, EntityRepository, EntityView, Observable, DirectoryImplementation, DirectoryViewImplementation, EntityAcceptingEntityImplementation<DirectoryView>, EntityAcceptingEntityViewImplementation<DirectoryView>, EntityImplementation, EntityViewImplementation, Lockable, ReadLockable, ReadWriteLockable, WriteLockable, Named, ViewCapable<DirectoryView>

public class DirectoryImpl
extends AbstractEntityAcceptingEntityImpl<DirectoryView>
implements DirectoryImplementation

Directory implementation.

Since:
1.0
Author:
Karl Gustafsson
In_jar:
entityfs-core

Field Summary
 
Fields inherited from interface org.entityfs.Directory
DEFAULT_TEMP_FILE_SUFFIX, MINIMUM_TEMP_FILE_PREFIX_LENGTH
 
Constructor Summary
protected DirectoryImpl(FileSystemImplementation fs, DirectoryImplementation parent, DirectoryDelegate dd)
           
 
Method Summary
 void addChildEntity(String name, EntityImplementation entity)
          Used to add a new entity to the directory when an entity is created.
 void assertCanBeDeleted()
          Perform entity type-specific checks in addition to the checks in the delete method.
 void assertNoChildEntityWithName(String name)
          Assert that an entity with the given name does not exist in the directory.
 boolean canBeDereferenced()
          Can the entity-accepting entity be dereferenced.
 boolean containsEntity(EntityView ev)
          Does this entity holder contain the supplied entity? If this is called on a symbolic link, it is checked if the link target is a directory and that directory contains the entity.
 boolean containsEntity(String name)
          Can an entity with the given name be retrieved from this object? If this is called on a symbolic link, it is checked if the link target is a directory and that directory contains an entity with the given name.
 Set<EntityView> copy(DirectoryView targetDir, String targetName, boolean preserveAttrs, EntityLock... setupLocks)
          Copy the viewed entity to a new directory and possibly give the target another name.
 Set<EntityView> copyFrom(EntityImplementation ent, String targetName, boolean preserveAttrs, List<EntityAttributeSetter> asl, EntityLock[] setupLocks)
          Copy the contents from the EntityAdapter to one or more new entities in the viewed directory.
 EntityDelegate createChildDelegate(EntityType et, String name, Object data)
           
protected  void deleteInternal()
          This method is called by AbstractEntityImpl.delete() after it has performed all validity, access control and lock checks.
 Entity getEntityOrNull(String name)
          Get the named entity.
 EntityType getType()
          Get the entity's EntityType.
 String getUniqueEntityName(String prefix, String suffix)
          Get a unique entity name with the given prefix and suffix in the directory.
 DirectoryView getViewedDir()
           
 DirectoryImplementation getViewedDirImplementation()
           
 void handleDisappearingChild(EntityImplementation entity)
          Called whenever it is detected that a child entity has disappeared from the directory.
 boolean isEmpty()
          Is the listable empty? If a directory view is empty, the viewed directory may still contain entities that are hidden by the view's filter.
 boolean isView()
           
 Iterator<EntityView> iterator()
           
 Set<EntityView> listEntities()
          Return all entities that are visible in this directory.
 EntityImplementation newEntity(EntityType et, String name, Object data)
          Create a new child entity in the directory.
 DirectoryView newView(Filter<? super EntityView> filter)
          Create a new, connected view on this entity or entity view.
 void removeChildEntity(EntityImplementation entity, DirectoryImplementation newParent)
          Called by the child entity when it should be removed from the directory.
 void renameChildEntity(String name, EntityImplementation entity, DirectoryImplementation oldParent, String oldName)
          Called by a child entity when it is renamed.
 String toString()
          Subclasses should override this to return something more pretty.
 
Methods inherited from class org.entityfs.impl.impl.AbstractEntityAcceptingEntityImpl
copyViewSettings, dereferenceOrNull, getFilter, hasFilters
 
Methods inherited from class org.entityfs.impl.impl.AbstractEntityImpl
addObserver, assertNotReadOnly, assertOk, assertParentIsReadLocked, assertParentIsWriteLocked, assertThisOrParentIsReadLocked, canRead, canWrite, compareTo, copy, copyAttributes, countObservers, createLock, createLockInternal, delete, deleteObserver, deleteObservers, disconnect, doDelete, equals, fireEvent, getAbsoluteLocationNoLocking, getCapability, getDelegate, getDelegateOfType, getDelegateOfTypeOrNull, getEntityAdapter, getFileSystem, getFileSystemImplementation, getLastModified, getLogAdapter, getName, getNameNoLocking, getObserverList, getParent, getParentInternal, getViewed, getViewedEntity, handleDisappeared, hasExecuteAccess, hashCode, hasReadAccess, hasWriteAccess, init, isConnected, isDeleted, isRootDirectory, isValid, rename, setLastModified, supportsCapability, touch, verifyHasExecuteAccess, verifyHasExecuteAccessToParent, verifyHasReadAccess, verifyHasReadAccessToParent, verifyHasWriteAccess, verifyHasWriteAccessToParent
 
Methods inherited from class org.entityfs.lock.AbstractLockable
assertIsReadLocked, assertIsWriteLocked, getLockAcquiringStrategy, getLockAdapter, getLocked, getReadLock, getWriteLock, isReadLockedByCurrentThread, isWriteLockedByCurrentThread, lock, lockForReading, lockForWriting, logLockStack
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.entityfs.EntityAcceptingEntityView
dereferenceOrNull
 
Methods inherited from interface org.entityfs.ViewCapable
getFilter, hasFilters
 
Methods inherited from interface org.entityfs.impl.EntityImplementation
assertIsReadLocked, assertIsWriteLocked, assertNotReadOnly, assertOk, assertParentIsReadLocked, assertParentIsWriteLocked, createLockInternal, fireEvent, getDelegate, getDelegateOfType, getDelegateOfTypeOrNull, getEntityAdapter, init, verifyHasExecuteAccess, verifyHasExecuteAccessToParent, verifyHasReadAccess, verifyHasReadAccessToParent, verifyHasWriteAccess, verifyHasWriteAccessToParent
 
Methods inherited from interface org.entityfs.impl.EntityViewImplementation
getAbsoluteLocationNoLocking, getFileSystemImplementation, getNameNoLocking, getObserverList
 
Methods inherited from interface org.entityfs.Entity
hasExecuteAccess, hasReadAccess, hasWriteAccess
 
Methods inherited from interface org.entityfs.EntityView
canRead, canWrite, copy, copyAttributes, createLock, disconnect, getCapability, getFileSystem, getLastModified, getParent, getViewed, getViewedEntity, isConnected, isRootDirectory, isValid, lock, rename, setLastModified, supportsCapability, touch
 
Methods inherited from interface java.lang.Comparable
compareTo
 
Methods inherited from interface org.entityfs.event.Observable
addObserver, countObservers, deleteObserver, deleteObservers
 
Methods inherited from interface org.entityfs.Named
getName
 
Methods inherited from interface org.entityfs.lock.ReadLockable
getReadLock, isReadLockedByCurrentThread, lockForReading
 
Methods inherited from interface org.entityfs.Deletable
delete, isDeleted
 
Methods inherited from interface org.entityfs.lock.WriteLockable
getWriteLock, isWriteLockedByCurrentThread, lockForWriting
 

Constructor Detail

DirectoryImpl

protected DirectoryImpl(FileSystemImplementation fs,
                        DirectoryImplementation parent,
                        DirectoryDelegate dd)
Method Detail

getType

public EntityType getType()
Description copied from interface: EntityView
Get the entity's EntityType.

Specified by:
getType in interface EntityView
Returns:
ETDirectory.TYPE, always.

isView

public boolean isView()
Returns:
false, always.

assertNoChildEntityWithName

public void assertNoChildEntityWithName(String name)
                                 throws EntityFoundException
Assert that an entity with the given name does not exist in the directory.

Specified by:
assertNoChildEntityWithName in interface DirectoryImplementation
Parameters:
name - The name of the child entity.
Throws:
EntityFoundException - If an entity with the given name exists in the directory.

assertCanBeDeleted

public void assertCanBeDeleted()
Description copied from class: AbstractEntityImpl
Perform entity type-specific checks in addition to the checks in the delete method.

Specified by:
assertCanBeDeleted in class AbstractEntityImpl

handleDisappearingChild

public void handleDisappearingChild(EntityImplementation entity)
Called whenever it is detected that a child entity has disappeared from the directory.

Specified by:
handleDisappearingChild in interface DirectoryImplementation
Parameters:
entity -

removeChildEntity

public void removeChildEntity(EntityImplementation entity,
                              DirectoryImplementation newParent)
Description copied from interface: DirectoryImplementation
Called by the child entity when it should be removed from the directory. Responsible for removing the entity from the directory's ChildEntityManager and for firing the EntityModifiedEvent and ChildEntityRemovedEvent events.

Specified by:
removeChildEntity in interface DirectoryImplementation
Parameters:
entity - The entity to remove.
newParent - The entity's parent directory after the current operation if the entity is being moved. This is set to null if the entity is deleted.

addChildEntity

public void addChildEntity(String name,
                           EntityImplementation entity)
Description copied from interface: DirectoryImplementation
Used to add a new entity to the directory when an entity is created. This method is responsible for adding the entity to the ChildEntityManager and for firing the ChildEntityAddedEvent and EntityModifiedEvent events.

Specified by:
addChildEntity in interface DirectoryImplementation
Parameters:
name - The entity's name.
entity - The entity.

renameChildEntity

public void renameChildEntity(String name,
                              EntityImplementation entity,
                              DirectoryImplementation oldParent,
                              String oldName)
Description copied from interface: DirectoryImplementation
Called by a child entity when it is renamed. If renamed within the same directory, the child entity should have removed itself by calling DirectoryImplementation.removeChildEntity(EntityImplementation, DirectoryImplementation) before calling this method, so it should not be present in the child entity manager by the time that this method is called. This method is responsible for adding the child entity to the child entity manager and for firing the ChildEntityRenamedEvent if the entity was renamed within the same directory, or the ChildEntityAddedEvent if the entity was moved in from another directory.

Specified by:
renameChildEntity in interface DirectoryImplementation
Parameters:
name - The entity's (new) name.
entity - The entity.
oldParent - The old parent directory.
oldName - The old name.

containsEntity

public boolean containsEntity(String name)
Description copied from interface: EntityHolder
Can an entity with the given name be retrieved from this object? If this is called on a symbolic link, it is checked if the link target is a directory and that directory contains an entity with the given name.

Specified by:
containsEntity in interface EntityHolder
Parameters:
name - The name of the entity.
Returns:
true if there is an entity with the given name referenced by this holder.

containsEntity

public boolean containsEntity(EntityView ev)
Description copied from interface: EntityHolder
Does this entity holder contain the supplied entity? If this is called on a symbolic link, it is checked if the link target is a directory and that directory contains the entity.

Specified by:
containsEntity in interface EntityHolder
Parameters:
ev - The entity (or a view of it).
Returns:
true If the entity holder contains the supplied entity.

canBeDereferenced

public boolean canBeDereferenced()
Description copied from interface: EntityAcceptingEntityView
Can the entity-accepting entity be dereferenced. An entity-accepting entity can be dereferenced if it is a reference to another entity (if it is a symbolic link, for instance).

Specified by:
canBeDereferenced in interface EntityAcceptingEntityView<DirectoryView>
Returns:
false, always.

getEntityOrNull

public Entity getEntityOrNull(String name)
Description copied from interface: EntityHolder
Get the named entity. For directories, this is an entity in the directory. For a symbolic link pointing to a directory, this is an entity in the target directory.

If this is called on a directory view, and the returned entity is ViewCapable, the returned entity view inherits the same view settings as the directory view (same filter instances).

If this is called on an EntityHolder that contains several entities with the same name, the implementation may choose how to deal with that. This can never happen for directories since entity names in a directory are required to be unique.

Specified by:
getEntityOrNull in interface EntityHolder
Parameters:
name - The name of the entity.
Returns:
An EntityView or null. If this method is called on a DirectoryView and the returned entity is ViewCapable , the returned object inherits the view settings of the parent. If the returned entity is a file, the returned object can be cast to an EFile, if it is a directory, it can be cast to a DirectoryView (or a Directory if this method is called on a Directory rather than a DirectoryView). The type of the returned object can be determined by calling its EntityView.getType() method or by using instanceof.

listEntities

public Set<EntityView> listEntities()
Description copied from interface: EntityListable
Return all entities that are visible in this directory. The result is not sorted in any way.

If this is called on a directory view, all returned ViewCapable entities inherit the view's filters.

Specified by:
listEntities in interface EntityListable
Returns:
A Set of entity views. If this is called on a directory entity object, a set of entity objects (not views) are returned. The set may be empty, but never null.

iterator

public Iterator<EntityView> iterator()
Specified by:
iterator in interface Iterable<EntityView>
See Also:
Iterable

isEmpty

public boolean isEmpty()
Description copied from interface: EntityListable
Is the listable empty? If a directory view is empty, the viewed directory may still contain entities that are hidden by the view's filter.

Specified by:
isEmpty in interface EntityListable
Returns:
true if the directory view is empty.

newEntity

public EntityImplementation newEntity(EntityType et,
                                      String name,
                                      Object data)
                               throws EntityFoundException
Description copied from interface: DirectoryView
Create a new child entity in the directory. If this is called on a directory view, and the created entity is ViewCapable, a view with the same filter instances as the directory view is returned.

Directories also contains methods for creating child entities.

Specified by:
newEntity in interface DirectoryView
Parameters:
et - The type of the entity to create.
name - The name of the new entity.
data - Entity type-specific data needed to create the entity. Most entity types (files and directories) happily accept null.
Returns:
The new entity. If the entity is ViewCapable, it inherits the view settings of this directory view. The result can be casted to the entity interface specified by the supplied entity type, et. For instance to an EFile if et is an ETFile or to a DirectoryView if et is an ETDirectory.
Throws:
EntityFoundException - If an entity with the same name already exists in the directory.

getUniqueEntityName

public String getUniqueEntityName(String prefix,
                                  String suffix)
Description copied from interface: DirectoryView
Get a unique entity name with the given prefix and suffix in the directory. The entity name is unique in the directory (not only in the view) at the time of calling.

Specified by:
getUniqueEntityName in interface DirectoryView
Parameters:
prefix - The prefix string to be used in generating the entity name. It must contain at least three characters.
suffix - The file name suffix, for instance .tmp.
Returns:
A unique entity name.

newView

public DirectoryView newView(Filter<? super EntityView> filter)
Description copied from interface: ViewCapable
Create a new, connected view on this entity or entity view.

Specified by:
newView in interface ViewCapable<DirectoryView>
Parameters:
filter - The filter that determines which child entities will be visible in the view.
Returns:
A new view on this entity or entity view. The type of the object returned is determined by the type of the entity viewed. A view for a Directory is a DirectoryView, for instance.

getViewedDir

public DirectoryView getViewedDir()

getViewedDirImplementation

public DirectoryImplementation getViewedDirImplementation()

copy

public Set<EntityView> copy(DirectoryView targetDir,
                            String targetName,
                            boolean preserveAttrs,
                            EntityLock... setupLocks)
Description copied from interface: EntityView
Copy the viewed entity to a new directory and possibly give the target another name. The target directory may be in another file system. If this is called on an entity view, an identical view of the new entity is returned (same Filter instances). If an entity view is copied is copied into a directory view, the both view's filters are combined in the returned view.

This method returns a list of entity view objects rather than just a single EntityView object. The reason for this is that the target file system may have different ideas about what constitutes an entity compared with the source file system. An entity in one file system might be three entities in another file system, or no entity at all. When copying within the same file system or between file systems with the same set of capabilities, the returned list always has one and only one entity view in it.

This method does not copy directories recursively. To do that, use the IteratorCopier.

This method can release locks that are only necessary for setting up the target entity before data is copied to it. This may be the write lock for the target directory and the read lock for the source entity's parent directory. See the setupLocks parameter.

If the copied entity supports any ECEntityAttributes capability, its attributes are copied by this method if the preserveAttrs parameter is set to true.

Specified by:
copy in interface EntityView
Parameters:
targetDir - A view of the target directory.
targetName - The target entity's name.
preserveAttrs - Should attributes from the source entity be copied to the target entity? An example of an entity attribute is its latest modification time. Capabilities may add their own attributes; see the documentation for each capability for details.
setupLocks - Locks only necessary for setting up the target entity. They are unlocked before data is copied to the target entity. The method is guaranteed to release locks given here, even if it throws an exception. The read lock on the source parent directory and/or the write lock on the target directory can be supplied in this collection. If any or both of the locks are, they are unlocked before any file data is copied to the target entity.
Returns:
A collection of entities that were created as a result of the copy operation viewed through the same set of filters as this view. When copying within the same file system (or between file systems with the same set of capabilities), this list always contains just one entity view. If the new entity is invisible in the target directory view, the entity view object is returned anyway.

copyFrom

public Set<EntityView> copyFrom(EntityImplementation ent,
                                String targetName,
                                boolean preserveAttrs,
                                List<EntityAttributeSetter> asl,
                                EntityLock[] setupLocks)
Description copied from interface: DirectoryViewImplementation
Copy the contents from the EntityAdapter to one or more new entities in the viewed directory. The directory implementation of this method delegates to DirectoryDelegate.copyFromEntity(EntityImplementation, String, boolean, List, EntityLock[]) after validating the target directory.

Specified by:
copyFrom in interface DirectoryViewImplementation
Parameters:
ent - The entity to copy from.
targetName - The name of the new entity.
preserveAttrs - Should entity attributes be copied to the target entity?
asl - A list of EntityAttributeSetter objects that are run just after the target entity has been created, before the setupLocks are released. May be null. This may only be non- null if preserveAttrs is true.
setupLocks - Locks required for setting up the target entity. The locks are released when the target entity has been created. It is the responsibility of this method to unlock the lock (once), even in case of errors.
Returns:
The created entities. ViewCapable entities inherit the filter instances from this directory view.

createChildDelegate

public EntityDelegate createChildDelegate(EntityType et,
                                          String name,
                                          Object data)

deleteInternal

protected void deleteInternal()
Description copied from class: AbstractEntityImpl
This method is called by AbstractEntityImpl.delete() after it has performed all validity, access control and lock checks. Subclasses implement this to call their delegate's delete method.

Specified by:
deleteInternal in class AbstractEntityImpl

toString

public String toString()
Description copied from class: AbstractEntityImpl
Subclasses should override this to return something more pretty.

Overrides:
toString in class AbstractEntityImpl