Search in sources :

Example 11 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class DefaultStreamObserver method onCompleted.

@Override
public void onCompleted() {
    Resource onCompleted = resourceMap.get(MessageConstants.ON_COMPLETE_RESOURCE);
    if (onCompleted == null) {
        String message = "Error in listener service definition. onCompleted resource does not exists";
        LOG.error(message);
        throw new RuntimeException(message);
    }
    List<ParamDetail> paramDetails = onCompleted.getParamDetails();
    BValue[] signatureParams = new BValue[paramDetails.size()];
    CallableUnitCallback callback = new GrpcCallableUnitCallBack(null);
    Executor.submit(onCompleted, callback, null, null, signatureParams);
}
Also used : GrpcCallableUnitCallBack(org.ballerinalang.net.grpc.GrpcCallableUnitCallBack) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BValue(org.ballerinalang.model.values.BValue) Resource(org.ballerinalang.connector.api.Resource) BString(org.ballerinalang.model.values.BString) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 12 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class DefaultStreamObserver method onNext.

@Override
public void onNext(Message value) {
    Resource resource = resourceMap.get(MessageConstants.ON_MESSAGE_RESOURCE);
    if (resource == null) {
        String message = "Error in listener service definition. onNext resource does not exists";
        LOG.error(message);
        throw new RuntimeException(message);
    }
    List<ParamDetail> paramDetails = resource.getParamDetails();
    BValue[] signatureParams = new BValue[paramDetails.size()];
    BValue requestParam = getRequestParameter(resource, value);
    if (requestParam != null) {
        signatureParams[0] = requestParam;
    }
    CallableUnitCallback callback = new GrpcCallableUnitCallBack(null);
    Executor.submit(resource, callback, null, null, signatureParams);
}
Also used : GrpcCallableUnitCallBack(org.ballerinalang.net.grpc.GrpcCallableUnitCallBack) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BValue(org.ballerinalang.model.values.BValue) Resource(org.ballerinalang.connector.api.Resource) BString(org.ballerinalang.model.values.BString) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 13 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class DefaultStreamObserver method onError.

@Override
public void onError(Throwable t) {
    Resource onError = resourceMap.get(MessageConstants.ON_ERROR_RESOURCE);
    if (onError == null) {
        String message = "Error in listener service definition. onError resource does not exists";
        LOG.error(message);
        throw new RuntimeException(message);
    }
    List<ParamDetail> paramDetails = onError.getParamDetails();
    BValue[] signatureParams = new BValue[paramDetails.size()];
    if (paramDetails.size() != 1) {
        String message = "Error in onError resource definition. It must have only error params, but have " + paramDetails.size();
        LOG.error(message);
        throw new RuntimeException(message);
    }
    BType errorType = paramDetails.get(1).getVarType();
    BStruct errorStruct = MessageUtils.getConnectorError((BStructType) errorType, t);
    signatureParams[0] = errorStruct;
    CallableUnitCallback callback = new GrpcCallableUnitCallBack(null);
    Executor.submit(onError, callback, null, null, signatureParams);
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) GrpcCallableUnitCallBack(org.ballerinalang.net.grpc.GrpcCallableUnitCallBack) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BValue(org.ballerinalang.model.values.BValue) BType(org.ballerinalang.model.types.BType) Resource(org.ballerinalang.connector.api.Resource) BString(org.ballerinalang.model.values.BString) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 14 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class BallerinaHTTPConnectorListener method extractPropertiesAndStartResourceExecution.

protected void extractPropertiesAndStartResourceExecution(HTTPCarbonMessage httpCarbonMessage, HttpResource httpResource) {
    boolean isTransactionInfectable = httpResource.isTransactionInfectable();
    Map<String, Object> properties = collectRequestProperties(httpCarbonMessage, isTransactionInfectable);
    properties.put(HttpConstants.REMOTE_ADDRESS, httpCarbonMessage.getProperty(HttpConstants.REMOTE_ADDRESS));
    properties.put(HttpConstants.ORIGIN_HOST, httpCarbonMessage.getHeader(HttpConstants.ORIGIN_HOST));
    BValue[] signatureParams = HttpDispatcher.getSignatureParameters(httpResource, httpCarbonMessage);
    // invoke the request path filters
    invokeRequestFilters(httpCarbonMessage, signatureParams[1], getRequestFilterContext(httpResource));
    Tracer tracer = TraceManagerWrapper.newTracer(null, false);
    httpCarbonMessage.getHeaders().entries().stream().filter(c -> c.getKey().startsWith(TraceConstants.TRACE_PREFIX)).forEach(e -> tracer.addProperty(e.getKey(), e.getValue()));
    Map<String, String> tags = new HashMap<>();
    tags.put("http.method", (String) httpCarbonMessage.getProperty("HTTP_METHOD"));
    tags.put("http.url", (String) httpCarbonMessage.getProperty("REQUEST_URL"));
    tracer.addTags(tags);
    CallableUnitCallback callback = new HttpCallableUnitCallback(httpCarbonMessage);
    // TODO handle BallerinaConnectorException
    Executor.submit(httpResource.getBalResource(), callback, properties, tracer, signatureParams);
}
Also used : HttpConnectorListener(org.wso2.transport.http.netty.contract.HttpConnectorListener) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) HashMap(java.util.HashMap) BLangFunctions(org.ballerinalang.util.program.BLangFunctions) BLangConnectorSPIUtil(org.ballerinalang.connector.api.BLangConnectorSPIUtil) Executor(org.ballerinalang.connector.api.Executor) TraceManagerWrapper(org.ballerinalang.util.tracer.TraceManagerWrapper) HashSet(java.util.HashSet) TraceConstants(org.ballerinalang.util.tracer.TraceConstants) PROTOCOL_PACKAGE_HTTP(org.ballerinalang.net.http.HttpConstants.PROTOCOL_PACKAGE_HTTP) BTypeDescValue(org.ballerinalang.model.values.BTypeDescValue) Tracer(org.ballerinalang.util.tracer.Tracer) Map(java.util.Map) FilterHolder(org.ballerinalang.net.http.serviceendpoint.FilterHolder) Constants(org.ballerinalang.runtime.Constants) BallerinaConnectorException(org.ballerinalang.connector.api.BallerinaConnectorException) BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) HashMap(java.util.HashMap) BValue(org.ballerinalang.model.values.BValue) Tracer(org.ballerinalang.util.tracer.Tracer) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 15 with CallableUnitCallback

