1 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 ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 34 public class ThemeServerImpl implements ThemeServer, Service 35 { 36 private static Logger log = Logger.getLogger(ThemeServerImpl.class); 37 private Map themes; 38 private Map themeNames; 39 private String defaultName; 40 41 public ThemeServerImpl() 42 { 43 log.debug("ThemeServerImpl instantiated."); 44 } 45 46 50 public void create() throws Exception 51 { 52 log.debug("create ThemeServerImpl ...."); 53 themes = Collections.synchronizedMap(new HashMap ()); 54 themeNames = Collections.synchronizedMap(new HashMap ()); 55 } 56 57 60 public void destroy() 61 { 62 log.debug("destroy ThemeServerImpl ...."); 63 themes.clear(); 64 themeNames.clear(); 65 } 66 67 71 public void start() throws Exception 72 { 73 log.debug("start ThemeServerImpl ...."); 74 } 75 76 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 if (themes == null) 90 { 91 try 92 { 93 create(); 94 } 95 catch (Exception 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 name) 114 { 115 log.debug("remove theme: " + name); 116 117 if (name == null || "".equals(name)) 118 { 119 throw new IllegalArgumentException ("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 (defaultName != null && defaultName.equals(theme.getName())) 136 { 137 log.debug("removed default theme; need to set a new default..."); 138 Iterator 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 157 public void removeThemes(String appName) 158 { 159 List themesToDelete = new ArrayList (); 161 162 for (Iterator 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 for (Iterator 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 198 public void setDefault(String 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 ("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 234 public PortalTheme getTheme(String name, boolean defaultOnNull) 235 { 236 log.debug("get theme " + name); 237 if (name == null || "".equals(name)) 238 { 239 throw new IllegalArgumentException ("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 getThemes() 259 { 260 return Collections.unmodifiableCollection(themes.values()); 261 } 262 263 public Collection getThemeNames() 264 { 265 return Collections.unmodifiableCollection(themeNames.keySet()); 266 } 267 } 268 | Popular Tags |