View Javadoc

1   package com.atlassian.plugin;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.collections.Predicate;
5   
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   /**
10   * Represents a configuration state for plugins and plugin modules. The configuration state (enabled
11   * or disabled) is separate from the plugins and modules themselves because a plugin may have multiple
12   * states depending on the context.
13   * <p/>
14   * <p>The state stored in this object represents only the <i>differences</i> between the desired state
15   * and the default state configured in the plugin. So if "getPluginState()" or "getPluginModuleState()" return
16   * null, then the manager should assume that the default state applies instead.
17   * <p>
18   * Please note that this method is not threadsafe.  Access to instances should be synchronised.
19   */
20  public class PluginManagerState
21  {
22      private Map map = new HashMap();
23  
24      public PluginManagerState()
25      {
26  
27      }
28  
29      public PluginManagerState(Map map)
30      {
31          this.map = map;
32      }
33  
34      /**
35       * Get the state of a given plugin.
36       */
37      public Boolean getState(String key)
38      {
39          return (Boolean) map.get(key);
40      }
41  
42      /**
43       * Get the map of all states.
44       */
45      public Map getMap()
46      {
47          return map;
48      }
49  
50      /**
51       * Whether or not a plugin is enabled, calculated from it's current state AND default state.
52       */
53      public boolean isEnabled(Plugin plugin)
54      {
55          Boolean bool = getState(plugin.getKey());
56          return (bool == null) ? plugin.isEnabledByDefault() : bool.booleanValue();
57      }
58  
59      /**
60       * Whether or not a given plugin module is enabled in this state, calculated from it's current state AND default state.
61       */
62      public boolean isEnabled(ModuleDescriptor pluginModule)
63      {
64          if (pluginModule == null)
65              return false;
66          
67          Boolean bool = getState(pluginModule.getCompleteKey());
68          return (bool == null) ? pluginModule.isEnabledByDefault() : bool.booleanValue();
69      }
70  
71      /**
72       * Set a plugins state.
73       */
74      public void setState(String key, Boolean enabled)
75      {
76          map.put(key, enabled);
77      }
78  
79      /**
80       * Remove a plugin's state.
81       */
82      public void removeState(String key)
83      {
84          map.remove(key);
85      }
86  
87      public Map getPluginStateMap(final Plugin plugin)
88      {
89          Map state = new HashMap(getMap());
90          CollectionUtils.filter(state.keySet(), new StringStartsWith(plugin.getKey()));
91          return state;
92      }
93  
94      private static class StringStartsWith implements Predicate
95      {
96          private final String prefix;
97  
98          public StringStartsWith(String keyPrefix)
99          {
100             this.prefix = keyPrefix;
101         }
102 
103         public boolean evaluate(Object object)
104         {
105             String str = (String) object;
106             return str.startsWith(prefix);
107         }
108     }
109 }