How do I load properties or configuration files via classpath in EAP for OpenShift?

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 8.x
    • 7.4
    • 7.3
  • OpenShift

Issue

Resolution

Create a custom module where you put your properties files and then list that module as a global module in standalone-openshift.xml.

From JBoss EAP 7.3 for OpenShift or later, you can add any script to run when starting a pod, including CLI scripts (9.13.5. Custom Scripts). This function executes the script in ${JBOSS_HOME}/extensions/postconfigure.sh before starting JBoss EAP. The following steps take advantage of this mechanism, custom script postconfigure.sh is used to register a module which contains myapp.properties.

1. Creating a script to mount as a ConfigMap

Create the following three files:

$ tree .
.
├── extensions
│   ├── extensions.cli
│   └── postconfigure.sh
└── myapp.properties

extensions.cli is called by the custom script postconfigure.sh, which creates a custom module containing properties just before starting the JBoss EAP. Starting with JBoss EAP 7.1, you can place the resources of the module outside of JBOSS_HOME/modules by --absolute-resources option (6.1. Modules)

Example extensions/extensions.cli

embed-server --std-out=echo --server-config=standalone-openshift.xml
batch
module add --name=conf --absolute-resources=/myapp/conf/
/subsystem=ee:write-attribute(name=global-modules,value=[{name=conf, slot=main}])
run-batch
quit

Example extensions/postconfigure.sh

#!/bin/sh
echo "Executing postconfigure.sh"
$JBOSS_HOME/bin/jboss-cli.sh --file=$JBOSS_HOME/extensions/extensions.cli

myapp.properties is an example of an application property file:

message=Hello, world!

2. Mounting configmaps to execute custom scripts

Mount your custom script and application properties on the EAP container as a ConfigMap as follows:

$ oc create configmap jboss-cli --from-file=postconfigure.sh=extensions/postconfigure.sh --from-file=extensions.cli=extensions/extensions.cli
$ oc create configmap myapp-config --from-file=myapp.properties

$ oc set volume dc/<eap-app> --add --name=myapp-config -m /myapp/conf/ -t configmap --configmap-name=myapp-config
$ oc set volume dc/<eap-app> --add --name=jboss-cli -m /opt/eap/extensions -t configmap --configmap-name=jboss-cli --default-mode='0755'

postconfigure.sh should be mounted on /opt/eap/extensions/postconfigure.sh, otherwise, it won't be loaded during startup JBoss EAP container. extensions.cli and myapp.properties can be mounted on any arbitrary directory.

3. Loading property file via getResourceAsStream()

Load the property file via classloader with the following example code:

Properties myappProps  = new Properties();
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("myapp.properties")) {
    myappProps.load(in);
    System.out.println(myappProps.getProperty("message")); // ==>> Hello, world!
} catch (IOException e) {
    // TODO error handling
}
Components

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.