View Javadoc
1   package com.atlassian.plugin.manager;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.PluginRestartState;
6   
7   import java.io.Serializable;
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import static com.atlassian.plugin.manager.PluginEnabledState.UNKNOWN_ENABLED_TIME;
12  import static com.atlassian.plugin.manager.PluginPersistentState.Util.buildStateKey;
13  import static java.util.Collections.unmodifiableMap;
14  import static java.util.stream.Collectors.toMap;
15  
16  /**
17   * Immutable implementation of the {@link PluginPersistentState} interface.
18   * <p>
19   * The state stored in this object represents changes done by a user to the plugin's state.
20   * If "getPluginState()" or "getPluginModuleState()" return null, then the manager
21   *  should assume that the default state applies instead.
22   */
23  public final class DefaultPluginPersistentState implements Serializable, PluginPersistentState {
24      private final Map<String, PluginEnabledState> map;
25  
26      /* for use from within this package, the second parameter is ignored */
27      DefaultPluginPersistentState(final Map<String, PluginEnabledState> map, final boolean ignore) {
28          this.map = unmodifiableMap(new HashMap<>(map));
29      }
30  
31      @Override
32      public Map<String, PluginEnabledState> getStatesMap() {
33          return unmodifiableMap(map);
34      }
35  
36      /* (non-Javadoc)
37       * @see com.atlassian.plugin.PluginPersistentState#isEnabled(com.atlassian.plugin.Plugin)
38       */
39      public boolean isEnabled(final Plugin plugin) {
40          final PluginEnabledState state = map.get(plugin.getKey());
41          return (state == null) ? plugin.isEnabledByDefault() : state.isEnabled();
42      }
43  
44      /* (non-Javadoc)
45       * @see com.atlassian.plugin.PluginPersistentState#isEnabled(com.atlassian.plugin.ModuleDescriptor)
46       */
47      public boolean isEnabled(final ModuleDescriptor<?> pluginModule) {
48          if (pluginModule == null) {
49              return false;
50          }
51  
52          final PluginEnabledState state = map.get(pluginModule.getCompleteKey());
53          return (state == null) ? pluginModule.isEnabledByDefault() : state.isEnabled();
54      }
55  
56      @Override
57      public Map<String, PluginEnabledState> getPluginEnabledStateMap(final Plugin plugin) {
58          return getStatesMap().entrySet().stream()
59                  .filter(e -> e.getKey().startsWith(plugin.getKey()))
60                  .collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
61      }
62  
63      public PluginRestartState getPluginRestartState(final String pluginKey) {
64          for (final PluginRestartState state : PluginRestartState.values()) {
65              if (map.containsKey(buildStateKey(pluginKey, state))) {
66                  return state;
67              }
68          }
69          return PluginRestartState.NONE;
70      }
71  
72      public static Map<String, PluginEnabledState> getPluginEnabledStateMap(final Map<String, Boolean> map) {
73          return unmodifiableMap(new HashMap<>(map.entrySet().stream()
74                  .collect(toMap(Map.Entry::getKey, e -> new PluginEnabledState(e.getValue(), UNKNOWN_ENABLED_TIME)))));
75      }
76  }