Friday, August 21, 2015

Read Value From Properties File

Read Value From Properties File

smo.set("/headers/HTTPHeader/control/DynamicOverrideURL",ESIPropertyReader.getString("MTV_GROUP_CREATE_URL"));




/**
 * 
 */
package com.humana.bpic.et.common.util;

import java.util.StringTokenizer;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

import com.ibm.wsspi.sibx.mediation.MediationBusinessException;

public class ESIPropertyReader {

public static final String SERVICES_PROP_FILE_LIST = System
.getProperty("ESIPropertyFileList");

private static CompositeConfiguration cc = null;
static {
if (SERVICES_PROP_FILE_LIST != null) {
cc = new CompositeConfiguration();
StringTokenizer st = new StringTokenizer(SERVICES_PROP_FILE_LIST);
while (st.hasMoreElements()) {
try {
String propFileConfig = st.nextToken(",");
StringTokenizer propFileConfigTokenizer = new StringTokenizer(
propFileConfig);
String propertyFileName = "";
String refreshDelay = null;
propertyFileName = propFileConfigTokenizer.nextToken(":")
+ ".properties";
if (propFileConfigTokenizer.hasMoreTokens()) {

refreshDelay = propFileConfigTokenizer.nextToken(":");
System.out.println("refreshDelay:" + refreshDelay);
} else {
refreshDelay = "1";
}
PropertiesConfiguration conf = new PropertiesConfiguration();
conf.clear();
conf.setDelimiterParsingDisabled(true);
// System.out.println("property file name  loaded:"+propertyFileName);
conf.load(propertyFileName);
// System.out.println("property file Loaded Successfully");
if (refreshDelay != null) {
FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
int refreshIntervalInMilliSeconds = new Integer(
refreshDelay).intValue() * 60 * 1000;
fileChangedReloadingStrategy
.setRefreshDelay(refreshIntervalInMilliSeconds);
conf.setReloadingStrategy(fileChangedReloadingStrategy);
}
cc.addConfiguration(conf);
} catch (Exception e) {
// e.printStackTrace();
}
}
}
}

public static String getString(String key, String defaultMessage) {
String message = null;
try {
if (cc != null) {
// System.out.println("key"+ key);
message = (String) cc.getProperty(key);
// System.out.println("here in getString :"+ message);
if (message == null) {
message = defaultMessage;
}
} else {
message = defaultMessage;
}

} catch (Exception e) {
e.printStackTrace();
}
return message;
}

public static String getString(String key)
throws MediationBusinessException {

if (cc != null) {
// System.out.println("key"+ key);
// System.out.println("here in getString :"+ cc.getProperty(key));
if (cc.getProperty(key) != null) {
return (String) cc.getProperty(key);
} else {

throw new MediationBusinessException("Property not found for '"
+ key + "'");
}
} else {
throw new MediationBusinessException("Property not found for '"
+ key + "' as property file could not be initialized");
}

}

public static String concatGetString(String prefix, String key)
throws MediationBusinessException {
String key1 = "";
key1 = prefix + key;
key1 = key1.replaceAll(" ", "");
// System.out.println("Here is the value of key1 "+key1);
if (cc != null) {
// System.out.println("key1:"+ key1);
// System.out.println("here in concatGetString :"+
// cc.getProperty(key1));
if (cc.getProperty(key1) != null) {
return (String) cc.getProperty(key1);
} else {

throw new MediationBusinessException("Property not found for '"
+ key1 + "'");
}
} else {
throw new MediationBusinessException("Property not found for '"
+ key1 + "' as property file could not be initialized");
}

}
}



Usage:

smo.set("/headers/SMOHeader/Target/address",ESIPropertyReader.getString("URAAP_MQ_URL"));

Properties File(ESIEnvironment.properties)

MTV_MEMBER_READ_URL=http://dev-enrollmentapi.humana.com/Member/api/members/stub/
MTV_MEMBER_CREATE_URL=http://dev-enrollmentapi.humana.com/Member/api/Members
MTV_MEMBER_SEARCH_URL=http://dev-enrollmentapi.humana.com/Member/api/members/Search
MTV_GROUP_READ_URL=http://dev-enrollmentapi.humana.com/Group/api/groups/
MTV_GROUP_CREATE_URL=http://dev-enrollmentapi.humana.com/Group/api/groups
URAAP_MQ_URL=wmq:/msg/queue/QA.BS.ESI.URAAP.EVENTS.DEV
SOLAR_MQ_URL=wmq:/msg/queue/QA.BS.ESI.SOLAR.EVENTS.DEV
SDR_MQ_URL=wmq:/msg/queue/QA.BS.ESI.SDR.EVENTS.DEV

PBS_MQ_URL=wmq:/msg/queue/QA.BS.ESI.PBS.EVENTS.DEV


No comments:

Post a Comment