Search in sources :

Example 1 with Context

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];
}
Also used : Path(java.nio.file.Path) Context(org.ballerinalang.bre.Context) ServiceInfo(org.ballerinalang.util.codegen.ServiceInfo) BLangRuntimeException(org.ballerinalang.util.exceptions.BLangRuntimeException) BLangProgramLoader(org.ballerinalang.BLangProgramLoader) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) BValue(org.ballerinalang.model.values.BValue) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Example 2 with Context

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 });
}
Also used : InitWorkerResponseContext(org.ballerinalang.bre.bvm.InitWorkerResponseContext) SyncCallableWorkerResponseContext(org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext) NativeCallContext(org.ballerinalang.bre.NativeCallContext) ForkJoinWorkerResponseContext(org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext) WorkerResponseContext(org.ballerinalang.bre.bvm.WorkerResponseContext) Context(org.ballerinalang.bre.Context) CallableWorkerResponseContext(org.ballerinalang.bre.bvm.CallableWorkerResponseContext) WorkerExecutionContext(org.ballerinalang.bre.bvm.WorkerExecutionContext) AsyncInvocableWorkerResponseContext(org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext) AsyncInvocableWorkerResponseContext(org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext) NativeCallContext(org.ballerinalang.bre.NativeCallContext) NativeCallableUnit(org.ballerinalang.model.NativeCallableUnit) BCallableFuture(org.ballerinalang.model.values.BCallableFuture) WorkerData(org.ballerinalang.bre.bvm.WorkerData)

Example 3 with Context

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;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BBoolean(org.ballerinalang.model.values.BBoolean) BBoolean(org.ballerinalang.model.values.BBoolean) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 4 with Context

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;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BTable(org.ballerinalang.model.values.BTable) BStruct(org.ballerinalang.model.values.BStruct) List(java.util.List) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 5 with Context

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;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BInteger(org.ballerinalang.model.values.BInteger) BStruct(org.ballerinalang.model.values.BStruct) BInteger(org.ballerinalang.model.values.BInteger) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Aggregations

Context (org.ballerinalang.bre.Context)15 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)12 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)11 BStruct (org.ballerinalang.model.values.BStruct)10 NativeCallableUnit (org.ballerinalang.model.NativeCallableUnit)3 BInteger (org.ballerinalang.model.values.BInteger)3 NativeCallContext (org.ballerinalang.bre.NativeCallContext)2 AsyncInvocableWorkerResponseContext (org.ballerinalang.bre.bvm.AsyncInvocableWorkerResponseContext)2 CallableWorkerResponseContext (org.ballerinalang.bre.bvm.CallableWorkerResponseContext)2 ForkJoinWorkerResponseContext (org.ballerinalang.bre.bvm.ForkJoinWorkerResponseContext)2 InitWorkerResponseContext (org.ballerinalang.bre.bvm.InitWorkerResponseContext)2 SyncCallableWorkerResponseContext (org.ballerinalang.bre.bvm.SyncCallableWorkerResponseContext)2 WorkerData (org.ballerinalang.bre.bvm.WorkerData)2 WorkerExecutionContext (org.ballerinalang.bre.bvm.WorkerExecutionContext)2 WorkerResponseContext (org.ballerinalang.bre.bvm.WorkerResponseContext)2 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)2 Path (java.nio.file.Path)1 List (java.util.List)1 BLangProgramLoader (org.ballerinalang.BLangProgramLoader)1 BLangCallableUnitCallback (org.ballerinalang.bre.BLangCallableUnitCallback)1