/**
  * @param layoutMap
  * @param layout
  */
 protected void cacheLayouts(Map layoutMap, Layout layout) {
   if (layout != null) {
     if (layout.getId() != null) {
       layoutMap.put(layout.getId(), layout);
     }
     if (layout instanceof CompositeLayout) {
       CompositeLayout cl = (CompositeLayout) layout;
       Iterator i = cl.getItems().iterator();
       while (i.hasNext()) {
         Item current = (Item) i.next();
         this.cacheLayouts(layoutMap, current.getLayout());
       }
     }
   }
 }
  /* (non-Javadoc)
   * @see org.apache.cocoon.portal.profile.ProfileManager#register(org.apache.cocoon.portal.layout.Layout)
   */
  public void register(Layout layout) {
    PortalService service = null;
    try {
      service = (PortalService) this.manager.lookup(PortalService.ROLE);
      final String layoutKey = service.getDefaultLayoutKey();

      Map layoutMap = (Map) service.getAttribute("Layout-Map:" + layoutKey);
      if (layoutMap == null) {
        layout = (Layout) service.getAttribute("Layout:" + layoutKey);
        if (layout != null) {
          layoutMap = new HashMap();
          this.cacheLayouts(layoutMap, layout);
          service.setAttribute("Layout-Map:" + layoutKey, layoutMap);
        }
      }

      if (layoutMap != null) {
        layoutMap.put(layout.getId(), layout);
      }

    } catch (ServiceException e) {
      throw new CascadingRuntimeException("Unable to lookup portal service.", e);
    } finally {
      this.manager.release(service);
    }
  }
  /* (non-Javadoc)
   * @see org.apache.cocoon.portal.profile.ProfileManager#unregister(org.apache.cocoon.portal.layout.Layout)
   */
  public void unregister(Layout layout) {
    PortalService service = null;
    try {
      service = (PortalService) this.manager.lookup(PortalService.ROLE);
      final String layoutKey = service.getDefaultLayoutKey();

      Map layoutMap = (Map) service.getAttribute("Layout-Map:" + layoutKey);

      if (layoutMap != null) {
        layoutMap.remove(layout.getId());
      }

    } catch (ServiceException e) {
      throw new CascadingRuntimeException("Unable to lookup portal service.", e);
    } finally {
      this.manager.release(service);
    }
  }