KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > il > oil2 > OIL2Request


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

7 package org.jboss.mq.il.oil2;
8
9 import java.io.Externalizable JavaDoc;
10 import java.io.IOException JavaDoc;
11
12 import org.jboss.mq.AcknowledgementRequest;
13 import org.jboss.mq.ReceiveRequest;
14 import org.jboss.mq.SpyMessage;
15 import org.jboss.mq.TransactionRequest;
16
17 /**
18  *
19  *
20  * @author <a HREF="mailto:hiram.chirino@jboss.org">Hiram Chirino</a>
21  * @version $Revision: 1.2 $
22  */

23 public class OIL2Request implements Externalizable JavaDoc
24 {
25    static final long serialVersionUID = -1040314575679482420L;
26    private static int lastRequestId = 0;
27    private static Object JavaDoc lastRequestIdLock = new Object JavaDoc();
28    public Integer JavaDoc requestId;
29    public byte operation;
30    public Object JavaDoc arguments[];
31    
32    
33    public OIL2Request() {}
34    public OIL2Request(byte operation, Object JavaDoc[] arguments)
35    {
36       synchronized (lastRequestIdLock)
37       {
38          this.requestId = new Integer JavaDoc(lastRequestId++);
39       }
40       this.operation = operation;
41       this.arguments = arguments;
42    }
43
44    public void writeExternal(java.io.ObjectOutput JavaDoc out) throws java.io.IOException JavaDoc
45    {
46       out.writeByte(operation);
47       out.writeInt(requestId.intValue());
48       switch (operation)
49       {
50          
51          //////////////////////////////////////////////////////////////////
52
// These are the requests that the Server makes to the client
53
//////////////////////////////////////////////////////////////////
54
case OIL2Constants.CLIENT_RECEIVE :
55             ReceiveRequest[] messages = (ReceiveRequest[]) arguments[0];
56             out.writeInt(messages.length);
57             for (int i = 0; i < messages.length; ++i)
58                messages[i].writeExternal(out);
59             break;
60
61          case OIL2Constants.CLIENT_DELETE_TEMPORARY_DESTINATION :
62             out.writeObject(arguments[0]);
63             break;
64
65          case OIL2Constants.CLIENT_CLOSE :
66             break;
67
68          case OIL2Constants.CLIENT_PONG :
69             out.writeLong(((Long JavaDoc) arguments[0]).longValue());
70             break;
71
72
73          //////////////////////////////////////////////////////////////////
74
// These are the requests that the client makes to the server
75
//////////////////////////////////////////////////////////////////
76
case OIL2Constants.SERVER_SET_SPY_DISTRIBUTED_CONNECTION :
77             out.writeObject(arguments[0]);
78             break;
79
80          case OIL2Constants.SERVER_ACKNOWLEDGE :
81             ((AcknowledgementRequest)arguments[0]).writeExternal(out);
82             break;
83
84          case OIL2Constants.SERVER_ADD_MESSAGE :
85             SpyMessage.writeMessage(((SpyMessage)arguments[0]), out);
86             break;
87
88          case OIL2Constants.SERVER_BROWSE :
89             out.writeObject(arguments[0]);
90             writeString(out,(String JavaDoc)arguments[1]);
91             break;
92
93          case OIL2Constants.SERVER_CHECK_ID :
94             writeString(out,(String JavaDoc)arguments[0]);
95             break;
96
97          case OIL2Constants.SERVER_CONNECTION_CLOSING :
98             arguments = null;
99             break;
100
101          case OIL2Constants.SERVER_CREATE_QUEUE :
102             writeString(out,(String JavaDoc)arguments[0]);
103             break;
104
105          case OIL2Constants.SERVER_CREATE_TOPIC :
106             writeString(out,(String JavaDoc)arguments[0]);
107             break;
108
109          case OIL2Constants.SERVER_GET_ID :
110             break;
111
112          case OIL2Constants.SERVER_GET_TEMPORARY_QUEUE :
113             break;
114
115          case OIL2Constants.SERVER_GET_TEMPORARY_TOPIC :
116             break;
117             
118          case OIL2Constants.SERVER_DELETE_TEMPORARY_DESTINATION :
119             out.writeObject(arguments[0]);
120             break;
121
122          case OIL2Constants.SERVER_RECEIVE :
123             out.writeInt(((Integer JavaDoc)arguments[0]).intValue());
124             out.writeLong(((Long JavaDoc)arguments[1]).longValue());
125             break;
126
127          case OIL2Constants.SERVER_SET_ENABLED :
128             out.writeBoolean(((Boolean JavaDoc)arguments[0]).booleanValue());
129             break;
130
131          case OIL2Constants.SERVER_SUBSCRIBE :
132             out.writeObject(arguments[0]);
133             break;
134
135          case OIL2Constants.SERVER_TRANSACT :
136             ((TransactionRequest)arguments[0]).writeExternal(out);
137             break;
138
139          case OIL2Constants.SERVER_UNSUBSCRIBE :
140             out.writeInt(((Integer JavaDoc)arguments[0]).intValue());
141             break;
142
143          case OIL2Constants.SERVER_DESTROY_SUBSCRIPTION :
144             out.writeObject(arguments[0]);
145             break;
146
147          case OIL2Constants.SERVER_CHECK_USER :
148             writeString(out,(String JavaDoc)arguments[0]);
149             writeString(out,(String JavaDoc)arguments[1]);
150             break;
151
152          case OIL2Constants.SERVER_PING :
153             out.writeLong(((Long JavaDoc)arguments[0]).longValue());
154             break;
155
156          case OIL2Constants.SERVER_AUTHENTICATE :
157             writeString(out,(String JavaDoc)arguments[0]);
158             writeString(out,(String JavaDoc)arguments[1]);
159             break;
160
161          default :
162             throw new IOException JavaDoc("Protocol Error: Bad operation code.");
163       }
164    }
165
166    public void readExternal(java.io.ObjectInput JavaDoc in) throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
167    {
168       operation = in.readByte();
169       requestId = new Integer JavaDoc(in.readInt());
170       switch (operation)
171       {
172          //////////////////////////////////////////////////////////////////
173
// These are the requests that the Server makes to the client
174
//////////////////////////////////////////////////////////////////
175
case OIL2Constants.CLIENT_RECEIVE :
176             int numReceives = in.readInt();
177             org.jboss.mq.ReceiveRequest[] messages = new org.jboss.mq.ReceiveRequest[numReceives];
178             for (int i = 0; i < numReceives; ++i)
179             {
180                messages[i] = new ReceiveRequest();
181                messages[i].readExternal(in);
182             }
183             arguments = new Object JavaDoc[] { messages };
184             break;
185
186          case OIL2Constants.CLIENT_DELETE_TEMPORARY_DESTINATION :
187             arguments = new Object JavaDoc[] { in.readObject()};
188             break;
189
190          case OIL2Constants.CLIENT_CLOSE :
191             arguments = null;
192             break;
193
194          case OIL2Constants.CLIENT_PONG :
195             arguments = new Object JavaDoc[] { new Long JavaDoc(in.readLong())};
196             break;
197             
198          //////////////////////////////////////////////////////////////////
199
// These are the requests that the client makes to the server
200
//////////////////////////////////////////////////////////////////
201
case OIL2Constants.SERVER_SET_SPY_DISTRIBUTED_CONNECTION :
202             // assert connectionToken == null
203
arguments = new Object JavaDoc[] {in.readObject()};
204             break;
205
206          case OIL2Constants.SERVER_ACKNOWLEDGE :
207             AcknowledgementRequest ack = new AcknowledgementRequest();
208             ack.readExternal(in);
209             arguments = new Object JavaDoc[] {ack};
210             break;
211
212          case OIL2Constants.SERVER_ADD_MESSAGE :
213             arguments = new Object JavaDoc[] { SpyMessage.readMessage(in) };
214             break;
215
216          case OIL2Constants.SERVER_BROWSE :
217             arguments = new Object JavaDoc[] {in.readObject(), readString(in)};
218             break;
219
220          case OIL2Constants.SERVER_CHECK_ID :
221             arguments = new Object JavaDoc[] { readString(in) };
222             break;
223
224          case OIL2Constants.SERVER_CONNECTION_CLOSING :
225             arguments = null;
226             break;
227
228          case OIL2Constants.SERVER_CREATE_QUEUE :
229             arguments = new Object JavaDoc[] { readString(in) };
230             break;
231
232          case OIL2Constants.SERVER_CREATE_TOPIC :
233             arguments = new Object JavaDoc[] { readString(in) };
234             break;
235
236          case OIL2Constants.SERVER_GET_ID :
237             arguments = null;
238             break;
239
240          case OIL2Constants.SERVER_GET_TEMPORARY_QUEUE :
241             arguments = null;
242             break;
243
244          case OIL2Constants.SERVER_GET_TEMPORARY_TOPIC :
245             arguments = null;
246             break;
247             
248          case OIL2Constants.SERVER_DELETE_TEMPORARY_DESTINATION :
249             arguments = new Object JavaDoc[] { in.readObject()};
250             break;
251
252          case OIL2Constants.SERVER_RECEIVE :
253             arguments = new Object JavaDoc[] { new Integer JavaDoc(in.readInt()), new Long JavaDoc(in.readLong())};
254             break;
255
256          case OIL2Constants.SERVER_SET_ENABLED :
257             arguments = new Object JavaDoc[] { new Boolean JavaDoc(in.readBoolean()) };
258             break;
259
260          case OIL2Constants.SERVER_SUBSCRIBE :
261             arguments = new Object JavaDoc[] {in.readObject()};
262             break;
263
264          case OIL2Constants.SERVER_TRANSACT :
265             TransactionRequest trans = new TransactionRequest();
266             trans.readExternal(in);
267             arguments = new Object JavaDoc[] {trans};
268             break;
269
270          case OIL2Constants.SERVER_UNSUBSCRIBE :
271             arguments = new Object JavaDoc[] {new Integer JavaDoc(in.readInt())};
272             break;
273
274          case OIL2Constants.SERVER_DESTROY_SUBSCRIPTION :
275             arguments = new Object JavaDoc[] {in.readObject()};
276             break;
277
278          case OIL2Constants.SERVER_CHECK_USER :
279             arguments = new Object JavaDoc[] {readString(in), readString(in)};
280             break;
281
282          case OIL2Constants.SERVER_PING :
283             arguments = new Object JavaDoc[] {new Long JavaDoc(in.readLong())};
284             break;
285
286          case OIL2Constants.SERVER_AUTHENTICATE :
287             arguments = new Object JavaDoc[] { readString(in), readString(in) };
288             break;
289
290          default :
291             throw new IOException JavaDoc("Protocol Error: Bad operation code.");
292       }
293    }
294    
295    private static void writeString(java.io.ObjectOutput JavaDoc out, String JavaDoc s) throws java.io.IOException JavaDoc {
296       if (s == null) {
297          out.writeByte(0);
298       } else {
299          out.writeByte(1);
300          out.writeUTF(s);
301       }
302    }
303
304    private static String JavaDoc readString(java.io.ObjectInput JavaDoc in) throws java.io.IOException JavaDoc {
305       byte b = in.readByte();
306       if (b == 0)
307          return null;
308       else
309          return in.readUTF();
310    }
311
312    public String JavaDoc toString() {
313       return "[operation:"+operation+","+"requestId:"+requestId+",arguments:"+arguments+"]";
314    }
315 }
316
Popular Tags