Reading property files from java.
In real time application deployment, few values for
application will be changing from environment to environment. Example, Database
details. In development, Testing and in production environments we use
different JDBC connection details. So hard coding this details in the code is
not a good approach. Because every time we change these details we need to
recreate the EAR and deploy. Instead we can refer these values from external file
and use in the code, we no need to recreate EAR every time when we change
environments. Or any other application properties which will change time to
time from environment to environment.
To handle the above scenario we have option called
properties files. Property file is simple flat text file which stores key-value
information like java.util.Map. In runtime, we read this property file and get
the value for which key we need the code. Below is the sample property file.
------------------------------------------------------------------------------------
MAILING_LIST = userAdminGroup@test.com
URL=https://devurl.test.com/url/faces/apps/pop/app/ui/page/Summary.jspx
IDM_SERVICE_URL=https://idm.test.com/OIMPortalServices/PortalSOAP?WSDL
OIM_USER_ID = dileepkumarrongaliblog@cswg.com
OIM_PASSWORD = test1234
-------------------------------------------------------------------------------------
In the below example I would like to explain how to read the
property file key-pair values in java code. In the below example I am reading
the OIM_USER_ID information.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import oracle.adf.share.logging.ADFLogger;
public class ReadPropertyFile {
//logger class
private static
ADFLogger _logger =
ADFLogger.createADFLogger(ReadPropertyFile .class);
//Property file name
public static final
String PROPERTY_FILE_NAME = "MYEXAMPLE.properties";
// file seperator to
work accordinlgy in windows and unix systems.
public static final
String FILE_SEPERATOR = System.getProperty("file.separator");
//if you deploye your
appllication in Weblogic server change below value accordingly to your server
path of file.
//public static final
String SERVER_PROPERTY_FILE_LOCATION =System.getProperty("wls.home")
+ FILE_SEPERATOR + "propertyFile";
//Just for demo
purpose I have put the file in below location
public static final
String SERVER_PROPERTY_FILE_LOCATION =
"D:\\Examples\\build\\propertyFile";
//Complete file path
public static final
String SERVER_PROPERTY_FILE = SERVER_PROPERTY_FILE_LOCATION + FILE_SEPERATOR +
PROPERTY_FILE_NAME;
public
ReadPropertyFile() {
super();
}
public String
getPropConstant(String key) {
Properties
propertiesBundle = new Properties();
try {
String
propertyFilePath = SERVER_PROPERTY_FILE;
//path of
properties file
_logger.info("PROERTIES FILE PATH IS::::::::::
"+propertyFilePath);
if
(propertyFilePath != null) {
//get file input stream
FileInputStream in = new FileInputStream(propertyFilePath);
//load
the property bundle
propertiesBundle.load(in);
//get
all the keys in the property file.
Enumeration em = propertiesBundle.keys();
//iterate over the keys
while
(em.hasMoreElements()) {
String keyStr = em.nextElement().toString();
String value = (String)propertiesBundle.get(keyStr);
if(value != null && !"".equals(value) &&
keyStr.equals(key)){
return value;
}
}
}
} catch
(IOException e) {
_logger.log(ADFLogger.ERROR, "Error getting Property file :"+
e.getStackTrace().toString());
}
return null;
}
public static void
main(String[] args) {
ReadPropertyFile
readPropertyFile = new ReadPropertyFile();
//reading property
value for given key.
String value =
readPropertyFile.getPropConstant("OIM_USER_ID");
System.out.println("---OIM_USER_ID-----"+value);
}
}
Comments
Post a Comment