Listing 1
build.properties:
server=weblogic
weblogic.server.name=myserver
weblogic.domain.name=mydomain
weblogic.admin.username=weblogic
weblogic.admin.password=weblogic
weblogic.home=C:/bea
weblogic.lib.dir=${weblogic.home}/weblogic81/server/lib
weblogic.mbeantypes.dir=${weblogic.lib.dir}/mbeantypes
server.project.root.dir=${weblogic.home}/user_projects/domains/\
${weblogic.domain.name}
server.deploy.dir=${server.project.root.dir}/applications
Listing 2
build.xml:
[...]
<path id="weblogic.classpath">
<fileset dir="${weblogic.lib.dir}">
<include name="weblogic.jar"/>
</fileset>
</path>
<target name="check-properties">
<condition property="wlproperties.ok">
<and>
<isset property="weblogic.server.name"/>
<isset property="weblogic.domain.name"/>
<isset property="weblogic.admin.username"/>
<isset property="weblogic.admin.password"/>
</and>
</condition>
<fail unless="wlproperties.ok">
Weblogic properties are missing. Double check build.properties.
</fail>
<fail>
<condition>
<not><available file="${weblogic.lib.dir}/weblogic.jar"/></? not>
</condition>
Missing file ${weblogic.lib.dir}/weblogic.jar
</fail>
</target>
<target name="create-server" depends="check-properties">
<taskdef name="wlserver" classname="weblogic.ant.taskdefs.? management.WLServer"
classpathref="weblogic.classpath"/>
<echo>Creating server ${weblogic.server.name} at ${server.project.? root.dir}</echo>
<delete dir="${server.project.root.dir}" includeemptydirs="true" ? quiet="true"/>
<mkdir dir="${server.project.root.dir}" />
<wlserver
dir="${server.project.root.dir}"
domainName="${weblogic.domain.name}"
serverName="${weblogic.server.name}"
host="${host.ip}"
port="${rmi.port}"
generateConfig="true"
productionModeEnabled="false"
weblogicHome="${weblogic.home}/weblogic81"
username="${weblogic.admin.username}"
password="${weblogic.admin.password}"
action="start"
>
<classpath refid="weblogic.classpath"/>
</wlserver>
Listing 3
<java classname="weblogic.WLST" fork="true" failOnError="true" dir="scripts/wlst">
<classpath refid="weblogic.classpath"/>
<classpath>
<fileset dir="lib/wlst">
<include name="*.jar"/>
</fileset>
</classpath>
<arg line="createAll.py" />
</java>
<wlserver
host="${host.ip}"
port="${rmi.port}"
username="${weblogic.admin.username}"
password="${weblogic.admin.password}"
action="shutdown"
/>
</target>
Listing 4
createAll.py:
execfile("AdminTool.py")
admin.configureServer()
admin.createXaPool()
AdminTool.py:
from javax.management import InstanceNotFoundException
# Python 2.4 will include booleans, but until then this is required
true = 1
false = 0
class AdminTool:
def __init__(self):
loadProperties("administration.properties")
# Connects with a weblogic instance
def connect(self):
connect(username, password, "t3://" + host + ":" + port)
self.myServer = getTarget("/Server/" + serverName)
# Server attributes that cannot be generated via ant
def configureServer(self):
# Activates console DEBUG mode - I really like that
self.myServer.setStdoutSeverityLevel(64)
print "Configured server " + self.myServer.getName()
# Creates a JDBC pool:
def createPool(self, poolName, driverName):
pool = create(poolName, "JDBCConnectionPool")
pool.setDriverName(driverName)
pool.setURL(dbURL)
pool.setPassword(dbPassword)
pool.setProperties(makePropertiesObject("user=" + dbUsername))
pool.setRemoveInfectedConnectionsEnabled(false)
pool.setTestConnectionsOnCreate(true)
pool.setTestTableName("SQL SELECT 1 FROM DUAL")
# setTestFrecuencySeconds is not soported by WLST objects
# so here is a workaround
cd('/JDBCConnectionPool/' + poolName)
set('TestFrequencySeconds', 300)
cd('/')
pool.addTarget(self.myServer)
def createXaPool(self):
self.createPool(MY_POOL_NAME, 'oracle.jdbc.xa.client.? OracleXADataSource')
# Removes an element if it exists
def removeIfExists(self, name, type):
try:
mbean = home.getAdminMBean(name, type)
home.deleteMBean(mbean)
print 'Removed the ' + type + ': ' + name
except InstanceNotFoundException, e:
print "Cannot remove " + name + ",type=" + type + " because ? it does not exist"
def removeXaPool(self):
self.removeIfExists(MY_POOL_NAME, "JDBCConnectionPool")
# The JDBC Connection Pool name
MY_POOL_NAME='MyPool'
# the instance we are going to use
admin = AdminTool()
admin.connect()
administration.properties:
host=127.0.0.1
port=7001
username=weblogic
password=weblogic
dbURL=jdbc:oracle:thin:@dbhost:1521:DATABASE
dbUsername=foo
dbPassword=bar
Listing 5
wls:/mydomain/config> ls()
[...]
drw- JDBCConnectionPools
drw- JDBCDataSourceFactories
drw- JDBCDataSources
drw- JDBCMultiPools
drw- JDBCTxDataSources
[...]
wls:/mydomain/config> cd('JDBCConnectionPools')
wls:/mydomain/config/JDBCConnectionPools> ls()
drw- MyPool
wls:/mydomain/config/JDBCConnectionPools> cd('MyPool')
wls:/mydomain/config/JDBCConnectionPools/MyPool> ls()
[...]
-rw- TestConnectionsOnCreate true
-rw- TestConnectionsOnRelease false
-rw- TestConnectionsOnReserve false
-rw- TestFrequencySeconds 300
-rw- TestStatementTimeout -1
-rw- TestTableName SQL SELECT 1 FROM DUAL
-r-- Type JDBCConnectionPool
-rw- URL jdbc:oracle:thin:@dbhost:1521:DATABASE
[...]
Listing 6
wls:/mydomain/config/JDBCConnectionPools/MyPool> set? ('TestFrequencySeconds', 500)
wls:/mydomain/config/JDBCConnectionPools/MyPool> set('TestConnectionsOnRelease', 1)
wls:/mydomain/config/JDBCConnectionPools/MyPool> ls()
-rw- TestConnectionsOnCreate true
-rw- TestConnectionsOnRelease 1
-rw- TestConnectionsOnReserve false
-rw- TestFrequencySeconds 500
-rw- TestStatementTimeout -1
-rw- TestTableName SQL SELECT 1 FROM DUAL
-r-- Type JDBCConnectionPool
-rw- URL jdbc:oracle:? thin:@dbhost:1521:DATABASE
[...]
wls:/mydomain/config/JDBCConnectionPools/MyPool> get('TestConnectionsOn? Release')
1
Listing 7
wls:/mydomain/config> datasource=create('MyDS', 'JDBCTxDataSource')
JDBCTxDataSource with name 'MyDS' has been created successfully.
wls:/mydomain/config> datasource.setJNDIName("MyDS")
wls:/mydomain/config> datasource.setPoolName("P6SPY Connection Pool")
wls:/mydomain/config> datasource.setEnableTwoPhaseCommit(true)
wls:/mydomain/config> datasource.addTarget(admin.myServer)
1
Listing 8
wls:/mydomain/config> cd ('JDBCTxDataSources')
wls:/mydomain/config/JDBCTxDataSources> ls()
drw- MyDS
wls:/mydomain/config/JDBCTxDataSources> cd('MyDS')
wls:/mydomain/config/JDBCTxDataSources/MyDS> ls()
[...]
-rw- PoolName MyPool
[...]
wls:/mydomain/config/JDBCTxDataSources/MyDS> set('PoolName', MY_POOL_? NAME)