找不到 Vaadin 类资源

发布于 2024-10-20 14:20:17 字数 2141 浏览 3 评论 0原文

我正在尝试使用 Vaadin ClassResource 类从将随 WAR 部署的文件加载数据库连接属性,但我似乎找不到该文件。我使用的是 Vaadin 6.5.2、Tomcat 7.0.6,并且我已将“app.properties”文件放在与应用程序主文件相同的包中。

我的代码是在 Scala 中。这是我尝试的:

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 文件夹的顶部以及它当前所在的位置(与主应用程序),但结果总是相同的。谁能告诉我我做错了什么???

I'm trying to use the Vaadin ClassResource class to load my database connection properties from a file that will be deployed with the WAR, but I can't seem to find the file. I am using Vaadin 6.5.2, Tomcat 7.0.6, and I've placed my "app.properties" file in the same package as my Application main file.

My code is in Scala. Here's what I try:

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")
}

And here is the result:

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

I've tried placing the config file in various locations including the top of src, inside the top of the VAADIN folder that holds themes, and where it currently is (in the same package as the main Application), but the results are always the same. Can anyone tell me what I'm doing wrong???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

属性 2024-10-27 14:20:17

我们就是这样做的。

        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 应用程序包装器。

This is how we do it.

        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);

However to be fair, we don't create wars and keep our application unexploded.
Keep in mind that Application.class is not the Vaadin application but our own wrapper around the Vaadin application.

千鲤 2024-10-27 14:20:17

我将 .properties 文件复制到应用程序上下文目录中,并按照以下方式读取它:

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

从应用程序类调用上面的代码。

I copied .properties file into application context directory and read it in following way

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

Called above code from application class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文