Eclipse RCP:利用配置目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/555275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:32:31  来源:igfitidea点击:

Eclipse RCP: Making use of configuration directory

javaeclipsercp

提问by

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?

我的 Eclipse RCP 应用程序需要一个包含一些信息的配置文件来连接到远程数据库。存储此配置文件的最佳位置在哪里?

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.

为此,我可以使用默认配置目录(通常存储 'config.ini' 的位置)吗?如果是这样,如何以编程方式将 File 实例获取到此位置?我还注意到这个目录在我的 Eclipse IDE 中不存在。

Thanks.

谢谢。

回答by jamesh

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

与往常一样,您有多种选择,具体取决于您的要求。

  • use the Runtime Preferencesto 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.
  • 使用运行时首选项通过合适的 PreferenceInitializer 存储在 PreferenceStore 中。相当大和广泛的 API,其中有很多想法。默认情况下,首选项不会向用户或管理员公开,因此您需要做一些工作来公开首选项页面或写入属性文件。

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

对于不太高级/较少的工作,特别是如果您无权访问 eclipse 首选项(例如服务器端 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.
  • 在 RCP.ini 中设置为系统属性。启动后用户不可更改,需要访问 RCP.ini (eclipse.ini) 文件,这可能是可能的,尤其是在您不贡献 IDE 的情况下。
  • 设置为系统属性,作为快捷方式中的参数。取决于使用快捷方式的用户。安装时需要生成专门的快捷方式。

If accessibility from the filesystem is really important, then I would consider using one of the methods above to set an etcdirectory, and the let your bundles generate default properties files in the etcdirectory 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 FAQmay also be helpful.

如果文件系统的可访问性真的很重要,那么我会考虑使用上述方法之一来设置etc目录,etc如果第一次使用时它们不存在,则让您的包在目录中生成默认属性文件。这实质上是在滚动您自己的偏好存储,因此如果您确实有访问偏好包,那么最好这样做。这个相当古老的用户设置常见问题也可能有帮助。

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.

我确实记得 Erich Gamma(如四人组和 JDT 技术负责人)的一次采访,他说大约有七种不同的偏好机制,但他不知道该使用哪一种。

回答by Gunnar

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.

正如已经指出的,Preferences API 值得一看。还有安全首选项 API,适用于在磁盘上存储加密的用户名和密码。

Another option is to use the 'org.eclipse.osgi.service.datalocation.Location' OSGi service. This provides access to the different locations available.

另一种选择是使用“org.eclipse.osgi.service.datalocation.Location”OSGi 服务。这提供了对可用的不同位置的访问。

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.

第三个选项是在“config.ini”中定义一个系统属性,它使用占位符指向带有连接信息的文件:“[email protected]/mysettings.ini”。'@config.dir' 是一个占位符,它被替换为配置目录的实际路径。

回答by Martin Dow

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

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

回答by Roy Paterson

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

要获取配置目录的文件位置,请运行:

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

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

回答by Ovi Tisler

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:

通常,我喜欢将配置文件隐藏在“bin”目录中,或者不在根目录中的某个地方。您可能应该将其保存在项目的子目录中,以免将系统上的某个随机位置弄乱。如果您需要获得文件的句柄,您可以执行以下操作:

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.

然后,如果它是一个真正的 ini 文件,您可以使用Properties.load()加载和使用 ini 文件中的值。

您还可以使用 Preferences API 来存储远程连接所需的数据。