Skip to content

Commit 4ad7311

Browse files
committed
GraalVM native support guide sample
1 parent d0012c0 commit 4ad7311

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
@@ -264,3 +264,130 @@ Refer to the [GraalVM documentation](https://www.graalvm.org/latest/docs/) for m
264264
- [Accessing Resources in Native Image](https://www.graalvm.org/jdk21/reference-manual/native-image/dynamic-features/Resources/)
265265

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

0 commit comments

Comments
 (0)