001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.geronimo.tomcat.cluster;
021    
022    import java.io.IOException;
023    
024    import org.apache.catalina.Session;
025    import org.apache.catalina.session.StandardManager;
026    import org.apache.catalina.session.StandardSession;
027    import org.apache.geronimo.clustering.SessionAlreadyExistException;
028    import org.apache.geronimo.clustering.SessionListener;
029    import org.apache.geronimo.clustering.SessionManager;
030    
031    /**
032     *
033     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
034     */
035    public class ClusteredManager extends StandardManager {
036        private final SessionManager sessionManager;
037    
038        public ClusteredManager(SessionManager sessionManager) {
039            if (null == sessionManager) {
040                throw new IllegalArgumentException("sessionManager is required");
041            }
042            this.sessionManager = sessionManager;
043    
044            sessionManager.registerListener(new MigrationListener());
045        }
046    
047        @Override
048        public Session createEmptySession() {
049            return new ClusteredSession();
050        }
051    
052        @Override
053        protected void doLoad() throws ClassNotFoundException, IOException {
054        }
055    
056        @Override
057        protected void doUnload() throws IOException {
058        }
059    
060        @Override
061        public void backgroundProcess() {
062        }
063    
064        private class MigrationListener implements SessionListener {
065    
066            public void notifyInboundSessionMigration(org.apache.geronimo.clustering.Session session) {
067                add(new ClusteredSession(session));
068            }
069    
070            public void notifyOutboundSessionMigration(org.apache.geronimo.clustering.Session session) {
071                ClusteredSession clusteredSession = getClusteredSession(session);
072                remove(clusteredSession);
073            }
074    
075            public void notifySessionDestruction(org.apache.geronimo.clustering.Session session) {
076                ClusteredSession clusteredSession = getClusteredSession(session);
077                remove(clusteredSession);
078            }
079            
080            protected ClusteredSession getClusteredSession(org.apache.geronimo.clustering.Session session) {
081                return (ClusteredSession) ClusteredManager.this.sessions.get(session.getSessionId());
082            }
083        }
084    
085        public class ClusteredSession extends StandardSession {
086            private org.apache.geronimo.clustering.Session session;
087    
088            protected ClusteredSession() {
089                super(ClusteredManager.this);
090            }
091    
092            protected ClusteredSession(org.apache.geronimo.clustering.Session session) {
093                super(ClusteredManager.this);
094                this.session = session;
095                
096                attributes = session.getState();
097    
098                super.setId(session.getSessionId());
099                setValid(true);
100                setNew(false);
101            }
102    
103            @Override
104            public void setId(String id) {
105                super.setId(id);
106                try {
107                    session = sessionManager.createSession(id);
108                } catch (SessionAlreadyExistException e) {
109                    throw (IllegalStateException) new IllegalStateException().initCause(e);
110                }
111                
112                attributes = session.getState();
113            }
114            
115            @Override
116            public void invalidate() throws IllegalStateException {
117                super.invalidate();
118                session.release();
119            }
120            
121            @Override
122            public void endAccess() {
123                super.endAccess();
124                session.onEndAccess();
125            }
126        }
127    
128    }