KickJava   Java API By Example, From Geeks To Geeks.

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


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.server.ServerRegistrationID;
12 import org.jboss.portal.server.ThemeServer;
13 import org.jboss.portal.server.metadata.ThemeRegistrationMetaData;
14 import org.jboss.portal.server.theme.PortalTheme;
15 import org.jboss.portal.server.theme.ThemeException;
16 import org.jboss.system.Service;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * TODO: A description of this class.
28  *
29  * @author <a HREF="mailto:mholzner@novell.com">Martin Holzner</a>.
30  * @version <tt>$Revision: 1.4 $</tt>
31  * @jmx.mbean
32  * @jboss.xmbean
33  */

34 public class ThemeServerImpl implements ThemeServer, Service
35 {
36    private static Logger log = Logger.getLogger(ThemeServerImpl.class);
37    private Map JavaDoc themes;
38    private Map JavaDoc themeNames;
39    private String JavaDoc defaultName;
40
41    public ThemeServerImpl()
42    {
43       log.debug("ThemeServerImpl instantiated.");
44    }
45
46    /**
47     * @throws Exception
48     * @jmx.managed-operation
49     */

50    public void create() throws Exception JavaDoc
51    {
52       log.debug("create ThemeServerImpl ....");
53       themes = Collections.synchronizedMap(new HashMap JavaDoc());
54       themeNames = Collections.synchronizedMap(new HashMap JavaDoc());
55    }
56
57    /**
58     * @jmx.managed-operation
59     */

60    public void destroy()
61    {
62       log.debug("destroy ThemeServerImpl ....");
63       themes.clear();
64       themeNames.clear();
65    }
66
67    /**
68     * @throws Exception
69     * @jmx.managed-operation
70     */

71    public void start() throws Exception JavaDoc
72    {
73       log.debug("start ThemeServerImpl ....");
74    }
75
76    /**
77     * @jmx.managed-operation
78     */

79    public void stop()
80    {
81       log.debug("stop ThemeServerImpl ....");
82    }
83
84    public void addTheme(ThemeRegistrationMetaData metaData) throws ThemeException
85    {
86       log.debug("add theme: " + metaData);
87
88       // make sure the hash maps are initialized
89
if (themes == null)
90       {
91          try
92          {
93             create();
94          }
95          catch (Exception JavaDoc e)
96          {
97             throw new ThemeException(e);
98          }
99       }
100
101       if (ServerRegistrationID.TYPE_THEME.equals(metaData.getRegistrationID().getType()))
102       {
103          PortalTheme theme = (PortalTheme)metaData;
104          themeNames.put(theme.getName(), metaData.getRegistrationID());
105          themes.put(metaData.getRegistrationID(), theme);
106       }
107       else
108       {
109          throw new ThemeException("wrong meta data type: " + metaData.getRegistrationID().getType());
110       }
111    }
112
113    public void removeTheme(String JavaDoc name)
114    {
115       log.debug("remove theme: " + name);
116
117       if (name == null || "".equals(name))
118       {
119          throw new IllegalArgumentException JavaDoc("theme name not provided");
120       }
121       ServerRegistrationID themeID = (ServerRegistrationID)themeNames.get(name);
122       removeTheme(themeID);
123    }
124
125    public void removeTheme(ServerRegistrationID themeID)
126    {
127       if (themes.containsKey(themeID))
128       {
129          PortalTheme theme = (PortalTheme)themes.get(themeID);
130          log.debug("remove theme " + themeID + ": " + theme.getName());
131          themeNames.remove(theme.getName());
132          themes.remove(themeID);
133
134          // if the default was removed, set a random one
135
if (defaultName != null && defaultName.equals(theme.getName()))
136          {
137             log.debug("removed default theme; need to set a new default...");
138             Iterator JavaDoc keySet = themes.keySet().iterator();
139             if (keySet.hasNext())
140             {
141
142                ServerRegistrationID id = (ServerRegistrationID)keySet.next();
143                defaultName = ((PortalTheme)themes.get(id)).getName();
144                log.debug("set new default to " + defaultName);
145             }
146          }
147       }
148    }
149
150    /**
151     * Remove all themes for the given WebApp (ServletContext).
152     * <p>In case a web app gets removed (undeployed), and this web app contains registered portal themes,
153     * all themes of that web app, need to be removed to prevent unresolvable links, etc. </p>
154     *
155     * @param appName the name of the application that contains the themes that need to be removed
156     */

157    public void removeThemes(String JavaDoc appName)
158    {
159       //+++TODO: make this more efficient (see todo in addTheme)
160
List JavaDoc themesToDelete = new ArrayList JavaDoc();
161
162       // first get all the themes that fit the criteria (can't remove while iterating)
163
for (Iterator JavaDoc allThemes = themes.keySet().iterator(); allThemes.hasNext();)
164       {
165          PortalTheme theme = (PortalTheme)themes.get(allThemes.next());
166          if (theme.getAppName().equals(appName))
167          {
168             themesToDelete.add(theme);
169          }
170       }
171
172       // now remove them
173
for (Iterator JavaDoc toDelete = themesToDelete.iterator(); toDelete.hasNext();)
174       {
175          PortalTheme t = (PortalTheme)toDelete.next();
176          removeTheme((ServerRegistrationID)themeNames.get(t.getName()));
177       }
178    }
179
180    public void setDefault(ServerRegistrationID themeID) throws ThemeException
181    {
182       log.debug("set default theme " + themeID);
183       if (themes.keySet().contains(themeID))
184       {
185          defaultName = ((PortalTheme)themes.get(themeID)).getName();
186          log.debug("set new default theme to " + defaultName);
187          return;
188       }
189       throw new ThemeException("Theme with name [" + themeID + "] does not exist");
190    }
191
192    /**
193     * Set the default theme.
194     *
195     * @param name the name of the theme that is to be set as default
196     * @throws ThemeException if the theme is not part of the available themes
197     */

198    public void setDefault(String JavaDoc name) throws ThemeException
199    {
200       log.debug("set default theme " + name);
201       if (themeNames.keySet().contains(name))
202       {
203          defaultName = name;
204          return;
205       }
206       throw new ThemeException("Theme with name [" + name + "] does not exist");
207    }
208
209    public PortalTheme getTheme(ServerRegistrationID themeID, boolean defaultOnNull)
210    {
211       log.debug("get theme " + themeID);
212       if (themeID == null)
213       {
214          throw new IllegalArgumentException JavaDoc("Theme ID must not be null");
215       }
216
217       if (!themes.keySet().contains(themeID) && defaultOnNull && defaultName != null)
218       {
219          return (PortalTheme)themes.get(themeNames.get(defaultName));
220       }
221
222       return (PortalTheme)themes.get(themeID);
223    }
224
225    /**
226     * Lookup the theme with the provided name and return it.
227     *
228     * @param name the theme to look for
229     * @param defaultOnNull if true, return the default theme if the theme with the provided name is not available
230     * @return the theme that matches the provided name, or the default theme, if <code>defaultOnNull</code> is true, and
231     * there is no theme with the provided name
232     * @throws IllegalArgumentException if the provided name is null or empty
233     */

234    public PortalTheme getTheme(String JavaDoc name, boolean defaultOnNull)
235    {
236       log.debug("get theme " + name);
237       if (name == null || "".equals(name))
238       {
239          throw new IllegalArgumentException JavaDoc("Name cannot be null or empty");
240       }
241
242       if (themeNames.keySet().contains(name))
243       {
244          log.debug("found theme " + name);
245          return (PortalTheme)themes.get(themeNames.get(name));
246       }
247       else if (defaultOnNull && defaultName != null)
248       {
249          log.debug("returning default theme " + defaultName);
250          return (PortalTheme)themes.get(themeNames.get(defaultName));
251       }
252
253       log.warn("Theme with name [" + name + "] not found");
254
255       return null;
256    }
257
258    public Collection JavaDoc getThemes()
259    {
260       return Collections.unmodifiableCollection(themes.values());
261    }
262
263    public Collection JavaDoc getThemeNames()
264    {
265       return Collections.unmodifiableCollection(themeNames.keySet());
266    }
267 }
268
Popular Tags