1 16 17 package org.apache.axis.handlers; 18 19 import org.apache.axis.AxisEngine; 20 import org.apache.axis.AxisFault; 21 import org.apache.axis.Constants; 22 import org.apache.axis.Message; 23 import org.apache.axis.MessageContext; 24 import org.apache.axis.components.logger.LogFactory; 25 import org.apache.axis.message.SOAPEnvelope; 26 import org.apache.axis.message.SOAPHeaderElement; 27 import org.apache.axis.session.SimpleSession; 28 import org.apache.axis.utils.Messages; 29 import org.apache.axis.utils.SessionUtils; 30 import org.apache.commons.logging.Log; 31 32 import javax.xml.namespace.QName ; 33 import javax.xml.rpc.server.ServiceLifecycle ; 34 import java.util.Enumeration ; 35 import java.util.HashSet ; 36 import java.util.Hashtable ; 37 import java.util.Iterator ; 38 import java.util.Map ; 39 import java.util.Set ; 40 41 78 public class SimpleSessionHandler extends BasicHandler 79 { 80 protected static Log log = 81 LogFactory.getLog(SimpleSessionHandler.class.getName()); 82 83 public static final String SESSION_ID = "SimpleSession.id"; 84 public static final String SESSION_NS = "http://xml.apache.org/axis/session"; 85 public static final String SESSION_LOCALPART = "sessionID"; 86 public static final QName sessionHeaderName = new QName (SESSION_NS, 87 SESSION_LOCALPART); 88 89 private Hashtable activeSessions = new Hashtable (); 90 91 private long reapPeriodicity = 30; 94 private long lastReapTime = 0; 95 96 private int defaultSessionTimeout = 60; 98 99 102 public void invoke(MessageContext context) throws AxisFault 103 { 104 long curTime = System.currentTimeMillis(); 106 boolean reap = false; 107 108 synchronized (this) { 110 if (curTime > lastReapTime + (reapPeriodicity * 1000)) { 111 reap = true; 112 lastReapTime = curTime; 113 } 114 } 115 116 if (reap) { 117 Set entries = activeSessions.entrySet(); 118 Set victims = new HashSet (); 119 Object key; 120 Iterator i; 121 for (i = entries.iterator(); i.hasNext();) { 122 Map.Entry entry = (Map.Entry ) i.next(); 123 key = entry.getKey(); 124 SimpleSession session = (SimpleSession) entry.getValue(); 125 if ((curTime - session.getLastAccessTime()) > 126 (session.getTimeout() * 1000)) { 127 log.debug(Messages.getMessage("timeout00", 128 key.toString())); 129 130 victims.add(key); 132 } 133 } 134 135 for (i = victims.iterator(); i.hasNext();) { 137 key = i.next(); 138 SimpleSession session = (SimpleSession)activeSessions.get(key); 139 activeSessions.remove(key); 140 141 Enumeration keys = session.getKeys(); 146 while (keys != null && keys.hasMoreElements()) { 147 String keystr = (String )keys.nextElement(); 148 Object obj = session.get(keystr); 149 if (obj != null && obj instanceof ServiceLifecycle ) { 150 ((ServiceLifecycle )obj).destroy(); 151 } 152 } 153 } 154 } 155 156 if (context.isClient()) { 157 doClient(context); 158 } else { 159 doServer(context); 160 } 161 } 162 163 166 public void doClient(MessageContext context) throws AxisFault 167 { 168 if (context.getPastPivot()) { 169 Message msg = context.getResponseMessage(); 171 if (msg == null) 172 return; 173 SOAPEnvelope env = msg.getSOAPEnvelope(); 174 SOAPHeaderElement header = env.getHeaderByName(SESSION_NS, 175 SESSION_LOCALPART); 176 if (header == null) 177 return; 178 179 try { 181 Long id = (Long )header. 182 getValueAsType(Constants.XSD_LONG); 183 AxisEngine engine = context.getAxisEngine(); 185 engine.setOption(SESSION_ID, id); 186 header.setProcessed(true); 188 } catch (Exception e) { 189 throw AxisFault.makeFault(e); 190 } 191 } else { 192 AxisEngine engine = context.getAxisEngine(); 193 Long id = (Long )engine.getOption(SESSION_ID); 194 if (id == null) 195 return; 196 197 Message msg = context.getRequestMessage(); 199 if (msg == null) 200 throw new AxisFault(Messages.getMessage("noRequest00")); 201 202 SOAPEnvelope env = msg.getSOAPEnvelope(); 203 SOAPHeaderElement header = new SOAPHeaderElement(SESSION_NS, 204 SESSION_LOCALPART, 205 id); 206 env.addHeader(header); 207 } 208 } 209 210 213 public void doServer(MessageContext context) throws AxisFault 214 { 215 if (context.getPastPivot()) { 216 Long id = (Long )context.getProperty(SESSION_ID); 219 if (id == null) 220 return; 221 222 Message msg = context.getResponseMessage(); 223 if (msg == null) 224 return; 225 SOAPEnvelope env = msg.getSOAPEnvelope(); 226 SOAPHeaderElement header = new SOAPHeaderElement(SESSION_NS, 227 SESSION_LOCALPART, 228 id); 229 env.addHeader(header); 230 } else { 231 Message msg = context.getRequestMessage(); 233 if (msg == null) 234 throw new AxisFault(Messages.getMessage("noRequest00")); 235 236 SOAPEnvelope env = msg.getSOAPEnvelope(); 237 SOAPHeaderElement header = env.getHeaderByName(SESSION_NS, 238 SESSION_LOCALPART); 239 Long id; 240 241 if (header != null) { 242 try { 244 id = (Long )header. 245 getValueAsType(Constants.XSD_LONG); 246 } catch (Exception e) { 247 throw AxisFault.makeFault(e); 248 } 249 } else { 250 id = getNewSession(); 251 } 252 253 SimpleSession session = (SimpleSession)activeSessions.get(id); 254 if (session == null) { 255 id = getNewSession(); 257 session = (SimpleSession)activeSessions.get(id); 258 } 259 260 session.touch(); 262 263 context.setSession(session); 265 context.setProperty(SESSION_ID, id); 266 } 267 } 268 269 274 private synchronized Long getNewSession() 275 { 276 Long id = SessionUtils.generateSession(); 277 SimpleSession session = new SimpleSession(); 278 session.setTimeout(defaultSessionTimeout); 279 activeSessions.put(id, session); 280 return id; 281 } 282 283 291 public void setReapPeriodicity(long reapTime) 292 { 293 reapPeriodicity = reapTime; 294 } 295 296 301 public void setDefaultSessionTimeout(int defaultSessionTimeout) { 302 this.defaultSessionTimeout = defaultSessionTimeout; 303 } 304 } 305 | Popular Tags |