In this blog entry we will see how to retrieve config values from the config file. For this we will be using the "Properties" java class.
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream.
Each key and its corresponding value in the property list is a string.Please see the following link "http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html" for more details on the "Properties" java class.
So, the code which is present in the below blog section will make use of the "Properties" java class.
The sample config file is mentioned below :-
# This is a sample Config File (# is used to commennt any line)
#Sample key value below
sample.key.name=sampleValue
The services which are mentioned below is used to retrieve the value for a corresponding key. But before retrieving the value you need to load the content
of the config file into the memory of the Integration Server.
getConfigFileName (This service is used to retrieve the config file and loading the data into IS memory) , the service is mentioned below :-
import com.wm.data.*;
import com.wm.util.Values;
import com.wm.app.b2b.server.Service;
import com.wm.app.b2b.server.ServiceException;
import java.io.*;
import java.util.*;
public final class getConfigFileName_SVC
{
/**
* The primary method for the Java service
*
* @param pipeline
* The IData pipeline
* @throws ServiceException
*/
public static final void getConfigFileName(IData pipeline)
throws ServiceException {
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
IDataUtil.put( pipelineCursor, "configFileName", CONFIG_FILE );
pipelineCursor.destroy();
}
// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---
// initialize static variables
private static String CONFIG_FILE = "packages/*********/config/*********.cnf";
private static boolean propertiesLoaded = false;
private static Properties properties = null;
/** gets a property from the config file **/
public static String getProperty( String key ) throws ServiceException
{
// check to see if the properties have been loaded
if (!propertiesLoaded) {
try{
IData idata = IDataFactory.create();
IDataUtil.put(idata.getCursor(),"configFile",CONFIG_FILE);
loadProperties( idata );
}catch (Exception e){
throw new ServiceException("Failed to load properties:"+e.toString());
}
}
if ( properties == null )
return( null );
else
return( properties.getProperty( key ) );
}
loadProperties service (This is the service which reads the file and converts it into propperties object , this service should be kept as startup service of the package)
loadProperties Service :-
public static final void loadProperties(IData pipeline) throws ServiceException {
/*
Retrieves values from configFile
directory [server]/packages/yourPackage/config/
*/
try{
// read in the file stream and convert it to a properties object
FileInputStream configFileInputStream = new FileInputStream( CONFIG_FILE );
// properties object is already defined in the Shared code
properties = new Properties();
properties.load( configFileInputStream );
propertiesLoaded = true;
}catch ( FileNotFoundException e ){
// throw an error
throw new ServiceException("Error finding property file: "+e);
}catch ( IOException e ){
//throw an error
throw new ServiceException("Error reading "+CONFIG_FILE+" property file: "+e);
}catch ( Exception e ){
//throw an error
throw new ServiceException("Error while registering "+CONFIG_FILE+" property file: "+e);
}
getProperties service (This service is used to retrieve the value for a corresponding key)
public static final void getProperty(IData pipeline) throws ServiceException {
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
String key = IDataUtil.getString( pipelineCursor, "key" );
pipelineCursor.destroy();
// check to see if properties object is null
if (properties == null)
{
loadProperties( null );
}
String value = getProperty(key);
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put( pipelineCursor_1, "value", value );
pipelineCursor_1.destroy();
}
Enjoy Reading , If you have any Question related to webMethods please comment below , I will try to answer the questions.Please Subscribe for latest update on the blogs
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream.
Each key and its corresponding value in the property list is a string.Please see the following link "http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html" for more details on the "Properties" java class.
So, the code which is present in the below blog section will make use of the "Properties" java class.
The sample config file is mentioned below :-
# This is a sample Config File (# is used to commennt any line)
#Sample key value below
sample.key.name=sampleValue
The services which are mentioned below is used to retrieve the value for a corresponding key. But before retrieving the value you need to load the content
of the config file into the memory of the Integration Server.
getConfigFileName (This service is used to retrieve the config file and loading the data into IS memory) , the service is mentioned below :-
import com.wm.data.*;
import com.wm.util.Values;
import com.wm.app.b2b.server.Service;
import com.wm.app.b2b.server.ServiceException;
import java.io.*;
import java.util.*;
public final class getConfigFileName_SVC
{
/**
* The primary method for the Java service
*
* @param pipeline
* The IData pipeline
* @throws ServiceException
*/
public static final void getConfigFileName(IData pipeline)
throws ServiceException {
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
IDataUtil.put( pipelineCursor, "configFileName", CONFIG_FILE );
pipelineCursor.destroy();
}
// --- <<IS-BEGIN-SHARED-SOURCE-AREA>> ---
// initialize static variables
private static String CONFIG_FILE = "packages/*********/config/*********.cnf";
private static boolean propertiesLoaded = false;
private static Properties properties = null;
/** gets a property from the config file **/
public static String getProperty( String key ) throws ServiceException
{
// check to see if the properties have been loaded
if (!propertiesLoaded) {
try{
IData idata = IDataFactory.create();
IDataUtil.put(idata.getCursor(),"configFile",CONFIG_FILE);
loadProperties( idata );
}catch (Exception e){
throw new ServiceException("Failed to load properties:"+e.toString());
}
}
if ( properties == null )
return( null );
else
return( properties.getProperty( key ) );
}
loadProperties service (This is the service which reads the file and converts it into propperties object , this service should be kept as startup service of the package)
loadProperties Service :-
public static final void loadProperties(IData pipeline) throws ServiceException {
/*
Retrieves values from configFile
directory [server]/packages/yourPackage/config/
*/
try{
// read in the file stream and convert it to a properties object
FileInputStream configFileInputStream = new FileInputStream( CONFIG_FILE );
// properties object is already defined in the Shared code
properties = new Properties();
properties.load( configFileInputStream );
propertiesLoaded = true;
}catch ( FileNotFoundException e ){
// throw an error
throw new ServiceException("Error finding property file: "+e);
}catch ( IOException e ){
//throw an error
throw new ServiceException("Error reading "+CONFIG_FILE+" property file: "+e);
}catch ( Exception e ){
//throw an error
throw new ServiceException("Error while registering "+CONFIG_FILE+" property file: "+e);
}
getProperties service (This service is used to retrieve the value for a corresponding key)
public static final void getProperty(IData pipeline) throws ServiceException {
// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
String key = IDataUtil.getString( pipelineCursor, "key" );
pipelineCursor.destroy();
// check to see if properties object is null
if (properties == null)
{
loadProperties( null );
}
String value = getProperty(key);
// pipeline
IDataCursor pipelineCursor_1 = pipeline.getCursor();
IDataUtil.put( pipelineCursor_1, "value", value );
pipelineCursor_1.destroy();
}
Enjoy Reading , If you have any Question related to webMethods please comment below , I will try to answer the questions.Please Subscribe for latest update on the blogs
1 Comments
Very thanks for the post
ReplyDeleteBut I had an issue with a Unicode files.
I receive no properties values