org.entityfs.util
Class IteratorCopier

java.lang.Object
  extended by org.entityfs.util.AbstractIteratorUtil
      extended by org.entityfs.util.IteratorCopier

public class IteratorCopier
extends AbstractIteratorUtil

The IteratorCopier takes a tree of entities returned from an iterator and copies them to another entity tree, possibly in another file system. It automatically creates necessary directories at the target.

The copier's source directory is used to create the target location of the copied entities. A copied entity will be put with the same location relative to the target directory as it had relative to the source directory.

The client can only use an IteratorCopier instance once.

The copier locks entities and parent entities as needed, but it never holds any locks between two copy operations. (In other words: when it calls next on the iterator, it does not hold any locks.)

An optional ErrorHandlingStrategy can be used to deal with errors. The default strategy when an exception is caught is to abort the copy operation and rethrow the exception.

The copier uses a DirectoryView as a source directory rather than an EntityListable since every copied entity's location relative to the source directory is significant for finding its destination location.

EntityFS iterators are not thread safe. Don't use them concurrently from several threads without synchronizing access to them.

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

Constructor Summary
IteratorCopier(DirectoryView sourceDir, DirectoryView target)
          Create an IteratorCopier with one target directory and the default error strategy (see above).
IteratorCopier(DirectoryView sourceDir, Iterator<? extends EntityView> itr, Collection<? extends DirectoryView> targets)
          Create an IteratorCopier with a collection of target directories and the default error strategy (see above).
IteratorCopier(DirectoryView sourceDir, Iterator<? extends EntityView> itr, Collection<? extends DirectoryView> targets, ErrorHandlingStrategy errorStrategy)
          Create an IteratorCopier with a collection of target directories and the supplied error strategy.
IteratorCopier(DirectoryView sourceDir, Iterator<? extends EntityView> itr, DirectoryView target)
          Create an IteratorCopier with one target directory and the default error strategy (see above).
IteratorCopier(DirectoryView sourceDir, Iterator<? extends EntityView> itr, DirectoryView target, ErrorHandlingStrategy errorStrategy)
          Create an IteratorCopier with one target directory and the supplied error strategy.
 
Method Summary
 void copy()
          Copy all entities returned from the iterator.
 void copy(boolean overwrite, boolean preserveAttr)
          Copy all entities returned from the iterator.
 void copy(OverwriteStrategy overwriteStrategy, boolean preserveAttr)
          Copy all entities returned from the iterator.
 
Methods inherited from class org.entityfs.util.AbstractIteratorUtil
letErrorStrategyHandle, letErrorStrategyHandle, letErrorStrategyHandle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IteratorCopier

public IteratorCopier(DirectoryView sourceDir,
                      DirectoryView target)
Create an IteratorCopier with one target directory and the default error strategy (see above). A depth-last iterator created on the source directory is used for copying the files.

Parameters:
sourceDir - The source directory
target - The target directory.

IteratorCopier

public IteratorCopier(DirectoryView sourceDir,
                      Iterator<? extends EntityView> itr,
                      DirectoryView target)
Create an IteratorCopier with one target directory and the default error strategy (see above).

Parameters:
sourceDir - The source directory. This is used to create target locations for copied entities.
itr - The Iterator the copier will read entities to copy from. It's a little more efficient to use a DepthLastIterator than a DepthFirstIterator when copying hierarchies.
target - The target directory. Entities will be copied to the same location relative to the target directory that they have relative to the source directory.

IteratorCopier

public IteratorCopier(DirectoryView sourceDir,
                      Iterator<? extends EntityView> itr,
                      DirectoryView target,
                      ErrorHandlingStrategy errorStrategy)
Create an IteratorCopier with one target directory and the supplied error strategy.

Parameters:
sourceDir - The source directory. This is used to create target locations for copied entities.
itr - The Iterator the copier will read entities to copy from. It's a little more efficient to use a DepthLastIterator than a DepthFirstIterator when copying hierarchies.
target - The target directory. Entities will be copied to the same location relative to the target directory that they have relative to the source directory.
errorStrategy - The error strategy.

IteratorCopier

public IteratorCopier(DirectoryView sourceDir,
                      Iterator<? extends EntityView> itr,
                      Collection<? extends DirectoryView> targets)
Create an IteratorCopier with a collection of target directories and the default error strategy (see above).

Parameters:
sourceDir - The source directory. This is used to create target locations for copied entities.
itr - The Iterator the copier will read entities to copy from. It's a little more efficient to use a DepthLastIterator than a DepthFirstIterator when copying hierarchies.
targets - The collection of target directories. Entities will be copied to the same location relative to a target directory that they have relative to the source directory.

IteratorCopier

public IteratorCopier(DirectoryView sourceDir,
                      Iterator<? extends EntityView> itr,
                      Collection<? extends DirectoryView> targets,
                      ErrorHandlingStrategy errorStrategy)
Create an IteratorCopier with a collection of target directories and the supplied error strategy.

Parameters:
sourceDir - The source directory. This is used to create target locations for copied entities.
itr - The Iterator the copier will read entities to copy from. It's a little more efficient to use a DepthLastIterator than a DepthFirstIterator when copying hierarchies.
targets - The target directories. Entities will be copied to the same location relative to a target directory that they have relative to the source directory.
errorStrategy - The error strategy.
Method Detail

copy

public void copy(OverwriteStrategy overwriteStrategy,
                 boolean preserveAttr)
Copy all entities returned from the iterator.

Parameters:
overwriteStrategy - The strategy to use for overwriting existing entities.
preserveAttr - Should entity attributes such as the last modification times of entities be copied to the target entities?
Throws:
UncheckedInterruptedException - If the copy operation is interrupted.
See Also:
copy(), copy(boolean, boolean)

copy

public void copy(boolean overwrite,
                 boolean preserveAttr)
          throws UncheckedInterruptedException
Copy all entities returned from the iterator.

Parameters:
overwrite - If true, this means that the overwrite strategy DontOverwriteAndLogWarning is used. If false, this means that the strategy DontOverwriteAndLogWarning is used.
preserveAttr - Should entity attributes such as the last modification times of entities be copied to the target entities?
Throws:
UncheckedInterruptedException - If the copy operation is interrupted.
See Also:
copy(), copy(OverwriteStrategy, boolean)

copy

public void copy()
          throws UncheckedInterruptedException
Copy all entities returned from the iterator. If an entity already exist at the target location, a warning is logged and the existing entity is not overwritten.

Entity attributes such as their last modification times are not copied.

Throws:
UncheckedInterruptedException - If the copy operation is interrupted.
See Also:
copy(boolean,boolean), copy(OverwriteStrategy, boolean)