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   * @see ValidFeatureKeyPredicate
24   */
25  public class DarkFeatureEnabledCondition implements Condition
26  {
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      {
33          this.darkFeatureManager = darkFeatureManager;
34      }
35  
36      @Override
37      public void init(final Map<String, String> params) throws PluginParseException
38      {
39          if (params.containsKey(FEATURE_KEY_INIT_PARAMETER_NAME))
40          {
41              featureKey = checkFeatureKey(params.get(FEATURE_KEY_INIT_PARAMETER_NAME));
42          }
43          else
44          {
45              throw new PluginParseException("Parameter '" + FEATURE_KEY_INIT_PARAMETER_NAME + "' is mandatory.");
46          }
47      }
48  
49      @Override
50      public boolean shouldDisplay(final Map<String, Object> stringObjectMap)
51      {
52          try
53          {
54              return darkFeatureManager.isFeatureEnabledForCurrentUser(featureKey);
55          }
56          catch (RuntimeException e)
57          {
58              return false;
59          }
60      }
61  }