There can be multiple approaches, and here is the groovy script with one of the way to do so:
/**This script reads the connections file and retrieves the data
* as requested by the user
**/
//Read connections xml file, change path as per your environment
def xml = new File('/tmp/connections.xml').text
//Provide the environment, and connection name below
//You can use different values for env and connName for different connections
def env = 'test'
def connName = 'wba'
def connStrings = new XmlSlurper().parseText(xml)
//Call the below defined methods
def testConnectionDetails = getConnectionDetails(connStrings, env, connName)
showConnectionDetails(testConnectionDetails, env, connName)
//Generate the connection string
def connStr = getConnectionString(testConnectionDetails)
log.info "Connection String is : ${connStr}"
/**
* this method will return the connection for the details provided
**/
def getConnectionDetails(def connectionStrings, String environmentName, String connectionName) {
def envronment = connectionStrings.'**'.find{it.@name == environmentName}
envronment.'**'.find{ it.name() == 'connName' && it == connectionName}.parent()
}
/**
* this method will show the details provided in the connection
**/
def showConnectionDetails(def connexionDetails, String environmentName, String connectionName){
log.info "Environment : ${environmentName}, Connection name : ${connectionName}"
log.info "Details followed: "
log.info "Host : ${connexionDetails.host}"
log.info "Port : ${connexionDetails.port}"
log.info "User : ${connexionDetails.user}"
log.info "Password: ${connexionDetails.password}"
log.info "Sid : ${connexionDetails.sid}"
}
/**
* This creates connection string and there is optional parameter prefix(which is for oracle)
* Change value for prefix otherwise
* Output format is : jdbc:oracle:thin:scott/tiger@myhost:1521:orcl
**/
def getConnectionString(def connexionDetails, def prefix='jdbc:oracle:thin:') {
"${prefix}${connexionDetails.user}/${connexionDetails.password}@${connexionDetails.host}:${connexionDetails.port}:${connexionDetails.sid}" as String
}
Not sure if you still need the conn count. Hope it may not needed anymore.