Skip to content

Instantly share code, notes, and snippets.

@skihero
Created March 2, 2011 09:30
Show Gist options
  • Save skihero/850687 to your computer and use it in GitHub Desktop.
Save skihero/850687 to your computer and use it in GitHub Desktop.
/* 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
*
*/
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;
/* To store the zimbra params. */
private Map <String,String>zimConfMap;
/* To store the zimbra misc params */
private Map <String,String> zimConfMiscMap ;
/* 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>() ;
System.out.println("in sipx");
/* This is dumb */
SipxConfMap.put("host", (String) this.readConfigFile().getList("sipx.host").get(0));
SipxConfMap.put("password", (String) this.readConfigFile().getList("sipx.password").get(0));
return SipxConfMap; /* TODO: add error checking */
}
/* Read the Zimbra related extra configuration params.
*/
public Map <String,String>readZimbraParams() throws Exception {
zimConfMap = new HashMap<String, String>() ;
System.out.println("in zimbra");
/* This is dumb */
zimConfMap.put("host", (String) this.readConfigFile().getList("zimbra.host").get(0) );
zimConfMap.put("domain", (String) this.readConfigFile().getList("zimbra.domain").get(0));
zimConfMap.put("admin_username", (String) this.readConfigFile().getList("zimbra.admin_username").get(0));
zimConfMap.put("admin_password", (String) this.readConfigFile().getList("zimbra.admin_password").get(0));
zimConfMap.put("json", (String) this.readConfigFile().getList("zimbra.json").get(0));
zimConfMap.put("trial_run", (String) this.readConfigFile().getList("zimbra.trial_run").get(0));
zimConfMap.put("verbosity", (String) this.readConfigFile().getList("zimbra.verbosity").get(0));
return zimConfMap; /* TODO: add error checking */
}
/* Read the Zimbra related extra configuration params(misc).
* These will be the ones added at the last like the
* displayname.
*/
public Map <String, String> readMiscZimbraParams() throws Exception {
zimConfMiscMap = new HashMap<String, String>() ;
System.out.println("in zimbra misc");
List attribute = this.readConfigFile().getList("zimbraMisc.name") ;
List value = this.readConfigFile().getList("zimbraMisc.description") ;
ListIterator attr_li = attribute.listIterator(0);
ListIterator value_li = value.listIterator(0);
for( ; attr_li.hasNext(); ) {
zimConfMiscMap.put( (String) attr_li.next(), (String) value_li.next() ) ;
}
System.out.println("Zim misc map : " + zimConfMiscMap );
return zimConfMiscMap ;
}
/* 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();
ConfReader tex = new ConfReader();
System.out.println("just before reading the file ");
tex.setConfigFile("/root/workspace/Adapter/com/domain/oib/adminportal/server/global.xml");
System.out.println("the path not found");
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) ) ;
}
Map SipxConfParams = tex.readSipxParams();
String host = (String) SipxConfParams.get("host");
String password = (String) SipxConfParams.get("password");
System.out.println("Sipx Host: "
+host
+" Sipx Password: "
+password ) ;
Map zimConfParams = tex.readZimbraParams();
host = (String) zimConfParams.get("domain");
password = (String) zimConfParams.get("admin_password");
System.out.println("Zimbra: " +host
+ " password " + password ) ;
// Test the misc params without using the get ()
Map<String,String> zimConfMiscParams = tex.readMiscZimbraParams();
String add_contact_comm_str_verb= "" ;
System.out.println(zimConfMiscParams.keySet() ) ;
System.out.println("Map entryset: "+ zimConfMiscParams.entrySet() ) ;
for ( Map.Entry <String,String> attr : zimConfMiscParams.entrySet() ) {
System.out.println("key "+ attr.getKey() ) ;
System.out.println("value"+ attr.getValue() ) ;
add_contact_comm_str_verb += " ../a=" + attr.getKey() + " -v "
+ "@n=" + attr.getValue() ;
System.out.println("String out : " + add_contact_comm_str_verb ) ;
}
}
catch(ConfigurationException ce ) {
System.err.println("Configuration file is not right ");
ce.printStackTrace();
}
catch (IndexOutOfBoundsException ie ) {
System.err.println("Configuration file is missing a parameter that is needed ");
ie.printStackTrace();
}
catch(Exception cex)
{
cex.printStackTrace();
}
}
/* */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment