1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.om.registry.base;
18
19 import org.apache.jetspeed.om.registry.PortletRegistry;
20 import org.apache.jetspeed.om.registry.RegistryEntry;
21 import org.apache.jetspeed.om.registry.PortletEntry;
22 import org.apache.jetspeed.om.registry.InvalidEntryException;
23 import org.apache.jetspeed.om.registry.Category;
24 import org.apache.jetspeed.om.registry.RegistryException;
25 import org.apache.jetspeed.services.Registry;
26 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
27 import org.apache.jetspeed.services.logging.JetspeedLogger;
28
29 import java.util.Map;
30 import java.util.SortedMap;
31 import java.util.TreeMap;
32 import java.util.HashMap;
33 import java.util.Iterator;
34
35 /***
36 * Provides base functionality within a Portlet Registry.
37 *
38 * <p>To avoid loops, a RegistryService implementation using this class
39 * nees to call the addLocalEntry/removeLocalEntry methods to modify
40 * the in memory state of this Registry</p>
41 *
42 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
43 * @version $Id: BasePortletRegistry.java,v 1.7 2004/02/23 03:08:26 jford Exp $
44 */
45 public class BasePortletRegistry extends BaseRegistry implements PortletRegistry
46 {
47
48 private Map catMap = new TreeMap();
49
50 /***
51 * Static initialization of the logger for this class
52 */
53 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(BasePortletRegistry.class.getName());
54
55 /***
56 @see Registry#setEntry
57 */
58 public void setEntry( RegistryEntry entry ) throws InvalidEntryException
59 {
60
61
62
63 try
64 {
65 Registry.addEntry(Registry.PORTLET, entry);
66 }
67 catch (RegistryException e)
68 {
69 logger.error("Exception", e);
70 }
71 }
72
73 /***
74 @see Registry#addEntry
75 */
76 public void addEntry( RegistryEntry entry ) throws InvalidEntryException
77 {
78
79
80
81 try
82 {
83 Registry.addEntry(Registry.PORTLET, entry);
84 }
85 catch (RegistryException e)
86 {
87 logger.error("Exception", e);
88 }
89 }
90
91 /***
92 @see Registry#removeEntry
93 */
94 public void removeEntry( String name )
95 {
96
97
98
99 Registry.removeEntry(Registry.PORTLET, name);
100 }
101
102 /***
103 @see Registry#removeEntry
104 */
105 public void removeEntry( RegistryEntry entry )
106 {
107
108
109
110 if (entry != null)
111 {
112 Registry.removeEntry(Registry.PORTLET, entry.getName());
113 }
114 }
115
116 protected void setPortletEntry(PortletEntry entry) throws InvalidEntryException
117 {
118 synchronized (catMap)
119 {
120 int count = 0;
121 Iterator it = ((PortletEntry)entry).listCategories();
122 while (it.hasNext())
123 {
124 Category category = (Category)it.next();
125 String key = getCategoryKey(category);
126 HashMap bucket = (HashMap)this.catMap.get(key);
127 if (null == bucket)
128 {
129 bucket = new HashMap();
130 bucket.put(entry.getName(), entry);
131 this.catMap.put(key, bucket);
132 }
133 else
134 {
135 bucket.put(entry.getName(), entry);
136 }
137 count++;
138 }
139
140
141
142 if (0 == count)
143 {
144 StringBuffer key = new StringBuffer(128);
145 key.append(PortletEntry.DEFAULT_GROUP);
146 key.append(".");
147 if (entry.getType().equals(PortletEntry.TYPE_ABSTRACT))
148 key.append(PortletEntry.DEFAULT_CATEGORY_ABSTRACT);
149 else
150 key.append(PortletEntry.DEFAULT_CATEGORY_REF);
151
152 HashMap bucket = (HashMap)this.catMap.get(key.toString());
153 if (null == bucket)
154 {
155 bucket = new HashMap();
156 bucket.put(entry.getName(), entry);
157 this.catMap.put(key.toString(), bucket);
158 }
159 else
160 {
161 bucket.put(entry.getName(), entry);
162 }
163 }
164
165 }
166 }
167
168
169 public String getCategoryKey(Category category)
170 {
171 if (category == null)
172 {
173 return PortletEntry.DEFAULT_GROUP;
174 }
175
176 String categoryName = category.getName();
177
178 if ((categoryName == null) || categoryName.equals(""))
179 {
180 return category.getGroup();
181 }
182
183 return category.getGroup() + "." + categoryName;
184 }
185
186
187
188
189
190
191
192
193 public Iterator findPortletsByCategory(String category)
194 {
195 String key;
196
197 if ((category == null) || category.equals(""))
198 {
199 key = PortletEntry.DEFAULT_GROUP;
200 }
201 else
202 {
203 key = PortletEntry.DEFAULT_GROUP + "." + category;
204 }
205
206 CategoryIterator iterator = new CategoryIterator((SortedMap)catMap, key);
207
208 return iterator;
209 }
210
211
212
213
214
215
216
217
218 public Iterator findPortletsByGroupCategory(String group, String category)
219 {
220 if ((group == null) || group.equals(""))
221 {
222 group = PortletEntry.DEFAULT_GROUP;
223 }
224
225 String key = group + "." + category;
226
227 CategoryIterator iterator = new CategoryIterator((SortedMap)catMap, key);
228
229 return iterator;
230 }
231
232
233
234
235
236
237 public Iterator listByCategory()
238 {
239 CategoryIterator iterator = new CategoryIterator((SortedMap)catMap, null);
240 return iterator;
241 }
242
243 /***
244 * Creates a new RegistryEntry instance compatible with the current
245 * Registry instance implementation
246 *
247 * @return the newly created RegistryEntry
248 */
249 public RegistryEntry createEntry()
250 {
251 return new BasePortletEntry();
252 }
253
254 /***
255 @see Registry#setEntry
256 */
257 public void setLocalEntry( RegistryEntry entry ) throws InvalidEntryException
258 {
259 super.setLocalEntry(entry);
260 setPortletEntry((PortletEntry)entry);
261 }
262
263 /***
264 @see Registry#addEntry
265 */
266 public void addLocalEntry( RegistryEntry entry ) throws InvalidEntryException
267 {
268 super.addLocalEntry(entry);
269 setPortletEntry((PortletEntry)entry);
270 }
271
272 /***
273 @see Registry#removeEntry
274 */
275 public void removeLocalEntry( String name )
276 {
277 if (name == null)
278 {
279 return;
280 }
281
282 RegistryEntry entry = (RegistryEntry)this.entries.get( name ) ;
283
284 if (entry == null)
285 {
286 return;
287 }
288
289 removeLocalEntry(entry);
290 }
291
292 /***
293 @see Registry#removeEntry
294 */
295 public void removeLocalEntry( RegistryEntry entry )
296 {
297 synchronized(catMap)
298 {
299 int count = 0;
300 Iterator it = ((PortletEntry)entry).listCategories();
301 while (it.hasNext())
302 {
303 Category category = (Category)it.next();
304 HashMap map = (HashMap)catMap.get(getCategoryKey(category));
305 if (map != null)
306 {
307 map.remove(entry.getName());
308 if (0 == map.size())
309 {
310 catMap.remove(getCategoryKey(category));
311 }
312 }
313 }
314 }
315 super.removeLocalEntry(entry);
316 }
317
318 }