KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > uil2 > ServerSocketManagerHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.il.uil2;
23
24 import java.rmi.RemoteException JavaDoc;
25 import javax.jms.Destination JavaDoc;
26 import javax.transaction.xa.Xid JavaDoc;
27
28 import org.jboss.logging.Logger;
29 import org.jboss.mq.ConnectionToken;
30 import org.jboss.mq.AcknowledgementRequest;
31 import org.jboss.mq.Recoverable;
32 import org.jboss.mq.SpyMessage;
33 import org.jboss.mq.SpyDestination;
34 import org.jboss.mq.TransactionRequest;
35 import org.jboss.mq.DurableSubscriptionID;
36 import org.jboss.mq.il.uil2.msgs.BaseMsg;
37 import org.jboss.mq.il.uil2.msgs.ConnectionTokenMsg;
38 import org.jboss.mq.il.uil2.msgs.AcknowledgementRequestMsg;
39 import org.jboss.mq.il.uil2.msgs.MsgTypes;
40 import org.jboss.mq.il.uil2.msgs.AddMsg;
41 import org.jboss.mq.il.uil2.msgs.BrowseMsg;
42 import org.jboss.mq.il.uil2.msgs.CheckIDMsg;
43 import org.jboss.mq.il.uil2.msgs.CreateDestMsg;
44 import org.jboss.mq.il.uil2.msgs.DeleteTemporaryDestMsg;
45 import org.jboss.mq.il.uil2.msgs.GetIDMsg;
46 import org.jboss.mq.il.uil2.msgs.RecoverMsg;
47 import org.jboss.mq.il.uil2.msgs.TemporaryDestMsg;
48 import org.jboss.mq.il.uil2.msgs.ReceiveMsg;
49 import org.jboss.mq.il.uil2.msgs.EnableConnectionMsg;
50 import org.jboss.mq.il.uil2.msgs.SubscribeMsg;
51 import org.jboss.mq.il.uil2.msgs.TransactMsg;
52 import org.jboss.mq.il.uil2.msgs.UnsubscribeMsg;
53 import org.jboss.mq.il.uil2.msgs.DeleteSubscriptionMsg;
54 import org.jboss.mq.il.uil2.msgs.CheckUserMsg;
55 import org.jboss.mq.il.uil2.msgs.PingMsg;
56 import org.jboss.mq.il.Invoker;
57
58 /** This is the SocketManager callback handler for the UIL2 server side
59  * socket. This handles messages that are requests from clients.
60  *
61  * @author Scott.Stark@jboss.org
62  * @version $Revision: 45317 $
63  */

64 public class ServerSocketManagerHandler implements MsgTypes, SocketManagerHandler
65 {
66    private static Logger log = Logger.getLogger(ServerSocketManagerHandler.class);
67
68    private ConnectionToken connectionToken;
69    private Invoker server;
70    private SocketManager socketMgr;
71    private boolean closed;
72
73    public ServerSocketManagerHandler(Invoker server, SocketManager socketMgr)
74    {
75       this.server = server;
76       this.socketMgr = socketMgr;
77       this.closed = false;
78    }
79
80    /** The callback from the SocketManager
81     * @param msg
82     */

83    public void handleMsg(BaseMsg msg)
84       throws Exception JavaDoc
85    {
86       boolean trace = log.isTraceEnabled();
87       int msgType = msg.getMsgType();
88       if (trace)
89          log.trace("Begin handleMsg, msgType: " + msgType);
90
91       switch (msgType)
92       {
93          case m_setSpyDistributedConnection:
94             log.debug("Setting up the UILClientIL Connection");
95             ConnectionTokenMsg cmsg = (ConnectionTokenMsg) msg;
96             connectionToken = cmsg.getToken();
97             UILClientIL clientIL = (UILClientIL) connectionToken.clientIL;
98             clientIL.setSocketMgr(socketMgr);
99             socketMgr.sendReply(msg);
100             log.debug("The UILClientIL Connection is set up");
101             break;
102          case m_acknowledge:
103             AcknowledgementRequestMsg ackmsg = (AcknowledgementRequestMsg) msg;
104             AcknowledgementRequest ack = ackmsg.getAck();
105             server.acknowledge(connectionToken, ack);
106             // We send the reply although on newer clients it is ignored.
107
socketMgr.sendReply(msg);
108             break;
109          case m_addMessage:
110             AddMsg amsg = (AddMsg) msg;
111             server.addMessage(connectionToken, amsg.getMsg());
112             socketMgr.sendReply(msg);
113             break;
114          case m_browse:
115             BrowseMsg bmsg = (BrowseMsg) msg;
116             SpyMessage[] msgs = server.browse(connectionToken, bmsg.getDest(), bmsg.getSelector());
117             bmsg.setMessages(msgs);
118             socketMgr.sendReply(msg);
119             break;
120          case m_checkID:
121             CheckIDMsg idmsg = (CheckIDMsg) msg;
122             String JavaDoc ID = idmsg.getID();
123             server.checkID(ID);
124             if (connectionToken != null)
125                connectionToken.setClientID(ID);
126             socketMgr.sendReply(msg);
127             break;
128          case m_connectionClosing:
129             server.connectionClosing(connectionToken);
130             closed = true;
131             socketMgr.sendReply(msg);
132             socketMgr.stop();
133             break;
134          case m_createQueue:
135             CreateDestMsg cqmsg = (CreateDestMsg) msg;
136             Destination JavaDoc queue = server.createQueue(connectionToken, cqmsg.getName());
137             cqmsg.setDest(queue);
138             socketMgr.sendReply(msg);
139             break;
140          case m_createTopic:
141             CreateDestMsg ctmsg = (CreateDestMsg) msg;
142             Destination JavaDoc topic = server.createTopic(connectionToken, ctmsg.getName());
143             ctmsg.setDest(topic);
144             socketMgr.sendReply(msg);
145             break;
146          case m_deleteTemporaryDestination:
147             DeleteTemporaryDestMsg dtdmsg = (DeleteTemporaryDestMsg) msg;
148             SpyDestination tmpdest = dtdmsg.getDest();
149             server.deleteTemporaryDestination(connectionToken, tmpdest);
150             socketMgr.sendReply(msg);
151             break;
152          case m_getID:
153             GetIDMsg gidmsg = (GetIDMsg) msg;
154             String JavaDoc gid = server.getID();
155             if (connectionToken != null)
156                connectionToken.setClientID(gid);
157             gidmsg.setID(gid);
158             socketMgr.sendReply(msg);
159             break;
160          case m_getTemporaryQueue:
161             TemporaryDestMsg tqmsg = (TemporaryDestMsg) msg;
162             Destination JavaDoc tmpQueue = server.getTemporaryQueue(connectionToken);
163             tqmsg.setDest(tmpQueue);
164             socketMgr.sendReply(msg);
165             break;
166          case m_getTemporaryTopic:
167             TemporaryDestMsg ttmsg = (TemporaryDestMsg) msg;
168             Destination JavaDoc tmpTopic = server.getTemporaryTopic(connectionToken);
169             ttmsg.setDest(tmpTopic);
170             socketMgr.sendReply(msg);
171             break;
172          case m_receive:
173             ReceiveMsg rmsg = (ReceiveMsg) msg;
174             SpyMessage reply = server.receive(connectionToken, rmsg.getSubscriberID(), rmsg.getWait());
175             rmsg.setMessage(reply);
176             socketMgr.sendReply(msg);
177             break;
178          case m_setEnabled:
179             EnableConnectionMsg ecmsg = (EnableConnectionMsg) msg;
180             server.setEnabled(connectionToken, ecmsg.isEnabled());
181             socketMgr.sendReply(msg);
182             break;
183          case m_subscribe:
184             SubscribeMsg smsg = (SubscribeMsg) msg;
185             server.subscribe(connectionToken, smsg.getSubscription());
186             socketMgr.sendReply(msg);
187             break;
188          case m_transact:
189             TransactMsg tmsg = (TransactMsg) msg;
190             TransactionRequest trans = tmsg.getRequest();
191             server.transact(connectionToken, trans);
192             socketMgr.sendReply(msg);
193             break;
194          case m_recover:
195             RecoverMsg recmsg = (RecoverMsg) msg;
196             int flags = recmsg.getFlags();
197             if (server instanceof Recoverable)
198             {
199                Recoverable recoverable = (Recoverable) server;
200                Xid JavaDoc[] xids = recoverable.recover(connectionToken, flags);
201                recmsg.setXids(xids);
202                socketMgr.sendReply(msg);
203                break;
204             }
205             throw new IllegalStateException JavaDoc("Invoker does not implement recoverable " + server);
206          case m_unsubscribe:
207             UnsubscribeMsg umsg = (UnsubscribeMsg) msg;
208             server.unsubscribe(connectionToken, umsg.getSubscriptionID());
209             socketMgr.sendReply(msg);
210             break;
211          case m_destroySubscription:
212             DeleteSubscriptionMsg dsmsg = (DeleteSubscriptionMsg) msg;
213             DurableSubscriptionID dsub = dsmsg.getSubscriptionID();
214             server.destroySubscription(connectionToken, dsub);
215             socketMgr.sendReply(msg);
216             break;
217          case m_checkUser:
218             CheckUserMsg cumsg = (CheckUserMsg) msg;
219             String JavaDoc uid = server.checkUser(cumsg.getUsername(), cumsg.getPassword());
220             cumsg.setID(uid);
221             cumsg.clearPassword();
222             socketMgr.sendReply(msg);
223             break;
224          case m_ping:
225             PingMsg ping = (PingMsg) msg;
226             server.ping(connectionToken, ping.getTime());
227             break;
228          case m_pong:
229             break;
230          // Ignore, this is an old client, that still wants to send us replies
231
case m_receiveRequest:
232             break;
233          case m_authenticate:
234             CheckUserMsg cumsg2 = (CheckUserMsg) msg;
235             String JavaDoc sessionID = server.authenticate(cumsg2.getUsername(), cumsg2.getPassword());
236             cumsg2.setID(sessionID);
237             cumsg2.clearPassword();
238             socketMgr.sendReply(msg);
239             break;
240          default:
241             throw new RemoteException JavaDoc("Unknown msgType: "+msgType);
242       }
243       if (trace)
244          log.trace("End handleMsg, msgType: " + msgType);
245    }
246
247    public void onStreamNotification(Object JavaDoc stream, int size)
248    {
249    }
250
251    public void asynchFailure(String JavaDoc error, Throwable JavaDoc e)
252    {
253       log.debug(error, e);
254    }
255
256    public void close()
257    {
258       try
259       {
260          if (closed == false)
261             server.connectionClosing(connectionToken);
262       }
263       catch (Exception JavaDoc e)
264       {
265          log.debug("Error closing connection: ", e);
266       }
267    }
268 }
269
Popular Tags