Jackson serialization for processes
This commit is contained in:
parent
49f1367adc
commit
aba8cd8430
@ -38,13 +38,21 @@ import java.util.Properties;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONWriter;
|
import org.json.JSONWriter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import com.google.refine.history.HistoryEntry;
|
import com.google.refine.history.HistoryEntry;
|
||||||
|
|
||||||
abstract public class LongRunningProcess extends Process {
|
abstract public class LongRunningProcess extends Process {
|
||||||
|
@JsonProperty("description")
|
||||||
final protected String _description;
|
final protected String _description;
|
||||||
|
@JsonIgnore
|
||||||
protected ProcessManager _manager;
|
protected ProcessManager _manager;
|
||||||
|
@JsonIgnore
|
||||||
protected Thread _thread;
|
protected Thread _thread;
|
||||||
|
@JsonProperty("progress")
|
||||||
protected int _progress; // out of 100
|
protected int _progress; // out of 100
|
||||||
|
@JsonIgnore
|
||||||
protected boolean _canceled;
|
protected boolean _canceled;
|
||||||
|
|
||||||
protected LongRunningProcess(String description) {
|
protected LongRunningProcess(String description) {
|
||||||
@ -67,10 +75,15 @@ abstract public class LongRunningProcess extends Process {
|
|||||||
writer.key("id"); writer.value(hashCode());
|
writer.key("id"); writer.value(hashCode());
|
||||||
writer.key("description"); writer.value(_description);
|
writer.key("description"); writer.value(_description);
|
||||||
writer.key("immediate"); writer.value(false);
|
writer.key("immediate"); writer.value(false);
|
||||||
writer.key("status"); writer.value(_thread == null ? "pending" : (_thread.isAlive() ? "running" : "done"));
|
writer.key("status"); writer.value(getStatus());
|
||||||
writer.key("progress"); writer.value(_progress);
|
writer.key("progress"); writer.value(_progress);
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonProperty("status")
|
||||||
|
public String getStatus() {
|
||||||
|
return _thread == null ? "pending" : (_thread.isAlive() ? "running" : "done");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isImmediate() {
|
public boolean isImmediate() {
|
||||||
|
@ -33,17 +33,29 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
package com.google.refine.process;
|
package com.google.refine.process;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import com.google.refine.Jsonizable;
|
import com.google.refine.Jsonizable;
|
||||||
import com.google.refine.history.HistoryEntry;
|
import com.google.refine.history.HistoryEntry;
|
||||||
|
|
||||||
public abstract class Process implements Jsonizable {
|
public abstract class Process implements Jsonizable {
|
||||||
|
@JsonProperty("immediate")
|
||||||
abstract public boolean isImmediate();
|
abstract public boolean isImmediate();
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
abstract public boolean isRunning();
|
abstract public boolean isRunning();
|
||||||
|
@JsonIgnore
|
||||||
abstract public boolean isDone();
|
abstract public boolean isDone();
|
||||||
|
|
||||||
|
@JsonIgnore
|
||||||
abstract public HistoryEntry performImmediate() throws Exception;
|
abstract public HistoryEntry performImmediate() throws Exception;
|
||||||
|
|
||||||
abstract public void startPerforming(ProcessManager manager);
|
abstract public void startPerforming(ProcessManager manager);
|
||||||
abstract public void cancel();
|
abstract public void cancel();
|
||||||
|
|
||||||
|
@JsonProperty("id")
|
||||||
|
public long getId() {
|
||||||
|
return hashCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,18 +37,34 @@ import java.util.Collections;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONWriter;
|
import org.json.JSONWriter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||||
|
|
||||||
import com.google.refine.Jsonizable;
|
import com.google.refine.Jsonizable;
|
||||||
import com.google.refine.history.HistoryEntry;
|
import com.google.refine.history.HistoryEntry;
|
||||||
import com.google.refine.history.HistoryProcess;
|
import com.google.refine.history.HistoryProcess;
|
||||||
|
|
||||||
public class ProcessManager implements Jsonizable {
|
public class ProcessManager implements Jsonizable {
|
||||||
|
@JsonProperty("processes")
|
||||||
protected List<Process> _processes = Collections.synchronizedList(new LinkedList<Process>());
|
protected List<Process> _processes = Collections.synchronizedList(new LinkedList<Process>());
|
||||||
|
@JsonIgnore
|
||||||
protected List<Exception> _latestExceptions = null;
|
protected List<Exception> _latestExceptions = null;
|
||||||
|
|
||||||
|
public static class ExceptionMessage {
|
||||||
|
@JsonProperty("message")
|
||||||
|
public final String message;
|
||||||
|
public ExceptionMessage(Exception e) {
|
||||||
|
message = e.getLocalizedMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public ProcessManager() {
|
public ProcessManager() {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -78,6 +94,17 @@ public class ProcessManager implements Jsonizable {
|
|||||||
|
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonProperty("exceptions")
|
||||||
|
@JsonInclude(Include.NON_NULL)
|
||||||
|
public List<ExceptionMessage> getJsonExceptions() {
|
||||||
|
if (_latestExceptions != null) {
|
||||||
|
return _latestExceptions.stream()
|
||||||
|
.map(e -> new ExceptionMessage(e))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public HistoryEntry queueProcess(Process process) throws Exception {
|
public HistoryEntry queueProcess(Process process) throws Exception {
|
||||||
if (process.isImmediate() && _processes.size() == 0) {
|
if (process.isImmediate() && _processes.size() == 0) {
|
||||||
|
@ -38,6 +38,8 @@ import java.util.Properties;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONWriter;
|
import org.json.JSONWriter;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
import com.google.refine.history.HistoryEntry;
|
import com.google.refine.history.HistoryEntry;
|
||||||
import com.google.refine.model.Project;
|
import com.google.refine.model.Project;
|
||||||
|
|
||||||
@ -58,6 +60,7 @@ abstract public class QuickHistoryEntryProcess extends Process {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@JsonProperty("immediate")
|
||||||
public boolean isImmediate() {
|
public boolean isImmediate() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -89,12 +92,21 @@ abstract public class QuickHistoryEntryProcess extends Process {
|
|||||||
|
|
||||||
writer.object();
|
writer.object();
|
||||||
writer.key("id"); writer.value(hashCode());
|
writer.key("id"); writer.value(hashCode());
|
||||||
writer.key("description"); writer.value(_historyEntry != null ? _historyEntry.description : _briefDescription);
|
writer.key("description"); writer.value(getDescription());
|
||||||
writer.key("immediate"); writer.value(true);
|
writer.key("immediate"); writer.value(true);
|
||||||
writer.key("status"); writer.value(_done ? "done" : "pending");
|
writer.key("status"); writer.value(getStatus());
|
||||||
writer.endObject();
|
writer.endObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JsonProperty("status")
|
||||||
|
public String getStatus() {
|
||||||
|
return _done ? "done" : "pending";
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonProperty("description")
|
||||||
|
public String getDescription() {
|
||||||
|
return _historyEntry != null ? _historyEntry.description : _briefDescription;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isDone() {
|
public boolean isDone() {
|
||||||
|
@ -11,20 +11,24 @@ import com.google.refine.util.JSONUtilities;
|
|||||||
public class ProcessManagerTests {
|
public class ProcessManagerTests {
|
||||||
|
|
||||||
ProcessManager processManager;
|
ProcessManager processManager;
|
||||||
Process process;
|
Process process1, process2;
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
processManager = new ProcessManager();
|
processManager = new ProcessManager();
|
||||||
process = new LongRunningProcessTests.LongRunningProcessStub("some description");
|
process1 = new LongRunningProcessTests.LongRunningProcessStub("some description");
|
||||||
|
process2 = new LongRunningProcessTests.LongRunningProcessStub("some other description");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serializeProcessManager() throws Exception {
|
public void serializeProcessManager() throws Exception {
|
||||||
processManager.queueProcess(process);
|
processManager.queueProcess(process1);
|
||||||
String processJson = JSONUtilities.serialize(process);
|
processManager.queueProcess(process2);
|
||||||
|
processManager.onFailedProcess(process1, new IllegalArgumentException("unexpected error"));
|
||||||
|
String processJson = JSONUtilities.serialize(process2);
|
||||||
TestUtils.isSerializedTo(processManager, "{"
|
TestUtils.isSerializedTo(processManager, "{"
|
||||||
+ "\"processes\":["+processJson+"]}");
|
+ "\"processes\":["+processJson+"],\n"
|
||||||
|
+ "\"exceptions\":[{\"message\":\"unexpected error\"}]"
|
||||||
|
+ "}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user