KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > cluster > ClusteredSessionManager


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jetty6.cluster;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
32  *
33  * @version $Rev$ $Date$
34  */

35 public class ClusteredSessionManager extends AbstractSessionManager {
36     
37     private final SessionManager sessionManager;
38     private final Map JavaDoc<String JavaDoc, ClusteredSession> idToSession = new HashMap JavaDoc<String JavaDoc, ClusteredSession>();
39     
40     public ClusteredSessionManager(SessionManager sessionManager) {
41         this.sessionManager = sessionManager;
42
43         String JavaDoc 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         // sessions are not removed by this manager. They are invalidated via a callback mechanism
52
setMaxInactiveInterval(-1);
53     }
54
55     @Override JavaDoc
56     protected Session newSession(HttpServletRequest JavaDoc 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 JavaDoc("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 JavaDoc request) {
82             super(request);
83             try {
84                 this.session = sessionManager.createSession(getId());
85             } catch (SessionAlreadyExistException e) {
86                 throw (IllegalStateException JavaDoc) new IllegalStateException JavaDoc().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 JavaDoc
102         protected Map JavaDoc newAttributeMap() {
103             return session.getState();
104         }
105     }
106
107 }
108
Popular Tags