1 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 34 35 public class SessionWrapper extends Wrapper { 36 static Logger logger = Logger.getLogger("session"); 37 38 40 protected static Hashtable sessions = new Hashtable(); 41 42 43 protected static Hashtable applications = new Hashtable(); 44 45 public SessionWrapper(AspectComponent ac) { 46 super(ac); 47 } 48 49 57 58 public void clearCurrentSessionAttribute(String name) { 59 String sid = (String ) 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 87 88 public Object handleSession(Interaction interaction) { 89 90 Object result = null; 91 92 String sid = (String ) 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 110 111 result = proceed(interaction); 112 113 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 ) applications.get(sid)); 139 Collaboration.get().setCurApp((String ) 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 [] 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 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 invoke(MethodInvocation invocation) throws Throwable { 196 return handleSession((Interaction) invocation); 197 } 198 199 public Object construct(ConstructorInvocation invocation) 200 throws Throwable { 201 return handleSession((Interaction) invocation); 202 } 203 204 } 205 | Popular Tags |