KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > session > JvmRouteSessionIDBinderListener


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

16
17 package org.apache.catalina.cluster.session;
18
19 import java.io.IOException JavaDoc;
20
21 import org.apache.catalina.Container;
22 import org.apache.catalina.Context;
23 import org.apache.catalina.LifecycleException;
24 import org.apache.catalina.Session;
25 import org.apache.catalina.cluster.CatalinaCluster;
26 import org.apache.catalina.cluster.ClusterMessage;
27 import org.apache.catalina.cluster.MessageListener;
28 import org.apache.catalina.core.StandardEngine;
29 import org.apache.catalina.util.StringManager;
30
31 /**
32  * Receive SessionID cluster change from other backup node after primary session
33  * node is failed.
34  *
35  * @author Peter Rossbach
36  * @version 1.0
37  */

38 public class JvmRouteSessionIDBinderListener implements MessageListener {
39     /*--Static Variables----------------------------------------*/
40     public static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
41             .getLog(JvmRouteSessionIDBinderListener.class);
42
43     /**
44      * The descriptive information about this implementation.
45      */

46     protected static final String JavaDoc info = "org.apache.catalina.session.JvmRouteSessionIDBinderListener/1.0";
47
48     /*--Instance Variables--------------------------------------*/
49
50     /**
51      * The string manager for this package.
52      */

53     private StringManager sm = StringManager.getManager(Constants.Package);
54
55     protected CatalinaCluster cluster = null;
56
57     protected boolean started = false;
58
59     /**
60      * number of session that goes to this cluster node
61      */

62     private long numberOfSessions = 0;
63
64     /*--Constructor---------------------------------------------*/
65
66     public JvmRouteSessionIDBinderListener() {
67     }
68
69     /*--Logic---------------------------------------------------*/
70
71     /**
72      * Return descriptive information about this implementation.
73      */

74     public String JavaDoc getInfo() {
75
76         return (info);
77
78     }
79
80     /**
81      * @return Returns the numberOfSessions.
82      */

83     public long getNumberOfSessions() {
84         return numberOfSessions;
85     }
86
87     /**
88      * Add this Mover as Cluster Listener ( receiver)
89      *
90      * @throws LifecycleException
91      */

92     public void start() throws LifecycleException {
93         if (started)
94             return;
95         getCluster().addClusterListener(this);
96         started = true;
97         if (log.isInfoEnabled())
98             log.info(sm.getString("jvmRoute.clusterListener.started"));
99     }
100
101     /**
102      * Remove this from Cluster Listener
103      *
104      * @throws LifecycleException
105      */

106     public void stop() throws LifecycleException {
107         started = false;
108         getCluster().removeClusterListener(this);
109         if (log.isInfoEnabled())
110             log.info(sm.getString("jvmRoute.clusterListener.stopped"));
111     }
112
113     /**
114      * Callback from the cluster, when a message is received, The cluster will
115      * broadcast it invoking the messageReceived on the receiver.
116      *
117      * @param msg
118      * ClusterMessage - the message received from the cluster
119      */

120     public void messageReceived(ClusterMessage msg) {
121         if (msg instanceof SessionIDMessage && msg != null) {
122             SessionIDMessage sessionmsg = (SessionIDMessage) msg;
123             if (log.isDebugEnabled())
124                 log.debug(sm.getString(
125                         "jvmRoute.receiveMessage.sessionIDChanged", sessionmsg
126                                 .getOrignalSessionID(), sessionmsg
127                                 .getBackupSessionID(), sessionmsg
128                                 .getContextPath()));
129             Container host = getCluster().getContainer();
130             if (host != null) {
131                 Context context = (Context) host.findChild(sessionmsg
132                         .getContextPath());
133                 if (context != null) {
134                     try {
135                         Session session = context.getManager().findSession(
136                                 sessionmsg.getOrignalSessionID());
137                         if (session != null) {
138                             session.setId(sessionmsg.getBackupSessionID());
139                         } else if (log.isInfoEnabled())
140                             log.info(sm.getString("jvmRoute.lostSession",
141                                     sessionmsg.getOrignalSessionID(),
142                                     sessionmsg.getContextPath()));
143                     } catch (IOException JavaDoc e) {
144                         log.error(e);
145                     }
146
147                 } else if (log.isErrorEnabled())
148                     log.error(sm.getString("jvmRoute.contextNotFound",
149                             sessionmsg.getContextPath(), ((StandardEngine) host
150                                     .getParent()).getJvmRoute()));
151             } else if (log.isErrorEnabled())
152                 log.error(sm.getString("jvmRoute.hostNotFound", sessionmsg.getContextPath()));
153         }
154     }
155
156     /**
157      * Accept only SessionIDMessages
158      *
159      * @param msg
160      * ClusterMessage
161      * @return boolean - returns true to indicate that messageReceived should be
162      * invoked. If false is returned, the messageReceived method will
163      * not be invoked.
164      */

165     public boolean accept(ClusterMessage msg) {
166         return (msg instanceof SessionIDMessage);
167     }
168
169     /*--Instance Getters/Setters--------------------------------*/
170     public CatalinaCluster getCluster() {
171         return cluster;
172     }
173
174     public void setCluster(CatalinaCluster cluster) {
175         this.cluster = cluster;
176     }
177
178     public boolean equals(Object JavaDoc listener) {
179         return super.equals(listener);
180     }
181
182     public int hashCode() {
183         return super.hashCode();
184     }
185
186 }
187
188
Popular Tags