Reputation:
My Eclipse RCP application requires a configuration file that contains some information to connect to a remote database. Where is the best location to store this configuration file?
Can I use the default configuration directory (where 'config.ini' is usually stored) for this purpose? If so, how can I get a File instance to this location programmatically? I also note that this directory does not exist in my Eclipse IDE.
Thanks.
Upvotes: 7
Views: 10751
Reputation: 2359
As already pointed out, the Preferences API is something to look at. There is also the Secure Preferences API which is suitable to store user names and passwords encrypted on disc.
Another option is to use the 'org.eclipse.osgi.service.datalocation.Location' OSGi service. This provides access to the different locations available.
A third option is to define a system property in 'config.ini' which points to file with your connection information using placeholders: '[email protected]/mysettings.ini'. '@config.dir' is a placeholder which gets replaced with the actual path to the configuration directory.
Upvotes: 2
Reputation: 20091
You have, as always, a number of options, depending on your requirements.
For less advanced/less work, especially if you don't have access to the eclipse preferences (e.g. server side OSGi):
If accessibility from the filesystem is really important, then I would consider using one of the methods above to set an etc
directory, and the let your bundles generate default properties files in the etc
directory if they don't exist on first use. This is essentially rolling your own preference store, so if you do have access preferences bundle, you may be better off doing that. This rather old User Settings FAQ may also be helpful.
I do recall an Erich Gamma (as in Gang of Four, and JDT technical lead) interview in which he says that there are about seven different preference mechanisms, and he never knew which one to use.
Upvotes: 5
Reputation: 863
To get the file location of the Configuration directory, run:
new org.eclipse.core.runtime.preferences.ConfigurationScope().getLocation().toFile();
Upvotes: 0
Reputation: 5321
Take a look at the resources plugin - might give you what you're looking for:
Upvotes: 1
Reputation: 6463
Usually, I like to hide the config files in a "bin" directory, or somewhere not in the root directory. You should probably keep it in a sub-directory of your project so you don't clutter up some random location on the system. If you need to get a handle to the File, you can just do:
File configFile = new File("./bin/remoteDbConfig.ini");
Then if its a true ini file, you can use Properties.load() to load and use the values from the ini file.
You could also use the Preferences API to store the data you need for the remote connection.
Upvotes: 0