KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > server > impl > LayoutServerImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.portal.server.impl;
9
10 import org.apache.log4j.Logger;
11 import org.jboss.portal.common.MediaType;
12 import org.jboss.portal.server.LayoutServer;
13 import org.jboss.portal.server.ServerRegistrationID;
14 import org.jboss.portal.server.metadata.LayoutRegistrationMetaData;
15 import org.jboss.portal.server.theme.LayoutException;
16 import org.jboss.portal.server.theme.PortalLayout;
17 import org.jboss.portal.server.theme.PortalRenderSet;
18 import org.jboss.portal.server.theme.strategy.AbstractLayoutStrategy;
19 import org.jboss.portal.server.theme.strategy.LayoutStrategy;
20 import org.jboss.system.Service;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * Implementaion of the layout server.
31  * <p>The layout server is a registry of all available layouts. The server also allows access to all available render sets
32  * and layout strategies. Render sets and layout strategies can be independent (named), or children of a layout.
33  * Accordingly, the layout server provides accessor methods to get strategies and render set by name, or by layout.
34  * Render sets and layout strategies can, and must, be defined for a specific media type (mime type).</p>
35  *
36  * @author <a HREF="mailto:mholzner@novell.com">Martin Holzner</a>.
37  * @version <tt>$Revision: 1.7 $</tt>
38  * @jmx.mbean
39  * @jboss.xmbean
40  * @see PortalLayout
41  * @see PortalRenderSet
42  * @see LayoutStrategy
43  * @see MediaType
44  */

