eclipse插件开发,读取其他插件的Preferences配置信息(记录一次低级错误)

公司有个需求,需要在eclipse插件中获取eclipse配置的tomcat的位置信息,于是我开始研究。

 

方式一:Platfrom.getPlugin()获取Preferences对象  (不可行)

Plugin plugin = org.eclipse.core.runtime.Platform.getPlugin("plugin_hello");
Preferences prefs = plugin.getPluginPreferences();

该方式不可行,Platfrom.getPlugin()方法已经过期了。另外运行了下,也无法获取到信息

 

方式二: 通过IPreferencesService来获取

IPreferencesService service = Platform.getPreferencesService();
Preferences instanceNode = 
    InstanceScope.INSTANCE.getNode("org.eclipse.jst.server.tomcat.core");
try {
	String[] keyArray = instanceNode.keys();
	for(String key : keyArray) {
		System.out.println(key + " -----> " + instanceNode.get(key, null));
	}
} catch (BackingStoreException e) {
	e.printStackTrace();
}

以上会在InstanceScope中进行查找,如果不确定在哪个Scope中,可以使用多个Scope进行查找

IPreferencesService ips = Platform.getPreferencesService();
IScopeContext[] contexts = new IScopeContext[] {InstanceScope.INSTANCE,
	    ConfigurationScope.INSTANCE, DefaultScope.INSTANCE};
String val1 = ips.getString("org.eclipse.wst.server.core", "runtimes", "abcdc",         
        contexts);
String val2 = ips.getString("org.eclipse.jst.server.tomcat.core", 
        "locationorg.eclipse.jst.server.tomcat.runtime.85", "abcdc", contexts);
System.out.println(val1);
System.out.println(val2);

或者这样:

IPreferencesService ips = Platform.getPreferencesService();
IScopeContext[] contexts = new IScopeContext[] {InstanceScope.INSTANCE,
	            ConfigurationScope.INSTANCE, DefaultScope.INSTANCE};
for (int i = 0; i < contexts.length; ++i) {
	value = contexts[i].getNode("org.eclipse.jst.server.tomcat.core")
        .get("locationorg.eclipse.jst.server.tomcat.runtime.85", null);

    if (value != null) {
        value = value.trim();
        if (!value.isEmpty()) {
            break;
        }
    }
}

也可以通过构建 ScopedPreferenceStore对象来获取:

IPreferenceStore prefs2 = new ScopedPreferenceStore(InstanceScope.INSTANCE, 
      "org.eclipse.jst.server.tomcat.core");
String v2 = prefs2.getString("locationorg.eclipse.jst.server.tomcat.runtime.85");

 

低级的大坑:

一开始运行的时候一直无法获取到配置的值,调试很久,后面才醒悟,eclipse中调试插件,会在当前eclipse工作空间下独立生产一个runtime-EclipseApplication的插件运行环境的工作空间,这个独立的instance,没有配置tomcat,所以一直无法获取到值。我没有意识到我自己的开发eclipse工作空间和调试插件生成的插件运行环境工作空间是2个独立个体。

 

Scopes说明:

There are 4 preference scopes, derived from IScopeContext:

The default preference search look-up order as defined by the platform is: project, instance, configuration, default.

 

 

持久化(Persistent stores)

preference配置信息持久化到可读写的文件中

  • project(项目级)
  • instance (工作空间级workspace)
  • configuration (Eclipse全局)

Project store

The project preferences are stored in a file below the project .settings folder:

 project/.settings/<pluginId>.prefs

Instance (workspace) store

The workspace preferences are stored is in a separate file named after the plug-in ID:

workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/<pluginId>.prefs

Configuration (Eclipse) store

The Eclipse preferences are stored is in a separate file named after the plug-in ID:

eclipse/configuration/.settings/<pluginId>.prefs

Defaults

Defaults are preference values used to initialise various variables.

Defaults are loaded into internal structures during Eclipse init and plug-in start-up, from several sources (see org.eclipse.core.internal.preferences.DefaultPreferences):

  • command line -pluginCustomization (highest priority)
  • product preferenceCustomization.ini
  • plug-in preferences.ini
  • AbstractPreferenceInitializer (lowest priority)

 

附录文章参考:

https://gnu-mcu-eclipse.github.io/developer/eclipse/runtime-preferences/

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值