View Javadoc

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