KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > server > ClientReconnectInterceptor


1 /*
2 * JBoss, the OpenSource EJB server
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.mq.server;
8
9 import java.util.Iterator JavaDoc;
10
11 import javax.jms.InvalidClientIDException JavaDoc;
12 import javax.jms.JMSException JavaDoc;
13
14 import org.jboss.logging.Logger;
15 import org.jboss.mq.ConnectionToken;
16 /**
17  * The JMS spec does not let a second client with the same clientID connect to
18  * the server. The second client gets an InvalidClientIDException exception.
19  * This interceptor modifies the server so that the first client gets
20  * disconnected and then the second client can connect successfully.
21  *
22  * Currently it only works if the client id is set by the client using the
23  * setClient method call.
24  *
25  * @author <a HREF="mailto:hchirino@jboss.org">Hiram Chirino</a>
26  */

27 public class ClientReconnectInterceptor extends JMSServerInterceptorSupport {
28
29     static protected Logger log =
30         Logger.getLogger(ClientReconnectInterceptor.class);
31
32     private JMSDestinationManager destinationManager;
33     private JMSDestinationManager getDestinationManager() {
34         if(destinationManager!=null)
35             return destinationManager;
36             
37         JMSServerInterceptor interceptor = getNext();
38         while( ! (interceptor instanceof JMSDestinationManager) )
39             interceptor = getNext();
40         destinationManager = (JMSDestinationManager)interceptor;
41         return destinationManager;
42     }
43     
44     private ConnectionToken findConnectionTokenFor(String JavaDoc clientID) {
45         Iterator JavaDoc iterator = getDestinationManager().clientConsumers.keySet().iterator();
46         while (iterator.hasNext()) {
47             ConnectionToken dc = (ConnectionToken) iterator.next();
48             if( dc.getClientID().equals(clientID))
49                 return dc;
50         }
51         return null;
52     }
53
54     /**
55      * @see org.jboss.mq.server.JMSServerInterceptor#checkID(java.lang.String)
56      */

57     public void checkID(String JavaDoc ID) throws JMSException JavaDoc {
58         try {
59             super.checkID(ID);
60         } catch (InvalidClientIDException JavaDoc e) {
61             ConnectionToken dc = findConnectionTokenFor(ID);
62             // The InvalidClientIDException could have been thrown
63
// due to another reason besides a double client connect.
64
if( dc == null )
65                 throw e;
66                 
67             // try to disconnect the previous client
68
try {
69                 super.connectionClosing(dc);
70             } catch (Throwable JavaDoc e2) {
71                 // Log it in case we ever care to find out the details.
72
log.trace("Disconnect of previously connected client caused an error:",e);
73             }
74             
75             // try it again...
76
super.checkID(ID);
77         }
78     }
79 }
80
Popular Tags