1

I have created a simple OSGi bundle for my CQ5 application. I want to read certain properties from a properties file (regular key-value file)

I have tried to put the file in the /resources folder of the bundle and outside also (in /etc/designs/) but the file always fails to load and throws exception as below:

java.io.FileNotFoundException: \path\to\the\file\filename.properties 
(The system cannot find the path specified)

I would like to keep the file outside the bundle so that it can be edited without altering the bundle

Riju Mahna
  • 6,718
  • 12
  • 52
  • 91

2 Answers2

1

Bundle resources can be returned using the Bundle.getEntry() method. See this answer for more details.

It is possible to load resources via the classloader (e.g. this.getClass().getClassLoader().getResource("path.to.the.file.filename.properties");) . Bear in mind though, that bundles cannot export the root/default classpath as this cannot be exported/imported , so your resources will need to live in a package which is exported.

To load files from the repository you would need to use ResourceResolver.getResource('/etc/design/path/to/the/file/filename.properties')

Community
  • 1
  • 1
diffa
  • 2,986
  • 1
  • 22
  • 35
0

Have you considered using the OSGi configuration admin for management of these properties?

You can define properties on your OSGi component/services that can be configured through the felix console (e.g. localhost:4502/system/console/configMgr). Or you can deploy configuration as content, if necessary specific to particular runmodes (e.g. author, publish)

This has some advantages over external configuration files. Configuration parameters can be changed independently of the deployed bundle. There is also the benefit that configuration changes can be made to a running application without the need to restart.

You would need to annotate your configuration properties with the @Property SCR annotation.

There is further information on the following blog post: http://www.wemblog.com/2012/10/how-to-work-with-configurations-in-cq.html or in this "OSGI for mere mortals" presentation

diffa
  • 2,986
  • 1
  • 22
  • 35
  • Thanks for the insight on OSGI configuration. But my project currently needs specifically to use the .properties file. But my bundle code never finds the file, wherever I keep it. – Riju Mahna May 20 '13 at 07:45
  • how are you loading the properties file? – diffa May 20 '13 at 08:00
  • I've tried adding it to the bundle (in and outside the /resources directory) and tried keeping it in the /etc/designs/myFolder .. but always got the same result i.e. FileNotFoundException – Riju Mahna May 20 '13 at 09:14