Skip to content

Commit

Permalink
Use factory method to obtain JsonObjectBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
slominskir committed Dec 14, 2020
1 parent 4efe65c commit c0c3a39
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonString;
import javax.json.JsonValue;
import javax.json.*;
import javax.websocket.Session;
import org.jlab.epics2web.Application;
import org.jlab.epics2web.epics.PvListener;
Expand All @@ -36,6 +30,8 @@ public class WebSocketSessionManager {

private static final Logger LOGGER = Logger.getLogger(WebSocketSessionManager.class.getName());

private final JsonBuilderFactory factory = Json.createBuilderFactory(null);

/*ConcurrentHashMap provides thread safety on map of listeners*/
final Map<Session, WebSocketSessionMonitor> listenerMap = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -242,12 +238,12 @@ private WebSocketSessionMonitor getListener(Session session) {
public void sendInfo(Session session, String pv, boolean couldConnect, DBRType type,
Integer count,
String[] enumLabels) {
JsonObjectBuilder objBuilder
= Json.createObjectBuilder().add("type", "info").add(
"pv", pv).add("connected", couldConnect);
JsonObjectBuilder builder = factory.createObjectBuilder();

builder.add("type", "info").add("pv", pv).add("connected", couldConnect);

if (couldConnect) {
objBuilder.add("datatype",
builder.add("datatype",
type.getName()).add("count", count);

if (enumLabels != null) {
Expand All @@ -256,11 +252,11 @@ public void sendInfo(Session session, String pv, boolean couldConnect, DBRType t
arrBuilder.add(label);
}

objBuilder.add("enum-labels", arrBuilder);
builder.add("enum-labels", arrBuilder);
}
}

JsonObject obj = objBuilder.build();
JsonObject obj = builder.build();
String msg = obj.toString();
send(session, pv, msg);
}
Expand All @@ -273,7 +269,9 @@ public void sendInfo(Session session, String pv, boolean couldConnect, DBRType t
* @param dbr The EPICS DataBaseRecord
*/
public void sendUpdate(Session session, String pv, DBR dbr) {
JsonObjectBuilder builder = Json.createObjectBuilder().add("type", "update").add(
JsonObjectBuilder builder = factory.createObjectBuilder();

builder.add("type", "update").add(
"pv", pv);
Application.channelManager.addValueToJSON(builder, dbr);
JsonObject obj = builder.build();
Expand Down

0 comments on commit c0c3a39

Please sign in to comment.