3

I am new to groovy. and looking for a way to store common configs at one place so my application can access it.

I am keeping configurations for my groovy applications in WEB-INF, how can I access the file from my main application?

I tried this ConfigSlurper().parse(new File("config.groovy").toURL()), but can't get it working, because the path where tomcat is expecting the config.groovy is where tomcat is installed. I have my appBase configured to some other location.

How do I give relative path to new File('') so that it can read the config file?

Salman
  • 9,299
  • 6
  • 40
  • 73

5 Answers5

2

You want to do this using the ClassLoader and not the filesystem directly. Basically, don't use File. I'm not sure how to do this in Groovy, but in plain-old Java it would look like this:

URL config = request.getServletContext().getResource("/WEB-INF/config.groovy");

... then do whatever you want with the URL. (FYI the getResource method uses the ClassLoader of the webapp). So, maybe in Groovy that might be:

ConfigSlurper().parse(request.ServletContext.getResource("/WEB-INF/config.groovy"))

Sorry I can't give you the exact Groovy syntax... not my world. :)

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • Thnx, I figured out, its `getClass().getResource("file")`. – Salman Oct 05 '13 at 05:53
  • It would be better to use the `ServletContext` since it will definitely give you the correct `ClassLoader`. `getClass()` will get you the `ClassLoader` of the servlet class which *should* be that of the context, but containers can play games with class loading and so obtaining the `ClassLoader` from the `ServletContext` is the safest bet. – Christopher Schultz Oct 07 '13 at 16:07
1

If your config must be set at deployment time, you can put the files in your war and access them with classloader.

For example, in a standard maven project, put your config files in src/main/resources/config/ directory (file1.config, file2.config. etc.). Then in your java code put the following

public class MyConfigLoader {

   void loadConfigFile(String configFileName) {
      InputStream is = MyConfigLoader.class.getClassLoader().getResourceAsStream("config/"+configFileName);
      ...
   }
}
  • Thanks, isn't there any option by which I can just include the config once and use it in all groovy files? I mean without the need to create a function to read the file and use `ConfigSlurper`. Can't I just use the way `grails` provie configs? Somewhat like [this answer](http://stackoverflow.com/a/198541/1520671) – Salman Oct 04 '13 at 11:26
1

For me this works every time

Inject ‘grailsApplication’ in your BoosStrap, Controllers, TagLibs and Services:

def grailsApplication

Place your files in /web-app/WEB-INF/resources and use this line of code to retrieve the directory of your resources:

def resources = grailsApplication.mainContext.getResource('/WEB-INF/resources').file

works both in development and production

Amit
  • 2,080
  • 3
  • 19
  • 24
0

I would recommend to move the configuration file from WEB-INF to the grails-app/conf folder. This folder is intended for the configuration files...

Grails allows you to easily import your custom config file by editing the grails.config.locations variable in Config.groovy. See http://grails.org/doc/latest/guide/conf.html#configExternalized

If the name of your config file is customConfig.groovy and you moved it to grails-app/conf you could use:

grails.config.locations = [ customConfig]
0
    def config = new ConfigSlurper().parse(servletContext.getResource("/WEB-INF/config.groovy"))

this works for all environments.

hemantgp
  • 352
  • 3
  • 7