Skip to content

Commit

Permalink
[UWS] Make the default backup mechanism independent from the library …
Browse files Browse the repository at this point in the history
…`cos`.

_This latter will soon be removed from UWSLib._
  • Loading branch information
gmantele committed Aug 20, 2018
1 parent e447a48 commit cb5cdd7
Showing 1 changed file with 63 additions and 29 deletions.
92 changes: 63 additions & 29 deletions src/uws/service/backup/DefaultUWSBackupManager.java
Expand Up @@ -40,16 +40,15 @@
import java.util.Timer;
import java.util.TimerTask;

import javax.xml.bind.DatatypeConverter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONWriter;
import org.json.Json4Uws;

import com.oreilly.servlet.Base64Decoder;
import com.oreilly.servlet.Base64Encoder;

import uws.ISO8601Format;
import uws.UWSException;
import uws.UWSToolBox;
Expand Down Expand Up @@ -87,7 +86,7 @@
* <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)
* @version 4.3 (05/2018)
* @version 4.4 (08/2018)
*/
public class DefaultUWSBackupManager implements UWSBackupManager {

Expand Down Expand Up @@ -148,7 +147,7 @@ public DefaultUWSBackupManager(final UWS uws, final long frequency){

if (backupFreq > 0){
timAutoBackup = new Timer();
timAutoBackup.scheduleAtFixedRate(new TimerTask(){
timAutoBackup.scheduleAtFixedRate(new TimerTask() {
@Override
public void run(){
saveAll();
Expand Down Expand Up @@ -193,7 +192,7 @@ public DefaultUWSBackupManager(final UWS uws, final boolean byUser, final long f
backupFreq = MANUAL;
else if (backupFreq > 0){
timAutoBackup = new Timer();
timAutoBackup.scheduleAtFixedRate(new TimerTask(){
timAutoBackup.scheduleAtFixedRate(new TimerTask() {
@Override
public void run(){
saveAll();
Expand All @@ -219,7 +218,7 @@ public final void setEnabled(boolean enabled){
if (this.enabled){
if (timAutoBackup == null){
timAutoBackup = new Timer();
timAutoBackup.scheduleAtFixedRate(new TimerTask(){
timAutoBackup.scheduleAtFixedRate(new TimerTask() {
@Override
public void run(){
saveAll();
Expand Down Expand Up @@ -271,7 +270,7 @@ else if (freq == AT_USER_ACTION && !byUser)

if (enabled && backupFreq > 0){
timAutoBackup = new Timer();
timAutoBackup.scheduleAtFixedRate(new TimerTask(){
timAutoBackup.scheduleAtFixedRate(new TimerTask() {
@Override
public void run(){
saveAll();
Expand Down Expand Up @@ -326,7 +325,7 @@ public int[] saveAll(){
int nbJobs = 0, nbOwners = 0;

// 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){
Iterator<JobOwner> it = jl.getUsers();
while(it.hasNext()){
Expand Down Expand Up @@ -409,7 +408,7 @@ public int[] saveAll(){
}

// 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);

lastBackup = new Date();
Expand All @@ -431,10 +430,10 @@ protected int[] saveOwner(JobOwner user, boolean fromSaveAll){

// DO NOTHING if the "save" order does not come from saveAll():
if (!fromSaveAll && backupFreq != AT_USER_ACTION)
return new int[]{-1,-1};
return new int[]{ -1, -1 };

UWSFileManager fileManager = uws.getFileManager();
int[] saveReport = new int[]{0,0};
int[] saveReport = new int[]{ 0, 0 };
PrintWriter writer = null;
try{
// Create a writer toward the backup file:
Expand Down Expand Up @@ -520,9 +519,9 @@ protected JSONObject getJSONUser(final JobOwner user) throws JSONException{
jsonUser.put("id", user.getID());
jsonUser.put("pseudo", user.getPseudo());
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()){
Map.Entry<String,Object> userData = itUserData.next();
Map.Entry<String, Object> userData = itUserData.next();
jsonUser.put(userData.getKey(), userData.getValue());
}
}
Expand Down Expand Up @@ -619,19 +618,21 @@ protected Object getJSONJobInfo(final JobInfo jobInfo) throws UWSException, JSON
oOutput = new ObjectOutputStream(bArray);
oOutput.writeObject(jobInfo);
oOutput.flush();
return Base64Encoder.encode(bArray.toByteArray());
return toBase64(bArray.toByteArray());
}catch(IOException ioe){
throw new UWSException(UWSException.INTERNAL_SERVER_ERROR, ioe, "Unexpected error while serializing the given JobInfo!");
}finally{
if (oOutput != null){
try{
oOutput.close();
}catch(IOException ioe){}
}catch(IOException ioe){
}
}
if (bArray != null){
try{
bArray.close();
}catch(IOException ioe){}
}catch(IOException ioe){
}
}
}
}
Expand All @@ -656,7 +657,7 @@ protected Object getJSONJobInfo(final JobInfo jobInfo) throws UWSException, JSON
protected JobInfo restoreJobInfo(final Object jsonValue) throws UWSException, JSONException{
ObjectInputStream oInput = null;
try{
byte[] bArray = Base64Decoder.decodeToBytes((String)jsonValue);
byte[] bArray = fromBase64((String)jsonValue);
oInput = new ObjectInputStream(new ByteArrayInputStream(bArray));
return (JobInfo)oInput.readObject();
}catch(Exception ex){
Expand All @@ -665,11 +666,42 @@ protected JobInfo restoreJobInfo(final Object jsonValue) throws UWSException, JS
if (oInput != null){
try{
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}.
*
Expand Down Expand Up @@ -736,7 +768,7 @@ public int[] restoreAll(){
// Create the JSON reader:
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;
JSONObject object = null;

Expand Down Expand Up @@ -850,7 +882,7 @@ else if (key.equalsIgnoreCase("jobs")){
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:
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);

return report;
Expand All @@ -874,7 +906,7 @@ protected JobOwner getUser(final JSONObject json) throws UWSException{
// Fetch all user data:
String ID = null, pseudo = null;
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){
try{
if (key.equalsIgnoreCase("id"))
Expand Down Expand Up @@ -907,7 +939,7 @@ else if (key.equalsIgnoreCase("pseudo"))
* or if the job list name is incorrect,
* 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)
return false;

Expand All @@ -916,7 +948,7 @@ protected boolean restoreJob(final JSONObject json, Map<String,JobOwner> users)
long quote = UWSJob.UNLIMITED_DURATION,
/*duration = UWSJob.UNLIMITED_DURATION, */startTime = -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;
List<Result> results = null;
ErrorSummary error = null;
Expand Down Expand Up @@ -1028,11 +1060,12 @@ else if (key.equalsIgnoreCase(UWSJob.PARAM_JOB_INFO)){
// Re-Build all the uploaded files' pointers for this job:
if (uploads != null){
@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;
try{
for(int i = 0; i < uploads.length(); i++){
upl = getUploadFile(uploads.getJSONObject(i));;
upl = getUploadFile(uploads.getJSONObject(i));
;
if (upl != null)
params.put(upl.paramName, upl);
}
Expand Down Expand Up @@ -1117,11 +1150,11 @@ protected void restoreOtherJobParams(final JSONObject json, final UWSJob job) th
* @return The corresponding list of parameters
* 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)
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);
for(String n : names){
try{
Expand Down Expand Up @@ -1150,7 +1183,8 @@ protected UploadFile getUploadFile(final JSONObject obj){
try{
if (obj.has("length"))
upl.length = Long.parseLong(obj.getString("length"));
}catch(NumberFormatException ex){}
}catch(NumberFormatException ex){
}
return upl;
}catch(JSONException je){
getLogger().logUWS(LogLevel.ERROR, obj, "RESTORATION", "Incorrect JSON format for the serialization of an uploaded file!", je);
Expand Down

0 comments on commit cb5cdd7

Please sign in to comment.