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
18 package org.apache.catalina.ha.session;
19
20 import java.util.Map;
21
22 import org.apache.catalina.ha.ClusterManager;
23 import org.apache.catalina.ha.ClusterMessage;
24 import org.apache.catalina.ha;
25
26 /**
27 * Receive replicated SessionMessage form other cluster node.
28 * @author Filip Hanik
29 * @author Peter Rossbach
30 * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
31 */
32 public class ClusterSessionListener extends ClusterListener {
33
34 /**
35 * The descriptive information about this implementation.
36 */
37 protected static final String info = "org.apache.catalina.session.ClusterSessionListener/1.1";
38
39 //--Constructor---------------------------------------------
40
41 public ClusterSessionListener() {
42 }
43
44 //--Logic---------------------------------------------------
45
46 /**
47 * Return descriptive information about this implementation.
48 */
49 public String getInfo() {
50
51 return (info);
52
53 }
54
55 /**
56 * Callback from the cluster, when a message is received, The cluster will
57 * broadcast it invoking the messageReceived on the receiver.
58 *
59 * @param myobj
60 * ClusterMessage - the message received from the cluster
61 */
62 public void messageReceived(ClusterMessage myobj) {
63 if (myobj != null && myobj instanceof SessionMessage) {
64 SessionMessage msg = (SessionMessage) myobj;
65 String ctxname = msg.getContextName();
66 //check if the message is a EVT_GET_ALL_SESSIONS,
67 //if so, wait until we are fully started up
68 Map managers = cluster.getManagers() ;
69 if (ctxname == null) {
70 java.util.Iterator i = managers.keySet().iterator();
71 while (i.hasNext()) {
72 String key = (String) i.next();
73 ClusterManager mgr = (ClusterManager) managers.get(key);
74 if (mgr != null)
75 mgr.messageDataReceived(msg);
76 else {
77 //this happens a lot before the system has started
78 // up
79 if (log.isDebugEnabled())
80 log.debug("Context manager doesn't exist:"
81 + key);
82 }
83 }
84 } else {
85 ClusterManager mgr = (ClusterManager) managers.get(ctxname);
86 if (mgr != null)
87 mgr.messageDataReceived(msg);
88 else if (log.isWarnEnabled())
89 log.warn("Context manager doesn't exist:" + ctxname);
90 }
91 }
92 return;
93 }
94
95 /**
96 * Accept only SessionMessage
97 *
98 * @param msg
99 * ClusterMessage
100 * @return boolean - returns true to indicate that messageReceived should be
101 * invoked. If false is returned, the messageReceived method will
102 * not be invoked.
103 */
104 public boolean accept(ClusterMessage msg) {
105 return (msg instanceof SessionMessage);
106 }
107 }
108