2011-03-07 53 views
0

我正嘗試使用Vaadin ClassResource類從將部署WAR的文件加載數據庫連接屬性,但似乎無法找到該文件。我使用Vaadin 6.5.2,Tomcat 7.0.6,並將我的「app.properties」文件放在與我的應用程序主文件相同的包中。找不到Vaadin ClassResource

我的代碼是在斯卡拉。這是我嘗試:

val cr = new ClassResource("app.properties",this) // "this" is the application 
debug("resource mimeType = {}",cr.getMIMEType) 
debug("resource bufferSize = {}", cr.getBufferSize) 
debug("resource cacheTime = {}",cr.getCacheTime) 
debug("resource fileName = {}", cr.getFilename) 
val ds = cr.getStream 
if (ds != null) { 
    debug("download stream bufferSize = {}", ds.getBufferSize) 
    debug("download stream cacheTime = {}",ds.getCacheTime) 

    val is = ds.getStream // get InputStream 

    if (is != null) { 
    val props = new Properties 
    props.load(is) 
    val dbHost = props.get("db.host").asInstanceOf[String] 
    val dbName = props.get("db.name").asInstanceOf[String] 
    val dbPort = props.get("db.port").asInstanceOf[String] 
    val dbUser = props.get("db.user").asInstanceOf[String] 
    val dbPass = props.get("db.pass").asInstanceOf[String] 
    val dbUri = props.get("db.uri").asInstanceOf[String] 
    } else { 
    debug("Input stream was null") 
    } 
} else { 
    debug("Download stream was null") 
} 

這裏是結果:

08:51:59.617 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - resource mimeType = application/octet-stream 
08:51:59.620 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - resource bufferSize = 0 
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - resource cacheTime = 86400000 
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - resource fileName = app.properties 
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - download stream bufferSize = 0 
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - download stream cacheTime = 86400000 
08:51:59.621 ["http-bio-8084"-exec-11] DEBUG c.sentientswarm.propdesk.AppConfig$ - Input stream was null 

我試圖把在不同的地點,包括SRC的頂部配置文件中,VAADIN文件夾頂部內部的持有主題和目前位置(與主應用程序在同一個包中),但結果始終相同。任何人都可以告訴我我做錯了什麼?

+0

你用什麼來建立戰爭檔案? – thoredge 2011-03-08 12:49:01

回答

1

這就是我們的做法。

 InputStream is=null; 
    try 
    { 
     is=Application.class.getClassLoader().getResourceAsStream("Application.properties"); 
    } 
    catch(Exception x) 
    { 
     log.error("Error loading 'Application.properties' properties",x); 
     return null; 
    } 

    if (is!=null) 
    { 
     try 
     { 
      Properties props=new Properties(); 
      props.load(is); 
      return(props); 
     } 
     catch (IOException e) 
     { 
      log.error("Error reading properties 'Application.properties' ",e); 
     } 
    } 

    return(null); 

但是公平起見,我們不創建戰爭,並保持我們的應用程序未爆炸。 請記住,Application.class不是Vaadin應用程序,而是我們自己的Vaadin應用程序包裝器。

0

我複製的.properties文件到應用程序上下文目錄,並按照上面的應用程序類代碼中調用的方式

Properties properties = new Properties(); 
properties.load(new FileInputStream(getContext().getBaseDirectory().getAbsolutePath() + "/application.properties")); 

閱讀。

相關問題