title: Set datasource values during run time
body: <p>So essentially I'm trying to get my project up and running on AppFog. The datasource information is stored in an enviornment variable, which is essentially JSON. My goal is to take this data and set my datasource config from it.</p>
  
  <p>Here is what I have tried:</p>
  
  <p>Code to set the datasource config which is a method in a POGO. The POGO is instantiated and the method called at the beginning of DataSource.groovy:</p>
  
  <pre><code>import appfog.ParseDataSource
  new ParseDataSource().setConfig()
  
  dataSource {
  ...
  }
  
  class ParseDataSource {
  
      void setConfig() {
          String env = java.lang.System.getenv("VCAP_SERVICES")
          if (env) {
              def config = JSON.parse(env)
              config = config["mysql-5.1"][0].credentials
              grailsApplication.config.environments.production.dataSource.username = config.username
              grailsApplication.config.environments.production.dataSource.password = config.password
              grailsApplication.config.environments.production.dataSource.url = "jdbc:mysql://" + config.host + ":" + config.port + "/" + config.name
          }
      }
  }
  </code></pre>
  
  <p>The problem is that grailsApplication is always null. I've tried registering a spring bean in resources.groovy:</p>
  
  <pre><code>beans = {
      parseDataSource(appfog.ParseDataSource) {
          grailsApplication = ref('grailsApplication')
      }
  }
  
  class ParseDataSource {
      def grailsAPplication
      ...
  }
  </code></pre>
  
  <p>I've also tried getting it via Holders:</p>
  
  <pre><code>    GrailsApplication grailsApplication = Holders.grailsApplication
  </code></pre>
  
  <p>Either way it is null so I'm not doing something right. Any ideas?</p>