Thursday 7 April 2016

Use external property file in WLST

I frequently create a wlst script, that needs properties. Not so exciting, but how to do that in a convenient way, and how to detect in a clean way that properties aren't set?

You could read a property file like described here. The basics are to use in fact Java to create a properties object and a FileInputStream to read it:
#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

I think the main disadvantage is that it clutters the script-code and you need to call 'myPorpFil.getProperty(key)' to get the property value.

Following the documentation you can use the commandline option '-loadProperties propertyFilename' to explicitly provide a property file. I found this actually quite clean. Every property in the file becomes automatically available as a variable in your script.

Besides that I found a teriffic blog-post on error handling in wlst. It states that with ' except NameError, e:' you can handle the reference to a variable that is not declared earlier.

I combined these two sources to come up with a script template that alows me to provide property files for different target environments as a commandline option, while detecting if properties are provided. So let's assume you create a porpererty file named for instance 'localhost.properties' like:
#############################################################################
# Properties voor localhost Integrated Weblogic
#
# @author Martien van den Akker, Darwin-IT Professionals
# @version 1.0, 2016-04-06
#
#############################################################################
#
# Properties voor localhost
adminUrl=localhost:7101
adminUser=weblogic
adminPwd=welcome1
clustername=LocalCluster
# Generieke properties voor het creeeren van JMS componenten 
#jmsFileStoresBaseDir=/app/oracle/config/cluster_shared/filestore/
jmsFileStoresBaseDir=c:/Data/JDeveloper/SOA/filestore
#Filestore 01
...

Then you can use that with the following script, named for instance 'createJMSServersWithFileStoreV2.py':
#############################################################################
# Create FileStores and JMS Servers
#
# @author Martien van den Akker, Darwin-IT Professionals
# @version 1.0, 2016-04-06
#
#############################################################################
# Modify these values as necessary
import sys, traceback
scriptName = 'createJMSServersWithFileStoreV2.py'
#
#
def usage():
  print 'Call script as: '
  print 'Windows: wlst.cmd'+scriptName+' -loadProperties localhost.properties'
  print 'Linux: wlst.sh'+scriptName+' -loadProperties environment.properties'
  print 'Property file should contain the following properties: '
  print "adminUrl='localhost:7101'"
  print "adminUser='weblogic'"
  print "adminPwd='welcome1'"
 
def main():
  try:
    #Connect to administration server
    print '\nConnect to AdminServer via '+adminUrl+' with user '+adminUser
    connect(adminUser, adminPwd, adminUrl)
...
  except NameError, e:
    print 'Apparently properties not set.'
    print "Please check the property: ", sys.exc_info()[0], sys.exc_info()[1]
    usage()
  except:
    apply(traceback.print_exception, sys.exc_info())
    stopEdit('y')
    exit(exitcode=1)

#call main()
main()
exit()

You can call it like 'wlst createJMSServersWithFileStoreV2.py -loadProperties localhost.properties'. If you don't provide a property file you'll get:
e:\wls>wlst createJMSServersWithFileStoreV2.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Apparently properties not set.
Please check the properties:  exceptions.NameError adminUrl
Call script as:
Windows: wlst.cmdcreateJMSServersWithFileStoreV2.py -loadProperties localhost.properties
Linux: wlst.shcreateJMSServersWithFileStoreV2.py -loadProperties environment.properties
Property file should contain the following properties:
adminUrl='localhost:7101'
adminUser='weblogic'
adminPwd='welcome1'


Exiting WebLogic Scripting Tool.


e:\wls>

Pretty clean. You could even use the 'except NameError, e:' construct to conditionally execute code when properties are set by ignoring/handling the situation when particular properties are intentionally not provided.

No comments :