<dependency>
<groupId>org.apache.juneau</groupId>
<artifactId>juneau-config</artifactId>
<version>9.0-B1</version>
</dependency>
juneau-config-9.0-B1.jar
org.apache.juneau.config_9.0-B1.jar
The juneau-config
module defines an API allows you to interact with INI-style configuration files using POJOs.
It builds upon the marshalling and SVL APIs to provide sophisticated dynamic configuration files.
#--------------------------
# My section
#--------------------------
[MySection]
# An integer
anInt = 1
# A boolean
aBoolean = true
# An int array
anIntArray = [1,2,3]
# A POJO that can be converted from a String
aURL = http://foo
# A POJO that can be converted from JSON
aBean = {foo:'bar',baz:123}
# A system property
locale = $S{java.locale, en_US}
# An environment variable
path = $E{PATH, unknown}
# A manifest file entry
mainClass = $MF{Main-Class}
# Another value in this config file
sameAsAnInt = $C{MySection/anInt}
# A command-line argument in the form "myarg=foo"
myArg = $ARG{myarg}
# The first command-line argument
firstArg = $ARG{0}
# Look for system property, or env var if that doesn't exist, or command-line arg if that doesn't exist.
nested = $S{mySystemProperty,$E{MY_ENV_VAR,$ARG{0}}}
# A POJO with embedded variables
aBean2 = {foo:'$ARG{0}',baz:$C{MySection/anInt}}
The beauty of these files is that they're easy to read and modify, yet sophisticated enough to allow you to
store arbitrary-complex data structures and retrieve them as simple values or complex POJOs:
// Load our config file
Config config = Config.create().name("MyIniFile.cfg").build();
int anInt = config.getInt("MySection/anInt");
boolean aBoolean = config.getBoolean("MySection/aBoolean");
int[] anIntArray = config.getObject("MySection/anIntArray", int[].class);
URL aURL = config.getObject("MySection/aURL", URL.class);
MyBean aBean = config.getObject("MySection/aBean", MyBean.class);
Locale locale = config.getObject("MySection/locale", Locale.class);
String path = config.getString("MySection/path");
String mainClass = config.getString("MySection/mainClass");
int sameAsAnInt = config.getInt("MySection/sameAsAnInt");
String myArg = config.getString("MySection/myArg");
String firstArg = config.getString("MySection/firstArg");
One of the more powerful aspects of the REST servlets is that you can pull values directly from
config files by using the "$C" variable in annotations.
For example, the HTML stylesheet for your REST servlet can be defined in a config file like so:
@Rest(
path="/myResource",
config="$S{my.config.file}" // Path to config file (here pulled from a system property)
)
@HtmlDocConfig(
stylesheet="$C{MyResourceSettings/myStylesheet}" // Stylesheet location pulled from config file.
)
public class MyResource extends RestServlet {
Other features:
-
Support for storing and retrieving any of the following data types:
- Primitives
- POJOs
- Beans
- Arrays, Maps, and Collections of anything
- Binary data
-
A listener API that allows you to, for example, reinitialize your REST resource if the config file
changes, or listen for changes to particular sections or values.
-
Filesystem watcher integration allows configs to reflect changes on the file system in real-time.
-
Config files can be modified through the Config class (e.g. add/remove/modify sections and keys, add/remove comments and whitespace, etc...).
When using these APIs, you DO NOT lose formatting in your existing configuration file.
All existing whitespace and comments are preserved for you!
-
Support for encoding of values for added security.
-
Config sections can be used to directly populate beans.
-
Config sections can be accessed and manipulated through Java interface proxies.
-
An extensible storage API allows you to write your own config storage location for files such as databases or the cloud.