tags:

views:

2258

answers:

5

Hello,

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.

A: 

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.

OTisler
Can the user change the settings, or are they to be relatively hard-coded?
Mario Ortegón
You can change the settings programmatically, but you have to give the user a way to do this. (GUI preference option like eclipse, cmd line arg, etc)Properties.setProperty(key, val), followed by Properties.store(stream, header)
OTisler
A: 

Take a look at the resources plugin - might give you what you're looking for:

http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/resInt_filesystem.htm

Martin Dow
This is quite a major set of plugins that the RCP may not already have. This does provide an implementation of the preference store, only to allow preferences to be stored in a workspace. Your link doesn't go to anything preference-centric either. :(
jamesh
+4  A: 

You have, as always, a number of options, depending on your requirements.

  • use the Runtime Preferences to store in a PreferenceStore with a suitable PreferenceInitializer. Quite a large and extensive API with quite a lot of thought gone into it. The preferences aren't exposed to the user or admin by default, so you'd need to do some work to expose a preference page, or write to a properties file.

For less advanced/less work, especially if you don't have access to the eclipse preferences (e.g. server side OSGi):

  • set as a system property, in the RCP.ini. Not user-changeable after launch, requires access to the RCP.ini (eclipse.ini) file which may be possible especially if you're not contributing the the IDE.
  • set as a system property, as an argument in the shortcut. Depends on the user using the shortcut. Specialized shortcut needs to be generated at installation time.

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.

jamesh
A: 

To get the file location of the Configuration directory, run:

new org.eclipse.core.runtime.preferences.ConfigurationScope().getLocation().toFile();

Roy Paterson
A: 

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.

Gunnar Wagenknecht