Search in sources :

Example 1 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class AsyncReadWriteTest method writeBytes.

@Test(description = "Write into a channel using async io framework")
public void writeBytes() throws IOException, URISyntaxException, ExecutionException, InterruptedException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "write.txt");
    Channel channel = new MockByteChannel(byteChannel);
    byte[] bytes = "hello".getBytes();
    int numberOfBytesWritten = IOUtils.writeFull(channel, bytes, 0, new EventContext());
    Assert.assertEquals(numberOfBytesWritten, bytes.length);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 2 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class CloseDelimitedRecordChannel method execute.

/**
 * <p>
 * Closes a text record channel.
 * </p>
 * <p>
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(RECORD_CHANNEL_INDEX);
    DelimitedRecordChannel recordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
    EventContext eventContext = new EventContext(context, callback);
    IOUtils.close(recordChannel, eventContext, CloseDelimitedRecordChannel::closeResponse);
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Example 3 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class HasNextTextRecord method response.

/**
 * Responds whether a next record exists.
 *
 * @param result the result processed.
 * @return result context.
 */
private static EventResult response(EventResult<Boolean, EventContext> result) {
    EventContext eventContext = result.getContext();
    Context context = eventContext.getContext();
    CallableUnitCallback callback = eventContext.getCallback();
    Boolean response = result.getResponse();
    context.setReturnValues(new BBoolean(response));
    callback.notifySuccess();
    return result;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BBoolean(org.ballerinalang.model.values.BBoolean) BBoolean(org.ballerinalang.model.values.BBoolean) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 4 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class LoadToTable method response.

private static EventResult response(EventResult<List, EventContext> result) {
    BStruct errorStruct;
    BTable table;
    EventContext eventContext = result.getContext();
    Context context = eventContext.getContext();
    Throwable error = eventContext.getError();
    if (null != error) {
        errorStruct = IOUtils.createError(context, error.getMessage());
        context.setReturnValues(errorStruct);
    } else {
        try {
            List records = result.getResponse();
            table = getbTable(context, records);
            context.setReturnValues(table);
        } catch (Throwable e) {
            errorStruct = IOUtils.createError(context, e.getMessage());
            context.setReturnValues(errorStruct);
        }
    }
    CallableUnitCallback callback = eventContext.getCallback();
    callback.notifySuccess();
    return result;
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) Context(org.ballerinalang.bre.Context) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BTable(org.ballerinalang.model.values.BTable) BStruct(org.ballerinalang.model.values.BStruct) List(java.util.List) CallableUnitCallback(org.ballerinalang.bre.bvm.CallableUnitCallback)

Example 5 with EventContext

use of org.ballerinalang.nativeimpl.io.events.EventContext in project ballerina by ballerina-lang.

the class LoadToTable method execute.

@Override
public void execute(Context context, CallableUnitCallback callback) {
    final String filePath = context.getStringArgument(0);
    Path path;
    try {
        path = Paths.get(filePath);
    } catch (InvalidPathException e) {
        String msg = "Unable to resolve the file path[" + filePath + "]: " + e.getMessage();
        context.setReturnValues(IOUtils.createError(context, msg));
        callback.notifySuccess();
        return;
    }
    if (Files.notExists(path)) {
        String msg = "Unable to find a file in given path: " + filePath;
        context.setReturnValues(IOUtils.createError(context, msg));
        callback.notifySuccess();
        return;
    }
    try {
        FileChannel sourceChannel = FileChannel.open(path, StandardOpenOption.READ);
        // FileChannel will close once we completely read the content.
        // Close will happen in DelimitedRecordReadAllEvent.
        DelimitedRecordChannel recordChannel = getDelimitedRecordChannel(context, sourceChannel);
        EventContext eventContext = new EventContext(context, callback);
        DelimitedRecordReadAllEvent event = new DelimitedRecordReadAllEvent(recordChannel, eventContext);
        CompletableFuture<EventResult> future = EventManager.getInstance().publish(event);
        future.thenApply(LoadToTable::response);
    } catch (IOException e) {
        String msg = "Failed to process the delimited file: " + e.getMessage();
        log.error(msg, e);
        context.setReturnValues(IOUtils.createError(context, msg));
    }
}
Also used : Path(java.nio.file.Path) EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) FileChannel(java.nio.channels.FileChannel) DelimitedRecordReadAllEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadAllEvent) IOException(java.io.IOException) InvalidPathException(java.nio.file.InvalidPathException) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Aggregations

EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)27 BStruct (org.ballerinalang.model.values.BStruct)20 Context (org.ballerinalang.bre.Context)11 CallableUnitCallback (org.ballerinalang.bre.bvm.CallableUnitCallback)11 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)6 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)5 EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)4 ByteChannel (java.nio.channels.ByteChannel)3 BInteger (org.ballerinalang.model.values.BInteger)3 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)3 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)3 Test (org.testng.annotations.Test)3 ExecutionException (java.util.concurrent.ExecutionException)2 BStringArray (org.ballerinalang.model.values.BStringArray)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 List (java.util.List)1 BBlob (org.ballerinalang.model.values.BBlob)1