diff options
author | Simon Rettberg | 2019-11-13 17:09:02 +0100 |
---|---|---|
committer | Simon Rettberg | 2019-11-13 17:09:02 +0100 |
commit | 642b82d0eabd6356713af172aea7ca51e7f3fb57 (patch) | |
tree | c5fac8d0d97362e48a5dc5770c759fa4c3d0f03f | |
parent | Add isFinished() to Task (diff) | |
download | taskman-lite-642b82d0eabd6356713af172aea7ca51e7f3fb57.tar.gz taskman-lite-642b82d0eabd6356713af172aea7ca51e7f3fb57.tar.xz taskman-lite-642b82d0eabd6356713af172aea7ca51e7f3fb57.zip |
BoundedLog type + serializer
3 files changed, 57 insertions, 1 deletions
diff --git a/api/src/main/java/org/openslx/taskmanager/api/BoundedLog.java b/api/src/main/java/org/openslx/taskmanager/api/BoundedLog.java new file mode 100644 index 0000000..86b8343 --- /dev/null +++ b/api/src/main/java/org/openslx/taskmanager/api/BoundedLog.java @@ -0,0 +1,25 @@ +package org.openslx.taskmanager.api; + +import java.util.concurrent.LinkedBlockingQueue; + +public class BoundedLog extends LinkedBlockingQueue<String> +{ + private static final long serialVersionUID = -5425030291381563963L; + + public final boolean implicitNewlines; + + public BoundedLog( int capacity, boolean implicitNewlines ) + { + super( capacity ); + this.implicitNewlines = implicitNewlines; + } + + public void addLog( String data ) + { + while ( !this.offer( data ) ) { + // Throw away oldest entry and try again + this.poll(); + } + } + +} diff --git a/daemon/src/main/java/org/openslx/taskmanager/network/RequestParser.java b/daemon/src/main/java/org/openslx/taskmanager/network/RequestParser.java index 2d46c5d..e1f883e 100644 --- a/daemon/src/main/java/org/openslx/taskmanager/network/RequestParser.java +++ b/daemon/src/main/java/org/openslx/taskmanager/network/RequestParser.java @@ -4,10 +4,13 @@ import java.nio.charset.StandardCharsets; import java.util.zip.Deflater; import org.apache.log4j.Logger; +import org.openslx.taskmanager.api.BoundedLog; import org.openslx.taskmanager.api.TaskStatus; import org.openslx.taskmanager.main.Taskmanager; +import org.openslx.taskmanager.util.BoundedLogSerializer; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; public class RequestParser { @@ -16,7 +19,7 @@ public class RequestParser /** * Our very own gson instance (for serializing replies) */ - private final Gson sendGson = new Gson(); + private final Gson sendGson = new GsonBuilder().registerTypeAdapter( BoundedLog.class, new BoundedLogSerializer() ).create(); /** * Same for the deflater diff --git a/daemon/src/main/java/org/openslx/taskmanager/util/BoundedLogSerializer.java b/daemon/src/main/java/org/openslx/taskmanager/util/BoundedLogSerializer.java new file mode 100644 index 0000000..1a43366 --- /dev/null +++ b/daemon/src/main/java/org/openslx/taskmanager/util/BoundedLogSerializer.java @@ -0,0 +1,28 @@ +package org.openslx.taskmanager.util; + +import java.lang.reflect.Type; + +import org.openslx.taskmanager.api.BoundedLog; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +public class BoundedLogSerializer implements JsonSerializer<BoundedLog> +{ + + @Override + public JsonElement serialize( BoundedLog src, Type typeOfSrc, JsonSerializationContext context ) + { + StringBuilder sb = new StringBuilder( 500 ); + for ( String s : src ) { + sb.append( s ); + if ( src.implicitNewlines ) { + sb.append( '\n' ); + } + } + return new JsonPrimitive( sb.toString() ); + } + +} |