Thursday, October 30, 2014

Weblogic : Creating Datasource using MAVEN and WLST Script

One of the interesting features of 12c is the option of using Maven for many of the administration tasks.

Maven along with WLST becomes the most convenient and powerful option to perform many of the administration tasks like creating domain,start and stop servers, listing deployed composites etc..

In this post I would like to introduce how maven plugin can be used to invoke a WLST script which creates a datasource in the weblogic domain.

As a first step we need to install maven plugin wls-maven-plugin.

This plugin present in the ORACLE_HOME/wlserver_12.1/server/lib

Open a command prompt and navigate to the above location

Now we need to install this plugin to our repository( here by default the repository will be the local repository which is present at  c:/users/<username>/.m2  (for windows)).

To install the plugin use the following command


mvn install:install-file -Dfile=wls-maven-plugin.jar -DpomFile=pom.xml

Once the installation is successful we can verify whether the required goals are available using the following command

mvn com.oracle.weblogic:wls-maven-plugin:help

Here we can see the wlst task is available. 

Now we are ready to execute the WLST script using maven

Create a datasource.properties file as shown below(change the values as per your requirement)
==================
domain.name=base_domain
admin.url=t3://localhost:7101
admin.userName=weblogic
admin.password=weblogic1

datasource.name=MyDataSource
datasource.database.name=XE
datasource.target=DefaultServer
datasource.filename=DS_One.xml
datasource.jndiname=DS_One_JNDI
datasource.driver.class=oracle.jdbc.xa.client.OracleXADataSource
datasource.url=jdbc:oracle:thin:@localhost:1521:XE
datasource.username=aq_user
datasource.password=aq_user

datasource.test.query=SQL SELECT * FROM DUAL
============================================

Save the following python script as datasource.py

note : remember to give the exact path to the datasource.properties file)

========start of python script ==========

from java.io import FileInputStream

propInputStream = FileInputStream("\\path\\to\\datasource.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")

dsName=configProps.get("datasource.name")
dsFileName=configProps.get("datasource.filename")
dsDatabaseName=configProps.get("datasource.database.name")
datasourceTarget=configProps.get("datasource.target")
dsJNDIName=configProps.get("datasource.jndiname")
dsDriverName=configProps.get("datasource.driver.class")
dsURL=configProps.get("datasource.url")
dsUserName=configProps.get("datasource.username")
dsPassword=configProps.get("datasource.password")
dsTestQuery=configProps.get("datasource.test.query")

connect(adminUserName, adminPassword, adminURL)
edit()
startEdit()
cd('/')
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))

save()
activate()

========================end of python script===============

Now open command prompt

Execute the following command after correcting the path as per your soa installation

mvn wls:wlst -DfileName=path/to/datasource.py  - DmiddlewareHome=path/to/oraclehome 
-DdomainHome=path/to/domainhome 

Once the script is executed successfully. Open the weblogic console and verify the datasource is created successfully

No comments:

Post a Comment