1 13 package com.tonbeller.wcf.bookmarks; 14 15 import java.util.Collections ; 16 import java.util.Enumeration ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import javax.servlet.http.HttpSession ; 22 23 26 public class BookmarkManager { 27 private HttpSession session; 28 private Map state = Collections.EMPTY_MAP; 29 private static final String WEBKEY = BookmarkManager.class.getName(); 30 31 32 BookmarkManager(HttpSession session) { 33 this.session = session; 34 } 35 36 public static BookmarkManager instance(HttpSession session) { 37 BookmarkManager bm = (BookmarkManager) session.getAttribute(WEBKEY); 38 if (bm == null) { 39 bm = new BookmarkManager(session); 40 session.setAttribute(WEBKEY, bm); 41 } 42 return bm; 43 } 44 45 49 public Object collectSessionState(int levelOfDetail) { 50 Map map = new HashMap (); 51 for (Enumeration en = session.getAttributeNames(); en.hasMoreElements();) { 52 String name = (String ) en.nextElement(); 53 Object attr = session.getAttribute(name); 54 if (attr instanceof Bookmarkable) { 55 Object value = ((Bookmarkable) attr).getBookmarkState(levelOfDetail); 56 if (value != null) 57 map.put(name, value); 58 } 59 } 60 return map; 61 } 62 63 68 public void restoreSessionState(Object memento) { 69 if (memento == null) { 71 state = Collections.EMPTY_MAP; 72 return; 73 } 74 state = (Map ) memento; 75 for (Iterator it = state.keySet().iterator(); it.hasNext();) { 76 String name = (String ) it.next(); 77 restoreAttributeState(name); 78 } 79 } 80 81 88 public void restoreAttributeState(String name) { 89 Object attr = session.getAttribute(name); 90 if (attr instanceof Bookmarkable) { 91 Object value = state.get(name); 92 if (value != null) 93 ((Bookmarkable) attr).setBookmarkState(value); 94 } 95 } 96 } 97 | Popular Tags |