KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.UndeclaredThrowableException JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.jboss.logging.Logger;
28 import org.jboss.mq.Connection;
29 import org.jboss.mq.ReceiveRequest;
30 import org.jboss.mq.SpyDestination;
31 import org.jboss.mq.il.ClientILService;
32 import org.jboss.mq.il.ClientIL;
33 import org.jboss.mq.il.uil2.msgs.MsgTypes;
34 import org.jboss.mq.il.uil2.msgs.BaseMsg;
35 import org.jboss.mq.il.uil2.msgs.ReceiveRequestMsg;
36 import org.jboss.mq.il.uil2.msgs.DeleteTemporaryDestMsg;
37 import org.jboss.mq.il.uil2.msgs.PingMsg;
38
39 /** The UILClientILService runs on the client side of a JMS server connection
40  * and acts as a factory for the UILClientIL passed to the server. It also
41  * handles the callbacks from the client side SocketManager.
42  *
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 38405 $
45  */

46 public class UILClientILService
47       implements ClientILService, MsgTypes, SocketManagerHandler
48 {
49    static Logger log = Logger.getLogger(UILClientILService.class);
50
51    // Attributes ----------------------------------------------------
52

53    //the client IL
54
private UILClientIL clientIL;
55    //The thread that is doing the Socket reading work
56
private SocketManager socketMgr;
57    //A link on my connection
58
private Connection connection;
59    // Whether to send receive replies
60
private boolean sendReceiveReplies = true;
61
62    /**
63     * getClientIL method comment.
64     *
65     * @return The ClientIL value
66     * @exception Exception Description of Exception
67     */

68    public ClientIL getClientIL()
69          throws Exception JavaDoc
70    {
71       return clientIL;
72    }
73
74    /**
75     * init method comment.
76     *
77     * @param connection Description of Parameter
78     * @param props Description of Parameter
79     * @exception Exception Description of Exception
80     */

81    public void init(Connection connection, Properties JavaDoc props)
82          throws Exception JavaDoc
83    {
84       this.connection = connection;
85       clientIL = new UILClientIL();
86       UILServerIL serverIL = (UILServerIL) connection.getServerIL();
87       socketMgr = serverIL.getSocketMgr();
88       String JavaDoc t = props.getProperty(UILServerILFactory.UIL_BUFFERSIZE_KEY);
89       if (t != null)
90          socketMgr.setBufferSize(Integer.parseInt(t));
91       t = props.getProperty(UILServerILFactory.UIL_CHUNKSIZE_KEY);
92       if (t != null)
93          socketMgr.setChunkSize(Integer.parseInt(t));
94       // If we have this property, it means it is a new server
95
t = props.getProperty(UILServerILFactory.UIL_RECEIVE_REPLIES_KEY);
96       if (t != null)
97          sendReceiveReplies = false;
98       socketMgr.setHandler(this);
99    }
100
101    /** Callback from the SocketManager
102     */

103    public void handleMsg(BaseMsg msg)
104       throws Exception JavaDoc
105    {
106       boolean trace = log.isTraceEnabled();
107       int msgType = msg.getMsgType();
108       if (trace)
109          log.trace("Begin handleMsg, msgType: " + msgType);
110       switch( msgType )
111       {
112          case m_acknowledge:
113             // We have to ignore NACK replies because of backwards compatibility
114
break;
115          case m_receiveRequest:
116             ReceiveRequestMsg rmsg = (ReceiveRequestMsg) msg;
117             ReceiveRequest[] messages = rmsg.getMessages();
118             connection.asynchDeliver(messages);
119             // It is an old server that needs a reply
120
if (sendReceiveReplies)
121             {
122                rmsg.trimTheMessages();
123                socketMgr.sendReply(msg);
124             }
125             break;
126          case m_deleteTemporaryDestination:
127             DeleteTemporaryDestMsg dmsg = (DeleteTemporaryDestMsg) msg;
128             SpyDestination dest = dmsg.getDest();
129             connection.asynchDeleteTemporaryDestination(dest);
130             socketMgr.sendReply(msg);
131             break;
132          case m_close:
133             connection.asynchClose();
134             socketMgr.sendReply(msg);
135             break;
136          case m_pong:
137             PingMsg pmsg = (PingMsg) msg;
138             long time = pmsg.getTime();
139             connection.asynchPong(time);
140             break;
141          default:
142             connection.asynchFailure("UILClientILService received bad msg: "+msg, null);
143       }
144       if (trace)
145          log.trace("End handleMsg");
146    }
147
148    /**
149     *
150     * @exception Exception Description of Exception
151     */

152    public void start() throws Exception JavaDoc
153    {
154       log.debug("Starting");
155    }
156
157    /**
158     * @exception Exception Description of Exception
159     */

160    public void stop() throws Exception JavaDoc
161    {
162       log.debug("Stopping");
163       socketMgr.stop();
164    }
165
166    public void onStreamNotification(Object JavaDoc stream, int size)
167    {
168       connection.asynchPong(System.currentTimeMillis());
169    }
170
171    public void asynchFailure(String JavaDoc error, Throwable JavaDoc e)
172    {
173       if (e instanceof Exception JavaDoc)
174          connection.asynchFailure(error, e);
175       else
176          connection.asynchFailure(error, new UndeclaredThrowableException JavaDoc(e));
177    }
178
179    public void close()
180    {
181    }
182 }
183
Popular Tags