public class LocalUWSFileManager extends java.lang.Object implements UWSFileManager
The name of the log file, the result files and the backup files may be
customised by overriding the following functions:
getLogFileName(uws.service.log.UWSLog.LogLevel, String)
,
getResultFileName(Result, UWSJob)
,
getBackupFileName(JobOwner)
and getBackupFileName()
.
By default, results and backups are grouped by owner/user and owners/users
are grouped thanks to DefaultOwnerGroupIdentifier
. By using the
appropriate constructor, you can change these default behaviours.
A log file rotation is set by default so that avoiding a too big log file
after several months/years of use. By default the rotation is done every
month on the 1st at 6am. This frequency can be changed easily thanks to the
function setLogRotationFreq(String)
.
Modifier and Type | Class and Description |
---|---|
protected static class |
LocalUWSFileManager.DirectoryFilter
Filter which lets returning only the directories.
|
protected class |
LocalUWSFileManager.LocalAllUserBackupInputs
Lets iterating on all user backup files.
|
protected class |
LocalUWSFileManager.OwnerFileFilter
Filter which lets returning only the backup file(s) of the specified user/owner.
|
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
DEFAULT_BACKUP_FILE_NAME
Default name of the general UWS backup file.
|
protected static java.lang.String |
DEFAULT_LOG_FILE_NAME
Default name of the log file.
|
protected boolean |
groupUserDirectories
Gather user directories, set by set.
|
protected java.io.PrintWriter |
logOutput
Output toward the service log file.
|
protected EventFrequency |
logRotation
Frequency at which the log file must be "rotated" (the file is renamed with the date of its first write and a new log file is created).
|
protected boolean |
oneDirectoryForEachUser
Indicate whether a directory must be used to gather all jobs, results and errors related to one identified user.
|
protected OwnerGroupIdentifier |
ownerGroupId
Object giving the policy about how to group user directories.
|
protected java.io.File |
rootDirectory
Directory in which all files managed by this class will be written and read.
|
protected java.io.File |
tmpDirectory
Directory in which temporary files (e.g.
|
TMP_UPLOAD_DIR
Constructor and Description |
---|
LocalUWSFileManager(java.io.File root)
Builds a
UWSFileManager which manages all UWS files in the given directory. |
LocalUWSFileManager(java.io.File root,
boolean oneDirectoryForEachUser,
boolean groupUserDirectories)
Builds a
UWSFileManager which manages all UWS files in the given directory. |
LocalUWSFileManager(java.io.File root,
boolean oneDirectoryForEachUser,
boolean groupUserDirectories,
OwnerGroupIdentifier ownerGroupIdentifier)
Builds a
UWSFileManager which manages all UWS files in the given directory. |
Modifier and Type | Method and Description |
---|---|
protected void |
cleanOwnerDirectory(JobOwner owner)
Removes the owner directory if there is no more file in it (except the backup file which is no more required).
|
protected boolean |
createParentDir(java.io.File f)
Creates the parent directory(ies) if it(they) does/do not exist.
|
boolean |
deleteError(ErrorSummary error,
UWSJob job)
Deletes the error file corresponding to the given job error summary.
|
boolean |
deleteResult(Result result,
UWSJob job)
Deletes the result file corresponding to the given job result's
description.
|
void |
deleteUpload(UploadFile upload)
Delete definitely the specified file, submitted inline in an HTTP
request.
|
java.util.Iterator<java.io.InputStream> |
getAllUserBackupInputs()
Gets an input stream on the backup file of ALL the job owners
(~ UWS user).
|
protected java.lang.String |
getBackupFileName()
Gets the name of the UWS general backup file.
|
protected java.lang.String |
getBackupFileName(JobOwner owner)
Gets the name of the backup file of the given job owner (~ UWS user).
|
java.io.InputStream |
getBackupInput()
Gets an input stream on the backup file of the whole UWS.
|
java.io.InputStream |
getBackupInput(JobOwner owner)
Gets an input stream on the backup file of ONLY the given job owner
(~ UWS user).
|
java.io.OutputStream |
getBackupOutput()
Gets an output stream on the backup file of the whole UWS.
|
java.io.OutputStream |
getBackupOutput(JobOwner owner)
Gets an output stream on the backup file of ONLY the given job owner
(~ UWS user).
|
protected java.io.File |
getErrorFile(ErrorSummary error,
UWSJob job)
Gets the file corresponding to the described error.
|
protected java.lang.String |
getErrorFileName(ErrorSummary error,
UWSJob job)
Gets the name of the file in which the described error is/must be written.
|
java.io.InputStream |
getErrorInput(ErrorSummary error,
UWSJob job)
Gets an input stream on the error file corresponding to the given error
and job.
|
java.io.OutputStream |
getErrorOutput(ErrorSummary error,
UWSJob job)
Gets an output stream on the error file corresponding to the given error
and job.
|
long |
getErrorSize(ErrorSummary error,
UWSJob job)
Gets the size of the specified error summary file.
|
protected java.io.File |
getFile(UploadFile upload)
Create a File instance from the given upload file description.
|
protected java.io.File |
getLogFile(UWSLog.LogLevel level,
java.lang.String context)
Gets the UWS log file.
|
protected java.lang.String |
getLogFileName(UWSLog.LogLevel level,
java.lang.String context)
Gets the name of the UWS log file.
|
java.io.InputStream |
getLogInput(UWSLog.LogLevel level,
java.lang.String context)
Gets an input stream on the log file of this UWS.
|
java.io.PrintWriter |
getLogOutput(UWSLog.LogLevel level,
java.lang.String context)
Gets an output stream on the log file of this UWS.
|
java.lang.String |
getLogRotationFreq()
Get the frequency of the log file rotation
in a human readable way.
|
java.io.File |
getOwnerDirectory(JobOwner owner)
Gets the directory of the given owner.
|
protected java.io.File |
getResultFile(Result result,
UWSJob job)
Gets the file corresponding to the given result.
|
protected java.lang.String |
getResultFileName(Result result,
UWSJob job)
Gets the name of the file in which the given result is/must be written.
|
java.io.InputStream |
getResultInput(Result result,
UWSJob job)
Gets an input stream on the result file corresponding to the given job
result.
|
java.io.OutputStream |
getResultOutput(Result result,
UWSJob job)
Gets an output stream on the result file corresponding to the given job
result.
|
long |
getResultSize(Result result,
UWSJob job)
Gets the size of the specified result file.
|
java.io.File |
getTmpDirectory()
Get the temporary directory to use in this service.
|
java.io.InputStream |
getUploadInput(UploadFile upload)
Open a stream toward the specified file, submitted inline in an HTTP
request.
|
java.lang.String |
moveUpload(UploadFile upload,
UWSJob destination)
Move the specified file from its current location to a location related
to the given job.
|
java.io.InputStream |
openURI(java.net.URI uri)
Open a stream toward the given URI.
|
protected void |
printLogHeader(java.io.PrintWriter out)
Print a header into the log file so that separating older log messages to the new ones.
|
void |
setLogRotationFreq(java.lang.String interval)
Set the frequency at which a rotation of the log file must be done.
|
boolean |
setTmpDirectory(java.io.File newTmpDir)
Set the temporary directory to use in this service.
|
protected static final java.lang.String DEFAULT_LOG_FILE_NAME
protected static final java.lang.String DEFAULT_BACKUP_FILE_NAME
protected final java.io.File rootDirectory
protected java.io.File tmpDirectory
protected java.io.PrintWriter logOutput
protected EventFrequency logRotation
protected final boolean oneDirectoryForEachUser
protected final boolean groupUserDirectories
oneDirectoryForEachUser
is TRUE.protected final OwnerGroupIdentifier ownerGroupId
public LocalUWSFileManager(java.io.File root) throws UWSException
Builds a UWSFileManager
which manages all UWS files in the given directory.
There will be one directory for each owner ID and owner directories will be grouped
thanks to DefaultOwnerGroupIdentifier
.
root
- UWS root directory.java.lang.NullPointerException
- If the given root directory is null.UWSException
- If the given file is not a directory or has not the READ and WRITE permissions.LocalUWSFileManager(File, boolean, boolean, OwnerGroupIdentifier)
public LocalUWSFileManager(java.io.File root, boolean oneDirectoryForEachUser, boolean groupUserDirectories) throws UWSException
Builds a UWSFileManager
which manages all UWS files in the given directory.
If, according to the third parameter, the owner directories must be grouped,
the DefaultOwnerGroupIdentifier
will be used.
root
- UWS root directory.oneDirectoryForEachUser
- true to create one directory for each owner ID, false otherwise.groupUserDirectories
- true to group user directories, false otherwise.
note: this value is ignored if the previous parameter is false.java.lang.NullPointerException
- If the given root directory is null.UWSException
- If the given file is not a directory or has not the READ and WRITE permissions.LocalUWSFileManager(File, boolean, boolean, OwnerGroupIdentifier)
public LocalUWSFileManager(java.io.File root, boolean oneDirectoryForEachUser, boolean groupUserDirectories, OwnerGroupIdentifier ownerGroupIdentifier) throws UWSException
UWSFileManager
which manages all UWS files in the given directory.root
- UWS root directory.oneDirectoryForEachUser
- true to create one directory for each owner ID, false otherwise.groupUserDirectories
- true to group user directories, false otherwise.
note: this value is ignored if the previous parameter is false.ownerGroupIdentifier
- The "function" to use to identify the group of a job owner.
DefaultOwnerGroupIdentifier
will be chosen as default group identifier.java.lang.NullPointerException
- If the given root directory is null.UWSException
- If the given file is not a directory or has not the READ and WRITE permissions.public java.io.File getOwnerDirectory(JobOwner owner)
owner
- A job owner.protected void cleanOwnerDirectory(JobOwner owner) throws java.io.IOException
owner
- The user whose the directory must be removed.java.io.IOException
- If there is an error while removing the owner directory.public final java.lang.String getLogRotationFreq()
public final void setLogRotationFreq(java.lang.String interval)
Set the frequency at which a rotation of the log file must be done.
"rotation" means here, to close the currently used log file, to rename it so that suffixing it with the date at which the first log has been written in it, and to create a new log file.
The frequency string must respect the following syntax:
Where: hh = integer between 0 and 23, mm = integer between 0 and 59, dd (for 'W') = integer between 1 and 7 (1:sunday, 2:monday, ..., 7:saturday), dd (for 'M') = integer between 1 and 31.
Warning: The frequency type is case sensitive! Then you should particularly pay attention at the case when using the frequency types 'M' (monthly) and 'm' (every minute).
Parsing errors are not thrown but "resolved" silently. The "solution" depends of the error. 2 cases of errors are considered:
Examples:
interval
- Interval between two log rotations.protected java.lang.String getLogFileName(UWSLog.LogLevel level, java.lang.String context)
Gets the name of the UWS log file.
By default: DEFAULT_LOG_FILE_NAME
.
level
- Level of the message to log (DEBUG, INFO, WARNING, ERROR, FATAL).context
- Context of the message to log (UWS, HTTP, THREAD, JOB, ...).protected java.io.File getLogFile(UWSLog.LogLevel level, java.lang.String context)
level
- Level of the message to log (DEBUG, INFO, WARNING, ERROR, FATAL).context
- Context of the message to log (UWS, HTTP, THREAD, JOB, ...).getLogFileName(uws.service.log.UWSLog.LogLevel, String)
public java.io.InputStream getLogInput(UWSLog.LogLevel level, java.lang.String context) throws java.io.IOException
UWSFileManager
getLogInput
in interface UWSFileManager
level
- Level of the message to log (DEBUG, INFO, WARNING,
ERROR or FATAL).context
- Context of the message to log (UWS, HTTP, JOB, THREAD,
...).java.io.IOException
- If there is an error while opening an input stream
on the log file.public java.io.PrintWriter getLogOutput(UWSLog.LogLevel level, java.lang.String context) throws java.io.IOException
UWSFileManager
Note: The log file must be automatically created if needed.
getLogOutput
in interface UWSFileManager
level
- Level of the message to log (DEBUG, INFO, WARNING,
ERROR or FATAL).context
- Context of the message to log (UWS, HTTP, JOB, THREAD,
...).java.io.IOException
- If there is an error while creating the log file or
while opening an output stream on it.protected void printLogHeader(java.io.PrintWriter out)
protected final java.io.File getFile(UploadFile upload)
upload
- Description of an uploaded file.public java.io.File getTmpDirectory()
UWSFileManager
Note: This directory is generally used when files are uploaded in a received HTTP request.
Important: As qualified above, this directory is temporary. It means that it should be sometimes emptied. It is particularly important because when a delete or move operation fails on a files, no log or error might be published and the files would take some space for no more reason.
getTmpDirectory
in interface UWSFileManager
null
.public boolean setTmpDirectory(java.io.File newTmpDir)
UWSFileManager
Note: This directory is generally used when files are uploaded in a received HTTP request.
Important: As qualified above, this directory is temporary. It means that it should be sometimes emptied. It is particularly important because when a delete or move operation fails on a files, no log or error might be published and the files would take some space for no more reason.
setTmpDirectory
in interface UWSFileManager
newTmpDir
- The new temporary directory to use.true
if the path of the temporary directory has
been successfully updated,
false
otherwise (e.g. null
, not a
directory, not writable, not readable).public java.io.InputStream getUploadInput(UploadFile upload) throws java.io.IOException
UWSFileManager
getUploadInput
in interface UWSFileManager
upload
- Description of the uploaded file.java.io.IOException
- If any error occurs while opening the stream.public java.io.InputStream openURI(java.net.URI uri) throws UnsupportedURIProtocolException, java.io.IOException
UWSFileManager
Most of the time, the given URI uses the protocol http, https or ftp,
which makes the URI perfectly understandable by URL
which is
then able to open easily a stream (cf URL.openStream()
).
However, a different scheme/protocol could be used ; particularly VO
ones like "ivo" and "vos". It is for these particular cases that this
function has been designed: in order to provide an implementation
supporting additional protocols.
openURI
in interface UWSFileManager
uri
- URI of any resource to read.java.io.IOException
- If another error occurs while opening
the stream.UnsupportedURIProtocolException
public void deleteUpload(UploadFile upload) throws java.io.IOException
UWSFileManager
deleteUpload
in interface UWSFileManager
upload
- Description of the uploaded file.java.io.IOException
- If any error occurs while deleting the file.public java.lang.String moveUpload(UploadFile upload, UWSJob destination) throws java.io.IOException
UWSFileManager
Note: This function is generally used only once: after the HTTP request parsing, when creating or updating a job and only if the action has been accepted.
Important:
This function might not be able to update the location inside the given
UploadFile
. For this reason, it is strongly recommended to not
call directly this function, but to use UploadFile.move(UWSJob)
.
moveUpload
in interface UWSFileManager
upload
- Description of the uploaded file to move.destination
- Job in which the uploaded file will be used.java.io.IOException
- If any error occurs while moving the file.protected java.lang.String getResultFileName(Result result, UWSJob job)
Gets the name of the file in which the given result is/must be written.
By default: jobID + "_" + resultID + "." + getFileExtension(resultMIMEType)
note: there is no file extension if the MIME type of the result is unknown !
result
- The result whose the file name is asked.job
- The job which owns the given result.UWSToolBox.getFileExtension(String)
protected java.io.File getResultFile(Result result, UWSJob job)
result
- The result whose the file is asked.job
- The job which owns the given result.getOwnerDirectory(JobOwner)
,
getResultFileName(Result, UWSJob)
public java.io.InputStream getResultInput(Result result, UWSJob job) throws java.io.IOException
UWSFileManager
getResultInput
in interface UWSFileManager
result
- The description of the result file to read.job
- The job of the given result.java.io.IOException
- If there is an error while opening an input stream
on the result file.public java.io.OutputStream getResultOutput(Result result, UWSJob job) throws java.io.IOException
UWSFileManager
Note: The result file must be automatically created if needed.
getResultOutput
in interface UWSFileManager
result
- The description of the result file to write.job
- The job of the given result.java.io.IOException
- If there is an error while creating the result file
or while opening an output stream on it.public long getResultSize(Result result, UWSJob job) throws java.io.IOException
UWSFileManager
getResultSize
in interface UWSFileManager
result
- Description of the result file whose the size is wanted.job
- The job of the given result.java.io.IOException
- If there is an error while getting the result file
size.public boolean deleteResult(Result result, UWSJob job) throws java.io.IOException
UWSFileManager
deleteResult
in interface UWSFileManager
result
- The description of the result file to delete.job
- The job of the given result.java.io.IOException
- If there is a grave and unexpected error while
deleting the result file.protected java.lang.String getErrorFileName(ErrorSummary error, UWSJob job)
Gets the name of the file in which the described error is/must be written.
By default: jobID + "_ERROR.log"
error
- The description of the error whose the file name is asked.job
- The job which owns the given error.protected java.io.File getErrorFile(ErrorSummary error, UWSJob job)
error
- The error whose the file is asked.job
- The job which owns the given error.getOwnerDirectory(JobOwner)
,
getErrorFileName(ErrorSummary, UWSJob)
public java.io.InputStream getErrorInput(ErrorSummary error, UWSJob job) throws java.io.IOException
UWSFileManager
getErrorInput
in interface UWSFileManager
error
- The description of the error file to read.job
- The job of the given error.java.io.IOException
- If there is an error while opening an input stream
on the error file.public java.io.OutputStream getErrorOutput(ErrorSummary error, UWSJob job) throws java.io.IOException
UWSFileManager
Note: The error file must be automatically created if needed.
getErrorOutput
in interface UWSFileManager
error
- The description of the error file to write.job
- The job of the given error.java.io.IOException
- If there is an error while creating the error file
or while opening an output stream on it.public long getErrorSize(ErrorSummary error, UWSJob job) throws java.io.IOException
UWSFileManager
getErrorSize
in interface UWSFileManager
error
- Description of the error file whose the size is wanted.job
- The job of the given error.java.io.IOException
- If there is an error while getting the error file
size.public boolean deleteError(ErrorSummary error, UWSJob job) throws java.io.IOException
UWSFileManager
deleteError
in interface UWSFileManager
error
- The description of the error file to delete.job
- The job of the given error.java.io.IOException
- If there is a grave and unexpected error while
deleting the error file.protected java.lang.String getBackupFileName(JobOwner owner) throws java.lang.IllegalArgumentException
Gets the name of the backup file of the given job owner (~ UWS user).
By default: ownerID + ".backup"
owner
- The job owner whose the name of the backup file is asked.java.lang.IllegalArgumentException
- If the given owner is null or an empty string.public java.io.InputStream getBackupInput(JobOwner owner) throws java.lang.IllegalArgumentException, java.io.IOException
UWSFileManager
getBackupInput
in interface UWSFileManager
owner
- Owner whose the jobs must be fetched from the file on which
the input stream is asked.java.lang.IllegalArgumentException
- If the given owner is null.java.io.IOException
- If there is an error while opening an
input stream on the backup file.public java.util.Iterator<java.io.InputStream> getAllUserBackupInputs()
UWSFileManager
getAllUserBackupInputs
in interface UWSFileManager
public java.io.OutputStream getBackupOutput(JobOwner owner) throws java.lang.IllegalArgumentException, java.io.IOException
UWSFileManager
Note: The backup file must be automatically created if needed.
getBackupOutput
in interface UWSFileManager
owner
- Owner whose the jobs must be saved in the file on which the
output stream is asked.java.lang.IllegalArgumentException
- If the given owner is null.java.io.IOException
- If there is an error while creating the
backup file or while opening an output
stream on it.protected java.lang.String getBackupFileName()
Gets the name of the UWS general backup file.
By default: DEFAULT_BACKUP_FILE_NAME
public java.io.InputStream getBackupInput() throws java.io.IOException
UWSFileManager
getBackupInput
in interface UWSFileManager
java.io.IOException
- If there is an error while opening an input stream
of the backup file.public java.io.OutputStream getBackupOutput() throws java.io.IOException
UWSFileManager
Note: The backup file must be automatically created if needed.
getBackupOutput
in interface UWSFileManager
java.io.IOException
- If there is an error while creating the backup file
or while opening an output stream on it.protected boolean createParentDir(java.io.File f)
f
- The file whose the parent directory must exist after the call of this function.