KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > session > SessionWrapper


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak <renaud@aopsys.com>
3                           Laurent Martelli <laurent@aopsys.com>
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU Lesser General Public License as
7   published by the Free Software Foundation; either version 2 of the
8   License, or (at your option) any later version.
9
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 Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser 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 package org.objectweb.jac.aspects.session;
20
21 import java.util.*;
22 import org.aopalliance.intercept.ConstructorInvocation;
23 import org.aopalliance.intercept.MethodInvocation;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.core.*;
26 import org.objectweb.jac.core.Collaboration;
27 import org.objectweb.jac.util.*;
28
29 /**
30  * This wrapper handles the session for each object within the JAC system.
31  *
32  * @see #handleSession(Interaction)
33  * @author Renaud Pawlak */

34
35 public class SessionWrapper extends Wrapper {
36     static Logger logger = Logger.getLogger("session");
37
38     /** Stores the sessions and their contextual attributes (sid ->
39         saved attributes). */

40     protected static Hashtable sessions = new Hashtable();
41
42     /** Stores the applications (sid -> applicationName) */
43     protected static Hashtable applications = new Hashtable();
44
45     public SessionWrapper(AspectComponent ac) {
46         super(ac);
47     }
48
49     /**
50      * Removes a session attribute.
51      *
52      * <p>The given attribute will be forgotten for the current
53      * interaction and for all the forthcoming interactions of the same
54      * session. This can be used for instance to log-out a user.
55      *
56      * @param name the name of the attribute to forget */

57
58     public void clearCurrentSessionAttribute(String JavaDoc name) {
59         String JavaDoc sid = (String JavaDoc) attr(SessionAC.SESSION_ID);
60         if (sid == null) {
61             logger.debug("clearCurrentSessionAttribute: no Session_ID found");
62             return;
63         }
64         logger.debug("clearCurrentSessionAttribute for session " + sid);
65         Hashtable attrs = (Hashtable) sessions.get(sid);
66         if (attrs != null) {
67             attrs.remove(name);
68         }
69         Collaboration.get().removeAttribute(name);
70     }
71
72     /**
73      * Handles sessions for the wrapped method.
74      *
75      * <p>The session handling algorithm is:
76      *
77      * <ul>
78      * <li>if the session id - <code>attr("Session.sid")</code> -
79      * is not defined, do nothing</li>
80      * <li>try to restore the saved context attributes for this session id
81      * if already saved</li>
82      * <li>else save them into the <code>sessions</code> field</li>
83      * </ul>
84      *
85      * @return the wrapped method return value
86      */

87
88     public Object JavaDoc handleSession(Interaction interaction) {
89
90         Object JavaDoc result = null;
91
92         String JavaDoc sid = (String JavaDoc) attr(SessionAC.SESSION_ID);
93
94         if (attr(SessionAC.INITIALIZED) != null) {
95
96             logger.debug("session initiliazed for "
97                          +interaction.wrappee+"."+ interaction.method);
98
99             // I believe we do not need this (Laurent)
100
/*
101             if (applications.containsKey(sid)) {
102                 Log.trace("application",
103                           "retreiving cur app from session: "
104                           + (String) applications.get(sid));
105                 if (!applications.get(sid).equals(Collaboration.get().getCurApp()))
106                     Log.warning("curr app changed from "+Collaboration.get().getCurApp()+" to "+applications.get(sid));
107                 Collaboration.get().setCurApp((String) applications.get(sid));
108             }
109             */

110
111             result = proceed(interaction);
112
113             /*
114             if (applications.containsKey(sid)) {
115                 Log.trace("application",
116                           "retreiving cur app from session: "
117                           + (String) applications.get(sid));
118                 if (!applications.get(sid).equals(Collaboration.get().getCurApp()))
119                     Log.warning("curr app changed from "+Collaboration.get().getCurApp()+" to "+applications.get(sid));
120                 Collaboration.get().setCurApp((String) applications.get(sid));
121             }
122             */

123
124             return result;
125
126         }
127
128         logger.debug("handling session "+sid+" for "
129                      + interaction.wrappee + "." + interaction.method);
130         
131         if (sid == null) {
132             logger.debug("session is not defined by client");
133             return proceed(interaction);
134         }
135
136         if (applications.containsKey(sid)) {
137             logger.debug("retreiving cur app from session: "
138                                      + (String JavaDoc) applications.get(sid));
139             Collaboration.get().setCurApp((String JavaDoc) applications.get(sid));
140         }
141
142         logger.debug("in session, application=" + Collaboration.get().getCurApp());
143
144         logger.debug("found session " + sid + " for " + interaction.method.getName());
145         Hashtable savedAttributes = null;
146
147         if (sessions.containsKey(sid)) {
148             savedAttributes = (Hashtable) sessions.get(sid);
149         } else {
150             savedAttributes = new Hashtable();
151             sessions.put(sid, savedAttributes);
152         }
153
154         String JavaDoc[] storedAttributes =
155             ((SessionAC) getAspectComponent()).getStoredAttributes();
156
157         for (int i = 0; i < storedAttributes.length; i++) {
158             if (savedAttributes.containsKey(storedAttributes[i])) {
159                 logger.debug("reading "+ storedAttributes[i]
160                              + "=" + savedAttributes.get(storedAttributes[i])
161                              + " for " + interaction.method.getName());
162                 Collaboration.get().addAttribute(
163                     storedAttributes[i],
164                     savedAttributes.get(storedAttributes[i]));
165             }
166         }
167
168         attrdef(SessionAC.INITIALIZED, "true");
169
170         result = proceed(interaction);
171
172         for (int i = 0; i < storedAttributes.length; i++) {
173             if (Collaboration.get().getAttribute(storedAttributes[i])
174                 != null) {
175                 logger.debug("saving " + storedAttributes[i]
176                              + "=" + Collaboration.get().getAttribute(storedAttributes[i])
177                              + " for " + interaction.method.getName());
178                 savedAttributes.put(
179                     storedAttributes[i],
180                     Collaboration.get().getAttribute(storedAttributes[i]));
181             } else {
182                 logger.debug("NOT saving " + storedAttributes[i]
183                              + "=" + Collaboration.get().getAttribute(storedAttributes[i])
184                              + " for " + interaction.method.getName());
185             }
186         }
187         String JavaDoc application = Collaboration.get().getCurApp();
188         if (application != null) {
189             logger.debug("session saves app "+application);
190             applications.put(sid, application);
191         }
192         return result;
193     }
194
195     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
196         return handleSession((Interaction) invocation);
197     }
198
199     public Object JavaDoc construct(ConstructorInvocation invocation)
200         throws Throwable JavaDoc {
201         return handleSession((Interaction) invocation);
202     }
203
204 }
205
Popular Tags