Monday, March 9, 2009

Grails configuration issue

As far as You know Grails offers many solutions for configuration of Grails application. But most of the configuration is handled by the Config.groovy file in grails-app/conf. I used standard configurations created by default and simply added mail settings in the end of the file as was described in one of my previous posts. But when I tried to read, for instance, grails.serverURL property using grailsApplication.config at controller's layer (and even using ConfigurationHolder at service layer) I've got empty string at runtime. Part of my Config.groovy file is listed below:
environments {
development {
grails.serverURL = "http://localhost:8080"
}
}

// Some other configuration.

grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "youracount@gmail.com"
password = "yourpassword"
props = ["mail.smtp.auth":"true"]
}
}

I was quiet confused cause I still could read configuration not connected with grails, for instance, log4j settings.
As I discovered later problem lied in fact that using grails { (and open bracket) rewrited all previous settings concerning grails. So one of the correct solution might be:
environments {
development {
grails.serverURL = "http://localhost:8080"
grails.mail.host ="smtp.gmail.com"
grails.mail.port = 465
grails.mail.username = "youracount@gmail.com"
grails.mail.password = "yourpassword"
grails.mail.props = ["mail.smtp.auth":"true"]
}
}

In this case settings are added not rewrited. Don't repeat somebody's mistakes. Enjoy!

Grails HTTPS problem

Today I secured some pages in Grails application using HTTPS. But when I tried to start application with using of a generated key store and SSL certificate for HTTPS:
grails run-app-https
I've got an exception:
Could not execute method PluginManagerHolder.
No such property: PluginManagerHolder for class: RunAppHttps_groovy
And application terminated immediately.
As I discovered later problem lied in RunAppHttps.groovy script which is located under GRAILS_HOME\scripts. You have to add missing import in it:
import org.codehaus.groovy.grails.plugins.PluginManagerHolder

That solved the problem. Enjoy!

Sunday, March 8, 2009

Grails GSP concatenation nuance

While working with GSP it is a common situation to perform actions with strings, domain objects or other classes. Recently I faced the simple situation where I needed to dynamically build full file name (with file extension) in order to allow user to download it from file system. In my simple GSP I created the code similar to listed below:



I use concatenation of strings in order to build full file name. But at runtime exception occurred which was very strange and gave me no tip for possible mistake made. As I discovered later in my example You can not use dot in double quotes (".") cause Grails wants to interprete expression in them and fails.
Therefore You can solve mentioned situation in two ways:
  • Use dot in single quotes ('.'). In that case correct solution might be:


  • Move mentioned logic to some separate class (for instance, let's call it DocumentFileNameBuilder) and call it as it listed below:


Simple stupid issue solved :)