public class DefaultUWSBackupManager extends java.lang.Object implements UWSBackupManager
Default implementation of the interface UWSBackupManager
.
With this class, a UWS can be saved and restored easily thanks to saveAll()
and restoreAll()
.
It is saved in JSON and in one or several files in function of the backup mode:
The backup frequency can also be changed and may have 2 special values:
AT_USER_ACTION
(=0): only the jobs of the user which has just created, destroyed, executed or stopped a job are saved. This frequency is possible only if the backup mode is by user.MANUAL
(=-1): you must call yourself the function saveAll()
to save the UWS.Another positive value will be considered as the frequency (in milliseconds) of the automatic backup (= saveAll()
).
Modifier and Type | Class and Description |
---|---|
protected static class |
DefaultUWSBackupManager.JSONArrayReader
Lets reading a JSON array from a
JSONTokener (that's to directly from a file)
as an iterator which returns all items which MUST BE JSONObject s. |
protected static class |
DefaultUWSBackupManager.JSONObjectReader
Lets reading a JSON object from a
JSONTokener (that's to say directly from a file),
as an iterator which returns all the keys. |
protected static class |
DefaultUWSBackupManager.SingleInputIterator
An iterator of input streams with ONLY ONE input stream.
|
Modifier and Type | Field and Description |
---|---|
static long |
AT_USER_ACTION
Special frequency to mean that this manager wait a user action (create, update, start, abort, destruction) to save the jobs of this user.
|
protected long |
backupFreq
Backup frequency (in milliseconds).
|
protected boolean |
byUser
Backup mode: one file by user or one file for all jobs and users.
|
static long |
DEFAULT_FREQUENCY
Default backup frequency.
|
protected boolean |
enabled
Tells whether the backup (and particularly the automatic one) of the associated UWS is enabled or not.
|
protected java.util.Date |
lastBackup
Date of the last backup.
|
protected java.util.Date |
lastRestoration
Date of the last restoration.
|
static long |
MANUAL
Special frequency to mean that this manager will NOT save automatically the UWS.
|
protected java.util.Timer |
timAutoBackup
Timer which saves the backup each backupFreq milliseconds.
|
protected UWS |
uws
The UWS to restore/save.
|
Constructor and Description |
---|
DefaultUWSBackupManager(UWS uws)
Builds a backup manager in the mode "auto": one file for all users and all jobs, and the backup
is done all minutes (see
DEFAULT_FREQUENCY . |
DefaultUWSBackupManager(UWS uws,
boolean byUser)
Builds a backup manager in the given mode: "by user" (one file for each user and the backup is done at each user action)
or not (one file for all users and all jobs and the backup is done all minutes (see
DEFAULT_FREQUENCY )). |
DefaultUWSBackupManager(UWS uws,
boolean byUser,
long frequency)
Builds a backup manager in the given mode and with the given frequency.
|
DefaultUWSBackupManager(UWS uws,
long frequency)
Builds a backup manager in the mode "auto" or "manual": one file for all users and all jobs, and the backup
is done at the given frequency.
|
Modifier and Type | Method and Description |
---|---|
protected byte[] |
fromBase64(java.lang.String base64Str)
Decode the given Base-64 string into a bytes array.
|
long |
getBackupFreq()
Gets the backup frequency.
|
protected ErrorSummary |
getError(org.json.JSONObject obj)
Builds the error summary corresponding to the given JSON object.
|
protected org.json.JSONObject |
getJSONJob(UWSJob job,
java.lang.String jlName)
Serializes the given job into a JSON object.
|
protected java.lang.Object |
getJSONJobInfo(JobInfo jobInfo)
Serialize the given
JobInfo so that being able later to restore this exact object as provided. |
protected org.json.JSONObject |
getJSONUser(JobOwner user)
Serializes the given user into a JSON object.
|
java.util.Date |
getLastBackup()
Gets the date of the last backup.
|
java.util.Date |
getLastRestoration()
Gets the date of the last restoration
|
UWSLog |
getLogger()
Gets the logger of its UWS, or the default one if it is unknown.
|
protected java.util.Map<java.lang.String,java.lang.Object> |
getParameters(org.json.JSONObject obj)
Builds the list of parameters corresponding to the given JSON object.
|
protected Result |
getResult(org.json.JSONObject obj)
Builds the result corresponding to the given JSON object.
|
protected java.util.List<Result> |
getResults(org.json.JSONArray array)
Builds the list of results corresponding to the given JSON array.
|
protected UploadFile |
getUploadFile(org.json.JSONObject obj)
Build the upload file corresponding to the given JSON object.
|
protected org.json.JSONObject |
getUploadJson(UploadFile upl)
Get the JSON representation of the given
UploadFile . |
protected JobOwner |
getUser(org.json.JSONObject json)
Builds the instance of
JobOwner corresponding to the given JSON object. |
boolean |
isEnabled()
Tells whether this backup manager is enabled or not.
|
int[] |
restoreAll()
Restore all jobs list and all information about the jobs owners.
|
protected boolean |
restoreJob(org.json.JSONObject json,
java.util.Map<java.lang.String,JobOwner> users)
Builds the job corresponding to the given JSON object and then restore it in the UWS.
|
protected JobInfo |
restoreJobInfo(java.lang.Object jsonValue)
Restore the JobInfo referenced or represented by the given JSON value.
|
protected void |
restoreOtherJobParams(org.json.JSONObject json,
UWSJob job)
Restores other job parameters, either from the given JSON object or from the parameters map of the given job.
|
int[] |
saveAll()
Save all jobs lists and all information about the jobs owners.
|
int[] |
saveOwner(JobOwner user)
Save ONLY the jobs of the given user and all information about this user.
|
protected int[] |
saveOwner(JobOwner user,
boolean fromSaveAll) |
void |
setBackupFreq(long freq)
Sets the backup frequency.
|
void |
setEnabled(boolean enabled)
Enables/Disables the backup of the associated UWS.
|
protected java.lang.String |
toBase64(byte[] bytes)
Encode the given bytes into a Base-64 string.
|
public static final long AT_USER_ACTION
public static final long MANUAL
saveAll()
MUST be explicitly called.public static final long DEFAULT_FREQUENCY
protected java.util.Date lastRestoration
protected java.util.Date lastBackup
protected final UWS uws
protected boolean enabled
protected final boolean byUser
protected long backupFreq
protected java.util.Timer timAutoBackup
public DefaultUWSBackupManager(UWS uws)
DEFAULT_FREQUENCY
.uws
- The UWS to save/restore.DefaultUWSBackupManager(UWS, long)
public DefaultUWSBackupManager(UWS uws, long frequency)
Builds a backup manager in the mode "auto" or "manual": one file for all users and all jobs, and the backup is done at the given frequency.
If the given frequency is 0 or negative (see MANUAL
), the backup will not be done automatically. You must manually
save the UWS thanks to the function saveAll()
.
If the given frequency is positive, the backup will be done automatically at the given frequency.
uws
- The UWS to save/restore.frequency
- The backup frequency (in ms ; MUST BE positive and different from 0. If negative or 0, the frequency will be automatically set to DEFAULT_FREQUENCY
).public DefaultUWSBackupManager(UWS uws, boolean byUser) throws UWSException
DEFAULT_FREQUENCY
)).uws
- The UWS to save/restore.byUser
- Backup mode.UWSException
- If the user identification is disabled (that's to say, if the given UWS has no UserIdentifier) while the parameter byUser is true.DefaultUWSBackupManager(UWS, boolean, long)
public DefaultUWSBackupManager(UWS uws, boolean byUser, long frequency) throws UWSException
uws
- The UWS to save/restore.byUser
- Backup mode (true means one file for each user and false, one file for all users and jobs).frequency
- Backup frequency (AT_USER_ACTION
, MANUAL
, DEFAULT_FREQUENCY
, or a positive value).UWSException
- If the user identification is disabled (that's to say, if the given UWS has no UserIdentifier) while the parameter byUser is true.public final boolean isEnabled()
public final void setEnabled(boolean enabled)
UWSBackupManager
setEnabled
in interface UWSBackupManager
enabled
- true to enable the backup, false otherwise.public final long getBackupFreq()
public final void setBackupFreq(long freq)
Sets the backup frequency.
note 1: A negative frequency will be interpreted as "manual"..
that's to say you will have to call yourself the saveAll()
method to save the UWS.
note 2: Nothing will be done if the given frequency is AT_USER_ACTION
although the current backup mode is "by user".
freq
- The new backup frequency (in milliseconds) (AT_USER_ACTION
, MANUAL
, DEFAULT_FREQUENCY
or any other positive value).public final java.util.Date getLastRestoration()
public final java.util.Date getLastBackup()
public UWSLog getLogger()
UWS.getLogger()
,
UWSToolBox.getDefaultLogger()
public int[] saveAll()
UWSBackupManager
saveAll
in interface UWSBackupManager
public int[] saveOwner(JobOwner user)
UWSBackupManager
saveOwner
in interface UWSBackupManager
protected int[] saveOwner(JobOwner user, boolean fromSaveAll)
protected org.json.JSONObject getJSONUser(JobOwner user) throws org.json.JSONException
Serializes the given user into a JSON object.
{ "id": "...", "pseudo": "...", ... }
note:
the last suspension points mean that other user data may be added into this JSON object.
These other user data to save MUST BE given by JobOwner.getDataToSave()
.
user
- The user to save.org.json.JSONException
- If there is an error while building the JSON object.protected org.json.JSONObject getJSONJob(UWSJob job, java.lang.String jlName) throws UWSException, org.json.JSONException
Serializes the given job into a JSON object.
note:
the structure of the returned JSON object is decided by Json4Uws.getJson(UWSJob)
.
Only one attribute is added: "jobListName".
job
- The job to save.jlName
- Name of the jobs list containing the given job.UWSException
- If there is an error while getting job parameters and serializing them.org.json.JSONException
- If there is an error while building the JSON object.protected java.lang.Object getJSONJobInfo(JobInfo jobInfo) throws UWSException, org.json.JSONException
JobInfo
so that being able later to restore this exact object as provided.
By default, this function use the Java Class serialization (see Serializable
)
and save the corresponding bytes into a Base-64 string.
jobInfo
- The jobInfo to backup.UWSException
- If any error occurs while representing the given JobInfo
.org.json.JSONException
- If any error occurs while manipulating a JSON object or array.protected JobInfo restoreJobInfo(java.lang.Object jsonValue) throws UWSException, org.json.JSONException
By default, this function considers that the given value is a Base-64 string encoding
the Java Class serialization (see Serializable
) of the JobInfo
to restore.
jsonValue
- The reference or backup representation of the JobInfo
to restore.JobInfo
.UWSException
- If any error occurs while restoring the JobInfo
.org.json.JSONException
- If any error occurs while manipulating a JSON object or array.protected final java.lang.String toBase64(byte[] bytes)
bytes
- Bytes to encode.fromBase64(String)
protected final byte[] fromBase64(java.lang.String base64Str)
base64Str
- Base-64 string to decode.toBase64(byte[])
protected org.json.JSONObject getUploadJson(UploadFile upl) throws org.json.JSONException
UploadFile
.upl
- The uploaded file to serialize in JSON.org.json.JSONException
- If there is an error while building the JSON object.public int[] restoreAll()
UWSBackupManager
restoreAll
in interface UWSBackupManager
protected JobOwner getUser(org.json.JSONObject json) throws UWSException
JobOwner
corresponding to the given JSON object.json
- JSON representation of the user to build.JobOwner
or null if the given object is empty.UWSException
- If the "id" parameter is missing (a user MUST have an id ; warning: the case sensitivity is enabled only for this attribute).JobOwner.restoreData(Map)
protected boolean restoreJob(org.json.JSONObject json, java.util.Map<java.lang.String,JobOwner> users) throws UWSException
json
- The JSON representation of the job to restore.users
- The list of all fetched users.UWSException
- If the job ID or the job list name is missing,
or if the job list name is incorrect,
or if there is an error with "parameters", "error" and "results".protected void restoreOtherJobParams(org.json.JSONObject json, UWSJob job) throws UWSException
Restores other job parameters, either from the given JSON object or from the parameters map of the given job. The job is supposed to be updated after the call of this function.
note: By default, this function does nothing ! It is called by restoreJob(JSONObject, Map)
just after the default restoration from the given JSON and just before to add the job in its dedicated jobs list.
json
- JSON backup of the given job.job
- Default restoration of the job.UWSException
- If there is an error while restoring other job parameters.restoreJob(JSONObject, Map)
protected java.util.Map<java.lang.String,java.lang.Object> getParameters(org.json.JSONObject obj)
obj
- The JSON representation of a parameters list.protected UploadFile getUploadFile(org.json.JSONObject obj)
obj
- The JSON representation of the UploadFile
to get.UploadFile
.protected java.util.List<Result> getResults(org.json.JSONArray array) throws UWSException
array
- The JSON representation of the results to restore.UWSException
- If there is an error while restoring one of the result.getResult(JSONObject)
protected Result getResult(org.json.JSONObject obj) throws org.json.JSONException, UWSException
obj
- The JSON representation of the result to restore.org.json.JSONException
- If there is an error while reading the JSON.UWSException
protected ErrorSummary getError(org.json.JSONObject obj) throws UWSException
obj
- The JSON representation of the error summary to restore.UWSException