Enhancements to MP-Config for handling files and strings
persistence.xml using variables from MP-Config
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="MyPU">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb"/>
<property name="hibernate.ogm.datastore.host" value="${app.db.host}"/>
<property name="hibernate.ogm.datastore.database" value="${app.db.name}"/>
<property name="hibernate.ogm.datastore.create_database" value="${app.devmode}"/>
<property name="javax.persistence.schema-generation.database.action" value="${jpa.initaction}" />
<property name="hibernate.show_sql" value="${app.debugging}"/>
</properties>
</persistence-unit>
</persistence>
Loading Strings and files with values pre-filled from Microprofile Config is very handy
Variables in Strings and files are in the format: ${var.name}
and can be skipped by adding a slash: \${var.name}
- Load a String
import eisiges.mp_conf_files.StringInterpolator;
...
System.setProperty("user.name", "Steve"); //set system properties read by MP-Config
System.setProperty("user.age", "101");
String originalString = "Hello, ${user.name}. You are ${user.age} years old."
System.out.println(StringInterpolator.interpolate(originalString));
// prints "Hello, Steve. You are 101 years old."
- Load a String with escaped variable names
import eisiges.mp_conf_files.StringInterpolator;
...
System.setProperty("app.timezone", "-0700"); //set system properties read by MP-Config
String originalString = "Timezone set to ${app.timezone} with \\${app.timezone}"
System.out.println(StringInterpolator.interpolate(originalString));
// prints "Timezone set to -0700 with ${app.timezone}"
- Load a resource via URL
import eisiges.mp_conf_files.FileInterpolator;
...
URL originalURL = ...
URL fixedUpURL = FileInterpolator.load(originalURL); // now can be used to read from URL with values filled in from mp-config
- Load a File without modifying the original
import eisiges.mp_conf_files.FileInterpolator;
...
File originalFile = ...
File fixedUpFile = FileInterpolator.load(originalFile); // now can be used to read from File with values filled in from mp-config
-
Add a Microprofile Config implementation to your project. There is a list of them here
-
Add mp-conf-files to your project
Maven:
<dependency>
<groupId>es.eisig</groupId>
<artifactId>mp-conf-files</artifactId>
<version>1.0.0</version>
</dependency>
Gradle:
repositories {
mavenCentral()
}
dependencies {
implementation 'es.eisig:mp-conf-files:1.0.0'
}