{"id":1952,"date":"2010-10-15T01:25:26","date_gmt":"2010-10-14T19:55:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1952"},"modified":"2010-10-15T01:25:26","modified_gmt":"2010-10-14T19:55:26","slug":"externalize-and-reload-grails-configuration-dynamically","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/externalize-and-reload-grails-configuration-dynamically\/","title":{"rendered":"Externalize and reload grails configuration dynamically"},"content":{"rendered":"<p>Configuration file plays great role in grails. You can store application specific configuration at one place. One of the grails goodies is that you can externalize configuration file i.e. store them at a location so that it is not packed with the war file.<br \/>\n<\/p>\n<p>Here is the part of Config.groovy that describes how to specify external configuration file :<\/p>\n<p>[groovy]<br \/>\n\/\/ configuration files can either be Java properties files or ConfigSlurper scripts.<br \/>\n\/\/Note that the file whose path is specified below is not included in war file.<\/p>\n<p>grails.config.locations = [&quot;file:${userHome}\/.myConfigurations\/${appName}-config.groovy&quot;]<br \/>\n[\/groovy]<\/p>\n<p>Externalizing the configuration helps us to change the configuration without re-creating the war file. But the problem I faced was changes to this external configuration file was not reloaded by grails automatically. I found the code to reload the configuration in <a href=\"http:\/\/grails.org\/plugin\/quartz\" target=\"_blank\">Grails Quartz plug-in<\/a> source code. Here is the code snippet.<\/p>\n<p>[groovy]<br \/>\n        def config = ConfigurationHolder.config<br \/>\n        GroovyClassLoader classLoader = new GroovyClassLoader(getClass().classLoader)<\/p>\n<p>        \/\/ merging default Quartz config into main application config<br \/>\n        config.merge(new ConfigSlurper(GrailsUtil.environment).parse(classLoader.loadClass(&#8216;DefaultQuartzConfig&#8217;)))<br \/>\n[\/groovy]<\/p>\n<p>However the code is reloading configuration from a groovy class(it should be in the class-path) that I could not use in my use case.<\/p>\n<p>Doing some googling and thanks to <a href=\"http:\/\/www.tothenew.com\/blog\/author\/himanshu\/\" target=\"_blank\">Himanshu<\/a> for useful code snippets, I found the constructor that accepts String . I used the following code to merge the changed external configuration file with the configuration of running application :<\/p>\n<p>[groovy]<br \/>\ndef config = ConfigurationHolder.config<br \/>\ndef locations = config.grails.config.locations<\/p>\n<p>locations.each {<br \/>\n\tString configFileName = it.split(&quot;file:&quot;)[1]<br \/>\n\tconfig.merge(new ConfigSlurper().parse(new File(configFileName).text))<br \/>\n}<br \/>\n\/\/above code iterates over all the external config files and<br \/>\n\/\/merges them with the running application configuration<br \/>\n[\/groovy]<\/p>\n<p>I wrote the above code inside a controller-action. Now whenever I change the external configuration file I simply hit the URL of the controller-action and the changes take effect that point onward. You can also use <a href=\"http:\/\/grails.org\/plugin\/console\" target=\"_blank\">Grails Console plug-in <\/a> that lets you execute groovy code from the running application or use <a href=\"http:\/\/grails.org\/plugin\/quartz\" target=\"_blank\">Grails Quartz Job<\/a> to periodically reload the external configuration.<br \/>\n<br \/>\nHope this helps you !<br \/>\n<br \/><strong><br \/>\n<em>Bhagwat Kumar<br \/>\nbhagwat(at)intelligrape(dot)com<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reloading configuration from external Configuration files without restarting the web container and putting the configuration file external to war file. I Used config.merge and new ConfigSlurper().parse( ) to merge the external configuration into the running application&#8217;s configuration<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":45},"categories":[7],"tags":[434,435,436,437,433],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1952"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1952"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1952\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}