Search in sources :

Example 1 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo 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 PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class SQLDatasourceUtils method getSQLConnectorError.

public static BStruct getSQLConnectorError(Context context, Throwable throwable) {
    PackageInfo sqlPackageInfo = context.getProgramFile().getPackageInfo(Constants.SQL_PACKAGE_PATH);
    StructInfo errorStructInfo = sqlPackageInfo.getStructInfo(Constants.SQL_CONNECTOR_ERROR);
    BStruct sqlConnectorError = new BStruct(errorStructInfo.getType());
    if (throwable.getMessage() == null) {
        sqlConnectorError.setStringField(0, Constants.SQL_EXCEPTION_OCCURED);
    } else {
        sqlConnectorError.setStringField(0, throwable.getMessage());
    }
    return sqlConnectorError;
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 3 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class Utils method createSpanContextStruct.

public static BStruct createSpanContextStruct(Context context, BMap spanContext) {
    PackageInfo observePackage = context.getProgramFile().getPackageInfo("ballerina.observe");
    StructInfo spanStructInfo = observePackage.getStructInfo("SpanContext");
    return BLangVMStructs.createBStruct(spanStructInfo, spanContext);
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 4 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class BLangVMErrors method createCallFailedException.

public static BStruct createCallFailedException(WorkerExecutionContext context, Map<String, BStruct> errors) {
    PackageInfo errorPackageInfo = context.programFile.getPackageInfo(PACKAGE_RUNTIME);
    StructInfo errorStructInfo = errorPackageInfo.getStructInfo(STRUCT_CALL_FAILED_EXCEPTION);
    return generateError(context, true, errorStructInfo, MSG_CALL_FAILED, createErrorCauseArray(errors));
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo)

Example 5 with PackageInfo

use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.

the class BLangVMErrors method generateError.

private static BStruct generateError(CallableUnitInfo callableUnitInfo, boolean attachCallStack, Object... values) {
    ProgramFile progFile = callableUnitInfo.getPackageInfo().getProgramFile();
    PackageInfo errorPackageInfo = progFile.getPackageInfo(PACKAGE_BUILTIN);
    StructInfo errorStructInfo = errorPackageInfo.getStructInfo(STRUCT_GENERIC_ERROR);
    return generateError(callableUnitInfo, attachCallStack, errorStructInfo, values);
}
Also used : StructInfo(org.ballerinalang.util.codegen.StructInfo) PackageInfo(org.ballerinalang.util.codegen.PackageInfo) ProgramFile(org.ballerinalang.util.codegen.ProgramFile)

Aggregations

PackageInfo (org.ballerinalang.util.codegen.PackageInfo)48 StructInfo (org.ballerinalang.util.codegen.StructInfo)35 BStruct (org.ballerinalang.model.values.BStruct)17 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)10 BValue (org.ballerinalang.model.values.BValue)6 BStructType (org.ballerinalang.model.types.BStructType)5 FunctionInfo (org.ballerinalang.util.codegen.FunctionInfo)5 Debugger (org.ballerinalang.util.debugger.Debugger)5 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)5 BInteger (org.ballerinalang.model.values.BInteger)2 BMap (org.ballerinalang.model.values.BMap)2 BString (org.ballerinalang.model.values.BString)2 SocketIOChannel (org.ballerinalang.nativeimpl.io.channels.SocketIOChannel)2 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)2 TesterinaFunction (org.ballerinalang.testerina.core.entity.TesterinaFunction)2 Instruction (org.ballerinalang.util.codegen.Instruction)2 PackageVarInfo (org.ballerinalang.util.codegen.PackageVarInfo)2 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)2 Test (org.testng.annotations.Test)2 PrintStream (java.io.PrintStream)1