Sunday, January 23, 2011

Reading a properties file in wlst (jython) script.

I had to write scripts to create domain and configure datasource and jms on weblogic server using wlst (weblogic script tool). Datasource credentials were in a properties file, which was to be loaded in script. Here is a piece of script to load the properties file and read the values from file.

myProps.jy
#Script to load properties file.

from java.io import File
from java.io import FileInputStream
from java.util import Properties


#Load properties file in java.util.Properties
def loadPropsFil(propsFil):

 inStream = FileInputStream(propsFil)
 propFil = Properties()
 propFil.load(inStream) 
 
 return propFil


#Displays all keys and values of properties file.
def dispayProps():

 myPorpFil = loadPropsFil('D:/myProps.properties')
 keys = myPorpFil.keySet()
 for key in keys:
  print key+': '+myPorpFil.getProperty(key)
 
 return

if(1):
 dispayProps()


The properties file to be read.
myProps.properties
dataSourceName=myds
host=localhost
port=1234
userName=usr
password=psswrd



Output after running script.
>>> execfile('D:\myProps.jy')
port: 1234
password: psswrd
host: localhost
userName: usr
dataSourceName: myds
 

No comments:

Post a Comment