Sources for this article are in GitHub: https://github.com/AMIS-Services/jfall2019-graalvm/tree/master/polyglot/java2js
I am developing a Java application. I need to perform validations on input data: Postal Code (various countries), Mobile Phone Numbers (many countries), Email Address, Credit Card Number etc.
In simplified pseudo code:
I need to implement (or get my hands on) the postalCode Validator – for starters.
The NPM Module Validator offers most of these OOTB (Out of the Box)
How could that possibly help me?
GraalVM to the rescue.
Spoiler alert: here comes the end result. This is the final code, after integrating NPM Module Validator into my Java application:
Running this code gives me:
The steps to turn an NPM module into a self contained bundle dat GraalVM can process are these:
- check GraalVM compatibility of NPM module
- install npx (executable runner – complement to npm which is not included with GraalVM platform)
- install webpack and webpack-cli
- install validator module with npm
- produce self contained bundle for validator module with webpack
1. Check GraalVM compatibility of NPM module with the GraalVM compatibility check:
2. install npx – executable runner – complement to npm which is not included with GraalVM platform
3. install webpack and webpack-cli
4. install validator module with npm
5. produce self contained bundle for validator module with webpack
#install npx npm install -g npx #install webpack npm install webpack webpack-cli #install validator module npm install validator #create single bundle for valudator module /usr/lib/jvm/graalvm-ce-19.2.1/jre/languages/js/bin/npx webpack-cli --entry=./node_modules/validator/index.js --output=./validator_bundled.js --output-library-target=this --mode=development #Argument: output-library-target, Choices are : "var", "assign", "this", "window", "self", "global", "commonjs", "commonjs2", "commonjs-module", "amd", "umd", "umd2", "jsonp"
Call Validator Module from Java application
With the Validator module turned into a single self-contained file without non-supported module constructs, we can load this resource into a GraalVM Polyglot context in our Java application running on the GraalVM runtime engine, and invoke any top level function in that context. In order to validate postal codes in Java – here is a very simple code snippet that does just that. Note: the validator_bundled.js is located in the root of our classpath.
The resulting output:
GitHub Repository with sources for this article: https://github.com/AMIS-Services/jfall2019-graalvm/tree/master/polyglot/java2js
NPM Module Validator
GitHub for GraalJS – https://github.com/graalvm/graaljs
Presentation at HolyJS 2019 (St Petersburg, Russia): Node.js: Just as fast, higher, stronger with GraalVM
Why the Java community should embrace GraalVM – https://hackernoon.com/why-the-java-community-should-embrace-graalvm-abd3ea9121b5
#WHATIS?: GraalVM – RieckPIL – https://rieckpil.de/whatis-graalvm/
GraalVM: the holy graal of polyglot JVM? – https://www.transposit.com/blog/2019.01.02-graalvm-holy/
JavaDocs for GraalVM Polyglot – https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/package-summary.html
GraalVM Docs – Polyglot – https://www.graalvm.org/docs/reference-manual/polyglot/
Mixing NodeJS and OpenJDK – Language interop and vertical architecture -Mike Hearn – https://blog.plan99.net/vertical-architecture-734495f129c4
Enhance your Java Spring application with R data science Oleg Šelajev – https://medium.com/graalvm/enhance-your-java-spring-application-with-r-data-science-b669a8c28bea
GraalVM Archives on Medium – https://medium.com/graalvm/archive
GraalVM GitHub Repo – https://github.com/oracle/graal
GraalVM Project WebSite – https://www.graalvm.org/