简体   繁体   中英

groovy MissingMethodException - parsing through ConfigSlurper

I am trying to parse through some properties file using configslurper.

ENT.adminserver.nodenumber=1
ENT.managedserver.1.host=vserver04
ENT.managedserver.2.host=vserver05
ENT.managedserver.3.host=vserver08
ENT.managedserver.4.host=vserver07

Said properties file. I am trying to read the host names from the properties.

Properties properties = new Properties()
File propertiesFile = new File('DomainBuild.properties')
propertiesFile.withInputStream {properties.load(it)}
def config = new ConfigSlurper().parse(properties)

    def domainname="ENT" //will be passed through paremeters
    def domain = config.get(domainname)
    def managedServerFlow= {
      println domain.managedserver
      println domain.managedserver.keySet()
      domain.managedserver.each { 
        println it.getClass()
        println it.get("1") 
      }

      for (server in domain.managedserver) {
        println server.getClass()
        println server
      }
    }
}

the it.get("1") is causing the following error.

No signature of method: java.util.LinkedHashMap$Entry.get() is applicable for argument types: (java.lang.String) values: [1]
Possible solutions: getAt(java.lang.String), grep(), grep(java.lang.Object), wait(), getKey(), any()

I looked through the java and groovy doc and spent few hours without resolution. Please help.

Instead of

println it.get("1") 

Try

println it.'1'

Or

println it.getAt("1") // as the exception shows you

Think about what types you are working with. config is a ConfigObject , which you can treat like a map. Its sub-objects domain and domain.managedserver are also ConfigObjects. When you call each on domain.managedserver and pass it a closure that takes no parameters, it gives you a set of Entries. Therefore you can't call it.get("1") because an Entry doesn't have a property called "1". It has key and value . So you can either println "$it.key: $it.value" or

  domain.managedserver.each { key, value ->
    println value.getClass()
    println "$key: $value"
  }

or if you want to get the value for key "1" directly:

  println domain.managedserver.'1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM