Search in sources :

Example 1 with BLangProgramLoader

use of org.ballerinalang.BLangProgramLoader 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 BLangProgramLoader

use of org.ballerinalang.BLangProgramLoader in project carbon-apimgt by wso2.

the class GenerateArchives method execute.

/**
 * Generate archives
 *
 * @param type        type of the bal file
 * @param packagePath package path to the bal
 * @param targetName  target file name
 */
public void execute(String type, String srcDir, String packagePath, String targetName) {
    Path sourcePath = Paths.get(packagePath);
    try {
        Path realPath = Paths.get(srcDir + File.separator + packagePath).toRealPath(LinkOption.NOFOLLOW_LINKS);
        if (!Files.isDirectory(realPath, LinkOption.NOFOLLOW_LINKS) && !realPath.toString().endsWith(BLangPrograms.BSOURCE_FILE_EXT)) {
            log.error("invalid file or package '" + sourcePath + "'");
            throw new IllegalArgumentException("invalid file or package '" + sourcePath + "'");
        }
    } catch (IOException e) {
        log.error("error reading from file: " + sourcePath + " reason: " + e.getMessage(), e);
        throw new RuntimeException("error reading from file: " + sourcePath + " reason: " + e.getMessage(), e);
    }
    Path programDirPath = Paths.get(srcDir);
    BLangProgram bLangProgram = null;
    if (MAIN_TYPE.equals(type)) {
        bLangProgram = new BLangProgramLoader().loadMain(programDirPath, sourcePath);
    } else if (SERVICE_TYPE.equals(type)) {
        bLangProgram = new BLangProgramLoader().loadService(programDirPath, sourcePath);
    } else {
        log.error("source type '" + type + "' not supported");
        throw new RuntimeException("source type '" + type + "' not supported");
    }
    new BLangProgramArchiveBuilder().build(bLangProgram, targetName.trim());
}
Also used : Path(java.nio.file.Path) BLangProgramArchiveBuilder(org.ballerinalang.BLangProgramArchiveBuilder) BLangProgramLoader(org.ballerinalang.BLangProgramLoader) BLangProgram(org.ballerinalang.model.BLangProgram) IOException(java.io.IOException)

Aggregations

Path (java.nio.file.Path)2 BLangProgramLoader (org.ballerinalang.BLangProgramLoader)2 IOException (java.io.IOException)1 BLangProgramArchiveBuilder (org.ballerinalang.BLangProgramArchiveBuilder)1 Context (org.ballerinalang.bre.Context)1 BLangProgram (org.ballerinalang.model.BLangProgram)1 BValue (org.ballerinalang.model.values.BValue)1 PackageInfo (org.ballerinalang.util.codegen.PackageInfo)1 ProgramFile (org.ballerinalang.util.codegen.ProgramFile)1 ServiceInfo (org.ballerinalang.util.codegen.ServiceInfo)1 BLangRuntimeException (org.ballerinalang.util.exceptions.BLangRuntimeException)1 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)1