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

1k views Asked by At

I try to load a property file in Java running on JBossFuse/karaf.

The file is located at $[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.

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

The property-File can be loaded in the blueprint-XML to configure beans, but to access the bean my code needs the 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.

Both the functions throw the NullPointerException and I guess, it is because the code runs in 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: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$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]

Any help would be highly appreciated.

2

There are 2 answers

3
Alessandro Da Rugna On BEST ANSWER

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)
    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.
    Don't mix managed beans with unmanaged static code unless you know very well what you are doing.

If some "static" code requires properties means that it is stateful, and this class deserves to be instantiated to a bean.

0
janstey On

Not sure why you are getting an NPE without a more complete example. If you need to use properties without a route, you should be using Camel's property placeholder facilities:

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