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

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

我尝试在 JBossFuse/karaf 上的 Java 运行 中加载 属性 文件。

文件位于 $[karaf.home]/etc/bean.properties

代码能够很好地加载包内的属性,但现在我尝试从项目本身中排除属性,并且代码抛出 Nullpointer-Exception。

路径在我的开发机器上正确解析为

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

可以在蓝图中加载属性文件-XML来配置bean,但是要访问bean,我的代码需要CamelContext。由于我有一些无需 exchange/context/registry 即可访问的静态代码块,因此我还希望能够加载 Java 中的属性。

这两个函数都抛出 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;
}

异常:

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:org.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.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]

非常感谢任何帮助。

不确定为什么在没有更完整的示例的情况下获得 NPE。如果你需要在没有路由的情况下使用属性,你应该使用 Camel 的 属性 占位符工具:

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

不要那样做。你这是在找麻烦。

  1. 以 OSGi 方式加载属性(使用 .cfg 作为扩展和蓝图 property-placeholder bean)
    如果文件发生更改(如果您愿意),您还可以获得通知
  2. 即使您只使用静态方法,也将它们注入到 bean 中。
    除非您非常清楚自己在做什么,否则不要将托管 bean 与非托管静态代码混合使用。

如果一些“static”代码需要属性意味着它是stateful,而这个class值得被实例化给一颗豆子。