1

I am currently updating my Grails project in order not to use the deprecated ConfigurationHolder class. This goes fine in most cases, but I am facing trouble in my custom codec classes, where I have been using the following approach until now:

import org.codehaus.groovy.grails.commons.ConfigurationHolder as CH
class MyCodec {

    static boolean myStaticConfigProperty=CH.config.myStaticConfigProperty

    static encode = { something ->
       if(myStaticConfigProperty)
          ...
    }

}

Direct injection using

def grailsApplication 

does not work in this case since this will be injected as a non-static object.

Instead I have tried to use the approach suggested in this post getting-grails-2-0-0m1-config-info-in-domain-object-and-static-scope, but I cannot make it work even after injecting the grailsApplication object into my codec metaclasses in the bootstrap:

class BootStrap {

   def grailsApplication

   def init = { servletContext ->
      for (cc in grailsApplication.codecClasses) {
         cc.clazz.metaClass.getGrailsApplication = { -> grailsApplication }
         cc.clazz.metaClass.static.getGrailsApplication = { -> grailsApplication }
      }      
   }
}

Could anyone suggest an approach that will allow me to access the config object in a static way inside codec classes?

Community
  • 1
  • 1
John Doppelmann
  • 544
  • 4
  • 15

2 Answers2

2

I'd suggest something like this completely untested code:

class MyCodec {

    static def grailsConfig

    static boolean myStaticConfigProperty = grailsConfig.myStaticConfigProperty

    static encode = { something ->
       if(myStaticConfigProperty)
          ...
    }

}

class BootStrap {

   def grailsApplication

   def init = { servletContext ->
      for (cc in grailsApplication.codecClasses) {
         cc.grailsConfig = grailsApplication.config 
      }      
   }
}

If all of your codec classes just need the same one configuration property, you could skip injecting the grailsApplication and/or the config object entirely, and just set the one static property from BootStrap.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • I tried your approach, but it does not seem to work. I believe the reason is that the MyCodec obejct is initialised before the Bootstrap.init() is run. Thereby the line "static boolean myStaticConfigProperty = grailsConfig.myStaticConfigProperty" seems to be the reason for failing. – John Doppelmann May 21 '12 at 11:43
  • So, you're probably going to have to make the property non-static. Here's an example that looks like it does what you want. http://www.zorched.net/2008/02/03/encryption-codecs-and-unit-tests-in-grails/ – GreyBeardedGeek May 23 '12 at 10:40
  • A bit late at following up. I tried your suggested approach, but could not make it work. Ended up using this approach instead. It works:http://burtbeckwith.com/blog/?p=993 – John Doppelmann Jul 02 '12 at 16:19
1

it works for me in grails 2.2.3

import grails.util.Holders as holders;

class MyFileCodec {
    static encode = {file ->
        def configPath= holders.grailsApplication.config.share.contextPath
        return "${configPath}/${file.name}"
    }
}

grails.util.Holders has been introduced since grails 2.0, it's the way to access config object.

ressol
  • 31
  • 2