Skip to content

Instantly share code, notes, and snippets.

@ninthdrug
Created November 15, 2012 11:59
Show Gist options
  • Save ninthdrug/4078301 to your computer and use it in GitHub Desktop.
Save ninthdrug/4078301 to your computer and use it in GitHub Desktop.
Script to patch the classpath in a Weblogic Server config.xml file
// script to patch the classpath in a Welogic Server config.xml file
import scala.xml._
import java.io._
def patchClassPath(configFile: String, edit: (String) => String) {
// inner function to apply the passed in edit function to the classpath
def patch(node: Node): Node = {
def patchElem(elem: Elem): Elem =
elem.copy(child = elem.child.map(patch(_)))
node match {
case elem: Elem if elem.label == "domain" => patchElem(elem)
case elem: Elem if elem.label == "server" => patchElem(elem)
case elem: Elem if elem.label == "server-start" => patchElem(elem)
case elem: Elem if elem.label == "class-path" =>
elem.copy(child = Text(edit(elem.text)))
case other @ _ => other
}
}
/**
* Returns the input XML node in a pretty print format String.
*/
def format(node: Node): String = {
val pp = new PrettyPrinter(Int.MaxValue, 2)
val buf = new StringBuilder()
buf.append("""<?xml version="1.0" encoding="UTF-8"?>""")
buf.append("\n")
buf.append(pp.format(node))
buf.append("\n")
buf.toString
}
val config = XML.loadFile(configFile) // load the old config from file
val patchedConfig = patch(config) // patch it
val writer = new PrintWriter(configFile) // output it out to file
writer.write(format(patchedConfig))
writer.close
}
/**
* Given a classpath for wls 10.3.5 returns the classpath with approriate
* changes for wls 10.3.6
*/
def patch_classpath_wls_1035_to_1036(classpath: String): String = {
val removes = List( // remove these jars from the classpath
"/opt/oracle/11g/patch_ocp360/profiles/default/sys_manifest_classpath/weblogic_patch.jar",
"/opt/oracle/11g/patch_wls1035/profiles/default/sys_manifest_classpath/weblogic_patch.jar"
)
val replaces = Map( // replace the 10.3.5.0 jar with 10.3.6.0 jar
"/opt/oracle/11g/modules/features/weblogic.server.modules_10.3.5.0.jar" ->
"/opt/oracle/11g/modules/features/weblogic.server.modules_10.3.6.0.jar"
).withDefault(x => x)
val paths = classpath.split(':').toList.map(_.trim).filter(_.length > 0)
paths.filter(s => !removes.contains(s)).map(replaces).mkString(":")
}
// START OF MAIN SCRIPT
def usage = {
println("usage: scala patch_classpath.scala <configfile>")
sys.exit
}
if (args.length != 1) usage
val configFile = args(0)
patchClassPath(configFile, patch_classpath_wls_1035_to_1036)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment