1 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 ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 45 public class LayoutServerImpl implements LayoutServer, Service 46 { 47 private static Logger log = Logger.getLogger(LayoutServerImpl.class); 48 private Map layouts; 50 private Map layoutNames; 52 private Map exactLayoutNames; 54 private Map namedStrategies; 56 private Map exactNamedStrategies; 58 private Map namedRenderSets; 60 private Map exactNamedRenderSets; 62 63 private ServerRegistrationID defaultID; 64 65 public LayoutServerImpl() 66 { 67 log.debug("LayoutServerImpl instantiated."); 68 } 69 70 74 public void create() throws Exception 75 { 76 log.debug("create LayoutServerImpl ...."); 77 layouts = Collections.synchronizedMap(new HashMap ()); 78 layoutNames = Collections.synchronizedMap(new HashMap ()); 79 exactLayoutNames = Collections.synchronizedMap(new HashMap ()); 80 namedStrategies = Collections.synchronizedMap(new HashMap ()); 81 exactNamedStrategies = Collections.synchronizedMap(new HashMap ()); 82 namedRenderSets = Collections.synchronizedMap(new HashMap ()); 83 exactNamedRenderSets = Collections.synchronizedMap(new HashMap ()); 84 } 85 86 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 106 public void start() throws Exception 107 { 108 log.debug("start LayoutServerImpl ...."); 109 } 110 111 115 public void stop() 116 { 117 log.debug("stop LayoutServerImpl ...."); 118 } 119 120 123 public void addLayout(LayoutRegistrationMetaData metaData) throws LayoutException 124 { 125 log.debug("adding layout: " + metaData); 126 if (metaData == null) 127 { 128 throw new IllegalArgumentException ("Layout metaData is null"); 129 } 130 131 if (layouts == null) 132 { 133 try 134 { 135 create(); 136 } 137 catch (Exception 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 key = ((PortalLayout)metaData).getAppName() + "." + ((PortalLayout)metaData).getName(); 145 exactLayoutNames.put(key, metaData.getRegistrationID()); 146 } 147 148 151 public void setDefaultLayout(String 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 172 public PortalLayout getLayout(ServerRegistrationID id, boolean defaultOnNull) 173 { 174 log.debug("get " + id + "..."); 175 if (id == null) 176 { 177 throw new IllegalArgumentException ("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 190 public PortalLayout getLayout(String name, boolean defaultOnNull) 191 { 192 log.debug("get " + name + "..."); 193 if (name == null) 194 { 195 throw new IllegalArgumentException ("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 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 ("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 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 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 271 public void removeLayouts(String appName) throws LayoutException 272 { 273 List layoutsToDelete = new ArrayList (); 274 ServerRegistrationID id = null; 275 276 for (Iterator 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 for (Iterator toDelete = layoutsToDelete.iterator(); toDelete.hasNext();) 289 { 290 removeLayout((ServerRegistrationID)toDelete.next()); 291 } 292 } 293 294 297 public void addStrategy(AbstractLayoutStrategy strategy) 298 { 299 log.debug("adding : " + strategy.getName()); 300 List strategies = (List )namedStrategies.get(strategy.getName()); 301 if (strategies == null) 302 { 303 strategies = new ArrayList (); 304 } 305 strategies.add(strategy); 306 exactNamedStrategies.put(strategy.getAppName() + "." + strategy.getName(), strategies); 307 namedStrategies.put(strategy.getName(), strategies); 308 } 309 310 313 public LayoutStrategy getStrategy(PortalLayout layout, MediaType mediaType) 314 { 315 return layout.getStrategy(mediaType); 316 } 317 318 321 public LayoutStrategy getStrategy(String strategyName, MediaType mediaType) 322 { 323 List strategyList = null; 324 325 strategyList = (List )exactNamedStrategies.get(strategyName); 326 if (strategyList == null) 327 { 328 strategyList = (List )namedStrategies.get(strategyName); 329 } 330 331 if (strategyList != null) 332 { 333 for (Iterator 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 348 public void removeStrategies(String appName) throws LayoutException 349 { 350 log.debug("removing named strategies for : " + appName); 351 List strategiesToDelete = new ArrayList (); 352 for (Iterator i = namedStrategies.values().iterator(); i.hasNext();) 353 { 354 List strategies = (List )i.next(); 355 356 Iterator 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 i = strategiesToDelete.iterator(); i.hasNext();) 369 { 370 AbstractLayoutStrategy strategy = (AbstractLayoutStrategy)i.next(); 371 String 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 381 public void addRenderSet(PortalRenderSet renderSet) 382 { 383 log.debug("adding : " + renderSet.getAppName() + "." + renderSet.getName()); 384 List renderSets = (List )namedRenderSets.get(renderSet.getName()); 385 if (renderSets == null) 386 { 387 renderSets = new ArrayList (); 388 } 389 renderSets.add(renderSet); 390 namedRenderSets.put(renderSet.getName(), renderSets); 391 exactNamedRenderSets.put(renderSet.getAppName() + "." + renderSet.getName(), renderSets); 392 } 393 394 397 public PortalRenderSet getRenderSet(PortalLayout layout, MediaType mediaType) 398 { 399 return layout.getRenderSet(mediaType); 400 } 401 402 405 public PortalRenderSet getRenderSet(String renderSetName, MediaType mediaType) 406 { 407 List setList = null; 408 setList = (List )exactNamedRenderSets.get(renderSetName); 409 410 if (setList == null) 411 { 412 setList = (List )namedRenderSets.get(renderSetName); 413 } 414 415 if (setList != null) 416 { 417 for (Iterator 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 432 public void removeRenderSets(String appName) throws LayoutException 433 { 434 log.debug("removing named render sets for : " + appName); 435 List renderSetsToDelete = new ArrayList (); 436 for (Iterator i = namedRenderSets.values().iterator(); i.hasNext();) 437 { 438 List renderSets = (List )i.next(); 439 Iterator 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 i = renderSetsToDelete.iterator(); i.hasNext();) 451 { 452 PortalRenderSet renderSet = (PortalRenderSet)i.next(); 453 String 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 |