开发者

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"
    ]
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