Bozho
Bozho

Reputation: 597076

Get the ServletContext in Config.groovy (or how to get the real path of the current context)

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

Answers (2)

Bram
Bram

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

Bozho
Bozho

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

Related Questions