Save This Page
Home » apache-tomcat-6.0.16-src » org.apache » catalina » ha » session » [javadoc | source]
    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.io.IOException;
   21   
   22   import org.apache.catalina.Container;
   23   import org.apache.catalina.Context;
   24   import org.apache.catalina.Engine;
   25   import org.apache.catalina.LifecycleException;
   26   import org.apache.catalina.Session;
   27   import org.apache.catalina.ha.ClusterMessage;
   28   import org.apache.catalina.core.StandardEngine;
   29   import org.apache.catalina.ha;
   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 $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
   37    */
   38   public class JvmRouteSessionIDBinderListener extends ClusterListener {
   39    
   40       /**
   41        * The descriptive information about this implementation.
   42        */
   43       protected static final String info = "org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener/1.1";
   44   
   45       //--Instance Variables--------------------------------------
   46   
   47   
   48       protected boolean started = false;
   49   
   50       /**
   51        * number of session that goes to this cluster node
   52        */
   53       private long numberOfSessions = 0;
   54   
   55       //--Constructor---------------------------------------------
   56   
   57       public JvmRouteSessionIDBinderListener() {
   58       }
   59   
   60       //--Logic---------------------------------------------------
   61   
   62       /**
   63        * Return descriptive information about this implementation.
   64        */
   65       public String getInfo() {
   66   
   67           return (info);
   68   
   69       }
   70   
   71       /**
   72        * @return Returns the numberOfSessions.
   73        */
   74       public long getNumberOfSessions() {
   75           return numberOfSessions;
   76       }
   77   
   78       /**
   79        * Add this Mover as Cluster Listener ( receiver)
   80        * 
   81        * @throws LifecycleException
   82        */
   83       public void start() throws LifecycleException {
   84           if (started)
   85               return;
   86           getCluster().addClusterListener(this);
   87           started = true;
   88           if (log.isInfoEnabled())
   89               log.info(sm.getString("jvmRoute.clusterListener.started"));
   90       }
   91   
   92       /**
   93        * Remove this from Cluster Listener
   94        * 
   95        * @throws LifecycleException
   96        */
   97       public void stop() throws LifecycleException {
   98           started = false;
   99           getCluster().removeClusterListener(this);
  100           if (log.isInfoEnabled())
  101               log.info(sm.getString("jvmRoute.clusterListener.stopped"));
  102       }
  103   
  104       /**
  105        * Callback from the cluster, when a message is received, The cluster will
  106        * broadcast it invoking the messageReceived on the receiver.
  107        * 
  108        * @param msg
  109        *            ClusterMessage - the message received from the cluster
  110        */
  111       public void messageReceived(ClusterMessage msg) {
  112           if (msg instanceof SessionIDMessage && msg != null) {
  113               SessionIDMessage sessionmsg = (SessionIDMessage) msg;
  114               if (log.isDebugEnabled())
  115                   log.debug(sm.getString(
  116                           "jvmRoute.receiveMessage.sessionIDChanged", sessionmsg
  117                                   .getOrignalSessionID(), sessionmsg
  118                                   .getBackupSessionID(), sessionmsg
  119                                   .getContextPath()));
  120               Container container = getCluster().getContainer();
  121               Container host = null ;
  122               if(container instanceof Engine) {
  123                   host = container.findChild(sessionmsg.getHost());
  124               } else {
  125                   host = container ;
  126               }
  127               if (host != null) {
  128                   Context context = (Context) host.findChild(sessionmsg
  129                           .getContextPath());
  130                   if (context != null) {
  131                       try {
  132                           Session session = context.getManager().findSession(
  133                                   sessionmsg.getOrignalSessionID());
  134                           if (session != null) {
  135                               session.setId(sessionmsg.getBackupSessionID());
  136                           } else if (log.isInfoEnabled())
  137                               log.info(sm.getString("jvmRoute.lostSession",
  138                                       sessionmsg.getOrignalSessionID(),
  139                                       sessionmsg.getContextPath()));
  140                       } catch (IOException e) {
  141                           log.error(e);
  142                       }
  143   
  144                   } else if (log.isErrorEnabled())
  145                       log.error(sm.getString("jvmRoute.contextNotFound",
  146                               sessionmsg.getContextPath(), ((StandardEngine) host
  147                                       .getParent()).getJvmRoute()));
  148               } else if (log.isErrorEnabled())
  149                   log.error(sm.getString("jvmRoute.hostNotFound", sessionmsg.getContextPath()));
  150           }
  151           return;
  152       }
  153   
  154       /**
  155        * Accept only SessionIDMessages
  156        * 
  157        * @param msg
  158        *            ClusterMessage
  159        * @return boolean - returns true to indicate that messageReceived should be
  160        *         invoked. If false is returned, the messageReceived method will
  161        *         not be invoked.
  162        */
  163       public boolean accept(ClusterMessage msg) {
  164           return (msg instanceof SessionIDMessage);
  165       }
  166   }
  167   

Save This Page
Home » apache-tomcat-6.0.16-src » org.apache » catalina » ha » session » [javadoc | source]