Search in sources :

Example 1 with BValue

use of org.ballerinalang.model.values.BValue in project carbon-apimgt by wso2.

the class PutCacheEntry method execute.

@Override
public BValue[] execute(Context context) {
    String cacheName = getStringArgument(context, 0);
    String cacheKey = getStringArgument(context, 1);
    BValue cacheEntry = getRefArgument(context, 0);
    // TODO If cache is not created then need to send proper message or create and put entry.
    CacheManagerHolder.getInstance().getCacheManager().getCache(cacheName).put(cacheKey, cacheEntry);
    return getBValues(new BString(cacheName));
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString)

Example 2 with BValue

use of org.ballerinalang.model.values.BValue in project carbon-apimgt by wso2.

the class DeployFile method execute.

@Override
public BValue[] execute(Context context) {
    String fileName = getStringArgument(context, 0);
    String config = getStringArgument(context, 1);
    String packageName = getStringArgument(context, 2);
    Path path = Paths.get(packageName);
    String filePath = path.toAbsolutePath() + File.separator + fileName;
    if (Util.saveFile(filePath, config)) {
        log.info("write config to File system");
    } else {
        log.error("Error saving API configuration in " + path);
    }
    return new BValue[0];
}
Also used : Path(java.nio.file.Path) BValue(org.ballerinalang.model.values.BValue)

Example 3 with BValue

use of org.ballerinalang.model.values.BValue 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 4 with BValue

use of org.ballerinalang.model.values.BValue in project carbon-apimgt by wso2.

the class SetProperty method execute.

@Override
public BValue[] execute(Context context) {
    BMessage msg = (BMessage) getRefArgument(context, 0);
    String propertyName = getStringArgument(context, 0);
    BValue propertyValue = getRefArgument(context, 1);
    if (propertyName != null && propertyValue != null) {
        msg.setProperty(propertyName, propertyValue);
    }
    return VOID_RETURN;
}
Also used : BMessage(org.ballerinalang.model.values.BMessage) BValue(org.ballerinalang.model.values.BValue)

Example 5 with BValue

use of org.ballerinalang.model.values.BValue in project carbon-apimgt by wso2.

the class CacheTest method testCacheForNonExistence.

@Test
public void testCacheForNonExistence() {
    ProgramFile bLangProgram1 = BTestUtils.parseBalFile("samples/cache/nonExistenceEntryCacheTest.bal");
    // Create arguments to initiate cache
    BValue[] args = { new BString("cacheName"), new BString("15"), new BString("cacheKey") };
    // Test ballerina cache create, put and get for BString
    BValue[] returns = BLangFunctions.invokeNew(bLangProgram1, "testCacheForNonExistence", args);
    Assert.assertTrue(returns[0] instanceof BBoolean);
    BBoolean value = (BBoolean) returns[0];
    Assert.assertFalse(value.booleanValue());
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BBoolean(org.ballerinalang.model.values.BBoolean) ProgramFile(org.ballerinalang.util.codegen.ProgramFile) Test(org.testng.annotations.Test)

Aggregations

BValue (org.ballerinalang.model.values.BValue)1043 Test (org.testng.annotations.Test)923 BString (org.ballerinalang.model.values.BString)437 BInteger (org.ballerinalang.model.values.BInteger)323 BStruct (org.ballerinalang.model.values.BStruct)188 BFloat (org.ballerinalang.model.values.BFloat)118 BJSON (org.ballerinalang.model.values.BJSON)112 BBoolean (org.ballerinalang.model.values.BBoolean)79 CompileResult (org.ballerinalang.launcher.util.CompileResult)60 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)45 BMap (org.ballerinalang.model.values.BMap)43 BXMLItem (org.ballerinalang.model.values.BXMLItem)42 BXML (org.ballerinalang.model.values.BXML)40 BStringArray (org.ballerinalang.model.values.BStringArray)30 BIntArray (org.ballerinalang.model.values.BIntArray)25 BBlob (org.ballerinalang.model.values.BBlob)23 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)19 BeforeTest (org.testng.annotations.BeforeTest)19 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)19 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)16