01: package com.salmonllc.examples;
02:
03: //The Salmon Open Framework for Internet Applications (SOFIA)
04: //Copyright (C) 1999 - 2002, Salmon LLC
05: //
06: //This program is free software; you can redistribute it and/or
07: //modify it under the terms of the GNU General Public License version 2
08: //as published by the Free Software Foundation;
09: //
10: //This program is distributed in the hope that it will be useful,
11: //but WITHOUT ANY WARRANTY; without even the implied warranty of
12: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: //GNU General Public License for more details.
14: //
15: //You should have received a copy of the GNU General Public License
16: //along with this program; if not, write to the Free Software
17: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18: //
19: //For more information please visit http://www.salmonllc.com
20:
21: import javax.servlet.http.HttpSession;
22:
23: /**
24: * Used to manage values put on the session. This class mainly adds the APP_NAME to all keys added with it.
25: * The class is not actually used in the application, but is here in case you want to use the sample application
26: * as a starting point for your own application. Generally it isn't good practice to write to the session directly.
27: * Instead implement get/set method in this class that do it for you. Then in your application always access the session
28: * via the get and set methods in this class. It makes it much easier to trap errors related to storing items on the session.
29: */
30: public class SessionManager {
31:
32: private HttpSession _sess;
33: public String _appName;
34:
35: /**
36: * SessionManager constructor comment.
37: */
38: public SessionManager(HttpSession sess, String appName) {
39: _sess = sess;
40: _appName = appName;
41: init();
42: }
43:
44: /**
45: * This method returns the current time with precision into milliseconds.
46: * @return java.sql.Timestamp current time
47: */
48:
49: public java.sql.Timestamp getCurrentTime() {
50: return new java.sql.Timestamp(System.currentTimeMillis());
51: }
52:
53: /**
54: * Gets a value off the session. Automatically using the APP_NAME in the key value.
55: * @return java.lang.Object
56: * @param name java.lang.String
57: */
58: public Object getValue(String name) {
59: return _sess.getAttribute("SessionManager_" + _appName + "_"
60: + name);
61: }
62:
63: private void init() {
64: String init = _appName + "_isInit";
65: if (getValue(init) == null) {
66: putValue(init, new Boolean(true));
67: }
68: //If you want to load any values from the database or property files into the session,
69: //this is the place to do it.
70: }
71:
72: /**
73: * Puts a value on the session. Automatically using the APP_NAME in the key value.
74: * @param name java.lang.String
75: * @param value java.lang.Object
76: */
77: public void putValue(String name, Object value) {
78: _sess.setAttribute("SessionManager_" + _appName + "_" + name,
79: value);
80: }
81:
82: /**
83: * Removes a value from the session. Automatically using the APP_NAME in the key value.
84: */
85: public void removeValue(String name) {
86: _sess
87: .removeAttribute("SessionManager_" + _appName + "_"
88: + name);
89: }
90:
91: }
|