Ballerina Native Client Connectors and all you need to know :)

Rajith Vitharana
Nov 12, 2017 · 6 min read
@JavaSPIService("org.ballerinalang.spi.ExtensionPackageRepositoryProvider")
public class PackageRepositoryProvider implements ExtensionPackageRepositoryProvider {

private static final String JAR_BAL_LIB_LOCATION = "/META-INF/natives/";

@Override
public PackageRepository loadRepository() {
return new ClasspathPackageRepository(this.getClass(), JAR_BAL_LIB_LOCATION);
}
}
package sample.con.print;public connector PrintConnector() {
native action printText(string text)(boolean);
}
@BallerinaAction(
packageName = "sample.con.print",
actionName = "printText",
connectorName = "PrintConnector",
args = {
@Argument(name = "text",
type = TypeKind.STRING)
},
returnType = {
@ReturnType(type = TypeKind.BOOLEAN)
}
)
public class PrintText extends AbstractNativeAction {

@Override
public ConnectorFuture execute(Context context) {
ClientConnectorFuture future = new ClientConnectorFuture();
//Implement your logic here
System.out.println("**** Action printText called ****");
String text = getStringArgument(context, 0);
System.out.println("**** " + text + " ****");
BBoolean result = new BBoolean(true);
future.notifyReply(result);
return future;

}
}
public int getIntArgument(Context context, int index)
public String getStringArgument(Context context, int index)
public BValue getRefArgument(Context context, int index)
...
..
native action test(string a, int b, string c, int d)
String a = getStringArgument(context, 0);
String c = getStringArgument(context, 1);
int b = getIntArgument(context, 0);
int d = getIntArgument(context, 1);
void notifySuccess();
void notifyReply(BValue... value);
void notifyFailure(BallerinaConnectorException ex);
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>${mvn.processor.plugin.version}</version>
<configuration>
<processors>
<processor>org.ballerinalang.codegen.BallerinaAnnotationProcessor</processor>
</processors>
<options>
<nativeEntityProviderPackage>org.sample.con.print.generated</nativeEntityProviderPackage>
<nativeEntityProviderClass>StandardNativeElementProvider</nativeEntityProviderClass>
</options>
</configuration>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
</plugin>
<resources>
<!-- copy built-in ballerina sources to the jar -->
<resource>
<directory>${project.build.directory}/../src/main/ballerina</directory>
<targetPath>META-INF/natives</targetPath>
</resource>
</resources>
import sample.con.print;

function main(string[] args) {
endpoint<print:PrintConnector> ep {
create print:PrintConnector();
}
boolean result = ep.printText("sample text");
println("result - " + result);
}
@BallerinaAction(packageName = "sample.con.print",
actionName = "<init>",
connectorName = "PrintConnector",
args = {
@Argument(name = "c",
type = TypeKind.CONNECTOR)
})
public class Init extends AbstractNativeAction {

@Override
public ConnectorFuture execute(Context context) {
System.out.println("** Action init called **");
ClientConnectorFuture future = new ClientConnectorFuture();
future.notifySuccess();
return future;

}
}

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade