use of org.ballerinalang.bre.Context in project carbon-apimgt by wso2.
the class DeployService method execute.
@Override
public BValue[] execute(Context context) {
String fileName = getStringArgument(context, 0);
String serviceName = getStringArgument(context, 1);
String config = getStringArgument(context, 2);
String packageName = getStringArgument(context, 3);
Path path = Paths.get(packageName);
String filePath = path.toAbsolutePath() + File.separator + fileName;
if (Util.saveFile(filePath, config)) {
ProgramFile programFile = new BLangProgramLoader().loadServiceProgramFile(programDirPath, path);
String[] servicePackageNameList = programFile.getServicePackageNameList();
if (servicePackageNameList.length == 0) {
throw new BallerinaException("no service found in '" + programFile.getProgramFilePath() + "'");
}
// This is required to invoke package/service init functions;
Context bContext = new Context(programFile);
// bContext.initFunction = true;
PackageInfo packageInfo = programFile.getPackageInfo(packageName.replace("/", "."));
// Invoke package init function
BLangFunctions.invokeFunction(programFile, packageInfo, packageInfo.getInitFunctionInfo(), bContext);
if (bContext.getError() != null) {
String stackTraceStr = BLangVMErrors.getPrintableStackTrace(bContext.getError());
throw new BLangRuntimeException("error: " + stackTraceStr);
}
for (ServiceInfo serviceInfo : packageInfo.getServiceInfoList()) {
// Invoke service init function
if (serviceName.equals(serviceInfo.getName())) {
BLangFunctions.invokeFunction(programFile, packageInfo, serviceInfo.getInitFunctionInfo(), bContext);
if (bContext.getError() != null) {
String stackTraceStr = BLangVMErrors.getPrintableStackTrace(bContext.getError());
throw new BLangRuntimeException("error: " + stackTraceStr);
}
// Deploy service
DispatcherRegistry.getInstance().getServiceDispatchers().forEach((protocol, dispatcher) -> dispatcher.serviceRegistered(serviceInfo));
}
}
}
return new BValue[0];
}
use of org.ballerinalang.bre.Context in project ballerina by ballerina-lang.
the class BLangFunctions method invokeNativeCallableAsync.
private static void invokeNativeCallableAsync(CallableUnitInfo callableUnitInfo, WorkerExecutionContext parentCtx, int[] argRegs, int[] retRegs) {
WorkerData caleeSF = BLangVMUtils.createWorkerDataForLocal(callableUnitInfo.getDefaultWorkerInfo(), parentCtx, argRegs, callableUnitInfo.getParamTypes());
Context nativeCtx = new NativeCallContext(parentCtx, callableUnitInfo, caleeSF);
NativeCallableUnit nativeCallable = callableUnitInfo.getNativeCallableUnit();
if (nativeCallable == null) {
return;
}
AsyncInvocableWorkerResponseContext respCtx;
if (nativeCallable.isBlocking()) {
respCtx = BLangScheduler.executeBlockingNativeAsync(nativeCallable, nativeCtx);
} else {
respCtx = BLangScheduler.executeNonBlockingNativeAsync(nativeCallable, nativeCtx);
}
BLangVMUtils.populateWorkerDataWithValues(parentCtx.workerLocal, retRegs, new BValue[] { new BCallableFuture(callableUnitInfo.getName(), respCtx) }, new BType[] { BTypes.typeFuture });
}
use of org.ballerinalang.bre.Context in project ballerina by ballerina-lang.
the class HasNextTextRecord method response.
/**
* Responds whether a next record exists.
*
* @param result the result processed.
* @return result context.
*/
private static EventResult response(EventResult<Boolean, EventContext> result) {
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
CallableUnitCallback callback = eventContext.getCallback();
Boolean response = result.getResponse();
context.setReturnValues(new BBoolean(response));
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.Context in project ballerina by ballerina-lang.
the class LoadToTable method response.
private static EventResult response(EventResult<List, EventContext> result) {
BStruct errorStruct;
BTable table;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
Throwable error = eventContext.getError();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
context.setReturnValues(errorStruct);
} else {
try {
List records = result.getResponse();
table = getbTable(context, records);
context.setReturnValues(table);
} catch (Throwable e) {
errorStruct = IOUtils.createError(context, e.getMessage());
context.setReturnValues(errorStruct);
}
}
CallableUnitCallback callback = eventContext.getCallback();
callback.notifySuccess();
return result;
}
use of org.ballerinalang.bre.Context in project ballerina by ballerina-lang.
the class Write method writeResponse.
/*
* Function which will be notified on the response obtained after the async operation.
*
* @param result context of the callback.
* @return Once the callback is processed we further return back the result.
*/
private static EventResult writeResponse(EventResult<Integer, EventContext> result) {
BStruct errorStruct = null;
EventContext eventContext = result.getContext();
Context context = eventContext.getContext();
Throwable error = eventContext.getError();
Integer numberOfBytesWritten = result.getResponse();
CallableUnitCallback callback = eventContext.getCallback();
if (null != error) {
errorStruct = IOUtils.createError(context, error.getMessage());
context.setReturnValues(errorStruct);
} else {
context.setReturnValues(new BInteger(numberOfBytesWritten));
}
callback.notifySuccess();
return result;
}
Aggregations