Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Read global property file in custom plugins

RJ Peterson November 30, 2011

Creating several custom plugins that share common properties such as db settings. Want to create a global property file that lives outside of the plugins that can be referenced by all plugins. Using InputStream, how do I determine the path for the file?

InputStream in = getClass().getClassLoader().getResourceAsStream(path);

Thanks

7 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Jobin Kuruvilla [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 6, 2014

Reminded me of an older post where I had discussed this and another option :)

Alied Perez
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 6, 2014

Nice!!!

And nice to have it posted here, so others can find it.

However; and this is why these solutions (still) don't work in my case; I want the properties to be read by Spring and used to configure my beans. I also don't want to do Spring's job, and I don't like to do what I should have for free. If anybody knows a way to load .properties files by Spring I promess a few karme points.

jacob delddd August 28, 2018

Sorry for disturbing you in this old post, but did you find a solution on using spring framework?

0 votes
David at David Simpson Apps
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 13, 2014

Here you are:

// Get the Confluence home directory
String applicationHome = BootstrapUtils.getBootstrapManager().getApplicationHome();
File file = new File(applicationHome, "some-file.properties");
FileInputStream fileInputStream = new FileInputStream(file);

Properties p = new Properties();
p.load(fileInputStream);

// read individual key
p.getProperty("test.key")

It's @jobin's example code adapted for Confluence. I'm using a properties file in the Confluence home directoryrather than in for example WEB-INF/classes so that it doesn't get blasted away on upgrade.

0 votes
Steve Gerstner [bridgingIT]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 6, 2014

You cannot use it in a spring.xml.

The bundle has its own classpath! That means, WEB_INF/classes is NOT in your classpath in a version 2 plugin. That's the world of OSGi.

Put them in your plugin and you can load them via spring.

Or you can load them by code, but then, you need a basic class like String to get the classloader of the system bundle and then you can access your properties file.

0 votes
David at David Simpson Apps
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 5, 2014

Update: Doesn't work so I've converted Jobin's comment to an answer :)

Using this in your plugin/add-on:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("some-file.properties");

...will access a file located at WEB-INF/classes/some-file.properties

This is handy for admins who don't want the restrictions of a bundled properties file.

0 votes
Steve Gerstner [bridgingIT]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 28, 2014

You are in an OSGi plugin, so the defaultclassloader will be the one from your plugin.

Take a class from the SystemBundle like org.apache.commons.lang.StrungUtils

and get the classloader from StringUtils.

To get a thrid option to Jospehs answer, if you have more date, you can use ActiveObjects to store your data.

Furthermore, you can put all your properties in one file and work with prefixes. Then just set an environmentvariable to determine wich environment you are using and pick the correct values.

Alied Perez
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 28, 2014

Thanks, Steve,

but all that seems overkill for a couple Strings. I'd rather make the properties in the jar to be edited in place, or fill the .properties at build time.

I was hoping to use (the very outdated) Spring and a PropertyPlaceholderConfigurer to set my values in the bean definitions. That's it. If I had to add one line of code other than that; I would be exceeding the requirements.

Anyway, there are things you can do with Maven.

0 votes
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 1, 2011

How are you planning to install the properties file onto the Confluence server? You'll probably have to manually copy it onto the file system in $confluence_webapp/WEB-INF/classes. The path you need to pass in will be relative to this directory.

I'm not 100% sure if a plugin written using version 2 of the plugin framework will actually be able to read resources from the confluence web-inf directory. You should probably perform a simple proof-of-concept test before going down this path, to be sure.

Atlernatives to this approach include:

* Storing the configuration values in the Bandana table in the database

* Storing the properties file in just one of the plugins, and then expose a Spring bean from the plugin to the other plugins that provides the configuration values.

Alied Perez
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 28, 2014

Hi, I know it's been a long time.

Can I have a .properties file with environment-dependent data, like, jdbc url and driver, so I can edit it and point each environment? I just need a couple values to be quickly edited at environment setup: and perhaps once every other year or sonething.

if so, How can I configure it? I have tried several combinations but without luck.

Alied Perez
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 28, 2014

It seems there is no straightforward way. At least not just with Spring context alone.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events