Making api's work in java

Hello,

I’m trying to store some data in the global secure storage using java. I’ve been following the instructions on https://docs.legato.io/16_07/javaBuild.html#javaBuild_apis and have in my adef file:

bindings:

{

helloJava.BluWaterInitComponent.secStoreGlobal -> secStore.secStoreGlobal

}


And cdef file:

api: {
secStoreGlobal = le_secStore.api
}

This causes the interface and the implementing client class to be generated. I then, in the main class that inherits component, puts

public void setsecStoreGlobal(secStoreGlobal sec) { this.sec = sec; }

Finally som test code in componentInit:

System.out.println(“Starting”);
BigInteger[] data = new BigInteger[2];
data[0] = BigInteger.valueOf(67L);
data[1] = BigInteger.valueOf(48L);
System.out.println(sec.toString()); //Crash here, sec is null
sec.Write(“Message”, data);

This causes a null pointer exception since sec is null. I cannot instantiate the secStoreGlobal with the generated secStoreGlobalClient since the class can’t find it and in the generated secStoreGlobalClient the package section there is actually an error that the package does not match the expected package. Which is strange since it’s an autogenerated class. If I try to import the secStoreGlobalClient class into the main class it can’t be found.

In the autogenerated secStoreGlobalClient class it looks like this:

package io.legato.api.implementation;

/**Gets error: The declared package “io.legato.api.implementation” does not match the expected package “”*/

Have I missed something when I’m setting up the java API generation? What is causing the autogenerated class to get an error like this?

Hi @marerl

Please can you tell which Legato version you’re using?
I can’t remember exactly, but at some time in the 17.x versions, the way to access to service instances has slightly changed to avoid all this kind of boilerplate code to register services (and I’m afraid this wasn’t properly documented)

In these recent versions, your main component class must inherit from https://github.com/legatoproject/legato-af/blob/master/framework/java/io/legato/Component.java
Then, for any binding you declare in the xdef files, the service instance is automatically “registered” in the component, and all you need to do is to use the getService method with the generated interface class to get access to them.

Hope it helps!

Hello daav,

Thank you that did indeed solve my problem. My version is 18.03 so i guess the that’s why it didn’t work when following the documentation. For anyone coming here looking for the same thing do the following:

setsecStoreGlobal(getService(secStoreGlobal.class));