Maven Dependency

<dependency> <groupId>org.apache.juneau</groupId> <artifactId>juneau-config</artifactId> <version>9.0-B1</version> </dependency>

Java Library


OSGi Module


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: