Saturday, February 28, 2015

Java Application Configuration Using XML

The is a simple tutorial in configuring your Java application using XML files.

For other options in configuring your Java application, please see the link at the end of this tutorial.

Here it goes:

1. Create the xml file for your configuration data.

EmailConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<config-dTO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
 <!-- FOR EMAIL -->
 <smtp-host xsi:type="java:java.lang.String">email.mysite.com</smtp-host>
 <system-email-address xsi:type="java:java.lang.String">admin@mysite.com</system-email-address>
 <email-subject xsi:type="java:java.lang.String">Your Daily Update</email-subject>
 <email-body xsi:type="java:java.lang.String">Please see the attached report.</email-body> 
 <recipient-email-list xsi:type="java:java.lang.String">person1@gmail.com,person2@gmail.com</recipient-email-list>
</config-dTO>


2. Create the Java class file that will map to the xml file. Place file in the Business package of the app.

ConfigDTO.java
public class ConfigDTO implements Serializable {
 
 private static final long serialVersionUID = 1L;
 
 //for email sender service
 private String smtpHost = null;
 private String systemEmailAddress = null;
 private String emailSubject = null;
 private String emailBody = null;
 private String recipientEmailList = null ;

 
 public String getSmtpHost() {
  return smtpHost;
 }
 public void setSmtpHost(String smtpHost) {
  this.smtpHost = smtpHost;
 }
 public String getSystemEmailAddress() {
  return systemEmailAddress;
 }
 public void setSystemEmailAddress(String systemEmailAddress) {
  this.systemEmailAddress = systemEmailAddress;
 }
 public String getEmailSubject() {
  return emailSubject;
 }
 public void setEmailSubject(String emailSubject) {
  this.emailSubject = emailSubject;
 }
 public String getEmailBody() {
  return emailBody;
 }
 public void setEmailBody(String emailBody) {
  this.emailBody = emailBody;
 }
 public String getRecipientEmailList() {
  return recipientEmailList;
 }
 public void setRecipientEmailList(String recipientEmailList) {
  this.recipientEmailList = recipientEmailList;
 }
}


3. Create your utility class that will unmarshall the xml data into the corresponding Java class. Place this file in the Business package of your app.

ConfigUtil.java
public class ConfigUtil {

 /**
  * @throws ValidationException 
  * @throws MarshalException 
  * 
  */
 public ConfigUtil() throws MarshalException, ValidationException {
 }
 
 public ConfigDTO getConfigDTO() throws MarshalException, ValidationException {
          
            InputStream inputStream = getClass().getResourceAsStream("/EmailConfig.xml");
        
            InputSource xmlInput = new InputSource(inputStream);
          
     ConfigDTO config = (ConfigDTO) Unmarshaller.unmarshal(ConfigDTO.class, xmlInput);
  
     return config;
 }
}


4. Use the utility class ConfigUtil in your java and servlet classes to get the java class (with the xml data) ConfigDTO.

EmailReport.java
public class EmailReport  {
  
 public void getEmailProps {
 
  ConfigDTO configDTO = new ConfigUtil.getConfigDTO();
  
  System.out.println(configDTO.getSmtpHost());
  System.out.println(configDTO.getSystemEmailAddress());
  System.out.println(configDTO.getEmailSubject());
  System.out.println(configDTO.getEmailBody());
  System.out.println(configDTO.getRecipientEmailList());
 } 
}



Here are various configuration APIs to consider:

1. java.util.Properties

If your configuration properties are just simple key-value pairs, then you might want to try java.util.Properties. If you have lists or maps, you may want to consider the other APIs in this list.

2. Apache Commons Configuration

Apache Commons Configuration allows having the configuration stored in a wide range of formats including properties, XML, JNDI, and more. It is easy to use and to extend. To get the most flexibility out of it use a factory to get the configuration and just use the Configuration interface after that.

3. XStream

XStream is a simple library to serialize objects to XML and back again. It is easy to extend  so that you make the file look the way you want. You can set up aliases for your class and even custom data formats to make the XML file more readable.

4. Apache Commons Digester.

The Digester API lets you configure an XML -> Java object mapping module, which triggers certain actions called rules whenever a particular pattern of nested XML elements is recognized. A rich set of predefined rules is available for your use, or you can also create your own.

5. JFig

6. JConfig