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:$ $Date:$
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(
041: "sessionManager is required");
042: }
043: this .sessionManager = sessionManager;
044:
045: sessionManager.registerListener(new MigrationListener());
046: }
047:
048: @Override
049: public Session createEmptySession() {
050: return new ClusteredSession();
051: }
052:
053: @Override
054: protected void doLoad() throws ClassNotFoundException, IOException {
055: }
056:
057: @Override
058: protected void doUnload() throws IOException {
059: }
060:
061: @Override
062: public void backgroundProcess() {
063: }
064:
065: private class MigrationListener implements SessionListener {
066:
067: public void notifyInboundSessionMigration(
068: org.apache.geronimo.clustering.Session session) {
069: add(new ClusteredSession(session));
070: }
071:
072: public void notifyOutboundSessionMigration(
073: org.apache.geronimo.clustering.Session session) {
074: ClusteredSession clusteredSession = getClusteredSession(session);
075: remove(clusteredSession);
076: }
077:
078: public void notifySessionDestruction(
079: org.apache.geronimo.clustering.Session session) {
080: ClusteredSession clusteredSession = getClusteredSession(session);
081: remove(clusteredSession);
082: }
083:
084: protected ClusteredSession getClusteredSession(
085: org.apache.geronimo.clustering.Session session) {
086: return (ClusteredSession) ClusteredManager.this .sessions
087: .get(session.getSessionId());
088: }
089: }
090:
091: public class ClusteredSession extends StandardSession {
092: private org.apache.geronimo.clustering.Session session;
093:
094: protected ClusteredSession() {
095: super (ClusteredManager.this );
096: }
097:
098: protected ClusteredSession(
099: org.apache.geronimo.clustering.Session session) {
100: super (ClusteredManager.this );
101: this .session = session;
102:
103: attributes = session.getState();
104:
105: super .setId(session.getSessionId());
106: setValid(true);
107: setNew(false);
108: }
109:
110: @Override
111: public void setId(String id) {
112: super .setId(id);
113: try {
114: session = sessionManager.createSession(id);
115: } catch (SessionAlreadyExistException e) {
116: throw (IllegalStateException) new IllegalStateException()
117: .initCause(e);
118: }
119:
120: attributes = session.getState();
121: }
122:
123: @Override
124: public void invalidate() throws IllegalStateException {
125: super .invalidate();
126: session.release();
127: }
128:
129: @Override
130: public void endAccess() {
131: super.endAccess();
132: session.onEndAccess();
133: }
134: }
135:
136: }
|