01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20:
21: import javax.servlet.http.HttpSessionEvent;
22: import javax.servlet.http.HttpSessionListener;
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 {
46:
47: public void sessionCreated(HttpSessionEvent event) {
48: event.getSession().setAttribute(
49: WebUtils.SESSION_MUTEX_ATTRIBUTE, new Mutex());
50: }
51:
52: public void sessionDestroyed(HttpSessionEvent event) {
53: event.getSession().removeAttribute(
54: WebUtils.SESSION_MUTEX_ATTRIBUTE);
55: }
56:
57: /**
58: * The mutex to be registered.
59: * Doesn't need to be anything but a plain Object to synchronize on.
60: * Should be serializable to allow for HttpSession persistence.
61: */
62: private static class Mutex implements Serializable {
63: }
64:
65: }
|