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

时间:2017-05-11 18:34:30

标签: java jboss jbossfuse karaf property-files

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

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

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

路径已在我的开发计算机上正确解析为

C:\ Users \用户有人\ DEVSTUDIO \运行时\ JBoss的熔丝-6.3.0.redhat-135 \等\ bean.properties

可以在blueprint-XML中加载property-File来配置bean,但是为了访问bean,我的代码需要CamelContext。由于我有一些在没有交换/上下文/注册表的情况下访问的静态代码块,我还希望能够在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]           在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.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]           在org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:196)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]           在org.apache.camel.processor.Pipeline.process(Pipeline.java:121)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]           在org.apache.camel.processor.Pipeline.process(Pipeline.java:83)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]           在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]           在org.apache.camel.component.timer.TimerConsumer $ 1.run(TimerConsumer.java:76)[231:org.apache.camel.camel-core:2.17.0.redhat-630135]           在java.util.TimerThread.mainLoop(Timer.java:555)[:1.8.0_91]           在java.util.TimerThread.run(Timer.java:505)[:1.8.0_91]

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

不要这样做。你正在寻找麻烦。

  1. 以OSGi方式加载属性(使用.cfg作为扩展名和蓝图property-placeholder bean)
    如果文件发生更改(如果您愿意),您还可以获得通知
  2. 如果您只使用静态方法,则将它们注入豆中即可 除非您非常清楚自己在做什么,否则不要将托管bean与非托管静态代码混合使用。
  3. 如果某些“ static ”代码需要属性,则表示它是有状态,并且此类值得实例化为bean。

答案 1 :(得分:0)

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

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