1 package com.atlassian.plugin.web;
2
3 import com.atlassian.plugin.PluginManager;
4 import com.atlassian.plugin.PluginAccessor;
5 import com.atlassian.plugin.web.descriptors.*;
6
7 import java.util.*;
8
9 import org.apache.commons.logging.LogFactory;
10 import org.apache.commons.logging.Log;
11
12
13
14
15 public class DefaultWebInterfaceManager implements WebInterfaceManager
16 {
17 private PluginAccessor pluginAccessor;
18 private WebFragmentHelper webFragmentHelper;
19 private Map<String,List<WebSectionModuleDescriptor>> sections;
20 private Map<String,List<WebItemModuleDescriptor>> items;
21 private static final Log log = LogFactory.getLog(DefaultWebInterfaceManager.class);
22
23 public static final WeightedDescriptorComparator WEIGHTED_DESCRIPTOR_COMPARATOR = new WeightedDescriptorComparator();
24
25 public DefaultWebInterfaceManager()
26 {
27 refresh();
28 }
29
30 public DefaultWebInterfaceManager(PluginAccessor pluginAccessor, WebFragmentHelper webFragmentHelper)
31 {
32 this.pluginAccessor = pluginAccessor;
33 this.webFragmentHelper = webFragmentHelper;
34 refresh();
35 }
36
37 public boolean hasSectionsForLocation(String location)
38 {
39 return !getSections(location).isEmpty();
40 }
41
42 public List<WebSectionModuleDescriptor> getSections(String location)
43 {
44 if (location == null)
45 {
46 return Collections.emptyList();
47 }
48
49 List<WebSectionModuleDescriptor> result = sections.get(location);
50
51 if (result == null)
52 {
53 result = new ArrayList<WebSectionModuleDescriptor>();
54 List<WebSectionModuleDescriptor> descriptors = pluginAccessor.getEnabledModuleDescriptorsByClass(WebSectionModuleDescriptor.class);
55 for (Iterator iterator = descriptors.iterator(); iterator.hasNext();)
56 {
57 WebSectionModuleDescriptor descriptor = (WebSectionModuleDescriptor) iterator.next();
58 if (location.equalsIgnoreCase(descriptor.getLocation()))
59 result.add(descriptor);
60 }
61
62 Collections.sort(result, WEIGHTED_DESCRIPTOR_COMPARATOR);
63 sections.put(location, result);
64 }
65
66 return result;
67 }
68
69 public List<WebSectionModuleDescriptor> getDisplayableSections(String location, Map<String,Object> context)
70 {
71 return filterFragmentsByCondition(getSections(location), context);
72 }
73
74 public List<WebItemModuleDescriptor> getItems(String section)
75 {
76 if (section == null)
77 {
78 return Collections.emptyList();
79 }
80
81 List<WebItemModuleDescriptor> result = items.get(section);
82
83 if (result == null)
84 {
85 result = new ArrayList<WebItemModuleDescriptor>();
86 List<WebItemModuleDescriptor> descriptors = pluginAccessor.getEnabledModuleDescriptorsByClass(WebItemModuleDescriptor.class);
87 for (Iterator iterator = descriptors.iterator(); iterator.hasNext();)
88 {
89 WebItemModuleDescriptor descriptor = (WebItemModuleDescriptor) iterator.next();
90 if (section.equalsIgnoreCase(descriptor.getSection()))
91 result.add(descriptor);
92 }
93
94 Collections.sort(result, WEIGHTED_DESCRIPTOR_COMPARATOR);
95 items.put(section, result);
96 }
97
98 return result;
99 }
100
101 public List<WebItemModuleDescriptor> getDisplayableItems(String section, Map<String,Object> context)
102 {
103 return filterFragmentsByCondition(getItems(section), context);
104 }
105
106 private <T extends WebFragmentModuleDescriptor> List<T> filterFragmentsByCondition(List<T> relevantItems, Map<String,Object> context)
107 {
108 if (relevantItems.isEmpty())
109 {
110 return relevantItems;
111 }
112
113 List<T> result = new ArrayList<T>(relevantItems);
114 for (Iterator iterator = result.iterator(); iterator.hasNext();)
115 {
116 WebFragmentModuleDescriptor descriptor = (WebFragmentModuleDescriptor) iterator.next();
117 try
118 {
119 if (descriptor.getCondition() != null && !descriptor.getCondition().shouldDisplay(context))
120 {
121 iterator.remove();
122 }
123 }
124 catch (Throwable t)
125 {
126 log.error("Could not evaluate condition '" + descriptor.getCondition() + "' for descriptor: " + descriptor, t);
127 iterator.remove();
128 }
129 }
130
131 return result;
132 }
133
134 public void refresh()
135 {
136 sections = Collections.synchronizedMap(new HashMap());
137 items = Collections.synchronizedMap(new HashMap());
138 }
139
140
141
142
143
144 @Deprecated
145 public void setPluginManager(PluginManager pluginManager)
146 {
147 setPluginAccessor(pluginManager);
148 }
149
150
151
152
153
154 public void setPluginAccessor(PluginAccessor pluginAccessor)
155 {
156 this.pluginAccessor = pluginAccessor;
157 }
158
159 public void setWebFragmentHelper(WebFragmentHelper webFragmentHelper)
160 {
161 this.webFragmentHelper = webFragmentHelper;
162 }
163
164 public WebFragmentHelper getWebFragmentHelper()
165 {
166 return webFragmentHelper;
167 }
168
169 }