Sunday, July 15, 2007

Groovy Weblogic Migration

This is a bit of a change in direction being that I prefer to rant about football and other such easy-on-the-eye material but I thought I'd bore you a bit with techie stuff...read on.

In the past few months I have begun to re-acquaint myself with Scripting/Dynamic languages and would say that they have become quite sexy and even more expressive than I remembered. I have had cause to look at Ruby and Groovy in more detail and they each have unique selling points for me. Ruby because of its expressive nature and its wicked cohort Rails and Groovy for its near Java syntax and its inherent support for all things Java. Blessings like JSR 223 and more support in the JVM for dynamic languages (JRuby and Jython) seem to have helped.

So you can imagine that the first chance I got to make something really helpful with a scripting language I decided to give Groovy a spin. I will say that it has good documentation and is quite mature. My use case? I needed to migrate a number of system resources (DataSources and Mail Sessions) from Weblogic 8.1 to Weblogic 9.2.

I had heard from a team member on my project about WLST (Weblogic Scripting Tool) and the administrative magic you could do with it..so my head started ticking. I bet there are a number of ways I could have gone about migrating these resources (I welcome suggestions) but a notion had formed in my mind about the path I would go.

The change in configuration information structure was much between the two versions so I decided I was going to read a WLS 8.1 config.xml, then generate WLST scripts to recreate interesting elements from it in WLS 9.2. Initially I wrote the first rough sketch in Java (being a masochist :-D ) but after it became unmanageable, I had written XSLT code in lieu of JAXP or XSD and JAXB, I decided to re-implement in Groovy and I am loving every minute of it..XmlSlurper rocks! I made sure to pepper my script with closures right Groovy-newbie that I am. I have put up a copy of the code here for your viewing....free gift eh?
So without further ado here is the code. Enjoy!


class Migrator {

static void main(args) {
def domain = new XmlSlurper().parse(new File(args[0]))
def msClosure = {ms ->
new PrintWriter(new FileOutputStream("c:\\stuff\\migration\\"+ms.@Name.text().replaceAll(" ","")+".py")).withWriter {writer ->
writer.println("connect('${args[1]}','${args[2]}','${args[3]}')")
writer.println("edit()")
writer.println("startEdit()")
writer.println("myMailSession = create('${ms.@Name.text()}','MailSession')")
writer.println("myMailSession.setJNDIName('${ms.@JNDIName.text()}')")
writer.println("myMailSession.setProperties(makePropertiesObject('${ms.@Properties.text()}'))")
writer.println("myMailSession.addTarget(getMBean('Clusters/${ms.@Targets.text()}'))")
writer.println("save()")
writer.println("activate(block='true')")
writer.println("dumpStack()")
writer.println("disconnect()")
}
}
def dsClosure = {ds ->
def wlstOut = new PrintWriter(new FileOutputStream("c:\\stuff\\migration\\"+ds.@Name.text().replaceAll(" ","")+".py")).withWriter {writer ->
def poolInfo = domain.JDBCConnectionPool.find{it.@Name.text() == ds.@PoolName.text()}
def startPos = poolInfo.@Properties.text().toString().indexOf("user=")
if (startPos > -1) {
def userName = poolInfo.@Properties.text().substring(startPos+5)
writer.println("connect('${args[1]}','${args[2]}','${args[3]}')")
writer.println("edit()")
writer.println("startEdit()")
writer.println("jdbcSR = create('${ds.@Name.text()}','JDBCSystemResource')")
writer.println("theJDBCResource = jdbcSR.getJDBCResource()")
writer.println("connectionPoolParams = theJDBCResource.getJDBCConnectionPoolParams()")
writer.println("connectionPoolParams.setConnectionReserveTimeoutSeconds(25)")
writer.println("connectionPoolParams.setMaxCapacity(100)")
writer.println("connectionPoolParams.setTestTableName('SQL SELECT 1 FROM DUAL')")
writer.println("dsParams = theJDBCResource.getJDBCDataSourceParams()")
writer.println("dsParams.addJNDIName('${ds.@JNDIName.text()}')")
writer.println("driverParams = theJDBCResource.getJDBCDriverParams()")
writer.println("driverParams.setDriverName('oracle.jdbc.OracleDriver')")
writer.println("driverProperties = driverParams.getProperties()")
writer.println("proper = driverProperties.createProperty('user')")
writer.println("proper.setValue('${userName}')")
writer.println("driverParams.setPassword('${userName}')")
if (args.length > 4) {
writer.println("driverParams.setUrl('${args[3]}')")
writer.println("jdbcSR.addTarget(getMBean('Clusters/${ds.@Targets.text()}'))")
} else {
writer.println("driverParams.setUrl('${poolInfo.@URL.text()}')")
writer.println("jdbcSR.addTarget(getMBean('Clusters/${ds.@Targets.text()}'))")
}
writer.println("save()")
writer.println("activate(block='true')")
writer.println("dumpStack()")
writer.println("disconnect()")
}
}
}

domain.JDBCTxDataSource.collect dsClosure
domain.JDBCDataSource.collect dsClosure
domain.MailSession.collect msClosure
}