1 55 56 package org.jboss.axis.handlers; 57 58 import org.jboss.axis.AxisEngine; 59 import org.jboss.axis.AxisFault; 60 import org.jboss.axis.Constants; 61 import org.jboss.axis.Message; 62 import org.jboss.axis.MessageContext; 63 import org.jboss.axis.message.SOAPEnvelopeAxisImpl; 64 import org.jboss.axis.message.SOAPHeaderElementAxisImpl; 65 import org.jboss.axis.session.SimpleSession; 66 import org.jboss.axis.utils.Messages; 67 import org.jboss.axis.utils.SessionUtils; 68 import org.jboss.logging.Logger; 69 70 import javax.xml.namespace.QName ; 71 import javax.xml.rpc.server.ServiceLifecycle ; 72 import java.util.Enumeration ; 73 import java.util.HashSet ; 74 import java.util.Hashtable ; 75 import java.util.Iterator ; 76 import java.util.Map ; 77 import java.util.Set ; 78 79 117 public class SimpleSessionHandler extends BasicHandler 118 { 119 private static Logger log = Logger.getLogger(SimpleSessionHandler.class.getName()); 120 121 public static final String SESSION_ID = "SimpleSession.id"; 122 public static final String SESSION_NS = "http://xml.apache.org/axis/session"; 123 public static final String SESSION_LOCALPART = "sessionID"; 124 public static final QName sessionHeaderName = new QName (SESSION_NS, 125 SESSION_LOCALPART); 126 127 private Hashtable activeSessions = new Hashtable (); 128 129 private long reapPeriodicity = 30; 132 private long lastReapTime = 0; 133 134 private int defaultSessionTimeout = 60; 136 137 140 public void invoke(MessageContext context) throws AxisFault 141 { 142 long curTime = System.currentTimeMillis(); 144 boolean reap = false; 145 146 synchronized (this) 148 { 149 if (curTime > lastReapTime + (reapPeriodicity * 1000)) 150 { 151 reap = true; 152 lastReapTime = curTime; 153 } 154 } 155 156 if (reap) 157 { 158 Set entries = activeSessions.entrySet(); 159 Set victims = new HashSet (); 160 Object key; 161 Iterator i; 162 for (i = entries.iterator(); i.hasNext();) 163 { 164 Map.Entry entry = (Map.Entry )i.next(); 165 key = entry.getKey(); 166 SimpleSession session = (SimpleSession)entry.getValue(); 167 if ((curTime - session.getLastAccessTime()) > 168 (session.getTimeout() * 1000)) 169 { 170 log.debug(Messages.getMessage("timeout00", 171 key.toString())); 172 173 victims.add(key); 175 } 176 } 177 178 for (i = victims.iterator(); i.hasNext();) 180 { 181 key = i.next(); 182 SimpleSession session = (SimpleSession)activeSessions.get(key); 183 activeSessions.remove(key); 184 185 Enumeration keys = session.getKeys(); 190 while (keys != null && keys.hasMoreElements()) 191 { 192 String keystr = (String )keys.nextElement(); 193 Object obj = session.get(keystr); 194 if (obj != null && obj instanceof ServiceLifecycle ) 195 { 196 ((ServiceLifecycle )obj).destroy(); 197 } 198 } 199 } 200 } 201 202 if (context.isClient()) 203 { 204 doClient(context); 205 } 206 else 207 { 208 doServer(context); 209 } 210 } 211 212 215 public void doClient(MessageContext context) throws AxisFault 216 { 217 if (context.getPastPivot()) 218 { 219 Message msg = context.getResponseMessage(); 221 if (msg == null) 222 return; 223 SOAPEnvelopeAxisImpl env = msg.getSOAPEnvelope(); 224 SOAPHeaderElementAxisImpl header = env.getHeaderByName(SESSION_NS, 225 SESSION_LOCALPART); 226 if (header == null) 227 return; 228 229 try 231 { 232 Long id = (Long )header. 233 getValueAsType(Constants.XSD_LONG); 234 AxisEngine engine = context.getAxisEngine(); 236 engine.setOption(SESSION_ID, id); 237 header.setProcessed(true); 239 } 240 catch (Exception e) 241 { 242 throw AxisFault.makeFault(e); 243 } 244 } 245 else 246 { 247 AxisEngine engine = context.getAxisEngine(); 248 Long id = (Long )engine.getOption(SESSION_ID); 249 if (id == null) 250 return; 251 252 Message msg = context.getRequestMessage(); 254 if (msg == null) 255 throw new AxisFault(Messages.getMessage("noRequest00")); 256 257 SOAPEnvelopeAxisImpl env = msg.getSOAPEnvelope(); 258 SOAPHeaderElementAxisImpl header = new SOAPHeaderElementAxisImpl(SESSION_NS, 259 SESSION_LOCALPART, 260 id); 261 env.addHeader(header); 262 } 263 } 264 265 268 public void doServer(MessageContext context) throws AxisFault 269 { 270 if (context.getPastPivot()) 271 { 272 Long id = (Long )context.getProperty(SESSION_ID); 275 if (id == null) 276 return; 277 278 Message msg = context.getResponseMessage(); 279 if (msg == null) 280 return; 281 SOAPEnvelopeAxisImpl env = msg.getSOAPEnvelope(); 282 SOAPHeaderElementAxisImpl header = new SOAPHeaderElementAxisImpl(SESSION_NS, 283 SESSION_LOCALPART, 284 id); 285 env.addHeader(header); 286 } 287 else 288 { 289 Message msg = context.getRequestMessage(); 291 if (msg == null) 292 throw new AxisFault(Messages.getMessage("noRequest00")); 293 294 SOAPEnvelopeAxisImpl env = msg.getSOAPEnvelope(); 295 SOAPHeaderElementAxisImpl header = env.getHeaderByName(SESSION_NS, 296 SESSION_LOCALPART); 297 Long id; 298 299 if (header != null) 300 { 301 try 303 { 304 id = (Long )header. 305 getValueAsType(Constants.XSD_LONG); 306 } 307 catch (Exception e) 308 { 309 throw AxisFault.makeFault(e); 310 } 311 } 312 else 313 { 314 id = getNewSession(); 315 } 316 317 SimpleSession session = (SimpleSession)activeSessions.get(id); 318 if (session == null) 319 { 320 id = getNewSession(); 322 session = (SimpleSession)activeSessions.get(id); 323 } 324 325 session.touch(); 327 328 context.setSession(session); 330 context.setProperty(SESSION_ID, id); 331 } 332 } 333 334 339 private synchronized Long getNewSession() 340 { 341 Long id = SessionUtils.generateSession(); 342 SimpleSession session = new SimpleSession(); 343 session.setTimeout(defaultSessionTimeout); 344 activeSessions.put(id, session); 345 return id; 346 } 347 348 356 public void setReapPeriodicity(long reapTime) 357 { 358 reapPeriodicity = reapTime; 359 } 360 361 366 public void setDefaultSessionTimeout(int defaultSessionTimeout) 367 { 368 this.defaultSessionTimeout = defaultSessionTimeout; 369 } 370 } 371 | Popular Tags |