Reputation: 597076
The question 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.
Upvotes: 1
Views: 2365
Reputation: 21
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"
]
}
Upvotes: 2
Reputation: 597076
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)
Upvotes: 2