# This program may be used, executed, copied, modified and distributed
# without royalty for the purpose of developing, using, marketing, or distribution
#-----------------------------------------------------------------
# ex7.py - Jython implementation of example script 7
#-----------------------------------------------------------------
#
# The purpose of this example is to demonstrate the creation of a
# JDBCProvider object using a template.
#
# This script can be included in the wsadmin command invocation like this:
#
# wsadmin -lang jython -f ex7.py mynode me secret
#
# or the script can be execfiled from the wsadmin command line like this:
# wsadmin> execfile ("ex7.py") or execfile("ex7.py")
# wsadmin> ex7("mynode", "me", "secret")
#
# The script expects 3 parameters:
# node name
# user
# password
# In addition, the script sets several constants that can be customized,
# or this script could be adapted to take some or all of these as
# parameters:
# the name of the new JDBCProvider object
# the name of the new DataSource object
# a string to be used when searching for an appropriate template
# the path to the driver needed for this JDBCProvider
#
# This example demonstrates many wsadmin features:
#
# - The use of the AdminConfig object to locate configuration objects
# - The use of the AdminConfig object to modify configuration objects
# - The use of the AdminConfig listTemplates and createUsingTemplate commands to create configuration objects
#-----------------------------------------------------------------
#
import sys, java
lineSeparator = java.lang.System.getProperty('line.separator')
def ex7(nodeName, username, password):
#--------------------------------------------------------------
# set up globals
#--------------------------------------------------------------
global AdminConfig
newJDBCname = "newjdbc"
newDSname = "newds"
templName = "DB2 Universal JDBC Driver Provider (XA)"
driverPath = "C:/SQLLIB/java"
#---------------------------------------------------------
# Get the config id for a JDBC Template
#---------------------------------------------------------
print "ex7: getting the JDBCProvider template whose name includes " + templName
templates = AdminConfig.listTemplates("JDBCProvider", templName).split(lineSeparator)
template = templates[0]
print "the template is: " + template
#---------------------------------------------------------
# Create a new JDBCProvider using this template
#---------------------------------------------------------
print "ex7: create a new JDBCProvider object named " + newJDBCname
name_attr = ["name", newJDBCname]
attrs = []
attrs.append(name_attr)
serv1 = AdminConfig.getid("/Node:" + nodeName + "/Server:server1/")
new1 = AdminConfig.createUsingTemplate("JDBCProvider", serv1, attrs, template)
print "the new1 is: " + new1
#---------------------------------------------------------
# For this example, we do not need the WAS40DataSource object, but
# we do want to modify the DataSource object.
# Note that the template for this JDBCProvider does not give
# names to the WAS40DataSource and DataSource objects associated with
# it. We can use "getid" to retrieve ids for objects that do not have
# names as shown below.
#---------------------------------------------------------
print "ex7: remove the WAS40DataSource object created via the template"
was40ds = AdminConfig.getid("/JDBCProvider:" + newJDBCname + "/WAS40DataSource:/")
AdminConfig.remove(was40ds)
#--------------------------------------------------------------
# Modify the DataSource as appropriate. In particular for this
# example, we give the DataSource a name, a jndiName, and we set
# user and password properties.
#--------------------------------------------------------------
print "ex7: modify the datasource object -- name, jndiName, properties."
ds = AdminConfig.getid("/JDBCProvider:" + newJDBCname + "/DataSource:/")
nameAttr = ["name", newDSname]
jndiNameAttr = ["jndiName", newDSname]
userAttr = [["name", "user"], ["value", username], ["type", "java.lang.String"]]
passwordAttr = [["name", "password"], ["value", password], ["type", "java.lang.String"]]
newprops = [userAttr, passwordAttr]
psAttr = ["propertySet", [["resourceProperties", newprops]]]
attrs = []
attrs.append(nameAttr)
attrs.append(jndiNameAttr)
attrs.append(psAttr)
AdminConfig.modify(ds, attrs)
#--------------------------------------------------------------
# Modify the DataSource to give it a relational resource adapter.
# We just use the first one we find for this server, but other
# schemes are possible.
#--------------------------------------------------------------
print "ex7: modify the datasource object -- relationalResourceAdapter"
rra = AdminConfig.list("J2CResourceAdapter", serv1).split(lineSeparator)[0]
rraAttr = [["relationalResourceAdapter", rra]]
AdminConfig.modify(ds, rraAttr)
#--------------------------------------------------------------
# Update the variable map -- this JDBCProvider introduced a
# new variable:
#--------------------------------------------------------------
print "ex7: update the variable map."
vm = AdminConfig.getid("/Node:" + nodeName + "/VariableMap:/")
name = ["symbolicName", "DB2_JDBC_DRIVER_PATH"]
value = ["value", "C:/SQLLIB/java"]
desc = ["description", "Path to DB2"]
new1 = []
new1.append(name)
new1.append(value)
new1.append(desc)
entriesAttr = [["entries", [new1]]]
AdminConfig.modify(vm, entriesAttr)
#--------------------------------------------------------------
# Save all the changes
#--------------------------------------------------------------
print "ex7: saving the configuration"
AdminConfig.save()
#-----------------------------------------------------------------
# Main
#-----------------------------------------------------------------
if (len(sys.argv) != 3):
print "ex7: this script requires 3 parameters: "
print " the name of the node under which to create resources,"
print " the name of a database user, and the password for that user."
print ""
print "e.g.: ex7 mynode me secret"
else:
ex7(sys.argv[0], sys.argv[1], sys.argv[2])
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment