Skip to content

Instantly share code, notes, and snippets.

@skihero
Created February 25, 2011 13:19
Show Gist options
  • Save skihero/843767 to your computer and use it in GitHub Desktop.
Save skihero/843767 to your computer and use it in GitHub Desktop.
Read the XML configuration file to obtain extra params
/* Reads the configuration from the settings XML File
* This is for providing the params needed in the
* server side
* Created Tue Feb 15 18:59:31 IST 2011: Kish
* Updated to include Sipx changes Feb 25 2011: Kish
*
*
Needed
commons-collections-3.2.jar
commons-configuration-1.6.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
*/
import org.apache.commons.configuration.*;
import org.apache.commons.lang.exception.*;
import org.apache.commons.configuration.ConfigurationException;
import java.io.FileInputStream;
import java.util.*;
public class ConfReader{
/* Private members */
/* To store the sipx params */
private Map <String, String > SipxConfMap;
/* Set the default conf filename */
String filename="global.xml";
/* Allow overriding the set conf filename */
public void setConfigFile(String filename){
this.filename = filename ;
}
/* Read the services defined in the conf file */
public List readServices() throws Exception{
/* The name of the root element is not needed while addressing the
elements */
List services = this.readConfigFile().getList("services.name");
/* if no services are available the conf file may be broken
throw exception here */
if (services.size() < 1 ) {
throw new Exception("Services.name not defined in your global.xml file");
}
else{
/* return the list of services that were read */
return services ;
}
}
/* Read the other User object parameters that will be needed to create the
User but are not provided from the front end forms
*/
public List readUser() throws Exception{
List user = this.readConfigFile().getList("user");
return user;
}
/* Read the Sipx related extra configuration params.
*/
public Map readSipxParams() throws Exception {
SipxConfMap = new HashMap<String, String>() ;
/* This is dumb */
SipxConfMap.put("host", (String) this.readConfigFile().getList("sipx.host").get(0) );
SipxConfMap.put("password", (String) this.readConfigFile().getList("sipx.host").get(0));
return SipxConfMap; /* TODO: add error checking */
}
/* Obtain a handle on the conf file */
private XMLConfiguration readConfigFile() throws ConfigurationException{
XMLConfiguration config = new XMLConfiguration(this.filename);
return config;
}
/* This clsss should be used like this */
public static void main(String a[]) {
try
{
Test_XML tex = new Test_XML();
tex.setConfigFile("global.xml");
List services = tex.readServices();
List user = tex.readUser();
System.out.println(services.size());
System.out.println(user.size());
for (int len = services.size(), i = 0 ; i < len ; i ++ ) {
System.out.println("Service: " + services.get(i) ) ;
}
for ( int len = user.size(), i = 0 ; i < len ; i ++ ) {
System.out.println("User: " + user.get(i) ) ;
}
Map <String,String> SipxConfParams = tex.readSipxParams();
String host = SipxConfParams.get("host");
String password = SipxConfParams.get("password");
System.out.println("Sipx Host: "
+host
+" Sipx Password: "
+password ) ;
}
catch(Exception cex)
{
cex.printStackTrace();
}
}
/* */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment