Eclipse RCP: Making use of configuration directory

独自空忆成欢 提交于 2019-12-12 08:38:21

问题


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.


回答1:


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.




回答2:


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: 'my.connection.settings=@config.dir/mysettings.ini'. '@config.dir' is a placeholder which gets replaced with the actual path to the configuration directory.




回答3:


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




回答4:


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.




回答5:


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

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



来源:https://stackoverflow.com/questions/555275/eclipse-rcp-making-use-of-configuration-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!