Reputation: 305
In an OSGi bundle in my application I have a property file included named ontologymapping.properties
.
Now I want to read the content of this property file.
In some example code I found something like this in the start method of the bundle:
public void startObservationAdapter(BundleContext context) {
String filename = context.getProperty("ontologymapping.properties");
try {
File file = new File(filename);
InputStream in = new FileInputStream(file);
ontologymapping.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
where ontologymapping
is a Properties instance.
When I run this code the JVM can't find the file.
I guess the reason is that the file isn't included in the BundleContext.
How can I do that? Or what is another way to read the contents of the property file?
The bundle is not declarative, it's an api bundle for another one which is declarative.
Upvotes: 3
Views: 9853
Reputation: 305
I fixed it. The problem was that the ontologymapper.properties was located in a bundle where an abstract class was defined. In that abstract class the start method of the extending classes was implemented and in that start method I called context.getBundle(). This seems to return the bundle of the implementing classes and not the bundle with the abstract class. So the problem was fixed with placing the properties file in the bundles with implementing classes.
Upvotes: 2
Reputation: 15372
I think you got a few wires crossed. context.getProperty(..) is similar to System.getProperty(...), and actually you find most of System.getProperties() in the context. So in your case, and your code seems to do this, you can put a file name in the system properties with the VM -D command line option: -Dontologymapping.properties="myfile.properties". This should make your code work.
However ... the problem with this type of code is you now have a bundle that needs to read a file on a file system. There are three cases to distinguish:
In case 1 you might want to look at Configuration Admin with Metatype. This allows you to create apps with a nice configuration GUI. Using the bnd DS annotations you can get away with awfully little code and a really nice gui in Apache Felix Webconsole.
In case 2, your code above is valid. you could think of adding a gogo shell and create a command so people can read and update it.
In the last case, #3, you should store the properties inside the bundle and read it with Myclass.class.getResourceAsStream("/ontology.properties"). In bnd(tools), just Include-Resource: ontolology.properties
and it will be present in your bundle, ready to be read.
Upvotes: 4
Reputation: 6046
You can get the URL of your properties file with bundle.getResource('mypropfile.properties') where bundle is the one that contains the properties file. Based on the URL you can get the inputStream of the file (resourceURL.openStream). You can pass the inputstream to properties.load function.
In case you do not want to use OSGi specific code or you cannot get the bundle object of the bundle that contains the properties file you can play with the classloaders. Imagine MyClass is in the same bundle as the properties file. In this case the following code snippet works as well:
Properties myProps = new Properties();
InputStream is = null;
try {
is = MyClass.class.getResourceAsStream("ontologymapping.properties");
myProps.load(is);
} finally {
is.close();
}
Upvotes: 2