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選項在應用程序初始化後可用。