01: /* SessionsCtrl.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Mon May 30 22:03:45 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.zk.ui.sys;
20:
21: import org.zkoss.zk.ui.Session;
22: import org.zkoss.zk.ui.Sessions;
23:
24: /**
25: * An addition interface to {@link Sessions} for implementation.
26: *
27: * @author tomyeh
28: */
29: public class SessionsCtrl extends Sessions {
30: protected SessionsCtrl() {
31: } //prevent from instantiation
32:
33: /** Sets the session for the current thread.
34: * Called only internally.
35: */
36: public static final void setCurrent(Session sess) {
37: _sess.set(sess);
38: }
39:
40: /** Returns the current {@link SessionCtrl}.
41: */
42: public static final SessionCtrl getCurrentCtrl() {
43: return (SessionCtrl) getCurrent();
44: }
45:
46: /** Called when a servlet/portlet starts to serve a request.
47: * It checks whether the number of concurrent requests per session
48: * exceeds the number specified in
49: * {@link org.zkoss.zk.ui.util.Configuration#getSessionMaxRequests}.
50: * If exceeded, false is returned, and the servlet/portlet shall stop
51: * processing and return an error code back the client.
52: *
53: * <p>If not exceeded, true is returned, and the servlet/portlet
54: * can continue the processing and it shall invoke {@link #requestExit}
55: * in the finally clause.
56: *
57: * @since 3.0.1
58: */
59: public static boolean requestEnter(Session sess) {
60: final Integer v = (Integer) sess
61: .getAttribute(ATTR_REQUEST_COUNT);
62: final int i = v != null ? v.intValue() + 1 : 1;
63: final int max = sess.getWebApp().getConfiguration()
64: .getSessionMaxRequests();
65: if (max < 0 || i <= max) {
66: sess.setAttribute(ATTR_REQUEST_COUNT, new Integer(i));
67: return true;
68: }
69: return false;
70: }
71:
72: /**
73: * Called when a servlet/portlet completes the service of a request.
74: * This method must be called if {@link #requestEnter} is called
75: * and returns true. This method shall not be called, otherwise.
76: *
77: * @since 3.0.1
78: */
79: public static void requestExit(Session sess) {
80: final Integer v = (Integer) sess
81: .getAttribute(ATTR_REQUEST_COUNT);
82: final int i = v != null ? v.intValue() - 1 : 0;
83: sess.setAttribute(ATTR_REQUEST_COUNT, new Integer(i >= 0 ? i
84: : 0));
85: }
86:
87: private static final String ATTR_REQUEST_COUNT = "org.zkoss.zk.ui.sys.RequestCount";
88: }
|