KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > RollerSession


1 package org.roller.presentation;
2
3 import org.apache.commons.collections.ArrayStack;
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpSession JavaDoc;
9 import javax.servlet.http.HttpSessionActivationListener JavaDoc;
10 import javax.servlet.http.HttpSessionEvent JavaDoc;
11 import javax.servlet.http.HttpSessionListener JavaDoc;
12 import java.io.Serializable JavaDoc;
13
14
15 //////////////////////////////////////////////////////////////////////////////
16
/**
17  * Roller session handles session startup and shutdown.
18  * @web.listener
19  */

20 public class RollerSession
21     implements HttpSessionListener JavaDoc, HttpSessionActivationListener JavaDoc, Serializable JavaDoc
22 {
23     // Although we have no actual members, we implement Serializable to meet expectations of
24
// session attributes for some container configurations.
25
static final long serialVersionUID = 5890132909166913727L;
26
27     private static Log mLogger =
28         LogFactory.getFactory().getInstance(RollerSession.class);
29
30     public static final String JavaDoc ROLLER_SESSION = "org.roller.rollersession";
31     public static final String JavaDoc BREADCRUMB = "org.roller.breadcrumb";
32     public static final String JavaDoc ERROR_MESSAGE = "rollererror_message";
33     public static final String JavaDoc STATUS_MESSAGE = "rollerstatus_message";
34
35
36     //------------------------------------------------------------------------
37
/** Create session's Roller instance */
38     public void sessionCreated(HttpSessionEvent JavaDoc se)
39     {
40         // put this in session, so that we get HttpSessionActivationListener callbacks
41
se.getSession().setAttribute( ROLLER_SESSION, this );
42         
43         RollerContext rctx = RollerContext.getRollerContext(
44             se.getSession().getServletContext());
45         rctx.sessionCreated(se);
46     }
47
48     //------------------------------------------------------------------------
49
public void sessionDestroyed(HttpSessionEvent JavaDoc se)
50     {
51         RollerContext rctx = RollerContext.getRollerContext(
52             se.getSession().getServletContext());
53         rctx.sessionDestroyed(se);
54         
55         clearSession(se);
56     }
57
58     //------------------------------------------------------------------------
59
/** Init session as if it was new */
60     public void sessionDidActivate(HttpSessionEvent JavaDoc se)
61     {
62     }
63
64     //------------------------------------------------------------------------
65
/**
66      * Clear bread crumb trail.
67      * @param req the request
68      */

69     public static void clearBreadCrumbTrail( HttpServletRequest JavaDoc req )
70     {
71         HttpSession JavaDoc ses = req.getSession(false);
72         if (ses != null && ses.getAttribute(BREADCRUMB) != null)
73         {
74             ArrayStack stack = (ArrayStack)ses.getAttribute(BREADCRUMB);
75             stack.clear();
76         }
77     }
78     
79     //------------------------------------------------------------------------
80
/**
81      * Store the url of the latest request stored in the session.
82      * @param useReferer If true try to return the "referer" header.
83      */

84     public static String JavaDoc getBreadCrumb(
85         HttpServletRequest JavaDoc req, boolean useReferer )
86     {
87         String JavaDoc crumb = null;
88         
89         HttpSession JavaDoc ses = req.getSession(false);
90         if (ses != null && ses.getAttribute(BREADCRUMB) != null)
91         {
92             ArrayStack stack = (ArrayStack) ses.getAttribute(BREADCRUMB);
93             if (stack != null && !stack.empty())
94             {
95                 crumb = (String JavaDoc)stack.peek();
96             }
97         }
98
99         if ( crumb == null && useReferer )
100         {
101             crumb = req.getHeader("referer");
102         }
103         
104         return crumb;
105     }
106     
107     //------------------------------------------------------------------------
108
/**
109      * Store the url of the latest request stored in the session.
110      * Else try to return the "referer" header.
111      */

112     public static String JavaDoc getBreadCrumb( HttpServletRequest JavaDoc req )
113     {
114         return getBreadCrumb(req,true);
115     }
116
117     //------------------------------------------------------------------------
118
/** Purge session before passivation. Because Roller currently does not
119       * support session recovery, failover, migration, or whatever you want
120       * to call it when sessions are saved and then restored at some later
121       * point in time.
122       */

123     public void sessionWillPassivate(HttpSessionEvent JavaDoc se)
124     {
125         clearSession(se);
126     }
127
128     //------------------------------------------------------------------------ /*
129
private void clearSession( HttpSessionEvent JavaDoc se )
130     {
131         HttpSession JavaDoc session = se.getSession();
132         try
133         {
134             session.removeAttribute( BREADCRUMB );
135         }
136         catch (Throwable JavaDoc e)
137         {
138             if (mLogger.isDebugEnabled())
139             {
140                 // ignore purge exceptions
141
mLogger.debug("EXCEPTION PURGING session attributes",e);
142             }
143         }
144     }
145 }
146
147
Popular Tags