Ravish Mody

Today, few hours back one of our subscriber  Kaushik had asked us that as per their requirement they need a WLST script which should get its details from a property file and create multiple datasource instead of only one, hence while watching the Quarter Final Cricket match between India Vs Australia I have created this script which would fulfil Kaushik’s and others subscribers requirements also. So make the best out of this WLST script and have fun with the cricket fever 😉

The best thing here is the administrator need to just edit the properties file with the datasource  details and the rest of the things will be taken care by the WLST Script.

Steps to Create Multiple DataSource Using WLST

Step1). Create a Directory somewhere in your file system like : “C:WLST_DS”

Step2). Write a Properties filedetails.properties inside “C:WLST_DS” like following:

domain.name=Domain_7001
admin.url=t3://localhost:7001
admin.userName=weblogic
admin.password=weblogic

total.DS=3

datasource.name.1=DS_1
datasource.database.name.1=demo
datasource.target.1=AdminServer
datasource.filename.1=DS_1.xml
datasource.jndiname.1=DS_1_JNDI
datasource.driver.class.1=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url.1=jdbc:pointbase:server://localhost:9092/demo
datasource.username.1=PBPUBLIC
datasource.password.1=PBPUBLIC
datasource.test.query.1=SQL SELECT * FROM DUAL

datasource.name.2=DS_2
datasource.database.name.2=demo
datasource.target.2=AdminServer
datasource.filename.2=DS_2.xml
datasource.jndiname.2=DS_2_JNDI
datasource.driver.class.2=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url.2=jdbc:pointbase:server://localhost:9092/demo
datasource.username.2=PBPUBLIC
datasource.password.2=PBPUBLIC
datasource.test.query.2=SQL SELECT * FROM DUAL

datasource.name.3=DS_3
datasource.database.name.3=demo
datasource.target.3=AdminServer
datasource.filename.3=DS_3.xml
datasource.jndiname.3=DS_3_JNDI
datasource.driver.class.3=com.pointbase.jdbc.jdbcUniversalDriver
datasource.url.3=jdbc:pointbase:server://localhost:9092/demo
datasource.username.3=PBPUBLIC
datasource.password.3=PBPUBLIC
datasource.test.query.3=SQL SELECT * FROM DUAL

Step2). Now in the same directory write the following WLST Script “createMultipleDataSource.py” like following:

#############################################################################
#
# @author Copyright (c) 2010 - 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################

from java.io import FileInputStream

propInputStream = FileInputStream("details.properties")
configProps = Properties()
configProps.load(propInputStream)

domainName=configProps.get("domain.name")
adminURL=configProps.get("admin.url")
adminUserName=configProps.get("admin.userName")
adminPassword=configProps.get("admin.password")

totalDataSource_to_Create=configProps.get("total.DS")

connect(adminUserName, adminPassword, adminURL)
edit()
startEdit()
print '========================================='
print 'Creating DataSource....'
print '========================================='
i=1
while (i <= int(totalDataSource_to_Create)) :

	try:
		cd('/')
		dsName=configProps.get("datasource.name."+ str(i))
		dsFileName=configProps.get("datasource.filename."+ str(i))
		dsDatabaseName=configProps.get("datasource.database.name."+ str(i))
		datasourceTarget=configProps.get("datasource.target."+ str(i))
		dsJNDIName=configProps.get("datasource.jndiname."+ str(i))
		dsDriverName=configProps.get("datasource.driver.class."+ str(i))
		dsURL=configProps.get("datasource.url."+ str(i))
		dsUserName=configProps.get("datasource.username."+ str(i))
		dsPassword=configProps.get("datasource.password."+ str(i))
		dsTestQuery=configProps.get("datasource.test.query."+ str(i))

		print ''
		print 'Creating DataSource: ',dsName,' ....'
		cmo.createJDBCSystemResource(dsName)
		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName)
		cmo.setName(dsName)

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
		set('JNDINames',jarray.array([String('jdbc/' + dsName )], String))

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName )
		cmo.setUrl(dsURL)
		cmo.setDriverName( dsDriverName )
		cmo.setPassword(dsPassword)

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCConnectionPoolParams/' + dsName )
		cmo.setTestTableName(dsTestQuery)
		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
		cmo.createProperty('user')

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/user')
		cmo.setValue(dsUserName)

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
		cmo.createProperty('databaseName')

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/databaseName')
		cmo.setValue(dsDatabaseName)

		cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
		cmo.setGlobalTransactionsProtocol('OnePhaseCommit')

		cd('/SystemResources/' + dsName )
		set('Targets',jarray.array([ObjectName('com.bea:Name=' + datasourceTarget + ',Type=Server')], ObjectName))

		print 'DataSource: ',dsName,', has been created Successfully !!!'
		print ''

	except:
		print '***** CANNOT CREATE DATASOURCE !!! Check If the DataSource With the Name : ' , dsName ,' Alreday exists or NOT...'
		print ''
	i = i + 1
print '========================================='
save()
activate()

Step3). Now Open a Command/Shell Prompt and then run the “setWLSEnv.sh” script to set the CLASSPATH and PATH environment variables. Run the “. ./setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the <BEA_HOME>/wlserver_10.3/server/bin) then run the following command….
. ./setWLSEnv.sh


Note: Here The first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step4). Run the Above WLST Script like following:

java weblogic.WLST createMultipleDataSource.py

Make sure that the Database is running while you are creating the DataSource.

Following would be the Output

java weblogic.WLST createMultipleDataSource.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'Domain_7001'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

Location changed to edit tree. This is a writable tree with
DomainMBean as the root. To make changes you will need to start
an edit session via startEdit().

For more help, use help(edit)
You already have an edit session in progress and hence WLST will
continue with your edit session.

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
=========================================
Creating DataSource....
=========================================

Creating DataSource:  DS_1  ....
DataSource:  DS_1 , has been created Successfully !!!

Creating DataSource:  DS_2  ....
DataSource:  DS_2 , has been created Successfully !!!

Creating DataSource:  DS_3  ....
DataSource:  DS_3 , has been created Successfully !!!

=========================================
Saving all your changes ...
Saved all your changes successfully.
Activating all your changes, this may take a while ...
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed

Regards,
Ravish Mody

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.