KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.session;
19
20 import java.util.*;
21 import org.aopalliance.intercept.ConstructorInvocation;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.apache.log4j.Logger;
24 import org.objectweb.jac.core.*;
25 import org.objectweb.jac.util.*;
26
27 /**
28  * This wrapper handles per-session objects within the JAC system.
29  *
30  * <p>For each session, it uses a different copy of the original
31  * object so that each client see a different state.
32  *
33  * @see #handlePerSessionObject(Interaction)
34  * @author Renaud Pawlak */

35
36 public class PerSessionObjectWrapper extends Wrapper {
37     static Logger logger = Logger.getLogger("session");
38
39     Hashtable sessionObjects = new Hashtable();
40
41     public PerSessionObjectWrapper(AspectComponent ac) {
42         super(ac);
43     }
44
45     /**
46      * This wrapping method handles an hashtable of copied objects.
47      *
48      * <p>There is one copied object per session with possibly a
49      * different state from the original. The call is thus forwarded to
50      * the copy that corresponds to the session.
51      *
52      * <p>The first time, the original object is cloned so that its
53      * state is that same as the original.
54      *
55      * @return the result given by the per-session copy */

56
57     public Object JavaDoc handlePerSessionObject(Interaction interaction) {
58
59         logger.debug("handling per-session object for " + interaction.wrappee);
60
61         String JavaDoc sid = (String JavaDoc) attr( "Session.sid" );
62       
63         if ( sid == null || sid.startsWith("Swing") ) {
64             logger.debug("session is not defined by client");
65             return proceed(interaction);
66         }
67
68         logger.debug("found session " + sid);
69       
70         Object JavaDoc sessionObject = null;
71       
72         if ( sessionObjects.containsKey( sid ) ) {
73             sessionObject = sessionObjects.get( sid );
74         } else {
75             // the initial session object has the same state as the original
76
// object
77
logger.debug("cloning object " + interaction.wrappee);
78             sessionObject = Wrapping.clone(interaction.wrappee);
79             sessionObjects.put(sid,sessionObject);
80         }
81
82         Object JavaDoc result = null;
83         if( sessionObject == interaction.wrappee ) {
84             // we are the session object, this should not happend if
85
// the session aspect is correctly defined
86
logger.warn(interaction.wrappee+
87                         " is a session object and is wrapped.");
88             result = proceed(interaction);
89         } else {
90             // we forward the call to the session object
91
// (and we do not use the original)
92
logger.debug(interaction.wrappee+" forwarding to session object " +
93                          sessionObject+"."+interaction.method);
94             result = interaction.invoke(sessionObject);
95         }
96         return result;
97     }
98
99     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
100         return handlePerSessionObject((Interaction) invocation);
101     }
102
103     public Object JavaDoc construct(ConstructorInvocation invocation)
104         throws Throwable JavaDoc {
105         throw new Exception JavaDoc("Wrapper "+this+" does not support construction interception.");
106     }
107
108 }
109
Popular Tags