1 17 package org.apache.geronimo.jetty6.cluster; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 import javax.servlet.http.HttpServletRequest ; 23 24 import org.apache.geronimo.clustering.SessionAlreadyExistException; 25 import org.apache.geronimo.clustering.SessionListener; 26 import org.apache.geronimo.clustering.SessionManager; 27 import org.mortbay.jetty.servlet.AbstractSessionManager; 28 import org.mortbay.jetty.servlet.HashSessionIdManager; 29 30 31 35 public class ClusteredSessionManager extends AbstractSessionManager { 36 37 private final SessionManager sessionManager; 38 private final Map <String , ClusteredSession> idToSession = new HashMap <String , ClusteredSession>(); 39 40 public ClusteredSessionManager(SessionManager sessionManager) { 41 this.sessionManager = sessionManager; 42 43 String workerName = sessionManager.getNode().getName(); 44 workerName = workerName.replaceAll(" ", ""); 45 HashSessionIdManager sessionIdManager = new HashSessionIdManager(); 46 sessionIdManager.setWorkerName(workerName); 47 setMetaManager(sessionIdManager); 48 49 sessionManager.registerListener(new MigrationListener()); 50 51 setMaxInactiveInterval(-1); 53 } 54 55 @Override 56 protected Session newSession(HttpServletRequest request) { 57 return new ClusteredSession(request); 58 } 59 60 private class MigrationListener implements SessionListener { 61 62 public void notifyInboundSessionMigration(org.apache.geronimo.clustering.Session session) { 63 addSession(new ClusteredSession(session), false); 64 } 65 66 public void notifyOutboundSessionMigration(org.apache.geronimo.clustering.Session session) { 67 ClusteredSession clusteredSession; 68 synchronized (idToSession) { 69 clusteredSession = (ClusteredSession) idToSession.remove(session.getSessionId()); 70 } 71 if (null == clusteredSession) { 72 throw new AssertionError ("Session [" + session + "] is undefined"); 73 } 74 removeSession(clusteredSession, false); 75 } 76 } 77 78 public class ClusteredSession extends Session { 79 private final org.apache.geronimo.clustering.Session session; 80 81 protected ClusteredSession(HttpServletRequest request) { 82 super(request); 83 try { 84 this.session = sessionManager.createSession(getId()); 85 } catch (SessionAlreadyExistException e) { 86 throw (IllegalStateException ) new IllegalStateException ().initCause(e); 87 } 88 synchronized (idToSession) { 89 idToSession.put(getId(), this); 90 } 91 } 92 93 protected ClusteredSession(org.apache.geronimo.clustering.Session session) { 94 super(session.getSessionId()); 95 this.session = session; 96 synchronized (idToSession) { 97 idToSession.put(getId(), this); 98 } 99 } 100 101 @Override 102 protected Map newAttributeMap() { 103 return session.getState(); 104 } 105 } 106 107 } 108 | Popular Tags |