开发者

Grails External Config Read Incorrectly on First Load

Grails 1.3.7

I have some configuration located in an external config file. One of the entires looks like this:

site.maintenance.mode = false

I have a filter which checks for certain config settings for specific URLs. When I do a run-app or deploy a WAR into Tomcat and do:

boolean maintenanceMode = grailsApplication.config.site.maintenance.mode

maintenanceMode is coming back true. If I look at the config object in debug mode, this is what I get:

site={maintenance={mode=false, message="<p>Our trail guides are working hard to get the system back on track.</p><p>We're sorry, the account system is down for maintenance开发者_如何学运维 at the moment.  We'll get it back online as quickly as we can.  Thanks for your patience.</p>"}}

I have a controller that I use to reload this config file dynamically and hitting this controller will fix the issue. But I'm curious as to why it is incorrect on first runs and why the discrepency in what is getting put in the maintenanceMode variable vs what is actually in the config object.


Are you using a Java properties file or a Groovy file? If you're using a properties file then I believe Grails will interpret site.maintenance.mode=false the same way as site.maintenance.mode='false' and since Groovy will interpret:

"false".asBoolean() == true

then that would explain why you would see that initial true value.

I just ran a simple test locally to verify this behavior. When I externalize my properties in a file called test.properties then site.maintenance.mode=false initially gets a boolean value of true, when I use a file called test.groovy then it interprets the boolean value of site.maintenance.mode=false as false. I believe this is because when you use a Groovy file Grails uses ConfigurationSlurper to process it but when you use a properties file Grails interprets everything as String name/value pairs.


What I do is to have an external Config.groovy file, for instance: MyConfig.groovy

At the end of the standard grails Config.groovy file, I have the following:

def ENV_NAME = "MY_EXTERNAL_CONFIG"
if(!grails.config.locations || !(grails.config.locations instanceof List)) {
    grails.config.locations = []
}
if(System.getenv(ENV_NAME)) {
    grails.config.locations << "file:" + System.getenv(ENV_NAME)
} else if(System.getProperty(ENV_NAME)) {
    grails.config.locations << "file:" + System.getProperty(ENV_NAME)
} else {
    println "No external Configs found."
}

So now you can have a MyConfig.groovy file anywhere in production environment (for example) and then set an Environment system variable to point to this file (or pass it as parameter to startup.sh), before you start tomcat:

MY_EXTERNAL_CONFIG="/home/tomcat/configs/MyConfig.groovy"
export MY_EXTERNAL_CONFIG

That's it. Now you have an external MyConfig.groovy file. The properties in it are accessible from your grails app as they were part of the standard Config.groovy

import org.codehaus.groovy.grails.commons.*
//... 
ConfigurationHolder.config.foo.bar.hello
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