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
11
12
13
14
15
16
17
18
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
36
37 public Boolean getState(String key)
38 {
39 return (Boolean) map.get(key);
40 }
41
42
43
44
45 public Map getMap()
46 {
47 return map;
48 }
49
50
51
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
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
73
74 public void setState(String key, Boolean enabled)
75 {
76 map.put(key, enabled);
77 }
78
79
80
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 }