1 16 package org.apache.cocoon.portal.profile.impl; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.CascadingRuntimeException; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceSelector; 28 import org.apache.cocoon.ProcessingException; 29 import org.apache.cocoon.portal.PortalService; 30 import org.apache.cocoon.portal.coplet.CopletData; 31 import org.apache.cocoon.portal.coplet.CopletFactory; 32 import org.apache.cocoon.portal.coplet.CopletInstanceData; 33 import org.apache.cocoon.portal.coplet.adapter.CopletAdapter; 34 import org.apache.cocoon.portal.layout.CompositeLayout; 35 import org.apache.cocoon.portal.layout.Item; 36 import org.apache.cocoon.portal.layout.Layout; 37 import org.apache.cocoon.portal.layout.LayoutFactory; 38 39 47 public abstract class AbstractUserProfileManager 48 extends AbstractProfileManager { 49 50 53 public void login() { 54 super.login(); 55 this.getPortalLayout(null, null); 58 } 59 60 63 public void logout() { 64 PortalService service = null; 65 ServiceSelector adapterSelector = null; 66 try { 67 adapterSelector = (ServiceSelector)this.manager.lookup(CopletAdapter.ROLE+"Selector"); 68 service = (PortalService)this.manager.lookup(PortalService.ROLE); 69 final String layoutKey = service.getDefaultLayoutKey(); 70 71 CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute("CopletInstanceData:"+layoutKey); 72 if ( copletInstanceDataManager != null ) { 73 Iterator iter = copletInstanceDataManager.getCopletInstanceData().values().iterator(); 74 while ( iter.hasNext() ) { 75 CopletInstanceData cid = (CopletInstanceData) iter.next(); 76 CopletAdapter adapter = null; 77 try { 78 adapter = (CopletAdapter)adapterSelector.select(cid.getCopletData().getCopletBaseData().getCopletAdapterName()); 79 adapter.logout( cid ); 80 } finally { 81 adapterSelector.release( adapter ); 82 } 83 } 84 } 85 86 service.removeAttribute("CopletData:"+layoutKey); 87 service.removeAttribute("CopletInstanceData:"+layoutKey); 88 service.removeAttribute("Layout:"+layoutKey); 89 } catch (ServiceException e) { 90 throw new CascadingRuntimeException("Unable to lookup portal service.", e); 91 } finally { 92 this.manager.release(service); 93 this.manager.release(adapterSelector); 94 } 95 super.logout(); 96 } 97 98 102 protected void cacheLayouts(Map layoutMap, Layout layout) { 103 if ( layout != null ) { 104 if ( layout.getId() != null ) { 105 layoutMap.put( layout.getId(), layout ); 106 } 107 if ( layout instanceof CompositeLayout ) { 108 CompositeLayout cl = (CompositeLayout)layout; 109 Iterator i = cl.getItems().iterator(); 110 while ( i.hasNext() ) { 111 Item current = (Item)i.next(); 112 this.cacheLayouts( layoutMap, current.getLayout() ); 113 } 114 } 115 } 116 117 } 118 119 122 protected void prepareObject(Object object, Object factory) 123 throws ProcessingException { 124 if (factory != null && object != null) { 125 if (object instanceof Layout) { 126 ((LayoutFactory)factory).prepareLayout((Layout)object); 127 } else if (object instanceof CopletDataManager) { 128 CopletFactory copletFactory = (CopletFactory)factory; 129 Iterator iterator = ((CopletDataManager)object).getCopletData().values().iterator(); 130 while (iterator.hasNext()) { 131 CopletData cd = (CopletData)iterator.next(); 132 copletFactory.prepare(cd); 133 } 134 } else if (object instanceof CopletInstanceDataManager) { 135 CopletFactory copletFactory = (CopletFactory)factory; 136 Iterator iterator = ((CopletInstanceDataManager)object).getCopletInstanceData().values().iterator(); 137 while (iterator.hasNext()) { 138 CopletInstanceData cid = (CopletInstanceData)iterator.next(); 139 copletFactory.prepare(cid); 140 } 141 } 142 } 143 } 144 145 148 public CopletInstanceData getCopletInstanceData(String copletID) { 149 PortalService service = null; 150 String attribute = null; 151 try { 152 service = (PortalService) this.manager.lookup(PortalService.ROLE); 153 final String layoutKey = service.getDefaultLayoutKey(); 154 155 attribute = "CopletInstanceData:"+layoutKey; 156 CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute); 157 158 return copletInstanceDataManager.getCopletInstanceData(copletID); 159 } catch (ServiceException e) { 160 throw new CascadingRuntimeException("Unable to lookup portal service.", e); 161 } finally { 162 this.manager.release(service); 163 } 164 } 165 166 169 public CopletData getCopletData(String copletDataId) { 170 PortalService service = null; 171 String attribute = null; 172 try { 173 service = (PortalService) this.manager.lookup(PortalService.ROLE); 174 final String layoutKey = service.getDefaultLayoutKey(); 175 176 attribute = "CopletInstanceData:"+layoutKey; 177 CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute); 178 179 Iterator i = copletInstanceDataManager.getCopletInstanceData().values().iterator(); 180 boolean found = false; 181 CopletInstanceData current = null; 182 while ( !found && i.hasNext() ) { 183 current = (CopletInstanceData)i.next(); 184 found = current.getCopletData().getId().equals(copletDataId); 185 } 186 if ( found ) { 187 return current.getCopletData(); 188 } 189 return null; 190 } catch (ServiceException e) { 191 throw new CascadingRuntimeException("Unable to lookup portal service.", e); 192 } finally { 193 this.manager.release(service); 194 } 195 } 196 197 200 public List getCopletInstanceData(CopletData data) { 201 List coplets = new ArrayList (); 202 PortalService service = null; 203 String attribute = null; 204 try { 205 service = (PortalService) this.manager.lookup(PortalService.ROLE); 206 final String layoutKey = service.getDefaultLayoutKey(); 207 208 attribute = "CopletInstanceData:" + layoutKey; 209 CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute); 210 211 Iterator iter = copletInstanceDataManager.getCopletInstanceData().values().iterator(); 212 while ( iter.hasNext() ) { 213 CopletInstanceData current = (CopletInstanceData)iter.next(); 214 if ( current.getCopletData().equals(data) ) { 215 coplets.add( current ); 216 } 217 } 218 return coplets; 219 } catch (ServiceException e) { 220 throw new CascadingRuntimeException("Unable to lookup portal service.", e); 221 } finally { 222 this.manager.release(service); 223 } 224 } 225 226 229 public void register(CopletInstanceData coplet) { 230 PortalService service = null; 231 String attribute = null; 232 try { 233 service = (PortalService) this.manager.lookup(PortalService.ROLE); 234 final String layoutKey = service.getDefaultLayoutKey(); 235 236 attribute = "CopletInstanceData:" + layoutKey; 237 CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute); 238 239 copletInstanceDataManager.putCopletInstanceData( coplet ); 240 241 } catch (ServiceException e) { 242 throw new CascadingRuntimeException("Unable to lookup portal service.", e); 243 } finally { 244 this.manager.release(service); 245 } 246 } 247 248 251 public void unregister(CopletInstanceData coplet) { 252 PortalService service = null; 253 String attribute = null; 254 try { 255 service = (PortalService) this.manager.lookup(PortalService.ROLE); 256 final String layoutKey = service.getDefaultLayoutKey(); 257 258 attribute = "CopletInstanceData:" + layoutKey; 259 CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute); 260 261 copletInstanceDataManager.getCopletInstanceData().remove(coplet.getId()); 262 263 } catch (ServiceException e) { 264 throw new CascadingRuntimeException("Unable to lookup portal service.", e); 265 } finally { 266 this.manager.release(service); 267 } 268 } 269 270 273 public void register(Layout layout) { 274 PortalService service = null; 275 try { 276 service = (PortalService) this.manager.lookup(PortalService.ROLE); 277 final String layoutKey = service.getDefaultLayoutKey(); 278 279 Map layoutMap = (Map )service.getAttribute("Layout-Map:" + layoutKey); 280 if ( layoutMap == null ) { 281 layout = (Layout)service.getAttribute("Layout:" + layoutKey); 282 if (layout != null) { 283 layoutMap = new HashMap (); 284 this.cacheLayouts(layoutMap, layout); 285 service.setAttribute("Layout-Map:" + layoutKey, layoutMap); 286 } 287 } 288 289 if ( layoutMap != null) { 290 layoutMap.put(layout.getId(), layout); 291 } 292 293 } catch (ServiceException e) { 294 throw new CascadingRuntimeException("Unable to lookup portal service.", e); 295 } finally { 296 this.manager.release(service); 297 } 298 } 299 300 303 public void unregister(Layout layout) { 304 PortalService service = null; 305 try { 306 service = (PortalService) this.manager.lookup(PortalService.ROLE); 307 final String layoutKey = service.getDefaultLayoutKey(); 308 309 Map layoutMap = (Map )service.getAttribute("Layout-Map:" + layoutKey); 310 311 if ( layoutMap != null) { 312 layoutMap.remove(layout.getId()); 313 } 314 315 } catch (ServiceException e) { 316 throw new CascadingRuntimeException("Unable to lookup portal service.", e); 317 } finally { 318 this.manager.release(service); 319 } 320 } 321 322 325 public Layout getPortalLayout(String layoutKey, String layoutID) { 326 PortalService service = null; 327 ServiceSelector adapterSelector = null; 328 329 try { 330 service = (PortalService) this.manager.lookup(PortalService.ROLE); 331 LayoutFactory factory = service.getComponentManager().getLayoutFactory(); 332 CopletFactory copletFactory = service.getComponentManager().getCopletFactory(); 333 334 adapterSelector = (ServiceSelector)this.manager.lookup(CopletAdapter.ROLE+"Selector"); 335 336 if ( null == layoutKey ) { 337 layoutKey = service.getDefaultLayoutKey(); 338 } 339 340 final String layoutAttributeKey = "Layout:" + layoutKey; 341 final String layoutObjectsAttributeKey = "Layout-Map:" + layoutKey; 342 343 Layout layout = (Layout)service.getAttribute(layoutAttributeKey); 344 if (layout == null) { 345 layout = this.loadProfile(layoutKey, service, copletFactory, factory, adapterSelector); 346 } 347 348 if ( layoutID != null ) { 349 Map layoutMap = (Map )service.getAttribute(layoutObjectsAttributeKey); 351 if ( layoutMap == null ) { 352 layoutMap = new HashMap (); 353 this.cacheLayouts(layoutMap, layout); 354 service.setAttribute(layoutObjectsAttributeKey, layoutMap); 355 } 356 if ( layoutMap != null) { 357 return (Layout) layoutMap.get( layoutID ); 358 } 359 } 360 361 return layout; 362 } catch (Exception ce) { 363 throw new CascadingRuntimeException("Exception during loading of profile.", ce); 364 } finally { 365 this.manager.release(service); 366 this.manager.release(adapterSelector); 367 } 368 } 369 370 373 protected abstract Layout loadProfile(String layoutKey, 374 PortalService service, 375 CopletFactory copletFactory, 376 LayoutFactory layoutFactory, 377 ServiceSelector adapterSelector) 378 throws Exception ; 379 380 383 public Collection getCopletDatas() { 384 PortalService service = null; 385 try { 386 service = (PortalService) this.manager.lookup(PortalService.ROLE); 387 final String layoutKey = service.getDefaultLayoutKey(); 388 CopletDataManager manager = (CopletDataManager)service.getAttribute("CopletData:" + layoutKey); 389 return manager.getCopletData().values(); 390 } catch (Exception e) { 391 throw new CascadingRuntimeException("Error in getCopletDatas.", e); 392 } finally { 393 this.manager.release(service); 394 } 395 } 396 397 400 public Collection getCopletInstanceDatas() { 401 PortalService service = null; 402 try { 403 service = (PortalService) this.manager.lookup(PortalService.ROLE); 404 final String layoutKey = service.getDefaultLayoutKey(); 405 CopletInstanceDataManager manager = (CopletInstanceDataManager)service.getAttribute("CopletInstanceData:" + layoutKey); 406 return manager.getCopletInstanceData().values(); 407 } catch (Exception e) { 408 throw new CascadingRuntimeException("Error in getCopletInstanceDatas.", e); 409 } finally { 410 this.manager.release(service); 411 } 412 } 413 414 417 public void storeProfile(Layout rootLayout, String layoutKey) { 418 PortalService service = null; 419 420 try { 421 service = (PortalService) this.manager.lookup(PortalService.ROLE); 422 if ( null == layoutKey ) { 423 layoutKey = service.getDefaultLayoutKey(); 424 } 425 426 final String layoutAttributeKey = "Layout:" + layoutKey; 427 428 service.setAttribute(layoutAttributeKey, rootLayout); 429 } catch (Exception ce) { 430 throw new CascadingRuntimeException("Exception during loading of profile.", ce); 431 } finally { 432 this.manager.release(service); 433 } 434 } 435 } 436 | Popular Tags |