use of org.ballerinalang.bre.bvm.CallableUnitCallback in project ballerina by ballerina-lang.

the class BidirectionalStreamingListener method invoke.

@Override
public StreamObserver<Message> invoke(StreamObserver<Message> responseObserver) {
    Resource onOpen = resourceMap.get(MessageConstants.ON_OPEN_RESOURCE);
    List<ParamDetail> paramDetails = onOpen.getParamDetails();
    BValue[] signatureParams = new BValue[paramDetails.size()];
    signatureParams[0] = getConnectionParameter(onOpen, responseObserver);
    CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
    Executor.submit(onOpen, callback, null, null, signatureParams);
    return new StreamObserver<Message>() {

        @Override
        public void onNext(Message value) {
            Resource onMessage = resourceMap.get(MessageConstants.ON_MESSAGE_RESOURCE);
            List<ParamDetail> paramDetails = onMessage.getParamDetails();
            BValue[] signatureParams = new BValue[paramDetails.size()];
            signatureParams[0] = getConnectionParameter(onMessage, responseObserver);
            BValue requestParam = getRequestParameter(onMessage, value);
            if (requestParam != null) {
                signatureParams[1] = requestParam;
            }
            CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, isEmptyResponse());
            Executor.submit(onMessage, callback, null, null, signatureParams);
        }

        @Override
        public void onError(Throwable t) {
            Resource onError = resourceMap.get(MessageConstants.ON_ERROR_RESOURCE);
            if (onError == null) {
                String message = "Error in listener service definition. onError resource does not exists";
                LOG.error(message);
                throw new RuntimeException(message);
            }
            List<ParamDetail> paramDetails = onError.getParamDetails();
            BValue[] signatureParams = new BValue[paramDetails.size()];
            signatureParams[0] = getConnectionParameter(onError, responseObserver);
            if (paramDetails.size() != 2) {
                String message = "Error in onError resource definition. It must have two input params, but have " + paramDetails.size();
                LOG.error(message);
                throw new RuntimeException(message);
            }
            BType errorType = paramDetails.get(1).getVarType();
            BStruct errorStruct = MessageUtils.getConnectorError((BStructType) errorType, t);
            signatureParams[1] = errorStruct;
            CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
            Executor.submit(onError, callback, null, null, signatureParams);
        }

        @Override
        public void onCompleted() {
            Resource onCompleted = resourceMap.get(MessageConstants.ON_COMPLETE_RESOURCE);
            if (onCompleted == null) {
                String message = "Error in listener service definition. onError resource does not exists";
                LOG.error(message);
                throw new RuntimeException(message);
            }
            List<ParamDetail> paramDetails = onCompleted.getParamDetails();
            BValue[] signatureParams = new BValue[paramDetails.size()];
            signatureParams[0] = getConnectionParameter(onCompleted, responseObserver);
            CallableUnitCallback callback = new GrpcCallableUnitCallBack(responseObserver, Boolean.FALSE);
            Executor.submit(onCompleted, callback, null, null, signatureParams);
        }
    };
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) GrpcCallableUnitCallBack(org.ballerinalang.net.grpc.GrpcCallableUnitCallBack) BStruct(org.ballerinalang.model.values.BStruct) Message(org.ballerinalang.net.grpc.Message) BValue(org.ballerinalang.model.values.BValue) Resource(org.ballerinalang.connector.api.Resource) ParamDetail(org.ballerinalang.connector.api.ParamDetail) BType(org.ballerinalang.model.types.BType) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Aggregations

CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)21 BStruct (org.ballerinalang.model.values.BStruct)15 Context (org.ballerinalang.bre.Context)12 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)11 BValue (org.ballerinalang.model.values.BValue)9 ParamDetail (org.ballerinalang.connector.api.ParamDetail)8 GrpcCallableUnitCallBack (org.ballerinalang.net.grpc.GrpcCallableUnitCallBack)7 Resource (org.ballerinalang.connector.api.Resource)6 BString (org.ballerinalang.model.values.BString)5 BType (org.ballerinalang.model.types.BType)4 BInteger (org.ballerinalang.model.values.BInteger)3 StreamObserver (io.grpc.stub.StreamObserver)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 BLangConnectorSPIUtil (org.ballerinalang.connector.api.BLangConnectorSPIUtil)2 BallerinaConnectorException (org.ballerinalang.connector.api.BallerinaConnectorException)2 Executor (org.ballerinalang.connector.api.Executor)2 Message (org.ballerinalang.net.grpc.Message)2 PROTOCOL_PACKAGE_HTTP (org.ballerinalang.net.http.HttpConstants.PROTOCOL_PACKAGE_HTTP)2