Skip to content

Commit 86dc986

Browse files
committed
GraalVM native support guide sample
1 parent c28e703 commit 86dc986

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,130 @@ Refer to the [GraalVM documentation](https://www.graalvm.org/latest/docs/) for m
250250
- [Accessing Resources in Native Image](https://www.graalvm.org/jdk21/reference-manual/native-image/dynamic-features/Resources/)
251251

252252
**TIP:** You can find many configuration samples in the [graalvm-reachability-metadata](https://github.com/oracle/graalvm-reachability-metadata) repository.
253+
254+
Here is a sample usage guide for ApacheFreeMarker + GraalVM.
255+
256+
To run the sample in classic Just In Time Way, we only need :
257+
258+
* FreeMarkerGraalVMSample.java
259+
* sample.ftl
260+
261+
But for the Ahead Of Time application with GraalVM some additional configuration is required :
262+
263+
* custom-reflect-config.json
264+
265+
#### FreeMarkerGraalVMSample.java sample class
266+
267+
```java
268+
import freemarker.log.Logger;
269+
import freemarker.template.Configuration;
270+
import freemarker.template.Template;
271+
import freemarker.template.TemplateException;
272+
273+
import java.io.IOException;
274+
import java.io.StringWriter;
275+
import java.io.Writer;
276+
import java.util.HashMap;
277+
import java.util.Map;
278+
279+
public class FreeMarkerGraalVMSample {
280+
281+
private final static Logger LOG = Logger.getLogger(FreeMarkerGraalVMSample.class.getName());
282+
283+
/* data model */
284+
public class Data {
285+
private String description;
286+
public String getDescription() {
287+
return description;
288+
}
289+
public void setDescription(String description) {
290+
this.description = description;
291+
}
292+
}
293+
294+
private void handleTemplate(Writer writer, String templatePath, Map<String, Object> dataModel) throws IOException, TemplateException {
295+
Configuration cfg = new Configuration( Configuration.VERSION_2_3_34 );
296+
cfg.setClassForTemplateLoading( FreeMarkerGraalVMSample.class, "/templates" );
297+
Template template = cfg.getTemplate( templatePath );
298+
template.process( dataModel, writer );
299+
}
300+
301+
public void runSample() {
302+
try ( StringWriter writer = new StringWriter() ) {
303+
Map<String, Object> dataModel = new HashMap<>();
304+
Data data = new Data();
305+
data.setDescription( "FreeMarkerGraalVMSample" );
306+
dataModel.put("data", data);
307+
handleTemplate( writer, "sample.ftl", dataModel );
308+
LOG.info( writer.toString() );
309+
} catch (Exception e) {
310+
LOG.error( e.getMessage(), e );
311+
}
312+
}
313+
314+
public static void main(String[] args) {
315+
FreeMarkerGraalVMSample sample = new FreeMarkerGraalVMSample();
316+
sample.runSample();
317+
}
318+
319+
}
320+
```
321+
322+
#### Apache FreeMarker template
323+
324+
```ftl
325+
<freemarker-graalvm-sample>
326+
<freemarker-version>${.version}</freemarker-version>
327+
<description>${data.description}</description>
328+
</freemarker-graalvm-sample>
329+
```
330+
331+
#### Reflection configuration, custom-reflect-config.json
332+
333+
Refers to [Reflection in Native Image](https://www.graalvm.org/jdk21/reference-manual/native-image/dynamic-features/Reflection/) guide
334+
335+
```json
336+
[{
337+
"name" : "FreeMarkerGraalVMSample$Data",
338+
"methods" : [ {
339+
"name" : "<init>",
340+
"parameterTypes" : [ ]
341+
},{
342+
"name" : "getDescription",
343+
"parameterTypes" : [ ]
344+
} ]
345+
}]
346+
```
347+
348+
#### Build the native image
349+
350+
```shell
351+
#!/bin/bash
352+
353+
# setting up environment
354+
export BASEDIR=.
355+
export CP=./lib/freemarker-gae-2.3.35-SNAPSHOT.jar:.
356+
357+
# just in time application build
358+
javac -cp ${CP} -d build ./src/FreeMarkerGraalVMSample.java
359+
360+
# ahead of time application build
361+
#
362+
# -H:IncludeResources=^templates/.*
363+
# will make the templates available to the native-image
364+
#
365+
# -H:ReflectionConfigurationFiles=./config/custom-reflect-config.json
366+
# will setup reflection custom configuration
367+
native-image \
368+
-cp "${CP}:build" \
369+
-H:Path=build \
370+
-H:Class=FreeMarkerGraalVMSample \
371+
-H:IncludeResources=^templates/.* \
372+
-H:+UnlockExperimentalVMOptions \
373+
-H:ReflectionConfigurationFiles=./config/custom-reflect-config.json \
374+
--no-fallback \
375+
--report-unsupported-elements-at-runtime
376+
377+
# running the application
378+
./build/freemarkergraalvmsample
379+
```

0 commit comments

Comments
 (0)