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   import org.apache.commons.collections.CollectionUtils;
7   import org.apache.commons.collections.Predicate;
8   
9   import java.io.Serializable;
10  import java.util.Collections;
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  import static com.atlassian.plugin.manager.PluginPersistentState.Util.buildStateKey;
15  import static java.util.Collections.unmodifiableMap;
16  
17  /**
18   * Immutable implementation of the {@link PluginPersistentState} interface.
19   * <p>
20   * The state stored in this object represents only the <i>differences</i> between the desired state
21   * and the default state configured in the plugin. So if "getPluginState()" or "getPluginModuleState()" return
22   * null, then the manager should assume that the default state applies instead.
23   */
24  public final class DefaultPluginPersistentState implements Serializable, PluginPersistentState {
25      private final Map<String, Boolean> map;
26  
27      /**
28       * Creates an empty {@link PluginPersistentState}.
29       *
30       * @deprecated create {@link PluginPersistentState} instances using the
31       * {@link PluginPersistentState.Builder}
32       */
33      @Deprecated
34      public DefaultPluginPersistentState() {
35          map = Collections.emptyMap();
36      }
37  
38      /**
39       * Creates a {@link PluginPersistentState} with the supplied states.
40       *
41       * @param map of the plugin states using the {@link Plugin#getKey()} as the key.
42       * @deprecated create {@link PluginPersistentState} instances using the
43       * {@link PluginPersistentState.Builder}
44       */
45      @Deprecated
46      public DefaultPluginPersistentState(final Map<String, Boolean> map) {
47          this.map = unmodifiableMap(new HashMap<String, Boolean>(map));
48      }
49  
50      /* for use from within this package, the second parameter is ignored */
51      DefaultPluginPersistentState(final Map<String, Boolean> map, final boolean ignore) {
52          this.map = unmodifiableMap(new HashMap<String, Boolean>(map));
53      }
54  
55      /**
56       * Copy constructor. Doesn't make sense to use as the map is immutable. Just use the
57       *
58       * @param state
59       */
60      @Deprecated
61      public DefaultPluginPersistentState(final PluginPersistentState state) {
62          this(state.getMap());
63      }
64  
65      /* (non-Javadoc)
66       * @see com.atlassian.plugin.PluginPersistentState#getMap()
67       */
68      public Map<String, Boolean> getMap() {
69          return Collections.unmodifiableMap(map);
70      }
71  
72      /* (non-Javadoc)
73       * @see com.atlassian.plugin.PluginPersistentState#isEnabled(com.atlassian.plugin.Plugin)
74       */
75      public boolean isEnabled(final Plugin plugin) {
76          final Boolean bool = map.get(plugin.getKey());
77          return (bool == null) ? plugin.isEnabledByDefault() : bool.booleanValue();
78      }
79  
80      /* (non-Javadoc)
81       * @see com.atlassian.plugin.PluginPersistentState#isEnabled(com.atlassian.plugin.ModuleDescriptor)
82       */
83      public boolean isEnabled(final ModuleDescriptor<?> pluginModule) {
84          if (pluginModule == null) {
85              return false;
86          }
87  
88          final Boolean bool = map.get(pluginModule.getCompleteKey());
89          return (bool == null) ? pluginModule.isEnabledByDefault() : bool.booleanValue();
90      }
91  
92      /* (non-Javadoc)
93       * @see com.atlassian.plugin.PluginPersistentState#getPluginStateMap(com.atlassian.plugin.Plugin)
94       */
95      public Map<String, Boolean> getPluginStateMap(final Plugin plugin) {
96          final Map<String, Boolean> state = new HashMap<String, Boolean>(getMap());
97          CollectionUtils.filter(state.keySet(), new StringStartsWith(plugin.getKey()));
98          return state;
99      }
100 
101     public PluginRestartState getPluginRestartState(final String pluginKey) {
102         for (final PluginRestartState state : PluginRestartState.values()) {
103             if (map.containsKey(buildStateKey(pluginKey, state))) {
104                 return state;
105             }
106         }
107         return PluginRestartState.NONE;
108     }
109 
110     private static class StringStartsWith implements Predicate {
111         private final String prefix;
112 
113         public StringStartsWith(final String keyPrefix) {
114             prefix = keyPrefix;
115         }
116 
117         public boolean evaluate(final Object object) {
118             final String str = (String) object;
119             return str.startsWith(prefix);
120         }
121     }
122 }