SourceForge.net Logo PHP Logo

Dynamic Configuration Toolkit

Code / Configuration Samples

This page lists some examples to web server set up , DConfig-lib library set up, DConfig data access, etc.
1. Prerequisites
2. Load or Reload DConfig Data into Memory
3. DConfigReader Samples
4. DConfigWriter Samples
5. Convert a properties file to DConfig format

1. Prerequisites      

Download dconfig-1.x-src.zip or dconfig-1.x-bin.zip, unzip them to a local folder.
If you download source code, first create a distribution by running the following commands on a terminal:

  cd dconfig-1.x-src

2. Load or Reload DConfig Data into Memory      

Copy dconfig-1.x-src/dist/dconfig-lib/*.jar or dconfig-1.x-bin/dconfig-lib/*.jar to WEB-INF/lib.
If DConfig data is changed, you can reload the DConfig data into memory by invocking the following method:

Source 2. Reload DConfig data into memory

#import org.moonwave.dconfig.dao.CacheManager; // locates in dconfig-lib.jar

3. DConfigReader Samples      

Table: DConfigReader.getXXXX method's Five Formats
Method Summary Example
XXXX getXXXX(java.lang.String keyName)Gets XXXX (boolean) attribute value of the primary attribute for a specified key name.static boolean getBoolean(java.lang.String keyName)
XXXX getXXXX(java.lang.String keyName, java.lang.String attributeName)Gets XXXX (Integer) attribute value for a specified key name, attribute name.static int getInteger(java.lang.String keyName, java.lang.String attributeName)
XXXX getXXXX(java.lang.String keyName, java.lang.String attributeName, XXXX defaultValue)Gets XXXX (java.util.Date) attribute value for a specified key name, attribute name, and default value.static java.util.Date getDate(java.lang.String keyName, java.lang.String attributeName, java.util.Date defaultValue)
XXXX[] getXXXXArray(java.lang.String keyName, java.lang.String attributeName)Gets XXXX (double) array attribute value for a specified key name, attribute name.static double[] getDoubleArray(java.lang.String keyName, java.lang.String attributeName)
XXXX[] getXXXXArray(java.lang.String keyName, java.lang.String attributeName, XXXX[] defaultValue)Gets XXXX (String) array attribute value for a specified key name, attribute name, and default value.static java.lang.String[] getStringArray(java.lang.String keyName, java.lang.String attributeName, java.lang.String[] defaultValue)

Source 3. DConfigReader Samples
#import org.moonwave.dconfig.dao.DConfigReader; // locates in dconfig-lib.jar

// get tab names array for key "config.view.demo.page1", attribute "tab names"
String[] tabeNames = DConfigReader.getStringArray("config.view.demo.page1", "tab names");

// get attribute value for key "config.view.demo.page1", attribute "#Rows Per Page"
int rowsPerPage = DConfigReader.getInteger("config.view.demo.page1", "#Rows Per Page", 5);

// get attribute value array for key "config.view.demo.page1", attribute "visible columns"
int[] visibleColumns = DConfigReader.getIntegerArray("config.view.demo.page1", "visible columns");

4. DConfigWriter Samples      

Table: DConfigWriter.writeXXXX method's Three Formats
Method Summary Example
public static boolean writeXXXX(String keyName, XXXX val)Writes XXXX (boolean) attribute value of the primary attribute for a specified key name.public static boolean writeBoolean(String keyName, boolean val)
public static boolean writeXXXX(java.lang.String keyName, java.lang.String attributeName, XXXX value)Writes XXXX (java.util.Date) attribute value for a specified key name, attribute name, and XXXX value.public static boolean writeDate(java.lang.String keyName, java.lang.String attributeName, java.util.Date date)
public static boolean writeXXXXArray(java.lang.String keyName, java.lang.String attributeName, XXXX[] value)Writes XXXX (String) array attribute value for a specified key name, attribute name, and XXXX array.public static boolean writeStringArray(java.lang.String keyName, java.lang.String attributeName, java.lang.String[] value)

Source 4. DConfigWriter Samples
#import org.moonwave.dconfig.dao.DConfigWriter; // locates in dconfig-lib.jar

// save '#Rows Per Page' - get data from web page
int rowsPerPage = Integer.parseInt(this.txtRowsPerPage.getText().toString());
DConfigWriter.writeInteger("config.view.demo.page1", "#Rows Per Page", rowsPerPage);

or set data explicitly

int rowsPerPage = 8;
DConfigWriter.writeInteger("config.view.demo.page1", "#Rows Per Page", rowsPerPage);

// save 'tab names' - get data from web page
String[] sArray = new String[5];
sArray[0] = this.txtTabname1.getText().toString();
sArray[1] = this.txtTabname2.getText().toString();
sArray[2] = this.txtTabname3.getText().toString();
sArray[3] = this.txtTabname4.getText().toString();
sArray[4] = this.txtTabname5.getText().toString();
DConfigWriter.writeStringArray("config.view.demo.page1", "tab names", sArray);

or set data explicitly

String[] sArray = new String[5];
sArray[0] = "Database";
sArray[1] = "DConfig UI - JNLP";
sArray[2] = "Refresh";
sArray[3] = "Search";
sArray[4] = "Preferences";
DConfigWriter.writeStringArray("config.view.demo.page1", "tab names", sArray);

// save 'visible columns' - get data from web page
Integer[] iArray = new Integer[6];
int count = 0;
if (this.ckCol1.isChecked())
  iArray[count++] = 1;
if (this.ckCol2.isChecked())
  iArray[count++] = 2;
if (this.ckCol3.isChecked())
  iArray[count++] = 3;
if (this.ckCol4.isChecked())
  iArray[count++] = 4;
if (this.ckCol5.isChecked())
  iArray[count++] = 5;
if (this.ckCol6.isChecked())
  iArray[count++] = 6;
DConfigWriter.writeIntegerArray("config.view.demo.page1", "visible columns", iArray);

or set data explicitly

Integer[] iArray = new Integer[6];
iArray[0] = 1;
iArray[1] = 2;
iArray[2] = 3;
iArray[3] = 4;
iArray[4] = 5;
iArray[5] = 6;
DConfigWriter.writeIntegerArray("config.view.demo.page1", "visible columns", iArray);

5. Convert a properties file to DConfig format      

Here is a sample properties file content:

Source 5. Sample data in properies file


email.notification.subject=Regarding ...
email.notification.ps=-------------------<br/>This is a system-generated email. Please do not reply.



Note that in a properties file, all data value is string type. Developer has to use data type convertion methods to convert the data to a desired data type.
To convert a property to corresponding DConfig format, using the following simple steps:
1. Create a key name
2. Create an attribute name
3. Set the data type
4. Set the data value
5. Add comments as needed

The following is a converted DConfig format sample:

Table: Converted DConfig Format Sample Table
Key Name Attribute Name Data Type Attribute Value Comments
config.email.support application support address String app.feedback@mycompany.com
config.email.support tech support address String tech.support@mycompany.com
config.email.notification from String app.notification@mycompany.com
config.email.notification subject String subject=Regarding ...
config.email.notification body String Hi,<br/>....<br/>
config.email.notification ps String -------------------<br/>This is a system-generated email.
Please do not reply.
config.format.decimal totalValueFractionDigits Integer 0 Show 0 decimal digits
config.format.decimal.page1 totalValueFractionDigits Integer 1 Show 1 decimal digit
config.format.decimal.page2 totalValueFractionDigits Integer 2 Show 2 decimal digit2
application.logic.newPlan start date Datetime 2007-05-01 00:00:00
application.logic.newPlan end date Datetime 2008-04-30 00:00:00

The above table can be easily entered into database through DConfig GUI Editor. Sample DConfig data read is listed below:

Source 6. Read DConfig Data
#import org.moonwave.dconfig.dao.DConfigReader; // locates in dconfig-lib.jar

// get email info
String from = DConfigReader.getString("config.email.notification", "from");
String subject = DConfigReader.getString("config.email.notification", "subject");

// get deciaml format setting
int totalValueFractionDigits = DConfigReader.getInteger("config.format.decimal", "totalValueFractionDigits");

// get new plan start / end dates
java.util.Date startDate = DConfigReader.getDate("application.logic.newPlan", "start date");
java.util.Date endDate = DConfigReader.getDate("application.logic.newPlan", "end date");