2011-09-21 51 views
0

我有像这样Config.groovy中一个JNDI条目:如何通过用户名/密码命令行选项来JNDI grails.naming.entries在Config.grooy

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", 
    driverClassName: "oracle.jdbc.driver.OracleDriver", 
    maxActive: "8", //and so on 
     maxIdle: "4" 
    ] 
] 

这工作得很好,但我不希望将用户名/密码存储在Config.groovy源文件中。有没有办法将凭据从命令行选项-Duser = someuser -Dpass-somepassword传递给Config.groovy中的grails.naming.entries?

回答

0

最好的办法是使用外部存储的配置设置。

这允许Grails在生产(或测试或开发)服务器的独特设置中加载,这些设置不存储在grails应用程序WAR中。另一个好处是这些可以在不更换任何代码的情况下更新,只需重新启动服务器上的应用程序即可。从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." 
} 

现在环境变量MY_GREAT_CONFIG设置为外部常规配置的绝对路径。查看更完整示例的链接。

0

看起来通过grails.config.locations添加的任何选项在Config.groovy中不可用。 “$ {System.getProperty('password')}”。toString()是这个工作的唯一方法。 这里是我的测试结果:

添加在Config.groovy中的开头:

if (new File("${userHome}/.grails/${appName}-config.groovy").exists()){ 
    grails.config.locations = ["file:${userHome}/.grails/${appName}-config.groovy"] 
} 

添加在Config.groovy中结束:的user.home /的

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

内容。 grails/mnroad-config.groovy:

testPassword='some_password' 

以下是使用-Dpassword = somePas剑:

(*) grails.config.locations = [file:C:\Documents and Settings\carr1den/.grails/mnroad-config.groovy] 
testPassword=some_password 
test password: [:] 
somePassword 

grailsApplication.config.testPassword选项在应用程序初始化后可用。

相关问题