wishihadabettername
wishihadabettername

Reputation: 14751

Grails: can settings in Config.groovy be overridden programmatically at runtime?

The mail plugin is documented to require its settings in Config.groovy. I want to have it stored in the database so it can be changed at runtime. And this is just one example.

I've read Does externalizing sensitive config variables into a .properties outside of Config.groovy provide a security advantage in Grails? but this is about using an external property file, I need to store the settings in the database (the app has an admin interface, with auditing and so on).

Thank you.

Upvotes: 4

Views: 6644

Answers (3)

Piyush Chaudhari
Piyush Chaudhari

Reputation: 1012

First way to change value dynamic:

grailsApplication.config.grails.mail.put("port",your database fetch value of port)
grailsApplication.config.grails.mail.put("username","your database fetch value of username")
grailsApplication.config.grails.mail.put("password","your database fetch value of username")
grailsApplication.config.grails.mail.put("props","your database fetch value of username")

second way to change value dynamic:

org.codehaus.groovy.grails.commons.DefaultGrailsApplication

DefaultGrailsApplication class provide the all the information about config. you can get email information using following code and change it dynamic.

def defaultGrailsApplication= new DefaultGrailsApplication()

// to get mails information from Config.groovy
defaultGrailsApplication?.config?.grails.mail?.get("host")
defaultGrailsApplication?.config?.grails.mail?.get("port")
defaultGrailsApplication?.config?.grails.mail?.get("username")
defaultGrailsApplication?.config?.grails.mail?.get("password")
defaultGrailsApplication?.config?.grails.mail?.get("props")


// to change mails information of Config.groovy
defaultGrailsApplication?.config?.grails.mail?.put("host","your database fetch value of host")
defaultGrailsApplication?.config?.grails.mail?.put("port",your database fetch value of port)
defaultGrailsApplication?.config?.grails.mail?.put("username","your database fetch value of username")
defaultGrailsApplication?.config?.grails.mail?.put("password","your database fetch value of username")
defaultGrailsApplication?.config?.grails.mail?.put("props","your database fetch value of username")

Now you need to assign this "defaultGrailsApplication" instance to inject value of application. just assume that we have one class service Temp.service having grailsApplication injected like:

Temp.service

class Temp{
  def grailsApplication

  def your method(){
         please add above code for set value
         grailsApplication = defaultGrailsApplication
  }

}

Upvotes: 1

Dónal
Dónal

Reputation: 187499

In Bootstrap.groovy you can read the property values from the database and replace those read from the config files by updating grailsApplication.config. For example, assume you want to replace the value of a config property named foo.bar

class BootStrap {

    GrailsApplication grailsApplication

    def init = { servletContext ->

        def fooBarDB = // read the value of foo.bar from the database
        grailsApplication.config.foo.bar = fooBarDB
    }
}

Rather than writing this code yourself, you could instead use the Dynamic Config Plugin to achieve the same outcome.

Upvotes: 7

Jeff Storey
Jeff Storey

Reputation: 57192

Yes, you can do this. During the grails bootstrap process using Bootstrap.groovy, retrieve the properties from the database and override them. It's similar to a properties file override, you're just storing them in a database.

Also see Properties in the database

Upvotes: 2

Related Questions