KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > util > HttpSessionMutexListener


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.util;
18
19 import java.io.Serializable JavaDoc;
20
21 import javax.servlet.http.HttpSessionEvent JavaDoc;
22 import javax.servlet.http.HttpSessionListener JavaDoc;
23
24 /**
25  * Servlet 2.3+ HttpSessionListener that automatically exposes the
26  * session mutex when an HttpSession gets created.
27  * To be registered as a listener in <code>web.xml</code>.
28  *
29  * <p>The session mutex is guaranteed to be the same object during
30  * the entire lifetime of the session, available under the key defined
31  * by the <code>SESSION_MUTEX_ATTRIBUTE</code> constant. It serves as a
32  * safe reference to synchronize on for locking on the current session.
33  *
34  * <p>In many cases, the HttpSession reference itself is a safe mutex
35  * as well, since it will always be the same object reference for the
36  * same active logical session. However, this is not guaranteed across
37  * different servlet containers; the only 100% safe way is a session mutex.
38  *
39  * @author Juergen Hoeller
40  * @since 1.2.7
41  * @see WebUtils#SESSION_MUTEX_ATTRIBUTE
42  * @see WebUtils#getSessionMutex(javax.servlet.http.HttpSession)
43  * @see org.springframework.web.servlet.mvc.AbstractController#setSynchronizeOnSession
44  */

45 public class HttpSessionMutexListener implements HttpSessionListener JavaDoc {
46
47     public void sessionCreated(HttpSessionEvent JavaDoc event) {
48         event.getSession().setAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE, new Mutex());
49     }
50
51     public void sessionDestroyed(HttpSessionEvent JavaDoc event) {
52         event.getSession().removeAttribute(WebUtils.SESSION_MUTEX_ATTRIBUTE);
53     }
54
55
56     /**
57      * The mutex to be registered.
58      * Doesn't need to be anything but a plain Object to synchronize on.
59      * Should be serializable to allow for HttpSession persistence.
60      */

61     private static class Mutex implements Serializable JavaDoc {
62     }
63
64 }
65
Popular Tags