Get the ServletContext in Config.groovy (or how to get the real path of the current context)
The questio开发者_运维百科n is in the title - how to obtain the ServletContext
in Config.groovy
. The purpose is to get the real (absolute) path of the current context.
It's not possible to get the ServletContext
there.
It is possible to get the absolute path via an ugly workaround:
def path = getClass().getProtectionDomain().getCodeSource().getLocation()
.getFile().replace(getClass().getSimpleName() + ".class", "").substring(1);
(the substring(1)
removes an unnecessary leading slash)
I did this in Config.groovy
:
def path = getClass().getProtectionDomain().getCodeSource().getLocation().getFile().replace("/WEB-INF/classes/" + getClass().getSimpleName() + ".class", "").substring(1);
path = path.substring(path.lastIndexOf("/") + 1)
println "path: $path ${path}"
def env = System.getenv()
if (!env['ISP_CONFIG']) {
System.err.println 'Environment variable EXTERNAL_CONFIG_DIR is not set.'
} else {
grails.config.locations = [
"file:${env['EXTERNAL_CONFIG_DIR']}/grails/${path}/grails-config.groovy",
"file:${env['EXTERNAL_CONFIG_DIR']}/grails/${path}/DataSource.groovy"
]
}
精彩评论