Monday, March 23, 2009

Reading csv File in WLST Script

One of the comment for an earlier post was about creating domain(s) from csv file with WLST. Creating domain using WLST is a fairly simple and straight forward process. So I decided to explain the simple concept of reading a csv file and creating a domain using an example.

The main trick is to use the Jython language libraries to perform some of the required operations. In my example I shall illustrate how to read command-line arguments, open and read files using Jython. I am also using some local function definitions to create some modular script here. The following is an example of a comma seperated value file that our WLST script can read to create one or more domains:


Comment lines start with # and each line that is not a comment line represent values for a domain such as domain name, location etc. The script to read the csv file and create the domain can be something like this:

### Script to create WebLogic Domain(s) from csv file
### Reusable Definitions
def buildDomain():
### Read Basic Template
set('ListenAddress', adminServerAddress)
set('ListenPort', int(adminServerPort))

### Create Admin User

### Write Domain
setOption('OverwriteDomain', 'true')

def printConfirmation():
### Print Confirmation
print ""
print "Created Domain With Following Values"
print "Domain Name = %s " % domainName
print "Domain Location = %s " % domainLocation
print "Admin User = %s " % adminUser
print "Admin Password = %s " % adminPassword
print "Admin Server Address = %s " % adminServerAddress
print "Admin Server port = %s " % adminServerPort

### Executable Script
import sys

### Define constants
WL_HOME = "/products/beaSB/wlserver_10.0"

### Read the command-line arguments
argslength = len(sys.argv)
if argslength < 2 :
print '==>Insufficient arguments'
print '==>Syntax: java weblogic.WLST csv.file'
### Read the csv file
fileName = sys.argv[1]
print('Reading File \"' + fileName + '\"' )
f = open(fileName)
for line in f.readlines():
### Strip the comment lines
if line.strip().startswith('#'):
### Split the comma seperated values
items = line.split(',')
items = [item.strip() for item in items]
if len(items) != 6:
print "==>Bad line: %s" % line
print "==>Syntax: domainName, domainLocation, adminUser, adminPassword, adminServerAddress, adminServerPort"
(domainName, domainLocation, adminUser, adminPassword, adminServerAddress, adminServerPort) = items

### Call the definition buildDomain

### Call the definition printConfirmation

except Exception, e:
print "==>Error Occured"
print e

The executable part of the script is something that is not marked as a definition and in this case it start with the import statement. Any command-line or program arguments passed to WLST are available through sys.argv array. The array is zero indexed and the first element of the array sys.argv[0] is the script file itself. In our script we check to make sure that atleast 2 arguments including the script filename exists.

### Read the command-line arguments
argslength = len(sys.argv)
if argslength < 2 :
print '==>Insufficient arguments'
print '==>Syntax: java weblogic.WLST csv.file'

Built-in functions are then used to open and read the csv file.

f = open(fileName)
for line in f.readlines():

We are Using the startswith to eliminate the comment lines and then using the split and strip string function to create a list of all the items passed from the command-line. Later after checking the number of values in each line the list is assigned to a tuple of variable names (tuple is immutable in Jython where as Lists are mutable).

(domainName, domainLocation, adminUser, adminPassword, adminServerAddress, adminServerPort) = items

Once we have all values read and stored in the respective variables then it is pretty straight forward to create a domain which in this case was built as function definition (buildDomain). Later all the values were sent to standard output for user's attention. Also any exception will be reported with the try...except block.


  1. ThankYou,Bala.
    I want to find how to read the boot.propeties file ie encrepted one using WLST and deploy my application.

    You input are value to me.

    I greatly admire your work.

  2. This is my Good luck that I found your post which is according to my search and topic, I think you are a great blogger, thanks for helping me outta my problem..
    Buy Dissertation Online

  3. Thank you Bala for sharing this script. It's very useful. It helped me to create WLST which is searching for errors in forms and reports log files in different domains.
    However I can't redirect the output of the displayLogs command to log file. I have tried "redirect" and

    f = File("D:\\Alex\\Scripts\\Weblogic\\wlst\\logs\\fnt1fmw002.log")
    fos = FileOutputStream(f)

    but it doesn't work. I am running WLST on Windows
    Do you know whether is possible to redirect the output of displayLogs?