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