简体   繁体   English

在Jboss Fuse / Karaf中从文件系统加载属性文件会抛出Nullpointer

[英]Loading Property File from Filesystem in Jboss Fuse / Karaf throws Nullpointer

I try to load a property file in Java running on JBossFuse/karaf. 我尝试在JBossFuse / karaf上运行的Java中加载属性文件。

The file is located at $[karaf.home]/etc/bean.properties 该文件位于$ [karaf.home] /etc/bean.properties中

The Code is able to load properties inside the bundle fine, but now I try to exclude the properties from the project itself and the code throws a Nullpointer-Exception. 代码可以很好地将属性加载到包中,但是现在我尝试从项目本身中排除属性,并且代码引发Nullpointer-Exception。

The Path is properly resolved on my development machine as 路径已在我的开发机器上正确解析为

C:\\Users\\someone\\devstudio\\runtimes\\jboss-fuse-6.3.0.redhat-135\\etc\\bean.properties C:\\ Users \\ someone \\ devstudio \\ runtimes \\ jboss-fuse-6.3.0.redhat-135 \\ etc \\ bean.properties

The property-File can be loaded in the blueprint-XML to configure beans, but to access the bean my code needs the CamelContext. 可以将属性文件加载到blueprint-XML中以配置bean,但是要访问bean,我的代码需要CamelContext。 As I have some static codeblocks that are accessed without an exchange/context/registry, I also wanted to be able to load the properties in Java. 由于我有一些无需交换/上下文/注册表即可访问的静态代码块,因此我也希望能够在Java中加载属性。

Both the functions throw the NullPointerException and I guess, it is because the code runs in Fuse. 这两个函数都抛出NullPointerException,我想这是因为代码在Fuse中运行。

public static Properties getProperties(String location) {
    Properties prop = new Properties();
    InputStream input = null;

    try {
        input = PropertyLoader.class.getClassLoader().getResourceAsStream(location);
        prop.load(input);
    } catch (IOException ex) {
        log.error("Error loading properties file from: " + location, ex);
        return null;
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }
    return prop;
}

public static Properties getPropertiesFromFilesystem(String location) {
    Properties prop = new Properties();
    InputStream input = null;

    try {
        input = new FileInputStream(location);
        prop.load(input);
    } catch (IOException ex) {
        log.error("Error loading properties file from: " + location, ex);
        return null;
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                log.error(e);
            }
        }
    }
    return prop;
}

The Exception: 例外:

java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434)[:1.8.0_91] at java.util.Properties.load0(Properties.java:353)[:1.8.0_91] at java.util.Properties.load(Properties.java:341)[:1.8.0_91] at com.mycompany.util.PropertyLoader.getProperties(PropertyLoader.java:19)[319:camel-archetype-blueprint:0.0.14] at com.mycompany.camel.blueprint.MyProcessor.process(MyProcessor.java:21)[319:camel-archetype-blueprint:0.0.14] at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:or java.util.Properties $ LineReader.readLine(Properties.java:434)[:1.8.0_91]处的java.lang.NullPointerException java.util.Properties.load0(Properties.java:353)[:1.8.0_91] java.util.Properties.load(Properties.java:341)[:1.8.0_91],位于com.mycompany.util.PropertyLoader.getProperties(PropertyLoader.java:19)[319:camel-archetype-blueprint:0.0.14]在com.mycompany.camel.blueprint.MyProcessor.process(MyProcessor.java:21)[319:camel-archetype-blueprint:0.0.14]在org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63 )[231:org.apache.camel.camel-core:2.17.0.redhat-630135]在org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)[231:org.apache.camel。骆驼核心:2.17.0.redhat-630135],位于org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468)[231:org.apache.camel.camel-core:2.17.0.redhat- 630135],网址为org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:or g.apache.camel.camel-core:2.17.0.redhat-630135] at org.apache.camel.processor.Pipeline.process(Pipeline.java:121)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:192)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:76)[231:org.apache.camel.camel-core:2.17.0.redhat-630135] at java.util.TimerThread.mainLoop(Timer.java:555)[:1.8.0_91] at java.util.TimerThread.run(Timer.java:505)[:1.8.0_91] g.apache.camel.camel-core:2.17.0.redhat-630135]在org.apache.camel.processor.Pipeline.process(Pipeline.java:121)[231:org.apache.camel.camel-core: org.apache.camel.processor.Pipeline.process(Pipeline.java:83)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]的2.17.0.redhat-630135] .apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]在org.apache.camel.component.timer.TimerConsumer .sendTimerExchange(TimerConsumer.java:192)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]在org.apache.camel.component.timer.TimerConsumer $ 1.run(TimerConsumer.java: 76)java.util.TimerThread.mainLoop(Timer.java:555)[:1.8.0_91]的[231:org.apache.camel.camel-core:2.17.0.redhat-630135] .run(Timer.java:505)[:1.8.0_91]

Any help would be highly appreciated. 任何帮助将不胜感激。

Do not do that. 不要那样做。 You are looking for trouble. 您正在寻找麻烦。

  1. Load properties the OSGi way (use .cfg as extension and a blueprint property-placeholder bean) 以OSGi方式加载属性(使用.cfg作为扩展名和一个blueprint property-placeholder bean)
    You have the added benefit of getting notified if the file changes (if you wish) 如果文件发生更改,您还具有获得通知的额外好处(如果您愿意)
  2. Inject them in a bean EVEN IF you are using only static methods. 即使仅使用静态方法,也要将它们注入bean。
    Don't mix managed beans with unmanaged static code unless you know very well what you are doing. 除非您非常了解自己在做什么,否则不要将托管bean与非托管静态代码混合使用。

If some " static " code requires properties means that it is stateful , and this class deserves to be instantiated to a bean. 如果某些“ 静态 ”代码需要属性,则意味着它是有状态的 ,并且该类应实例化到bean。

Not sure why you are getting an NPE without a more complete example. 不知道为什么没有更完整的示例就得到了NPE。 If you need to use properties without a route, you should be using Camel's property placeholder facilities: 如果您需要使用没有路由的属性,则应使用Camel的属性占位符功能:

https://access.redhat.com/documentation/en-us/red_hat_jboss_fuse/6.3/html/apache_camel_development_guide/basicprinciples#BasicPrinciples-PropPlaceholders https://access.redhat.com/documentation/zh-CN/red_hat_jboss_fuse/6.3/html/apache_camel_development_guide/basicprinciples#BasicPrinciples-PropPlaceholders

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM