View Javadoc
1   package com.atlassian.sal.api.features;
2   
3   import com.atlassian.plugin.PluginParseException;
4   import com.atlassian.plugin.web.Condition;
5   
6   import java.util.Map;
7   
8   import static com.atlassian.sal.api.features.ValidFeatureKeyPredicate.checkFeatureKey;
9   
10  /**
11   * A parameterised plugin module condition for enabling modules in the presence of a dark feature.
12   * Pass a param with parameter name "featureKey" containing the dark feature key. Example:
13   * <pre>
14   *  &lt;web-item key="some-key" section="some/section" weight="1"&gt;
15   *      &lt;label key="menu.title"/&gt;
16   *      &lt;link&gt;/some/path&lt;/link&gt;
17   *      &lt;condition class="com.atlassian.sal.api.features.DarkFeatureEnabledCondition"&gt;
18   *          &lt;param name="featureKey"&gt;feature.key&lt;/param&gt;
19   *      &lt;/condition&gt;
20   *  &lt;/web-item&gt;
21   * </pre>
22   * The feature key is validated using the {@link ValidFeatureKeyPredicate}.
23   *
24   * @see ValidFeatureKeyPredicate
25   */
26  public class DarkFeatureEnabledCondition implements Condition {
27      private static final String FEATURE_KEY_INIT_PARAMETER_NAME = "featureKey";
28      private final DarkFeatureManager darkFeatureManager;
29      private String featureKey;
30  
31      public DarkFeatureEnabledCondition(final DarkFeatureManager darkFeatureManager) {
32          this.darkFeatureManager = darkFeatureManager;
33      }
34  
35      @Override
36      public void init(final Map<String, String> params) throws PluginParseException {
37          if (params.containsKey(FEATURE_KEY_INIT_PARAMETER_NAME)) {
38              featureKey = checkFeatureKey(params.get(FEATURE_KEY_INIT_PARAMETER_NAME));
39          } else {
40              throw new PluginParseException("Parameter '" + FEATURE_KEY_INIT_PARAMETER_NAME + "' is mandatory.");
41          }
42      }
43  
44      @Override
45      public boolean shouldDisplay(final Map<String, Object> stringObjectMap) {
46          try {
47              return darkFeatureManager.isFeatureEnabledForCurrentUser(featureKey);
48          } catch (RuntimeException e) {
49              return false;
50          }
51      }
52  }