001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.catalina.ha.authenticator;
019:
020: import java.util.Map;
021: import java.io.IOException;
022:
023: import org.apache.catalina.Session;
024: import org.apache.catalina.ha.ClusterManager;
025: import org.apache.catalina.ha.ClusterMessage;
026: import org.apache.catalina.ha.ClusterListener;
027:
028: /**
029: * Receive replicated SingleSignOnMessage form other cluster node.
030: *
031: * @author Fabien Carrion
032: */
033: public class ClusterSingleSignOnListener extends ClusterListener {
034:
035: /**
036: * The descriptive information about this implementation.
037: */
038: protected static final String info = "org.apache.catalina.session.ClusterSingleSignOnListener/1.0";
039:
040: // ------------------------------------------------------------- Properties
041:
042: private ClusterSingleSignOn clusterSSO = null;
043:
044: //--Constructor---------------------------------------------
045:
046: public ClusterSingleSignOnListener() {
047: }
048:
049: //--Logic---------------------------------------------------
050:
051: /**
052: * Return descriptive information about this implementation.
053: */
054: public String getInfo() {
055:
056: return (info);
057:
058: }
059:
060: public ClusterSingleSignOn getClusterSSO() {
061:
062: return clusterSSO;
063:
064: }
065:
066: public void setClusterSSO(ClusterSingleSignOn clusterSSO) {
067:
068: this .clusterSSO = clusterSSO;
069:
070: }
071:
072: /**
073: * Callback from the cluster, when a message is received, The cluster will
074: * broadcast it invoking the messageReceived on the receiver.
075: *
076: * @param myobj
077: * ClusterMessage - the message received from the cluster
078: */
079: public void messageReceived(ClusterMessage myobj) {
080: if (myobj != null && myobj instanceof SingleSignOnMessage) {
081: SingleSignOnMessage msg = (SingleSignOnMessage) myobj;
082: int action = msg.getAction();
083: Session session = null;
084:
085: if (log.isDebugEnabled())
086: log.debug("SingleSignOnMessage Received with action "
087: + msg.getAction());
088:
089: switch (action) {
090: case SingleSignOnMessage.ADD_SESSION:
091: session = getSession(msg.getSessionId(), msg
092: .getContextName());
093: if (session != null)
094: clusterSSO.associateLocal(msg.getSsoId(), session);
095: break;
096: case SingleSignOnMessage.DEREGISTER_SESSION:
097: session = getSession(msg.getSessionId(), msg
098: .getContextName());
099: if (session != null)
100: clusterSSO.deregisterLocal(msg.getSsoId(), session);
101: break;
102: case SingleSignOnMessage.LOGOUT_SESSION:
103: clusterSSO.deregisterLocal(msg.getSsoId());
104: break;
105: case SingleSignOnMessage.REGISTER_SESSION:
106: clusterSSO.registerLocal(msg.getSsoId(), null, msg
107: .getAuthType(), msg.getUsername(), msg
108: .getPassword());
109: break;
110: case SingleSignOnMessage.UPDATE_SESSION:
111: clusterSSO.updateLocal(msg.getSsoId(), null, msg
112: .getAuthType(), msg.getUsername(), msg
113: .getPassword());
114: break;
115: case SingleSignOnMessage.REMOVE_SESSION:
116: session = getSession(msg.getSessionId(), msg
117: .getContextName());
118: if (session != null)
119: clusterSSO.removeSessionLocal(msg.getSsoId(),
120: session);
121: break;
122: }
123: }
124: }
125:
126: /**
127: * Accept only SingleSignOnMessage
128: *
129: * @param msg
130: * ClusterMessage
131: * @return boolean - returns true to indicate that messageReceived should be
132: * invoked. If false is returned, the messageReceived method will
133: * not be invoked.
134: */
135: public boolean accept(ClusterMessage msg) {
136: return (msg instanceof SingleSignOnMessage);
137: }
138:
139: private Session getSession(String sessionId, String ctxname) {
140:
141: Map managers = clusterSSO.getCluster().getManagers();
142: Session session = null;
143:
144: if (ctxname == null) {
145: java.util.Iterator i = managers.keySet().iterator();
146: while (i.hasNext()) {
147: String key = (String) i.next();
148: ClusterManager mgr = (ClusterManager) managers.get(key);
149: if (mgr != null) {
150: try {
151: session = mgr.findSession(sessionId);
152: } catch (IOException io) {
153: log.error("Session doesn't exist:" + io);
154: }
155: return session;
156: } else {
157: //this happens a lot before the system has started
158: // up
159: if (log.isDebugEnabled())
160: log.debug("Context manager doesn't exist:"
161: + key);
162: }
163: }
164: } else {
165: ClusterManager mgr = (ClusterManager) managers.get(ctxname);
166: if (mgr != null) {
167: try {
168: session = mgr.findSession(sessionId);
169: } catch (IOException io) {
170: log.error("Session doesn't exist:" + io);
171: }
172: return session;
173: } else if (log.isErrorEnabled())
174: log.error("Context manager doesn't exist:" + ctxname);
175: }
176:
177: return null;
178: }
179: }
|