Parth Bhagat
Parth Bhagat

Reputation: 509

Grails 3 PluginDescriptor configuration

I am converting existing Grails 2.5.6 project to Grails 3.3.11.

With my existing application (Grails 2.5.6), the plugin descriptor is having code like below:

def doWithApplicationContext = { applicationContext ->
    def config = applicationContext.grailsApplication.config
    def key = config.property.key
    key.put(Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"])
}

This code works fine with earlier version of grails. But after I have upgraded to grails 3.3.11 then it throws exception: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

This is at line:

key.put(Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"])

After looking at the type of key i.e. config.property.key it is showing type as org.grails.config.NavigableMap$NullSafeNavigator.

Which with older version was LinkedHashMap. property.key is set on application.groovy fine under /grails-app/conf/application.groovy property.key = [:]

I've also tried setting type of property.key in plugin descriptor externally to java.util.HashMap. But it seems like not adopting new type.

What I am doing wrong here?

Upvotes: 0

Views: 328

Answers (2)

Parth Bhagat
Parth Bhagat

Reputation: 509

Thank you @Jeff for your input and suggestion.

Here is the consolidated code for setting config parameters and retrieve them in controller or any other grails component.

Plugin Descriptor:

class ResultGrailsPlugin extends Plugin {
   void doWithApplicationContext() { 
      config.merge(['property': ['key': ["${Constants.RESULT_CONST}": [controller: "results", action: "showData", templatePath: "/results/data"]]]])
   }
}

Controller:

class ResultController {
   def index() {
      def resultConfigMap = grailsApplication.config.get('property.key.' + Constants.RESULT_CONST)
      ...
   }
}

Upvotes: 0

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27255

Instead of trying to do this dynamically with this:

def doWithApplicationContext = { applicationContext ->
    def config = applicationContext.grailsApplication.config
    def key = config.property.key
    key.put(2, [controller: "results", action: "showData", templatePath: "/results/data"]) 
}

You could define those values in grails-app/conf/plugin.yml like this:

---
property:
  key:
    '2':
      controller: results
      action: showData
      templatePath: '/results/data`

EDIT

The question has changed such that the above is no longer valid.

Instead of doing this:

def config = applicationContext.grailsApplication.config
def key = config.property.key
key.put(Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"])

You can simplify that to this:

config.merge([property: [key: [Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"]]]])

Upvotes: 2

Related Questions