WLST script to add DataSource and DBAdapter EIS Connection Factory

Creating a EIS ConnectionFactory in your Database Adapter can be done with the WebLogic Administration Console, but of course this is also “scriptable”. What I needed was a script that created a Data Source with EIS Connection factory bound to the specific datasource.

First I created a properties file, let’s call it DsCf.properties. Everything between <> should be replaced with your own values:

# Propertie file for creating datasource and EIS DB Adapter
# Created by Michel Schildmeijer
# Domain settings
domainname="&lt;your WLS DOMAIN&gt;"
adminurl=&lt;WLS HOST:Admin Port&gt;
adminusername=weblogic
adminpassword=&lt;passwd weblogic&gt;
#datasource settings
datasourcename=&lt;Name DataSource&gt;
datasourcedatabasename=&lt;database&gt;
datasourcetarget=&lt;targeted manaegd server&gt;
datasourcefilename= datasourcename + '.xml'
datasourcejndiname= 'jdbc/' + datasourcename
datasourcedriverclass=oracle.jdbc.OracleDriver
datasourceurl=jdbc:oracle:thin:@&lt;db host&gt;:1521:&lt;db sid&gt;
datasourceusername=&lt;db user&gt;
datasourcepassword=&lt;db user password
datasourcetestquery=SQL SELECT * FROM DUAL
#EIS Connection Factory settings
connfactname=eis/DB/&lt;Connection factory Name&gt;

Second, I created a WLST script to execute these properties and added the DataSource and ConnectionFactory. See the script below.

When running on a UNIX or linux system, navigate to the WLS DOMAIN DIR /bin( Specified during domaincreation) like /app/oracle/middleware/user_projects/domains/<Your domain>/bin and run

./setDomainEnv.sh

After this you are able to deploy the DataSource en ConnectionFactory:

java weblogic.WLST loadProperties DsCf.properties

After finished you can check in the WebLogic Admin Console to see:

  • If datasource is created
  • If in your outbound connections of the database adapter the EIS connection factory with binding to the DataSource has been created.

Script DsCf.py

 

<code></code><code>import os
</code><code>import time
</code><code>domainName = domainname
</code><code>adminURL= adminurl
</code><code>adminUserName= adminusername
</code><code>adminPassword= adminpassword
</code><code>dsName= datasourcename
</code><code>dsFileName= datasourcefilename
</code><code>dsDatabaseName=datasourcedatabasename
</code><code>datasourceTarget=datasourcetarget
</code><code>dsJNDIName=datasourcejndiname
</code><code>dsDriverName=datasourcedriverclass
</code><code>dsURL=datasourceurl
</code><code>dsUserName=datasourceusername
</code><code>dsPassword=datasourcepassword
</code><code>dsTestQuery=datasourcetestquery
</code><code>connect(adminUserName, adminPassword, adminURL)
</code><code>TargetServerName='soa_server1'
</code><code>edit()
</code><code>startEdit()
</code><code>cd('/')
</code><code>cmo.createJDBCSystemResource(dsName)
</code><code>cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName)
</code><code>cmo.setName(dsName)
</code><code>cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
</code><code>set('JNDINames',jarray.array([String('jdbc/' + dsName )], String))
</code><code>cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName )
</code><code>cmo.setUrl(dsURL)
</code><code>cmo.setDriverName( dsDriverName )
</code><code>cmo.setPassword(dsPassword)
</code><code>cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCConnectionPoolParams/' + dsName )
</code><code>cmo.setTestTableName(dsTestQuery)
</code><code>cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
</code><code>cmo.createProperty('user')
</code><code>cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/user')
</code><code>cmo.setValue(dsUserName)
</code><code>cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName )
</code><code>cmo.createProperty('databaseName')
</code><code>cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDriverParams/' + dsName + '/Properties/' + dsName + '/Properties/databaseName')
</code><code>cmo.setValue(dsDatabaseName)
</code><code>cd('/JDBCSystemResources/' + dsName + '/JDBCResource/' + dsName + '/JDBCDataSourceParams/' + dsName )
</code><code>cmo.setGlobalTransactionsProtocol('OnePhaseCommit')
</code><code>cd('/SystemResources/' + dsName )
</code><code>set('Targets',jarray.array([ObjectName('com.bea:Name=' + datasourceTarget + ',Type=Server')], ObjectName))
</code><code>save()
</code><code>activate()
</code><code>print 'Data source ' + dsName + ' configured'</code>
<code></code><code>
</code><code>#Connect
</code><code>#appPath='/app/oracle/middleware/soa11g/soa/connectors/DbAdapter.rar'
</code><code>#planPath='/app/oracle/middleware/soa11g/soa/connectors/DbAdapterPlanGenerated.xml'</code>
<code></code><code></code>oracleHome = os.environ.get(“ORACLE_HOME”)
<code></code><code>appPath= oracleHome + 'soa/connectors/DbAdapter.rar'
planPath=oracleHome + 'soa/connectors/DbAdapterPlanGenerated.xml'</code>
<code></code><code></code>
<code>CFName=connfactname
</code><code>dsName=datasourcename
</code><code>appName='DbAdapter'
</code><code>moduleOverrideName=appName+'.rar'
</code><code>moduleDescriptorName='META-INF/weblogic-ra.xml'
</code><code>def makeDeploymentPlanVariable(wlstPlan, name, value, xpath, origin='planbased'):
</code><code>while wlstPlan.getVariableAssignment(name, moduleOverrideName, moduleDescriptorName):
</code><code>wlstPlan.destroyVariableAssignment(name, moduleOverrideName, moduleDescriptorName)
</code><code>while wlstPlan.getVariable(name):
</code><code>wlstPlan.destroyVariable(name)
</code><code>variableAssignment = wlstPlan.createVariableAssignment( name, moduleOverrideName, moduleDescriptorName )
</code><code>variableAssignment.setXpath( xpath )
</code><code>variableAssignment.setOrigin( origin )
</code><code>wlstPlan.createVariable( name, value )
</code><code>             def main():
</code><code>                     connect(adminUserName, adminPassword, adminURL)
</code><code>                     edit()
</code><code>                           try:
</code><code>                           startEdit()
</code><code>                           planPath = get('/AppDeployments/DbAdapter/PlanPath')
</code><code>                           print '__ Using plan ' + planPath
</code><code>                           myPlan=loadApplication(appPath, planPath)
</code><code>                           print '___ BEGIN change plan'
</code><code>                           makeDeploymentPlanVariable(myPlan, 'ConnectionInstance_' '+ CFName' +'_JNDIName_abc123XXX','+ CFName', '/weblogic-connector/outbound-resource-adapter/connection-definition-group/[connection-factory-interface="javax.resource.cci.ConnectionFactory"]/connection-instance/[jndi-name="'+ CFName + '"]/jndi-name')
</code><code>                           makeDeploymentPlanVariable(myPlan, 'ConfigProperty_xADataSourceName_'+ dsName +'_abc123XXX',dsName, '/weblogic-connector/outbound-resource-adapter/connection-definition-group/[connection-factory-interface="javax.resource.cci.ConnectionFactory"]/connection-instance/[jndi-name="'+ CFName + '"]/connection-properties/properties/property/[name="xADataSourceName"]/value')
</code><code>                           print '___ DONE change plan'
</code><code>                           myPlan.save();
</code><code>                           save();
</code><code>                           activate(block='true');
</code><code>                           cd('/AppDeployments/DbAdapter/Targets');
</code><code>
</code><code>                           redeploy(appName, planPath,targets=cmo.getTargets());
</code><code>                           print 'EIS Connection factory ' + CFName + 'using' + dsName + ' configured';
</code><code>                    except:
</code><code>                           stopEdit('y')
</code><code>main()</code>

<code>
</code>

Leave a Reply

  1. Hi,
    I’m new to this WLST script. I’m trying to do this.
    BTW, you have provided the Python script in this article but I’m confused where to keep this script and how to run.
    Please clarify.

    Thanks,
    Kamal

  2. Hi Edwin
    Thanks for you input
    About the hard paths
    You als could set the paths in you property file instead of the run script
    First get the ORACLE_HOME thats been set
    oracleHome = os.environ.get(“ORACLE_HOME”)
    And add this to you plan and app path:
    appPath= oracleHome + ‘/soa/connectors/DbAdapter.rar’

    planPath=oracleHome + ‘/soa/connectors/DbAdapterPlanGenerated.xml’
    About specific connectionfactory settings, it depends on wht you want to create with it. I’ll try to extend it with more options later on.
     


     

  3. Hi,
    great post, I was always wondering if it is possible. but it would be great to make it more flexible and add some warnings
    like
    appPath='/app/oracle/middleware/soa11g/soa/connectors/DbAdapter.rar'
    planPath='/app/oracle/middleware/soa11g/soa/connectors/DbAdapterPlanGenerated.xml'

    this is a hard path to your soa env and also has a DB plan with a hard value.
    And second you can only add the XA Datasource property , sometimes you need to add the datasource or set transacted.
    but I make some ANT macrodefs and use it in my scripts, also do this for AQ and JMS and make it more flexible.
    Keep up the good work.