KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > bookmarks > BookmarkManager


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.bookmarks;
14
15 import java.util.Collections JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.servlet.http.HttpSession JavaDoc;
22
23 /**
24  * @author av
25  */

26 public class BookmarkManager {
27   private HttpSession JavaDoc session;
28   private Map JavaDoc state = Collections.EMPTY_MAP;
29   private static final String JavaDoc WEBKEY = BookmarkManager.class.getName();
30
31   /** session singleton */
32   BookmarkManager(HttpSession JavaDoc session) {
33     this.session = session;
34   }
35
36   public static BookmarkManager instance(HttpSession JavaDoc 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   /**
46    * collects tht state of all Bookmarkable Session Attributes
47    * @return memento object
48    */

49   public Object JavaDoc collectSessionState(int levelOfDetail) {
50     Map JavaDoc map = new HashMap JavaDoc();
51     for (Enumeration JavaDoc en = session.getAttributeNames(); en.hasMoreElements();) {
52       String JavaDoc name = (String JavaDoc) en.nextElement();
53       Object JavaDoc attr = session.getAttribute(name);
54       if (attr instanceof Bookmarkable) {
55         Object JavaDoc value = ((Bookmarkable) attr).getBookmarkState(levelOfDetail);
56         if (value != null)
57           map.put(name, value);
58       }
59     }
60     return map;
61   }
62
63   /**
64    * restores the State of all Bookmarkable Session Atributes
65    * @param memento created by collectBookmarkState()
66    * @see #collectBookmarkState
67    */

68   public void restoreSessionState(Object JavaDoc memento) {
69     // no bookmark state?
70
if (memento == null) {
71       state = Collections.EMPTY_MAP;
72       return;
73     }
74     state = (Map JavaDoc) memento;
75     for (Iterator JavaDoc it = state.keySet().iterator(); it.hasNext();) {
76       String JavaDoc name = (String JavaDoc) it.next();
77       restoreAttributeState(name);
78     }
79   }
80
81   /**
82    * restores bookmark state for a single session attribute.
83    * This is used
84    * for objects that were created after restoreSessionState() was called.
85    * (e.g. via jsp tags on different jsp pages).
86    * @param name name of the session attribute
87    */

88   public void restoreAttributeState(String JavaDoc name) {
89     Object JavaDoc attr = session.getAttribute(name);
90     if (attr instanceof Bookmarkable) {
91       Object JavaDoc value = state.get(name);
92       if (value != null)
93          ((Bookmarkable) attr).setBookmarkState(value);
94     }
95   }
96 }
97
Popular Tags