Eclipse RCP:使用配置目录

时间:2022-02-02 10:47:28

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.

5 个解决方案

#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.
  • 使用运行时首选项存储在具有合适的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.
  • 在RCP.ini中设置为系统属性。启动后不能用户更改,需要访问RCP.ini(eclipse.ini)文件,这可能是特别是如果您没有贡献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.

如果来自文件系统的可访问性非常重要,那么我会考虑使用上面的方法之一来设置etc目录,如果在第一次使用时它们不存在,那么让你的bundle在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技术主管)访谈中他说有大约七种不同的偏好机制,他从来不知道使用哪一种。

#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.

正如已经指出的那样,首选API是值得关注的。还有Secure Preferences 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: 'my.connection.settings=@config.dir/mysettings.ini'. '@config.dir' is a placeholder which gets replaced with the actual path to the configuration directory.

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

#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:

通常,我喜欢将配置文件隐藏在“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存储远程连接所需的数据。

#5


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

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

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

#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.
  • 使用运行时首选项存储在具有合适的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.
  • 在RCP.ini中设置为系统属性。启动后不能用户更改,需要访问RCP.ini(eclipse.ini)文件,这可能是特别是如果您没有贡献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.

如果来自文件系统的可访问性非常重要,那么我会考虑使用上面的方法之一来设置etc目录,如果在第一次使用时它们不存在,那么让你的bundle在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技术主管)访谈中他说有大约七种不同的偏好机制,他从来不知道使用哪一种。

#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.

正如已经指出的那样,首选API是值得关注的。还有Secure Preferences 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: 'my.connection.settings=@config.dir/mysettings.ini'. '@config.dir' is a placeholder which gets replaced with the actual path to the configuration directory.

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

#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:

通常,我喜欢将配置文件隐藏在“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存储远程连接所需的数据。

#5


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

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

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