45 public class LayoutServerImpl implements LayoutServer, Service
46 {
47    private static Logger log = Logger.getLogger(LayoutServerImpl.class);
48    // map of layout id to layout
49
private Map JavaDoc layouts;
50    // map of layout name to layout id
51
private Map JavaDoc layoutNames;
52    // map of appname + "." + layout name to layout id
53
private Map JavaDoc exactLayoutNames;
54    // map of strategy name to layout strategy
55
private Map JavaDoc namedStrategies;
56    // map of appName + "." + strategy name to layout strategy
57
private Map JavaDoc exactNamedStrategies;
58    // map of render set name to render set
59
private Map JavaDoc namedRenderSets;
60    // map of appName + "." + render set name to render set
61
private Map JavaDoc exactNamedRenderSets;
62
63    private ServerRegistrationID defaultID;
64
65    public LayoutServerImpl()
66    {
67       log.debug("LayoutServerImpl instantiated.");
68    }
69
70    /**
71     * @jmx.managed-operation
72     * @see org.jboss.system.Service#create()
73     */

74    public void create() throws Exception JavaDoc
75    {
76       log.debug("create LayoutServerImpl ....");
77       layouts = Collections.synchronizedMap(new HashMap JavaDoc());
78       layoutNames = Collections.synchronizedMap(new HashMap JavaDoc());
79       exactLayoutNames = Collections.synchronizedMap(new HashMap JavaDoc());
80       namedStrategies = Collections.synchronizedMap(new HashMap JavaDoc());
81       exactNamedStrategies = Collections.synchronizedMap(new HashMap JavaDoc());
82       namedRenderSets = Collections.synchronizedMap(new HashMap JavaDoc());
83       exactNamedRenderSets = Collections.synchronizedMap(new HashMap JavaDoc());
84    }
85
86    /**
87     * @jmx.managed-operation
88     * @see org.jboss.system.Service#destroy()
89     */

90    public void destroy()
91    {
92       log.debug("destroy LayoutServerImpl ....");
93       layouts.clear();
94       layoutNames.clear();
95       exactLayoutNames.clear();
96       namedStrategies.clear();
97       exactNamedStrategies.clear();
98       namedRenderSets.clear();
99       exactNamedRenderSets.clear();
100    }
101
102    /**
103     * @jmx.managed-operation
104     * @see org.jboss.system.Service#start()
105     */

106    public void start() throws Exception JavaDoc
107    {
108       log.debug("start LayoutServerImpl ....");
109    }
110
111    /**
112     * @jmx.managed-operation
113     * @see org.jboss.system.Service#stop()
114     */

115    public void stop()
116    {
117       log.debug("stop LayoutServerImpl ....");
118    }
119
120    /**
121     * @see LayoutServer#addLayout(LayoutRegistrationMetaData)
122     */

123    public void addLayout(LayoutRegistrationMetaData metaData) throws LayoutException
124    {
125       log.debug("adding layout: " + metaData);
126       if (metaData == null)
127       {
128          throw new IllegalArgumentException JavaDoc("Layout metaData is null");
129       }
130
131       if (layouts == null)
132       {
133          try
134          {
135             create();
136          }
137          catch (Exception JavaDoc e)
138          {
139             throw new LayoutException(e);
140          }
141       }
142       layouts.put(metaData.getRegistrationID(), metaData);
143       layoutNames.put(((PortalLayout)metaData).getName(), metaData.getRegistrationID());
144       String JavaDoc key = ((PortalLayout)metaData).getAppName() + "." + ((PortalLayout)metaData).getName();
145       exactLayoutNames.put(key, metaData.getRegistrationID());
146    }
147
148    /**
149     * @see LayoutServer#setDefaultLayout(String)
150     */

151    public void setDefaultLayout(String JavaDoc name) throws LayoutException
152    {
153       log.debug("setting default: " + name);
154
155       if (exactLayoutNames.keySet().contains(name))
156       {
157          defaultID = (ServerRegistrationID)exactLayoutNames.get(name);
158       }
159       else if (layoutNames.keySet().contains(name))
160       {
161          defaultID = (ServerRegistrationID)layoutNames.get(name);
162       }
163       else
164       {
165          throw new LayoutException("Cannot set default: " + name + " is not a valid layout");
166       }
167    }
168
169    /**
170     * @see LayoutServer#getLayout(org.jboss.portal.server.ServerRegistrationID, boolean)
171     */

172    public PortalLayout getLayout(ServerRegistrationID id, boolean defaultOnNull)
173    {
174       log.debug("get " + id + "...");
175       if (id == null)
176       {
177          throw new IllegalArgumentException JavaDoc("No null id argument allowed");
178       }
179
180       if (!layouts.keySet().contains(id) && defaultOnNull && defaultID != null)
181       {
182          return (PortalLayout)layouts.get(defaultID);
183       }
184       return (PortalLayout)layouts.get(id);
185    }
186
187    /**
188     * @see LayoutServer#getLayout(String, boolean)
189     */

190    public PortalLayout getLayout(String JavaDoc name, boolean defaultOnNull)
191    {
192       log.debug("get " + name + "...");
193       if (name == null)
194       {
195          throw new IllegalArgumentException JavaDoc("No null name argument allowed");
196       }
197
198       if (exactLayoutNames.containsKey(name))
199       {
200          log.debug("found exact: " + name);
201          return (PortalLayout)layouts.get(exactLayoutNames.get(name));
202       }
203       else if (layoutNames.containsKey(name))
204       {
205          log.debug("found " + name);
206          return (PortalLayout)layouts.get(layoutNames.get(name));
207       }
208       else if (defaultOnNull && defaultID != null)
209       {
210          log.debug("try to return default " + name);
211          return (PortalLayout)layouts.get(defaultID);
212       }
213
214       log.debug("oops , not found " + name);
215       return null;
216    }
217
218    /**
219     * Remove the layout identified by the provided registration id.
220     *
221     * @param id the id of the layout that needs to be removed
222     * @throws LayoutException
223     */

224    public void removeLayout(ServerRegistrationID id) throws LayoutException
225    {
226       log.debug("removing layout with id: " + id);
227       if (id == null)
228       {
229          throw new IllegalArgumentException JavaDoc("No null id argument allowed");
230       }
231
232       PortalLayout layout = (PortalLayout)layouts.get(id);
233
234       if (layout == null)
235       {
236          log.debug("Not found. Nothing to remove.");
237          return;
238       }
239
240       final String JavaDoc layoutName = layout.getAppName() + "." + layout.getName();
241       if (exactLayoutNames.keySet().contains(layoutName))
242       {
243          log.debug("removing layout exact " + layoutName);
244          exactLayoutNames.remove(layoutName);
245       }
246       else if (layoutNames.keySet().contains(layout.getName()))
247       {
248          log.debug("removing layout " + layout.getName());
249          layoutNames.remove(layout.getName());
250       }
251
252       layouts.remove(id);
253
254       if (defaultID != null && defaultID.equals(id))
255       {
256          log.debug("removed default layout, need to set new one...");
257          Iterator JavaDoc i = layouts.keySet().iterator();
258
259          if (i.hasNext())
260          {
261             defaultID = (ServerRegistrationID)i.next();
262             layout = (PortalLayout)layouts.get(defaultID);
263             log.debug("set default layout to " + layout.getAppName() + "." + layout.getName());
264          }
265       }
266    }
267
268    /**
269     * @see LayoutServer#removeLayouts(String)
270     */

271    public void removeLayouts(String JavaDoc appName) throws LayoutException
272    {
273       List JavaDoc layoutsToDelete = new ArrayList JavaDoc();
274       ServerRegistrationID id = null;
275
276       // first get all the layouts that fit the criteria (can't remove while iterating)
277
for (Iterator JavaDoc allLayouts = layouts.keySet().iterator(); allLayouts.hasNext();)
278       {
279          id = (ServerRegistrationID)allLayouts.next();
280          PortalLayout layout = (PortalLayout)layouts.get(id);
281          if (layout.getAppName().equals(appName))
282          {
283             layoutsToDelete.add(id);
284          }
285       }
286
287       // now remove them
288
for (Iterator JavaDoc toDelete = layoutsToDelete.iterator(); toDelete.hasNext();)
289       {
290          removeLayout((ServerRegistrationID)toDelete.next());
291       }
292    }
293
294    /**
295     * @see LayoutServer#addStrategy(org.jboss.portal.server.theme.strategy.AbstractLayoutStrategy)
296     */

297    public void addStrategy(AbstractLayoutStrategy strategy)
298    {
299       log.debug("adding : " + strategy.getName());
300       List JavaDoc strategies = (List JavaDoc)namedStrategies.get(strategy.getName());
301       if (strategies == null)
302       {
303          strategies = new ArrayList JavaDoc();
304       }
305       strategies.add(strategy);
306       exactNamedStrategies.put(strategy.getAppName() + "." + strategy.getName(), strategies);
307       namedStrategies.put(strategy.getName(), strategies);
308    }
309
310    /**
311     * @see LayoutServer#getStrategy(org.jboss.portal.server.theme.PortalLayout, org.jboss.portal.common.MediaType)
312     */

313    public LayoutStrategy getStrategy(PortalLayout layout, MediaType mediaType)
314    {
315       return layout.getStrategy(mediaType);
316    }
317
318    /**
319     * @see LayoutServer#getStrategy(String, org.jboss.portal.common.MediaType)
320     */

321    public LayoutStrategy getStrategy(String JavaDoc strategyName, MediaType mediaType)
322    {
323       List JavaDoc strategyList = null;
324
325       strategyList = (List JavaDoc)exactNamedStrategies.get(strategyName);
326       if (strategyList == null)
327       {
328          strategyList = (List JavaDoc)namedStrategies.get(strategyName);
329       }
330
331       if (strategyList != null)
332       {
333          for (Iterator JavaDoc i = strategyList.iterator(); i.hasNext();)
334          {
335             AbstractLayoutStrategy strategy = (AbstractLayoutStrategy)i.next();
336             if (mediaType.equals(strategy.getMediaType()))
337             {
338                return strategy;
339             }
340          }
341       }
342       return null;
343    }
344
345    /**
346     * @see LayoutServer#removeStrategies(String)
347     */

348    public void removeStrategies(String JavaDoc appName) throws LayoutException
349    {
350       log.debug("removing named strategies for : " + appName);
351       List JavaDoc strategiesToDelete = new ArrayList JavaDoc();
352       for (Iterator JavaDoc i = namedStrategies.values().iterator(); i.hasNext();)
353       {
354          List JavaDoc strategies = (List JavaDoc)i.next();
355
356          Iterator JavaDoc s = strategies.iterator();
357
358          if (s.hasNext())
359          {
360             AbstractLayoutStrategy strategy = (AbstractLayoutStrategy)s.next();
361             if (appName.equals(strategy.getAppName()))
362             {
363                strategiesToDelete.add(strategy);
364             }
365          }
366       }
367
368       for (Iterator JavaDoc i = strategiesToDelete.iterator(); i.hasNext();)
369       {
370          AbstractLayoutStrategy strategy = (AbstractLayoutStrategy)i.next();
371          String JavaDoc key = strategy.getAppName() + "." + strategy.getName();
372          log.debug("removing render sets: " + key);
373          exactNamedStrategies.remove(key);
374          namedStrategies.remove(strategy.getName());
375       }
376    }
377
378    /**
379     * @see LayoutServer#addRenderSet(org.jboss.portal.server.theme.PortalRenderSet)
380     */

381    public void addRenderSet(PortalRenderSet renderSet)
382    {
383       log.debug("adding : " + renderSet.getAppName() + "." + renderSet.getName());
384       List JavaDoc renderSets = (List JavaDoc)namedRenderSets.get(renderSet.getName());
385       if (renderSets == null)
386       {
387          renderSets = new ArrayList JavaDoc();
388       }
389       renderSets.add(renderSet);
390       namedRenderSets.put(renderSet.getName(), renderSets);
391       exactNamedRenderSets.put(renderSet.getAppName() + "." + renderSet.getName(), renderSets);
392    }
393
394    /**
395     * @see LayoutServer#getRenderSet(org.jboss.portal.server.theme.PortalLayout, org.jboss.portal.common.MediaType)
396     */

397    public PortalRenderSet getRenderSet(PortalLayout layout, MediaType mediaType)
398    {
399       return layout.getRenderSet(mediaType);
400    }
401
402    /**
403     * @see LayoutServer#getRenderSet(String, org.jboss.portal.common.MediaType)
404     */

405    public PortalRenderSet getRenderSet(String JavaDoc renderSetName, MediaType mediaType)
406    {
407       List JavaDoc setList = null;
408       setList = (List JavaDoc)exactNamedRenderSets.get(renderSetName);
409
410       if (setList == null)
411       {
412          setList = (List JavaDoc)namedRenderSets.get(renderSetName);
413       }
414
415       if (setList != null)
416       {
417          for (Iterator JavaDoc i = setList.iterator(); i.hasNext();)
418          {
419             PortalRenderSet set = (PortalRenderSet)i.next();
420             if (mediaType.equals(set.getMediaType()))
421             {
422                return set;
423             }
424          }
425       }
426       return null;
427    }
428
429    /**
430     * @see LayoutServer#removeRenderSets(String)
431     */

432    public void removeRenderSets(String JavaDoc appName) throws LayoutException
433    {
434       log.debug("removing named render sets for : " + appName);
435       List JavaDoc renderSetsToDelete = new ArrayList JavaDoc();
436       for (Iterator JavaDoc i = namedRenderSets.values().iterator(); i.hasNext();)
437       {
438          List JavaDoc renderSets = (List JavaDoc)i.next();
439          Iterator JavaDoc s = renderSets.iterator();
440          if (s.hasNext())
441          {
442             PortalRenderSet renderSet = (PortalRenderSet)s.next();
443             if (appName.equals(renderSet.getAppName()))
444             {
445                renderSetsToDelete.add(renderSet);
446             }
447          }
448       }
449
450       for (Iterator JavaDoc i = renderSetsToDelete.iterator(); i.hasNext();)
451       {
452          PortalRenderSet renderSet = (PortalRenderSet)i.next();
453          String JavaDoc key = renderSet.getAppName() + "." + renderSet.getName();
454          log.debug("removing render sets: " + key);
455          exactNamedRenderSets.remove(key);
456          namedRenderSets.remove(renderSet.getName());
457       }
458    }
459 }
460
Popular Tags