Skip to content
Snippets Groups Projects
Commit cb5cdd73 authored by Grégory Mantelet's avatar Grégory Mantelet
Browse files

[UWS] Make the default backup mechanism independent from the library `cos`.

_This latter will soon be removed from UWSLib._
parent e447a487
No related branches found
No related tags found
No related merge requests found
...@@ -40,6 +40,8 @@ import java.util.NoSuchElementException; ...@@ -40,6 +40,8 @@ import java.util.NoSuchElementException;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import javax.xml.bind.DatatypeConverter;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -47,9 +49,6 @@ import org.json.JSONTokener; ...@@ -47,9 +49,6 @@ import org.json.JSONTokener;
import org.json.JSONWriter; import org.json.JSONWriter;
import org.json.Json4Uws; import org.json.Json4Uws;
import com.oreilly.servlet.Base64Decoder;
import com.oreilly.servlet.Base64Encoder;
import uws.ISO8601Format; import uws.ISO8601Format;
import uws.UWSException; import uws.UWSException;
import uws.UWSToolBox; import uws.UWSToolBox;
...@@ -87,7 +86,7 @@ import uws.service.request.UploadFile; ...@@ -87,7 +86,7 @@ import uws.service.request.UploadFile;
* <p>Another positive value will be considered as the frequency (in milliseconds) of the automatic backup (= {@link #saveAll()}).</p> * <p>Another positive value will be considered as the frequency (in milliseconds) of the automatic backup (= {@link #saveAll()}).</p>
* *
* @author Gr&eacute;gory Mantelet (CDS;ARI) * @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 4.3 (05/2018) * @version 4.4 (08/2018)
*/ */
public class DefaultUWSBackupManager implements UWSBackupManager { public class DefaultUWSBackupManager implements UWSBackupManager {
...@@ -148,7 +147,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -148,7 +147,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
if (backupFreq > 0){ if (backupFreq > 0){
timAutoBackup = new Timer(); timAutoBackup = new Timer();
timAutoBackup.scheduleAtFixedRate(new TimerTask(){ timAutoBackup.scheduleAtFixedRate(new TimerTask() {
@Override @Override
public void run(){ public void run(){
saveAll(); saveAll();
...@@ -193,7 +192,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -193,7 +192,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
backupFreq = MANUAL; backupFreq = MANUAL;
else if (backupFreq > 0){ else if (backupFreq > 0){
timAutoBackup = new Timer(); timAutoBackup = new Timer();
timAutoBackup.scheduleAtFixedRate(new TimerTask(){ timAutoBackup.scheduleAtFixedRate(new TimerTask() {
@Override @Override
public void run(){ public void run(){
saveAll(); saveAll();
...@@ -219,7 +218,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -219,7 +218,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
if (this.enabled){ if (this.enabled){
if (timAutoBackup == null){ if (timAutoBackup == null){
timAutoBackup = new Timer(); timAutoBackup = new Timer();
timAutoBackup.scheduleAtFixedRate(new TimerTask(){ timAutoBackup.scheduleAtFixedRate(new TimerTask() {
@Override @Override
public void run(){ public void run(){
saveAll(); saveAll();
...@@ -271,7 +270,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -271,7 +270,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
if (enabled && backupFreq > 0){ if (enabled && backupFreq > 0){
timAutoBackup = new Timer(); timAutoBackup = new Timer();
timAutoBackup.scheduleAtFixedRate(new TimerTask(){ timAutoBackup.scheduleAtFixedRate(new TimerTask() {
@Override @Override
public void run(){ public void run(){
saveAll(); saveAll();
...@@ -326,7 +325,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -326,7 +325,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
int nbJobs = 0, nbOwners = 0; int nbJobs = 0, nbOwners = 0;
// List all users of this UWS: // List all users of this UWS:
HashMap<String,JobOwner> users = new HashMap<String,JobOwner>(); HashMap<String, JobOwner> users = new HashMap<String, JobOwner>();
for(JobList jl : uws){ for(JobList jl : uws){
Iterator<JobOwner> it = jl.getUsers(); Iterator<JobOwner> it = jl.getUsers();
while(it.hasNext()){ while(it.hasNext()){
...@@ -409,7 +408,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -409,7 +408,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
} }
// Build the report and log it: // Build the report and log it:
int[] report = new int[]{nbSavedJobs,nbJobs,nbSavedOwners,nbOwners}; int[] report = new int[]{ nbSavedJobs, nbJobs, nbSavedOwners, nbOwners };
getLogger().logUWS(LogLevel.INFO, report, "BACKUPED", "UWS Service \"" + uws.getName() + "\" backuped!", null); getLogger().logUWS(LogLevel.INFO, report, "BACKUPED", "UWS Service \"" + uws.getName() + "\" backuped!", null);
lastBackup = new Date(); lastBackup = new Date();
...@@ -431,10 +430,10 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -431,10 +430,10 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
// DO NOTHING if the "save" order does not come from saveAll(): // DO NOTHING if the "save" order does not come from saveAll():
if (!fromSaveAll && backupFreq != AT_USER_ACTION) if (!fromSaveAll && backupFreq != AT_USER_ACTION)
return new int[]{-1,-1}; return new int[]{ -1, -1 };
UWSFileManager fileManager = uws.getFileManager(); UWSFileManager fileManager = uws.getFileManager();
int[] saveReport = new int[]{0,0}; int[] saveReport = new int[]{ 0, 0 };
PrintWriter writer = null; PrintWriter writer = null;
try{ try{
// Create a writer toward the backup file: // Create a writer toward the backup file:
...@@ -520,9 +519,9 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -520,9 +519,9 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
jsonUser.put("id", user.getID()); jsonUser.put("id", user.getID());
jsonUser.put("pseudo", user.getPseudo()); jsonUser.put("pseudo", user.getPseudo());
if (user.getDataToSave() != null){ if (user.getDataToSave() != null){
Iterator<Map.Entry<String,Object>> itUserData = user.getDataToSave().entrySet().iterator(); Iterator<Map.Entry<String, Object>> itUserData = user.getDataToSave().entrySet().iterator();
while(itUserData.hasNext()){ while(itUserData.hasNext()){
Map.Entry<String,Object> userData = itUserData.next(); Map.Entry<String, Object> userData = itUserData.next();
jsonUser.put(userData.getKey(), userData.getValue()); jsonUser.put(userData.getKey(), userData.getValue());
} }
} }
...@@ -619,19 +618,21 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -619,19 +618,21 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
oOutput = new ObjectOutputStream(bArray); oOutput = new ObjectOutputStream(bArray);
oOutput.writeObject(jobInfo); oOutput.writeObject(jobInfo);
oOutput.flush(); oOutput.flush();
return Base64Encoder.encode(bArray.toByteArray()); return toBase64(bArray.toByteArray());
}catch(IOException ioe){ }catch(IOException ioe){
throw new UWSException(UWSException.INTERNAL_SERVER_ERROR, ioe, "Unexpected error while serializing the given JobInfo!"); throw new UWSException(UWSException.INTERNAL_SERVER_ERROR, ioe, "Unexpected error while serializing the given JobInfo!");
}finally{ }finally{
if (oOutput != null){ if (oOutput != null){
try{ try{
oOutput.close(); oOutput.close();
}catch(IOException ioe){} }catch(IOException ioe){
}
} }
if (bArray != null){ if (bArray != null){
try{ try{
bArray.close(); bArray.close();
}catch(IOException ioe){} }catch(IOException ioe){
}
} }
} }
} }
...@@ -656,7 +657,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -656,7 +657,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
protected JobInfo restoreJobInfo(final Object jsonValue) throws UWSException, JSONException{ protected JobInfo restoreJobInfo(final Object jsonValue) throws UWSException, JSONException{
ObjectInputStream oInput = null; ObjectInputStream oInput = null;
try{ try{
byte[] bArray = Base64Decoder.decodeToBytes((String)jsonValue); byte[] bArray = fromBase64((String)jsonValue);
oInput = new ObjectInputStream(new ByteArrayInputStream(bArray)); oInput = new ObjectInputStream(new ByteArrayInputStream(bArray));
return (JobInfo)oInput.readObject(); return (JobInfo)oInput.readObject();
}catch(Exception ex){ }catch(Exception ex){
...@@ -665,11 +666,42 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -665,11 +666,42 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
if (oInput != null){ if (oInput != null){
try{ try{
oInput.close(); oInput.close();
}catch(IOException ioe){} }catch(IOException ioe){
}
} }
} }
} }
/**
* Encode the given bytes into a Base-64 string.
*
* @param bytes Bytes to encode.
*
* @return Base-64 encoded string.
*
* @since 4.4
*
* @see #fromBase64(String)
*/
protected final String toBase64(final byte[] bytes){
return DatatypeConverter.printBase64Binary(bytes);
}
/**
* Decode the given Base-64 string into a bytes array.
*
* @param base64Str Base-64 string to decode.
*
* @return Decoded bytes.
*
* @since 4.4
*
* @see #toBase64(byte[])
*/
protected final byte[] fromBase64(final String base64Str){
return DatatypeConverter.parseBase64Binary(base64Str);
}
/** /**
* Get the JSON representation of the given {@link UploadFile}. * Get the JSON representation of the given {@link UploadFile}.
* *
...@@ -736,7 +768,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -736,7 +768,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
// Create the JSON reader: // Create the JSON reader:
JSONTokener in = new JSONTokener(new InputStreamReader(inputStream)); JSONTokener in = new JSONTokener(new InputStreamReader(inputStream));
HashMap<String,JobOwner> users = new HashMap<String,JobOwner>(); HashMap<String, JobOwner> users = new HashMap<String, JobOwner>();
String key; String key;
JSONObject object = null; JSONObject object = null;
...@@ -850,7 +882,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -850,7 +882,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
getLogger().logUWS(LogLevel.WARNING, null, "RESTORATION", nbUsers + " job owners have not been restored because the user identification is disabled in this UWS! => Jobs of these users have not been restored.", null); getLogger().logUWS(LogLevel.WARNING, null, "RESTORATION", nbUsers + " job owners have not been restored because the user identification is disabled in this UWS! => Jobs of these users have not been restored.", null);
// Build the restoration report and log it: // Build the restoration report and log it:
int[] report = new int[]{nbRestoredJobs,nbJobs,nbRestoredUsers,nbUsers}; int[] report = new int[]{ nbRestoredJobs, nbJobs, nbRestoredUsers, nbUsers };
getLogger().logUWS(LogLevel.INFO, report, "RESTORED", "UWS restored!", null); getLogger().logUWS(LogLevel.INFO, report, "RESTORED", "UWS restored!", null);
return report; return report;
...@@ -874,7 +906,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -874,7 +906,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
// Fetch all user data: // Fetch all user data:
String ID = null, pseudo = null; String ID = null, pseudo = null;
String[] keys = JSONObject.getNames(json); String[] keys = JSONObject.getNames(json);
Map<String,Object> userData = new HashMap<String,Object>(keys.length - 2); Map<String, Object> userData = new HashMap<String, Object>(keys.length - 2);
for(String key : keys){ for(String key : keys){
try{ try{
if (key.equalsIgnoreCase("id")) if (key.equalsIgnoreCase("id"))
...@@ -907,7 +939,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -907,7 +939,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
* or if the job list name is incorrect, * or if the job list name is incorrect,
* or if there is an error with "parameters", "error" and "results". * or if there is an error with "parameters", "error" and "results".
*/ */
protected boolean restoreJob(final JSONObject json, Map<String,JobOwner> users) throws UWSException{ protected boolean restoreJob(final JSONObject json, Map<String, JobOwner> users) throws UWSException{
if (json == null || json.length() == 0) if (json == null || json.length() == 0)
return false; return false;
...@@ -916,7 +948,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -916,7 +948,7 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
long quote = UWSJob.UNLIMITED_DURATION, long quote = UWSJob.UNLIMITED_DURATION,
/*duration = UWSJob.UNLIMITED_DURATION, */startTime = -1, /*duration = UWSJob.UNLIMITED_DURATION, */startTime = -1,
endTime = -1, creationTime = -1; endTime = -1, creationTime = -1;
HashMap<String,Object> inputParams = new HashMap<String,Object>(10); HashMap<String, Object> inputParams = new HashMap<String, Object>(10);
//Map<String, Object> params = null; //Map<String, Object> params = null;
List<Result> results = null; List<Result> results = null;
ErrorSummary error = null; ErrorSummary error = null;
...@@ -1028,11 +1060,12 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -1028,11 +1060,12 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
// Re-Build all the uploaded files' pointers for this job: // Re-Build all the uploaded files' pointers for this job:
if (uploads != null){ if (uploads != null){
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String,Object> params = (Map<String,Object>)(inputParams.get(UWSJob.PARAM_PARAMETERS)); Map<String, Object> params = (Map<String, Object>)(inputParams.get(UWSJob.PARAM_PARAMETERS));
UploadFile upl; UploadFile upl;
try{ try{
for(int i = 0; i < uploads.length(); i++){ for(int i = 0; i < uploads.length(); i++){
upl = getUploadFile(uploads.getJSONObject(i));; upl = getUploadFile(uploads.getJSONObject(i));
;
if (upl != null) if (upl != null)
params.put(upl.paramName, upl); params.put(upl.paramName, upl);
} }
...@@ -1117,11 +1150,11 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -1117,11 +1150,11 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
* @return The corresponding list of parameters * @return The corresponding list of parameters
* or <i>null</i> if the given object is empty. * or <i>null</i> if the given object is empty.
*/ */
protected Map<String,Object> getParameters(final JSONObject obj){ protected Map<String, Object> getParameters(final JSONObject obj){
if (obj == null || obj.length() == 0) if (obj == null || obj.length() == 0)
return null; return null;
HashMap<String,Object> params = new HashMap<String,Object>(obj.length()); HashMap<String, Object> params = new HashMap<String, Object>(obj.length());
String[] names = JSONObject.getNames(obj); String[] names = JSONObject.getNames(obj);
for(String n : names){ for(String n : names){
try{ try{
...@@ -1150,7 +1183,8 @@ public class DefaultUWSBackupManager implements UWSBackupManager { ...@@ -1150,7 +1183,8 @@ public class DefaultUWSBackupManager implements UWSBackupManager {
try{ try{
if (obj.has("length")) if (obj.has("length"))
upl.length = Long.parseLong(obj.getString("length")); upl.length = Long.parseLong(obj.getString("length"));
}catch(NumberFormatException ex){} }catch(NumberFormatException ex){
}
return upl; return upl;
}catch(JSONException je){ }catch(JSONException je){
getLogger().logUWS(LogLevel.ERROR, obj, "RESTORATION", "Incorrect JSON format for the serialization of an uploaded file!", je); getLogger().logUWS(LogLevel.ERROR, obj, "RESTORATION", "Incorrect JSON format for the serialization of an uploaded file!", je);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment