Using Configuration Manager - 2 Minute tutorial

This section describes how to use configuration manager. For examples please see sample applications provided with the configuration manager and configuration manager tests provided with the source distribution of configuration manager.

Creating config.xml file

Create a configuration config.xml file and put it in a class path of your application (along with SampleAppProperties.properties and log4j.xml). Simple config.xml file below has log4j.xml file and property file loaded. The advantage of having log4j.xml in configuration manager is that you can re-define priorities and reload log4j.xml configuration while your server/ program is running.

config.xml

			<?xml version="1.0" encoding="UTF-8" ?>
				<configurationManager key="Sample App1 Configuration" jmx="true">	
				<properties key="sampleAppProps" fileName="SampleAppProperties.properties" />
				<log4j key="app2Log4J" fileName="log4j.xml" />				
			</configurationManager>
			

SampleAppProperties.properties

			serverName=MyAppserver
			numThread=5
			

Using Configuration Manager API

To access the sample applications properties in your code use the following API.

				import org.naftulin.configmgr.ConfigurationUtility;
				....
				
				String serverName = ConfigurationUtility.getString("sampleAppProps", "serverName");
				int numThreads = ConfigurationUtility.getInteger("sampleAppProps", "numThread");
			
The idea is that you read the values from configuration manager every time you need it. Since configuration manager caches the value until reload is called, getting the value from configuration manager is inexpensive operation

Unit testing code that has calls to configuration manager

You don't have to load configurations in order to unit-test the code that calls configuration manager. To setup configurations without initializing configuration management engine in your test cases use the following APIs:

				import org.naftulin.configmgr.ConfigurationUtilityHelper;
				
				public void testCodeThatUsesConfigurationManager() {
					ConfigurationUtilityHelper.setUnitTest(true);
					ConfigurationUtilityHelper.setConfigPropertyObject("sampleAppProps", "serverName", "HelloWorldServer");
					ConfigurationUtilityHelper.setConfigPropertyObject("sampleAppProps", "numThread", "3");
					... do testing of your method here....
				

Reloding configuration

You can use jconsole (or otehr JMX client) application to reload configuration, as long as you set jmx="true" in your config.xml (example above has it), or you can programmatically reload configuration, by calling ConfigurationType.reload() inside your code - just like web client or sample applications do it.

For more advance settings look at configuration page