Show LiteStatus.java syntax highlighted
/*
* Copyright (c) 2005
* Helsinki Institute of Physics
* see LICENSE file for details
*
* LiteStatus.java
* Created on Aug 19, 2003
*/
package fi.hip.gb.midlet.core;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Calendar;
import fi.hip.gb.midlet.util.StorageUtils;
/**
* Minimal status container for one jobs. Stores information about the state of
* the job and its execution and transfers statuses.
* <p>
* All results are also saved under this class.
*
* @author Juho Karppinen
*/
public class LiteStatus {
/** possible states for the job */
/** not yet started */
public static final int IDLE_STATE = 0;
/** dispathing job into its target */
public static final int DISPATCHING_STATE = 1;
/** executing the job */
public static final int EXECUTING_STATE = 2;
/** job is waiting for response */
public static final int WAITING_STATE = 3;
/** execution is finished, finalizing */
public static final int FINISHING_STATE = 4;
/** execution is completed */
public static final int COMPLETED_STATE = 5;
/** transfering results from remote host */
public static final int TRANSFER_STATE = 6;
/** results are downloaded */
public static final int RESULT_STATE = 7;
/** no results found */
public static final int NO_RESULT_STATE = 8;
/** job is canceled */
public static final int CANCELED_STATE = 9;
/** possible types for the job */
/** normal job */
public static final int NORMAL_TYPE = 0;
/** this is broadcast message */
public static final int BROADCAST_TYPE = 1;
/** textual presentation for the user */
public static final String[] STATES = {
"Idle",
"Dispatching",
"Executing",
"Waiting",
"Finishing",
"Completed",
"Transfering",
"Results",
"No results",
"Canceled"};
/** state of the job */
private int state;
/** where the status comes from */
private String serviceURL;
/** combined status of all executions */
private int[] exec = new int[2];
/** combined status of all transfers */
private int[] transfer = new int[2];
/** time when execution started */
private String timestampStart = "";
/** time when execution ended */
private String timestampEnd = "";
/** error message or empty string */
private String error = "";
/** own record ID */
private int recordID = -1;
/** name of the record store for results */
public final static String STORAGE_NAME = "status";
/**
* Creates new container for status information
*/
public LiteStatus() {
Calendar now = Calendar.getInstance();
timestampStart = now.get(Calendar.HOUR_OF_DAY)+ ":" + now.get(Calendar.MINUTE);
}
/*
* @see java.lang.Object#equals(Object);
*/
public boolean equals(Object liteStatus) {
LiteStatus status = (LiteStatus)liteStatus;
return (state != status.getState())
|| (this.exec[0] != status.getExecModel()[0])
|| (this.exec[1] != status.getExecModel()[1])
|| (this.timestampStart == null && status.getStartTime() != null)
|| (this.timestampStart != null && status.getStartTime() != null
&& ! this.timestampStart.equals(status.getStartTime()))
|| (this.timestampEnd == null && status.getEndTime() != null)
|| (this.timestampEnd != null && status.getEndTime() != null
&& ! this.timestampEnd.equals(status.getEndTime()))
|| (this.transfer[0] != status.getTransferModel()[0])
|| (this.transfer[1] != status.getTransferModel()[1]);
}
/**
* Creates new container for status information
* @param jobState state of the job
* @param servURL service where these statuses are combined
* @param startTimestamp start time of execution
* @param endTimestamp end time of execution
* @param execArray total execution status in array, first element is value, second is maximum
* @param transferArray total transfer status, first element is value, second is maximum
* @param error error message or empty string
*/
public LiteStatus(
int jobState,
String servURL,
String startTimestamp,
String endTimestamp,
int[] execArray,
int[] transferArray,
String error) {
this.state = jobState;
this.serviceURL = servURL;
this.timestampStart = startTimestamp;
this.timestampEnd = endTimestamp;
this.exec = execArray;
this.transfer = transferArray;
this.error = error;
}
/**
* Loads the object from the memory
* @param recID record ID
*/
public LiteStatus(int recID) {
this.recordID = recID;
try {
DataInputStream dis = StorageUtils.read(STORAGE_NAME, this.recordID);
deSerialize(dis);
dis.close();
} catch (Exception e) {
System.out.println("Failed to read the status from " + recordID
+ " : " + e.getMessage());
}
}
/**
* Gets the canceled status of the the jobs.
*
* @return true if the job are canceled
*/
public boolean isCanceled() {
return (state == LiteStatus.CANCELED_STATE);
}
/**
* Gets the finished status. Canceled job is also finished,
* so returing true doesn't mean that the execution was successfull.
*
* @return true if all jobs are finished
*/
public boolean isFinished() {
return state >= LiteStatus.COMPLETED_STATE;
}
/**
* Gets the waiting status.
*
* @return true if job is waiting input from the user
*/
public boolean isWaiting() {
return state == LiteStatus.WAITING_STATE;
}
/**
* Gets the state of the job
* @return state of the job in integer format
*/
public int getState() {
return state;
}
/**
* Sets state of the job.
* @param jobState new state for the job
*/
public void setState(int jobState) {
state = jobState;
}
/**
* Gets the place where status comes from.
* @return Returns the serviceURL.
*/
public String getServiceURL() {
return serviceURL;
}
/**
* Sets the place where status comes from.
* @param serviceURL The serviceURL to set.
*/
public void setServiceURL(String serviceURL) {
this.serviceURL = serviceURL;
}
/**
* Gets the status of executions
* @return the first item is current execution status, the second is maximum value
*/
public int[] getExecModel() {
return exec;
}
/**
* Sets the status of executions
* @param value the current execution status
* @param maximum the maximum execution value
*/
public void setExecModel(int value, int maximum) {
exec = new int[] {value, maximum};
}
/**
* Gets status of transfers
* @return the first item is current transfer status, the second is total size of results
*/
public int[] getTransferModel() {
return transfer;
}
/**
* Sets the status of transfer
* @param value the current transfer status
* @param maximum the maximum transfer value
*/
public void setTransferModel(int value, int maximum) {
transfer = new int[]{value, maximum};
}
/**
* Gets the time when execution was started
* @return time in millisecondes after January 1, 1970, 00:00:00 GMT, or null
* if execution is not yet started
*/
public String getStartTime() {
return timestampStart;
}
/**
* Gets the time when execution was ended
* @return time in millisecondes after January 1, 1970, 00:00:00 GMT, or null
* if execution is not yet finished
*/
public String getEndTime() {
return timestampEnd;
}
/**
* Gets the error message of the job or empty string if no errors.
* @return error message
*/
public String getError() {
return this.error;
}
/**
* Gets the record ID
* @return positive id if status is saved on the memory
*/
public int getRecordID() {
return this.recordID;
}
/**
* Stores the object into the record store
* @return record ID of the result on the record store
* @throws Exception if result cannot be save
*/
public int store() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
this.serialize(dos);
dos.close();
this.recordID = StorageUtils.store(baos.toByteArray(), STORAGE_NAME);
return this.recordID;
}
/**
* Serialize the object
* @param dos the stream to write to
* @throws IOException if failed to serialize
*/
protected void serialize(DataOutputStream dos) throws IOException {
dos.writeInt(this.state);
dos.writeUTF(this.serviceURL);
dos.writeUTF(this.timestampStart);
dos.writeUTF(this.timestampEnd);
dos.writeInt(this.exec[0]);
dos.writeInt(this.exec[1]);
dos.writeInt(this.transfer[0]);
dos.writeInt(this.transfer[1]);
dos.writeUTF(this.error);
}
/**
* Deserialize the object from inputstream
* @param dis inputstream to read
* @throws IOException if failed to deserialize from the stream
*/
protected void deSerialize(DataInputStream dis) throws IOException {
this.state = dis.readInt();
this.serviceURL = dis.readUTF();
this.timestampStart = dis.readUTF();
this.timestampEnd = dis.readUTF();
this.exec = new int[] {dis.readInt(), dis.readInt()};
this.transfer = new int[] {dis.readInt(), dis.readInt()};
this.error = dis.readUTF();
}
public String toString() {
return
"state: " + this.state + "\n"
//+ " url: " + this.serviceURL + "\n"
+ " start time: " + this.timestampStart + "\n"
+ " end time: " + this.timestampEnd + "\n"
+ " exec: " + this.exec[0] + " / " + this.exec[1] + "\n"
+ " transfer: " + this.transfer[0] + " / " + this.transfer[1]
+ " error: " + this.error;
}
}
See more files for this project here