How pass username/password command line options to jndi grails.naming.entries in Config.grooy
I have a jndi entry in Config.groovy like so:
grails.naming.entries = ['jdbc/test_me': [
type: "javax.sql.DataSource", //required
auth: "Container", // optional
description: "Data source for ...", //optional
//properties for particular type of resource
url: "jdbc:oracle:thin:@testserver:1521:SID",
username: "someuser",
password: "somepassword",
driverClassNam开发者_Python百科e: "oracle.jdbc.driver.OracleDriver",
maxActive: "8", //and so on
maxIdle: "4"
]
]
This works fine but I do not want to store the username/password in the Config.groovy source. Is there a way to pass the credential from command line options, -Duser=someuser -Dpass-somepassword, to grails.naming.entries in Config.groovy?
Your best bet is to use externally stored configuration settings.
This allows Grails to load in unique settings for the production (or test or dev) server, that are not stored within the grails application WAR. The other nice thing is that these can be updated without replacing any code, just restart the application on the server.
Example from this great article on the subject:
// Put this at the top of your Config.groovy
// Copied from http://blog.zmok.net/articles/2009/04/22/playing-with-grails-application-configuration
if(System.getenv("MY_GREAT_CONFIG")) {
println( "Including configuration file: " + System.getenv("MY_GREAT_CONFIG"));
grails.config.locations << "file:" + System.getenv("MY_GREAT_CONFIG")
} else {
println "No external configuration file defined."
}
Now set the environment variable MY_GREAT_CONFIG
to the absolute path for the external groovy config. See the link for a more complete example.
It appears any options added via grails.config.locations are not available in Config.groovy. "${System.getProperty('password')}".toString() is the only way this worked. Here are my test results:
Added at the beginning of Config.groovy:
if (new File("${userHome}/.grails/${appName}-config.groovy").exists()){
grails.config.locations = ["file:${userHome}/.grails/${appName}-config.groovy"]
}
Added at the end of Config.groovy:
println "(*) grails.config.locations = ${grails.config.locations}"
def f = new File("${userHome}/.grails/${appName}-config.groovy")
f.eachLine{ line -> println line }
println "test password: ${testPassword}" // same result ([:]) with grails.config.testPassword
println "${System.getProperty('password')}"
grails.naming.entries = ['jdbc/test_mnr': [
type: "javax.sql.DataSource", //required
auth: "Container", // optional
description: "Data source for ...",
url: "jdbc:oracle:thin:@server:1521:SID",
username: "username",
password: "${System.getProperty('password')}".toString(),
driverClassName: "oracle.jdbc.driver.OracleDriver",
maxActive: "8",
maxIdle: "4",
removeAbandoned: "true",
removeAbandonedTimeout: "60",
testOnBorrow: "true",
logAbandoned: "true",
factory: "org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory",
validationQuery: "select count(*) from dual",
maxWait: "-1"
]
]
Content of user.home/.grails/mnroad-config.groovy:
testPassword='some_password'
Here is the result when run with -Dpassword=somePassword :
(*) grails.config.locations = [file:C:\Documents and Settings\carr1den/.grails/mnroad-config.groovy]
testPassword=some_password
test password: [:]
somePassword
The grailsApplication.config.testPassword option is available after the app initializes.
精彩评论