use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.
the class BRunUtil method invokePackageInit.
/**
* Invoke package init function.
*
* @param compileResult CompileResult instance
* @param packageName Name of the package to invoke
*/
protected static void invokePackageInit(CompileResult compileResult, String packageName) {
if (compileResult.getErrorCount() > 0) {
throw new IllegalStateException(compileResult.toString());
}
ProgramFile programFile = compileResult.getProgFile();
PackageInfo packageInfo = programFile.getPackageInfo(packageName);
WorkerExecutionContext context = new WorkerExecutionContext(programFile);
Debugger debugger = new Debugger(programFile);
programFile.setDebugger(debugger);
compileResult.setContext(context);
BLangFunctions.invokePackageInitFunction(packageInfo.getInitFunctionInfo(), context);
}
use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.
the class TransactionUtils method invokeCoordinatorFunction.
private static BValue[] invokeCoordinatorFunction(WorkerExecutionContext ctx, String functionName, BValue[] args) {
PackageInfo packageInfo = ctx.programFile.getPackageInfo(TransactionConstants.COORDINATOR_PACKAGE);
FunctionInfo functionInfo = packageInfo.getFunctionInfo(functionName);
return BLangFunctions.invokeCallable(functionInfo, args);
}
use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.
the class OpenSocket method execute.
@Override
public void execute(Context context) {
final String host = context.getStringArgument(0);
final int port = (int) context.getIntArgument(0);
final BStruct options = (BStruct) context.getRefArgument(0);
if (log.isDebugEnabled()) {
log.debug("Remote host: " + host);
log.debug("Remote port: " + port);
}
Socket socket;
SocketChannel channel;
try {
// Open a client connection
SocketChannel socketChannel = SocketChannel.open();
if (options.getIntField(LOCAL_PORT_OPTION_FIELD_INDEX) > 0) {
if (log.isDebugEnabled()) {
log.debug("Bind client socket to local port: " + options.getIntField(0));
}
socketChannel.bind(new InetSocketAddress((int) options.getIntField(0)));
}
socketChannel.connect(new InetSocketAddress(host, port));
log.debug("Successfully connect to remote server.");
socket = socketChannel.socket();
if (log.isDebugEnabled()) {
log.debug("Bound local port: " + socket.getLocalPort());
log.debug("Timeout on blocking Socket operations: " + socket.getSoTimeout());
log.debug("ReceiveBufferSize: " + socket.getReceiveBufferSize());
log.debug("KeepAlive: " + socket.getKeepAlive());
}
channel = socket.getChannel();
PackageInfo ioPackageInfo = context.getProgramFile().getPackageInfo(SOCKET_PACKAGE);
// Create ByteChannel Struct
StructInfo channelStructInfo = ioPackageInfo.getStructInfo(BYTE_CHANNEL_STRUCT_TYPE);
Channel ballerinaSocketChannel = new SocketIOChannel(channel, 0);
BStruct channelStruct = BLangVMStructs.createBStruct(channelStructInfo, ballerinaSocketChannel);
channelStruct.addNativeData(IOConstants.BYTE_CHANNEL_NAME, ballerinaSocketChannel);
// Create Socket Struct
StructInfo socketStructInfo = ioPackageInfo.getStructInfo(SOCKET_STRUCT_TYPE);
BStruct socketStruct = BLangVMStructs.createBStruct(socketStructInfo);
socketStruct.setRefField(0, channelStruct);
socketStruct.setIntField(0, socket.getPort());
socketStruct.setIntField(1, socket.getLocalPort());
socketStruct.setStringField(0, socket.getInetAddress().getHostAddress());
socketStruct.setStringField(1, socket.getLocalAddress().getHostAddress());
socketStruct.addNativeData(IOConstants.CLIENT_SOCKET_NAME, channel);
context.setReturnValues(socketStruct);
} catch (Throwable e) {
String msg = "Failed to open a connection to [" + host + ":" + port + "] : " + e.getMessage();
log.error(msg, e);
context.setReturnValues(IOUtils.createError(context, msg));
}
}
use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.
the class MessageUtils method getConnectorError.
public static BStruct getConnectorError(Context context, Throwable throwable) {
PackageInfo grpcPackageInfo = context.getProgramFile().getPackageInfo(MessageConstants.PROTOCOL_STRUCT_PACKAGE_GRPC);
StructInfo errorStructInfo = grpcPackageInfo.getStructInfo(MessageConstants.CONNECTOR_ERROR);
return getConnectorError(errorStructInfo.getType(), throwable);
}
use of org.ballerinalang.util.codegen.PackageInfo in project ballerina by ballerina-lang.
the class GetLocale method createLocale.
private BStruct createLocale(Context context) {
String language = System.getProperty("user.language");
if (language == null) {
language = BTypes.typeString.getZeroValue().stringValue();
}
String country = System.getProperty("user.country");
if (country == null) {
country = BTypes.typeString.getZeroValue().stringValue();
}
PackageInfo utilsPackageInfo = context.getProgramFile().getPackageInfo("ballerina.util");
StructInfo localeStructInfo = utilsPackageInfo.getStructInfo("Locale");
return BLangVMStructs.createBStruct(localeStructInfo, language, country);
}
Aggregations