use of org.ballerinalang.util.codegen.ProgramFile 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.util.codegen.ProgramFile 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());
}
Aggregations