使用 PluginSettings 的 Jira SAL

Jira SAL using PluginSettings

我正在寻找一种方法来为我的插件存储特定于项目的配置。 在第一步中,我只想存储一个简单的字符串,如 "Hello".

所以,我找到的是 SAL 和 PluginSettings。 https://developer.atlassian.com/docs/atlassian-platform-common-components/shared-access-layer/sal-services

这看起来很容易使用,但我不知道如何在我的代码中实现它。 我在项目管理部分使用了 WebWork:

@Override
public String doDefault() throws Exception {
    Project project = getProjectManager().getProjectObjByKey(_projectKey);
    HttpServletRequest request = ExecutingHttpRequest.get();
    request.setAttribute((new StringBuilder()).append("com.atlassian.jira.projectconfig.util.ServletRequestProjectConfigRequestCache").append(":project").toString(), project);
    return INPUT;
}

@Override
protected String doExecute() throws Exception {
    Project project = getProjectManager().getProjectObjByKey(_projectKey);
    HttpServletRequest request = ExecutingHttpRequest.get();
    request.setAttribute((new StringBuilder()).append("com.atlassian.jira.projectconfig.util.ServletRequestProjectConfigRequestCache").append(":project").toString(), project);
    String param = request.getParameter("param");
    return SUCCESS;
}

public void setProjectKey(String projectKey) {
    _projectKey = projectKey;
    }

public String getProjectKey() {
    return _projectKey;
}

public String getBaseUrl() {
    return ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL);
}

正如 SAL 所说,我实现了一个设置-Class:

public CustomProjectSettings(
        final PluginSettingsFactory pluginSettingsFactory,
        final String projectKey) {
    this.pluginSettingsFactory = pluginSettingsFactory;
    this.projectKey = projectKey;
}

public void setValue(final String key, final String value) {
    final PluginSettings settings = pluginSettingsFactory
            .createSettingsForKey(projectKey);
    settings.put(key, value);
}

public Object getValue(final String key) {
    final PluginSettings settings = pluginSettingsFactory
            .createSettingsForKey(projectKey);
    return settings.get(key);
}

并且我在 xml 中添加了组件:

<component-import key="pluginSettingsFactory" interface="com.atlassian.sal.api.pluginsettings.PluginSettingsFactory" />

那么我如何将其连接并实现到我的网络中说

protected String doExecute() throws Exception{
    [...]
    pluginSettings.setValue("Key", param);
    [...]
}

这比我想象的要容易。 我只需要将设置作为我的 WebWork 的依赖项注入:

public WebWorkAction(CustomProjectSettings settings){
    this.settings = settings
}

设置-Class 由

自动装配
<component-import key="pluginSettingsFactory" 
interface="com.atlassian.sal.api.pluginsettings.PluginSettingsFactory" />

并添加

<component key="settingsComponent"
      class="com.xxx.CustomProjectSettings">
</component>