Low-Code Polyglot Resource
In this tutorial, we will create a native resource for Netuno. This means we will create a Java class that will reside in Netuno's base directory and can be used by any application inside the apps folder using the syntax _foo.methodName(args).
The example we will create, following the previous tutorials, is the _calc resource, which is an example resource for performing simple arithmetic operations.
To start, you first need to create the project's folder structure. In this example, we will consider that the project will be created inside a folder called Calc. So you need to create the folder structure that mirrors the package name (in our example, org.netuno.addon.calc). Our folder structure will therefore be:
Calc/src/main/java/org/netuno/addon/calc/resource/
Inside this structure, we will create a Java class called CalcResource, which will define our custom native resource.
Calc/src/main/java/org/netuno/addon/calc/resource/CalcResource.java
Then the package defined inside the class will be:
package org.netuno.addon.calc.resource;
For the class to define a custom native resource, it must be annotated with the org.netuno.tritao.ResourceBase annotation. The name parameter will be the name of the resource, which can be accessed with an underscore (_) before the name (in our case, _calc). The easiest way to create the resource is by extending the org.netuno.tritao.resource.ResourceBase class:
package org.netuno.addon.calc.resource;
import org.netuno.tritao.resource.Resource;
import org.netuno.tritao.resource.ResourceBase;
@Resource(name = "calc")
public class CalcResource extends ResourceBase {
// ...
}
Extending the ResourceBase class saves us from having to initialize the resource, and we just need to call super with the correct arguments:
package org.netuno.addon.calc.resource;
import org.netuno.proteu.Proteu;
import org.netuno.tritao.hili.Hili;
import org.netuno.tritao.resource.Resource;
import org.netuno.tritao.resource.ResourceBase;
@Resource(name = "calc")
public class CalcResource extends ResourceBase {
// ...
public CalcResource(Proteu proteu, Hili hili) {
super(proteu, hili);
}
}
The init function is mandatory, and inside it we will initialize the resource by calling our class's constructor with the parent class's getters:
package org.netuno.addon.calc.resource;
import org.netuno.proteu.Proteu;
import org.netuno.tritao.hili.Hili;
import org.netuno.tritao.resource.Resource;
import org.netuno.tritao.resource.ResourceBase;
@Resource(name = "calc")
public class CalcResource extends ResourceBase {
// ...
public CalcResource(Proteu proteu, Hili hili) {
super(proteu, hili);
}
public CalcResource init() {
return new CalcResource(getProteu(), getHili());
}
}
After that, we will define our own fields and functions:
package org.netuno.addon.calc.resource;
import org.netuno.proteu.Proteu;
import org.netuno.tritao.hili.Hili;
import org.netuno.tritao.resource.Resource;
import org.netuno.tritao.resource.ResourceBase;
@Resource(name = "calc")
public class CalcResource extends ResourceBase {
public double value = 0;
public CalcResource(Proteu proteu, Hili hili) {
super(proteu, hili);
}
public CalcResource init() {
return new CalcResource(getProteu(), getHili());
}
public CalcResource set(double v) {
value = v;
return this;
}
public CalcResource plus(double v) {
value += v;
return this;
}
public CalcResource minus(double v) {
value -= v;
return this;
}
public double get() {
return value;
}
}
Now you just need to compile the class, making sure that the CalcResource.class file is in the folder:
netuno/core/web/WEB-INF/classes/org/netuno/addon/calc/resource/
Or produce a JAR file, making sure that it is in the folder:
netuno/lib/
Let's create a test application to use this resource:
./netuno app name=calc
Let's also create a service in this application to use the resource.
In the example, we will create the netuno/apps/calc/server/services/calc-test.js file:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
_calc.set(10);
_calc.plus(120);
_out.println(_calc.get());
_calc.set(10);
_calc.plus(120);
_out.println(_calc.get());
_calc.set(10)
_calc.plus(120)
_out.println(_calc.get())
_calc.set(10)
_calc.plus(120)
_out.println(_calc.get())
_calc.set(10)
_calc.plus(120)
_out.println(_calc.get())
_calc.set(10)
_calc.plus(120)
_out.println(_calc.get())
Let's start our application:
./netuno server app=calc
And now, when we make a call to this service:
http://localhost:9000/services/calc-test
It will return the value 130.
To use a resource in Java code, some classes such as org.netuno.tritao.Web
and org.netuno.tritao.event.EventBase provide the resource function, which initializes this resource.
In the class where you want to use the calc resource, do:
import org.netuno.addon.calc.resource.CalcResource;
// ...
CalcResource calc = resource(CalcResource.class);
calc.set(10);
calc.plus(120);
System.out.println(calc.get());
// ...
You can see the complete project files at this link.