@MinDoc(copyright="Copyright 2000 - 2009, 2015, 2021 A. Weinert", author="Albrecht Weinert", version="V.51", lastModified="7.06.2021", usage="use when multiple file and directories have to be worked on", purpose="file and directory services") public class FileService extends Object implements Serializable
recursion
,
"revert or equalise the roles of source and destination"
(recursion
respectively biDirect
)filCrit
) and
directories (dirCrit
) to act on as two (associated)
FileCriteria
objects.FileCriteria
.recursion
,
createLowerCase
, noLCforTypes
, delEmpty
etc..
These properties form a fairly complete set for a very wide variety of
services, so some of them are, of course not relevant, for a concrete
service to be implemented. Examples of services / tools using
FileService
are de.frame4j.Update, FuR
,
de.frame4j.Del and SVNkeys
, to name a few.FileService
object have
to be set. The most comfortable way is do do that in one step by the
method set(PropMap)
in one step using PropMap
object with
the right settings .dirVisit()
.
It will visit all directories and files, that meet the selection criteria
and other properties.FileVisitor
. By providing up to three such objects one
can (but must not) implement separately the single isolated action onFileVisitor
object. Is nothing to do specially in one of the
cases, simply supply null.FileVisitor
's method
FileVisitor.visit
(File
)
implements the wanted action in each case. And that can be anything like
even just do nothing, just list, delete, or even quit complex text
operation like the Subversion (SVN) / CVS keyword beautifying for
deployment by a tool like SVNkeys
.dirVisit()
FuR
(Find and Replace on a set of files with arbitrary complex criteria).
After preparing all criteria and text replacements (most of
FuR
's code) all text replacements on all files are just
done by using the FileService
object (here called dS):
dS.dirVisit
(path, null,
replaceBesucher, // the visitor for files
null, // visitor for empty directory (no action)
null, // visitor for filled directory (no action)
null); // dito for visit after content visiting
Implementation hint: Writing the FileVisitor
as an inner class
of the application / tool, as is done with replaceBesucher in
FuR
, gives that visitor access to all application
information needed. (If just one visitor is needed it can, of course
be directly implemented by the application class.makeOutputListVisitor(PrintWriter)
,
makeOutputInfoVisitor(PrintWriter)
and
makeInCollectionVisitor()
)
this class offers three types of multiply usable
FileVisitor
object, used, of course, in Frame4J's tools.doUpdate()
(used / wrapped in the tool
de.frame4j.Update),doClean()
.dirVisit()
.
and hence all calls to the the FileVisitor
's methods
FileVisitor.visit
(File
) come in a
deterministic sequence in one thread. Hence, from this respect, those
methods implementing FileVisitor
don't have to be thread safe and
don't have to bother with concurrency in their book keeping or
whatever.FuR
and
SVNkeys
as examples — are done sequentially
adding their execution time. If the actions on single files (as in the
example SVNkeys
) were independent, concurrent execution
would be allowed. The gain in time could be quite significant alone by
not adding waits on I/O operations (even on just one core).dirVisit(... queuingVisitor ..)
that returns at once
delegating the real work
(via aBlockingQueue
) to an arbitrary
number of working threads.Modifier and Type | Field and Description |
---|---|
protected boolean |
biDirect
Use both copy or update directions at once.
|
protected boolean |
createLowerCase
Make new files and directories with lower case names.
|
protected boolean |
delEmpty
Delete empty files (length 0) or directories (0 files, subdirectories).
|
protected boolean |
delEmptySource
Delete empty files or directories in the source directory.
|
protected boolean |
delTree
Delete directories, even when not empty.
|
protected long |
difOld
Minimal age difference of files.
|
FileCriteria |
dirCrit
Directory criteria.
|
FileCriteria |
filCrit
File criteria.
|
protected boolean |
makeDirs
Make not yet existing (sub) directories.
|
protected String |
noLCforTypes
Avoid lower casing names for specified types.
|
protected boolean |
nonew
No creation of non existing files.
|
protected boolean |
recursion
Recursively visit subdirectories.
|
protected boolean |
reverse
Reversing the copy or update direction.
|
protected Verbos |
verbosity
Verbosity of reports.
|
protected boolean |
zoneSafe
Summer / winter time switch precaution.
|
Constructor and Description |
---|
FileService()
Standard Constructor.
|
FileService(FileCriteria filCrit,
FileCriteria dirCrit,
boolean recursion)
Constructor with criteria.
|
FileService(FileService other)
Copy constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
dirVisit(String path,
String name,
FileVisitor fileBes,
FileVisitor emptyDirBes,
FileVisitor dirBes)
Working method to visit files and directories.
|
void |
dirVisit(String path,
String name,
FileVisitor fileBes,
FileVisitor emptyDirBes,
FileVisitor dirBes,
FileVisitor dirBes2)
This FileVisitors working method to visit files and directories.
|
int |
doClean(File soD,
File deD,
File bcD,
PrintWriter log)
Working method to clean up files and directories.
|
int |
doUpdate(File soD,
File deD,
File bcD,
PrintWriter log)
Working method for dating or backing up files and directories.
|
long |
getDifOld()
Minimal age difference of files.
|
String |
getNoLCforTypes()
Avoid lower casing names for specified types.
|
Verbos |
getVerbosity()
Verbosity of reports.
|
boolean |
isBidirect()
Use both copy or update directions at once.
|
boolean |
isCreateLowerCase()
Make new files and directories with lower case names.
|
boolean |
isDelEmpty()
Delete empty files (length 0) or directories (0 files, subdirectories).
|
boolean |
isDelEmptySource()
Delete empty files or directories in the source directory.
|
boolean |
isDelTree()
Delete directories, even when not empty.
|
boolean |
isMakeDirs()
Make not yet existing (sub) directories.
|
boolean |
isNonew()
No creation of non existing files.
|
boolean |
isRecursion()
Recursively visit subdirectories..
|
boolean |
isReverse()
Reversing the copy or update direction.
|
boolean |
isZoneSafe()
Summer / winter time switch precaution.
|
static FileVisitor |
makeInCollectionVisitor(Collection<File> vec,
boolean noDoub)
Produce a collecting visitor (a FileVisitor factory).
|
static FileVisitor |
makeOutputInfoVisitor(PrintWriter pos)
Produce an informing visitor (a FileVisitor factory).
|
static FileVisitor |
makeOutputListVisitor(PrintWriter pos)
Produce a listing visitor (a FileVisitor factory).
|
void |
set(PropMap prop)
Set the properties by a PropMap object.
|
void |
setBidirect(boolean biDirect)
Use both copy or update directions at once.
|
void |
setCreateLowerCase(boolean createLowerCase)
Make new files and directories with lower case names.
|
void |
setDelEmpty(boolean delEmpty)
Delete empty files (length 0) or directories (0 files, subdirectories).
|
void |
setDelEmptySource(boolean delEmptySource)
Delete empty files or directories in the source directory.
|
void |
setDelTree(boolean delTree)
Delete directories, even when not empty.
|
void |
setDifOld(long difOld)
Set the minimal age difference of files.
|
void |
setMakeDirs(boolean makeDirs)
Make not yet existing (sub) directories.
|
void |
setNoLCforTypes(String noLCforTypes)
Avoid lower casing names for specified types.
|
void |
setNonew(boolean nonew)
No creation of non existing files.
|
void |
setRecursion(boolean recursion)
Recursively visit subdirectories.
|
void |
setReverse(boolean reverse)
Reversing the copy or update direction.
|
void |
setZoneSafe(boolean zoneSafe)
Summer / winter time switch precaution.
|
String |
toString()
State as String.
|
protected boolean reverse
isReverse()
protected boolean biDirect
isBidirect()
protected boolean delTree
isDelTree()
protected boolean recursion
isRecursion()
protected boolean delEmpty
isDelEmpty()
protected boolean delEmptySource
isDelEmptySource()
protected boolean nonew
isNonew()
protected boolean makeDirs
isMakeDirs()
protected boolean createLowerCase
isCreateLowerCase()
protected String noLCforTypes
getNoLCforTypes()
protected Verbos verbosity
getVerbosity()
protected long difOld
getDifOld()
protected boolean zoneSafe
isZoneSafe()
public final FileCriteria filCrit
FileCriteria
objects, filCrit
and dirCrit
,
hold all (filter) criteria for files and directories for services /
operations using this FileService
object.public final FileCriteria dirCrit
filCrit
public FileService()
public FileService(FileCriteria filCrit, FileCriteria dirCrit, boolean recursion)
public FileService(FileService other)
FileService
object that must not be null.public boolean isReverse()
public void setReverse(boolean reverse)
isReverse()
public boolean isBidirect()
reverse
no services implemented in this
class uses (obeys) this flag. It's here to make FileService
more
commonly usable.public void setBidirect(boolean biDirect)
isBidirect()
public boolean isDelTree()
public void setDelTree(boolean delTree)
isDelTree()
public boolean isRecursion()
public void setRecursion(boolean recursion)
public boolean isDelEmpty()
doClean()
:doUpdate()
:delEmptySource
and delEmpty
are true and if
the (younger) source file has zero (0) length not only the corresponding
destination file will be deleted, but (on success) this zero length source
file also.doClean()
to delete all
destination files that don't exist in the sourcepublic void setDelEmpty(boolean delEmpty)
isDelEmpty()
public boolean isDelEmptySource()
FileService
based
service) may result in modifying source directories, which might be
semantically surprising.public void setDelEmptySource(boolean delEmptySource)
isDelEmptySource()
public boolean isNonew()
doUpdate()
:public void setNonew(boolean nonew)
nonew
is set to false by this method
makeDirs
will be set so too.isNonew()
public boolean isMakeDirs()
doUpdate()
:public void setMakeDirs(boolean makeDirs)
public boolean isCreateLowerCase()
doUpdate()
public void setCreateLowerCase(boolean createLowerCase)
isCreateLowerCase()
public final String getNoLCforTypes()
noLCforTypes
has no effect if
null or if createLowerCase
is false.createLowerCase
is true and
noLCforTypes
is a not empty list of types
files of that type will be created in the case given by the source's name
and not converted to lower case.public void setNoLCforTypes(String noLCforTypes)
public final Verbos getVerbosity()
AppHelper
.Verbos.toString()
.TEST
all implemented services
shall show no effect to the out side world, i.e. manipulate no files or
directories.Verbos.NORMAL
(10)Verbos.SILENT
,
Verbos.NORMAL
,
Verbos.VERBOSE
,
Verbos.DEBUG
,
Verbos.TEST
public long getDifOld()
doUpdate(File, File, File, PrintWriter)
that means
the switch to "hard" XCopy behaviour.doUpdate()
public void setDifOld(long difOld)
difOld < 0
and difOld != -2
will be
taken as -1.getDifOld()
public boolean isZoneSafe()
public void setZoneSafe(boolean zoneSafe)
isZoneSafe()
public void set(PropMap prop) throws IllegalArgumentException
FileService
object will
be set by the provided PropMap
's following
properties (if given):createLowerCase
makeDirs
.nonew
.dirCrit
and in
filCrit
.dirCrit
.dirCrit
.filCrit
.filCrit
.filCrit
to
"since days * 24 h before now until ... ".filCrit
to
"from ... until days * 24 h before now".filCrit
.TimeHelper.parse()
as a time. Otherwise an Exception will occur. The same will occur if the
time boundary in case will also be defined by property days respectively
daysOld.filCrit
.prop
(or prop itself is null) nothing happens in each case.prop
- a Map containing the properties to set. In applications /
tools this will most often be the App
's own PropMapIllegalArgumentException
- in case of wrong or contradictory
(like since as well as days) propertiespublic String toString()
public final int doUpdate(File soD, File deD, File bcD, PrintWriter log)
FileService
object files will be copied from a source directory soD
to a
destination directory deD
.
If ordered so, files before being replaced in the destination directory
may be copied before to a backup directory bcD
.reverse
if true will interchange
the roles of destination and source, i.e.
soD
and deD
.recursively
including sub-directories. The than perhaps required making of
destinations's new sub-directories would be done automatically, but
can be forbidden.nonew
).noRepalce
).difOld
makes the copying independent
of age (xcopy option).difOLd
and zoneSafe
can form a line of defence (of proven efficiency in many those
circumstances).bcD
if that is
given.File.list()
used
here to also on descending to sub-directories returns null on
I/O error. This will be logged if possible. That event will, of course,
halt further descending recursively from this directory, just as if
would contain no sub-directories (be that true or not).soD
- source directorydeD
- destination directorybcD
- backup directory or null, if no "two stage "
backup wanted.log
- for reports or null, if no logging at all is asked forpublic final int doClean(File soD, File deD, File bcD, PrintWriter log)
FileService
object files will be deleted in the destination directory deD
if
the related file does not exist in the source directory soD
.soD
directory is not specified or does not exist, the latter
condition is regarded as fulfilled: the file will be deleted.bcD
.recursively
including sub-directories.reverse
if true will interchange
the roles of destination and source, i.e. soD
and
deD
.deD
must denote an existing directory.soD
and bcD
may be null, denote and existing or not
existing directory, but never an existing file.soD
- source directory (acts quasi as list of files and directories
NOT to be deleted)deD
- destination directory (it's here where the cleaning
work is done)bcD
- backup directory for files deleted in deD or null, if no two
stage clean up is asked for. (must not be a file!)log
- for reports or null, if no logging at all is asked fordoUpdate()
public final void dirVisit(String path, String name, FileVisitor fileBes, FileVisitor emptyDirBes, FileVisitor dirBes, FileVisitor dirBes2)
path
and name
denote exactly any one file it will be
visited calling filBes.visit()
if fileBes
is not null.
"Any one file" in this means all that could mean an existing or
not existing file but not an existing directory.path
and name
denote an existing directory all files
matching filCrit
and all directories matching dirCrit
are determined. (Its an internal snapshot of that diretory's state.)
Then the following is done:filCrit
).recursion
is false dirBes.visit()
will be called for all directories in the snapshot list (matching
dirCrit
) be they empty or not.recursion
is true the whole
proceeding will be done for every directory in the snapshot list
(matching dirCrit
). (That is done, of course, by
by calling this method recursively.)path
- the start (parent) path (not null!)name
- the start file or directory; may be null if completely
included in pathfileBes
- implements the action on each file in case (matching
filCrit
)emptyDirBes
- implements the action on the (start) directory
if it contains neither files nor directories matching the
criteria (dirCrit
or filCrit
);
"empty" means nothing with respect to those criteria.dirBes
- implements a) on one hand implements the action on
the (start) directory if it is not empty in the above
sense.dirCrit
.dirBes2
- implements the action on the (start) directory do be
done after returning from all file and / or directory visits
on its content.public final void dirVisit(String path, String name, FileVisitor fileBes, FileVisitor emptyDirBes, FileVisitor dirBes)
dirVisit
(path, name, fileBes, emptyDirBes, dirBes, null)public static FileVisitor makeOutputListVisitor(PrintWriter pos)
FileVisitor
object made by this method just lists all visited
File
s on the PrintWriter provided.FileVisitor
's method
visit(File dD)
returns 0 as
normal, -1 if pos
is null and -2 if the provided File
dD
is null.pos
- the list output (null is accepted but makes no sense)FileVisitor
,
FileHelper.listLine(File)
public static FileVisitor makeOutputInfoVisitor(PrintWriter pos)
FileVisitor
object made by this method just lists all visited
File
s on the PrintWriter provided.FileVisitor
's method
visit(File dD)
returns 0 as
normal, -1 if pos
is null and -2 if the provided File
dD
is null.pos
- the list output (null is accepted but makes no sense)FileVisitor
,
FileHelper.infoLine(File)
public static FileVisitor makeInCollectionVisitor(Collection<File> vec, boolean noDoub)
FileVisitor
object made by this method just puts all visited
File
s into the Collection provided.FileVisitor
's method
visit(File dD)
returns 0 as
normal, -1 if vec
is null and -2 if the provided File
dD
is null. null is not put into vec
.vec
- the collecting containernoDoub
- do not enter a File
twice; i.e. enforce set
behaviour even if vec
if not of type Set
FileVisitor