All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.doip.cluster.engine.DoipMessageUtil Maven / Gradle / Ivy

The newest version!
package org.bdware.doip.cluster.engine;

import com.google.gson.JsonObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.cluster.client.DoaClusterClient;
import org.bdware.doip.cluster.client.TraceableDoaClusterClient;
import org.bdware.doip.cluster.util.JSONTool;
import org.bdware.doip.codec.JsonDoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
import org.bdware.doip.codec.operations.BasicOperations;
import org.bdware.doip.endpoint.client.DoipMessageCallback;
import org.bdware.sc.util.JsonUtil;
import wrp.jdk.nashorn.api.scripting.NashornScriptEngineUtil;
import wrp.jdk.nashorn.internal.runtime.ScriptFunction;

public class DoipMessageUtil {
    public DoaClusterClient client;
    static Logger LOGGER = LogManager.getLogger(DoipMessageUtil.class);
    NashornScriptEngineUtil engine;

    public DoipMessageUtil(NashornScriptEngineUtil engineUtil) {
        engine = engineUtil;
    }

    public DoipMessageFactory.DoipMessageBuilder builder() {
        return new DoipMessageFactory.DoipMessageBuilder();
    }

    public DoipMessage createRequest(String id, String op, DoipMessage original) {
        BasicOperations operation = BasicOperations.valueOf(op);
        DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
        builder.createRequest(id, operation == BasicOperations.Unknown ? op : operation.getName());
        DoipMessage ret;
        if (client instanceof TraceableDoaClusterClient) {
            ret = ((TraceableDoaClusterClient) client).wrapSpanWithParent(builder.create(), original);
        } else
            ret = builder.create();
        return ret;
    }

    public Object toJSObject(DoipMessage message) {
        JsonObject jo = toJsonObject(message);
        return JSONTool.convertJsonElementToMirror(jo);
    }

    public JsonObject toJsonObject(DoipMessage message) {
        JsonDoipMessage jsonDoipMessage = JsonDoipMessage.fromDoipMessage(message);
        return JsonUtil.parseObjectAsJsonObject(jsonDoipMessage);
    }

    public DoipMessageCallback wrapCallback(ScriptFunction som, Object... appendArgs) {
        return result -> {
            Object[] newArr;
            if (appendArgs != null && appendArgs.length > 0) {
                newArr = new Object[appendArgs.length + 1];
                newArr[0] = result;
                System.arraycopy(appendArgs, 0, newArr, 1, appendArgs.length);
            } else {
                newArr = new Object[1];
                newArr[0] = result;
            }
            engine.invokeFunctionWithObject(som, newArr);
        };
    }

    public DoipResponseCode getResponseCode(String respCode) {
        return DoipResponseCode.getDoResponse(respCode);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy