use of org.ballerinalang.bre.Context in project ballerina by ballerina-lang.
the class WriteCharacters method writeResponse.
/**
* Processors the response after reading characters.
*
* @param result the response returned after reading characters.
* @return the response returned from the event.
*/
private static EventResult writeResponse(EventResult<Integer, EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Integer numberOfCharactersWritten = result.getResponse();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
context.setReturnValues(errorStruct);
} else {
context.setReturnValues(new BInteger(numberOfCharactersWritten));
}
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.Context in project ballerina by ballerina-lang.
the class WriteTextRecord method writeResponse.
/**
* Callback response received after the bytes are written.
*
* @param result the response received.
* @return the result context.
*/
private static EventResult writeResponse(EventResult<Integer, EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
}
context.setReturnValues(errorStruct);
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.Context in project ballerina by ballerina-lang.
the class CloseCharacterChannel method closeResponse.
private static EventResult closeResponse(EventResult<Boolean, EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
}
context.setReturnValues(errorStruct);
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.Context in project ballerina by ballerina-lang.
the class BLangFunctions method invokeNativeCallable.
private static WorkerExecutionContext invokeNativeCallable(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs, int flags) {
WorkerData parentLocalData = parentCtx.workerLocal;
BType[] retTypes = callableUnitInfo.getRetParamTypes();
WorkerData caleeSF = BLangVMUtils.createWorkerDataForLocal(callableUnitInfo.getDefaultWorkerInfo(), parentCtx, argRegs, callableUnitInfo.getParamTypes());
Context ctx = new NativeCallContext(parentCtx, callableUnitInfo, caleeSF);
NativeCallableUnit nativeCallable = callableUnitInfo.getNativeCallableUnit();
if (nativeCallable == null) {
return parentCtx;
}
try {
if (nativeCallable.isBlocking()) {
nativeCallable.execute(ctx, null);
BLangVMUtils.populateWorkerDataWithValues(parentLocalData, retRegs, ctx.getReturnValues(), retTypes);
if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
TraceUtil.finishTraceSpan(TraceUtil.getTracer(parentCtx));
}
/* we want the parent to continue, since we got the response of the native call already */
return parentCtx;
} else {
CallableUnitCallback callback;
if (TraceManagerWrapper.getInstance().isTraceEnabled() && FunctionFlags.isObserved(flags)) {
callback = new TraceableCallbackWrapper(parentCtx, new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes));
} else {
callback = new BLangCallableUnitCallback(ctx, parentCtx, retRegs, retTypes);
}
nativeCallable.execute(ctx, callback);
/* we want the parent to suspend (i.e. go to wait for response state) and stay until notified */
return null;
}
} catch (BLangNullReferenceException e) {
return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createNullRefException(callableUnitInfo));
} catch (Throwable e) {
return BLangVMUtils.handleNativeInvocationError(parentCtx, BLangVMErrors.createError(callableUnitInfo, e.getMessage()));
}
}
use of org.ballerinalang.bre.Context in project ballerina by ballerina-lang.
the class AppointmentJob method execute.
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
NativeCallableUnit fn = (NativeCallableUnit) jobDataMap.get(AppointmentConstants.BALLERINA_FUNCTION);
Context balParentContext = (Context) jobDataMap.get(AppointmentConstants.BALLERINA_PARENT_CONTEXT);
FunctionRefCPEntry onTriggerFunction = (FunctionRefCPEntry) jobDataMap.get(AppointmentConstants.BALLERINA_ON_TRIGGER_FUNCTION);
FunctionRefCPEntry onErrorFunction = (FunctionRefCPEntry) jobDataMap.get(AppointmentConstants.BALLERINA_ON_ERROR_FUNCTION);
ProgramFile programFile = balParentContext.getProgramFile();
TaskExecutor.execute(fn, balParentContext, onTriggerFunction, onErrorFunction, programFile);
}
Aggregations