001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq.il.oil2;
023:
024: import java.io.Externalizable;
025: import java.io.IOException;
026:
027: import org.jboss.mq.AcknowledgementRequest;
028: import org.jboss.mq.ReceiveRequest;
029: import org.jboss.mq.SpyMessage;
030: import org.jboss.mq.TransactionRequest;
031:
032: /**
033: *
034: *
035: * @author <a href="mailto:hiram.chirino@jboss.org">Hiram Chirino</a>
036: * @version $Revision: 1.2 $
037: */
038: public class OIL2Request implements Externalizable {
039: static final long serialVersionUID = -1040314575679482420L;
040: private static int lastRequestId = 0;
041: private static Object lastRequestIdLock = new Object();
042: public Integer requestId;
043: public byte operation;
044: public Object arguments[];
045:
046: public OIL2Request() {
047: }
048:
049: public OIL2Request(byte operation, Object[] arguments) {
050: synchronized (lastRequestIdLock) {
051: this .requestId = new Integer(lastRequestId++);
052: }
053: this .operation = operation;
054: this .arguments = arguments;
055: }
056:
057: public void writeExternal(java.io.ObjectOutput out)
058: throws java.io.IOException {
059: out.writeByte(operation);
060: out.writeInt(requestId.intValue());
061: switch (operation) {
062:
063: //////////////////////////////////////////////////////////////////
064: // These are the requests that the Server makes to the client
065: //////////////////////////////////////////////////////////////////
066: case OIL2Constants.CLIENT_RECEIVE:
067: ReceiveRequest[] messages = (ReceiveRequest[]) arguments[0];
068: out.writeInt(messages.length);
069: for (int i = 0; i < messages.length; ++i)
070: messages[i].writeExternal(out);
071: break;
072:
073: case OIL2Constants.CLIENT_DELETE_TEMPORARY_DESTINATION:
074: out.writeObject(arguments[0]);
075: break;
076:
077: case OIL2Constants.CLIENT_CLOSE:
078: break;
079:
080: case OIL2Constants.CLIENT_PONG:
081: out.writeLong(((Long) arguments[0]).longValue());
082: break;
083:
084: //////////////////////////////////////////////////////////////////
085: // These are the requests that the client makes to the server
086: //////////////////////////////////////////////////////////////////
087: case OIL2Constants.SERVER_SET_SPY_DISTRIBUTED_CONNECTION:
088: out.writeObject(arguments[0]);
089: break;
090:
091: case OIL2Constants.SERVER_ACKNOWLEDGE:
092: ((AcknowledgementRequest) arguments[0]).writeExternal(out);
093: break;
094:
095: case OIL2Constants.SERVER_ADD_MESSAGE:
096: SpyMessage.writeMessage(((SpyMessage) arguments[0]), out);
097: break;
098:
099: case OIL2Constants.SERVER_BROWSE:
100: out.writeObject(arguments[0]);
101: writeString(out, (String) arguments[1]);
102: break;
103:
104: case OIL2Constants.SERVER_CHECK_ID:
105: writeString(out, (String) arguments[0]);
106: break;
107:
108: case OIL2Constants.SERVER_CONNECTION_CLOSING:
109: arguments = null;
110: break;
111:
112: case OIL2Constants.SERVER_CREATE_QUEUE:
113: writeString(out, (String) arguments[0]);
114: break;
115:
116: case OIL2Constants.SERVER_CREATE_TOPIC:
117: writeString(out, (String) arguments[0]);
118: break;
119:
120: case OIL2Constants.SERVER_GET_ID:
121: break;
122:
123: case OIL2Constants.SERVER_GET_TEMPORARY_QUEUE:
124: break;
125:
126: case OIL2Constants.SERVER_GET_TEMPORARY_TOPIC:
127: break;
128:
129: case OIL2Constants.SERVER_DELETE_TEMPORARY_DESTINATION:
130: out.writeObject(arguments[0]);
131: break;
132:
133: case OIL2Constants.SERVER_RECEIVE:
134: out.writeInt(((Integer) arguments[0]).intValue());
135: out.writeLong(((Long) arguments[1]).longValue());
136: break;
137:
138: case OIL2Constants.SERVER_SET_ENABLED:
139: out.writeBoolean(((Boolean) arguments[0]).booleanValue());
140: break;
141:
142: case OIL2Constants.SERVER_SUBSCRIBE:
143: out.writeObject(arguments[0]);
144: break;
145:
146: case OIL2Constants.SERVER_TRANSACT:
147: ((TransactionRequest) arguments[0]).writeExternal(out);
148: break;
149:
150: case OIL2Constants.SERVER_UNSUBSCRIBE:
151: out.writeInt(((Integer) arguments[0]).intValue());
152: break;
153:
154: case OIL2Constants.SERVER_DESTROY_SUBSCRIPTION:
155: out.writeObject(arguments[0]);
156: break;
157:
158: case OIL2Constants.SERVER_CHECK_USER:
159: writeString(out, (String) arguments[0]);
160: writeString(out, (String) arguments[1]);
161: break;
162:
163: case OIL2Constants.SERVER_PING:
164: out.writeLong(((Long) arguments[0]).longValue());
165: break;
166:
167: case OIL2Constants.SERVER_AUTHENTICATE:
168: writeString(out, (String) arguments[0]);
169: writeString(out, (String) arguments[1]);
170: break;
171:
172: default:
173: throw new IOException("Protocol Error: Bad operation code.");
174: }
175: }
176:
177: public void readExternal(java.io.ObjectInput in)
178: throws java.io.IOException, ClassNotFoundException {
179: operation = in.readByte();
180: requestId = new Integer(in.readInt());
181: switch (operation) {
182: //////////////////////////////////////////////////////////////////
183: // These are the requests that the Server makes to the client
184: //////////////////////////////////////////////////////////////////
185: case OIL2Constants.CLIENT_RECEIVE:
186: int numReceives = in.readInt();
187: org.jboss.mq.ReceiveRequest[] messages = new org.jboss.mq.ReceiveRequest[numReceives];
188: for (int i = 0; i < numReceives; ++i) {
189: messages[i] = new ReceiveRequest();
190: messages[i].readExternal(in);
191: }
192: arguments = new Object[] { messages };
193: break;
194:
195: case OIL2Constants.CLIENT_DELETE_TEMPORARY_DESTINATION:
196: arguments = new Object[] { in.readObject() };
197: break;
198:
199: case OIL2Constants.CLIENT_CLOSE:
200: arguments = null;
201: break;
202:
203: case OIL2Constants.CLIENT_PONG:
204: arguments = new Object[] { new Long(in.readLong()) };
205: break;
206:
207: //////////////////////////////////////////////////////////////////
208: // These are the requests that the client makes to the server
209: //////////////////////////////////////////////////////////////////
210: case OIL2Constants.SERVER_SET_SPY_DISTRIBUTED_CONNECTION:
211: // assert connectionToken == null
212: arguments = new Object[] { in.readObject() };
213: break;
214:
215: case OIL2Constants.SERVER_ACKNOWLEDGE:
216: AcknowledgementRequest ack = new AcknowledgementRequest();
217: ack.readExternal(in);
218: arguments = new Object[] { ack };
219: break;
220:
221: case OIL2Constants.SERVER_ADD_MESSAGE:
222: arguments = new Object[] { SpyMessage.readMessage(in) };
223: break;
224:
225: case OIL2Constants.SERVER_BROWSE:
226: arguments = new Object[] { in.readObject(), readString(in) };
227: break;
228:
229: case OIL2Constants.SERVER_CHECK_ID:
230: arguments = new Object[] { readString(in) };
231: break;
232:
233: case OIL2Constants.SERVER_CONNECTION_CLOSING:
234: arguments = null;
235: break;
236:
237: case OIL2Constants.SERVER_CREATE_QUEUE:
238: arguments = new Object[] { readString(in) };
239: break;
240:
241: case OIL2Constants.SERVER_CREATE_TOPIC:
242: arguments = new Object[] { readString(in) };
243: break;
244:
245: case OIL2Constants.SERVER_GET_ID:
246: arguments = null;
247: break;
248:
249: case OIL2Constants.SERVER_GET_TEMPORARY_QUEUE:
250: arguments = null;
251: break;
252:
253: case OIL2Constants.SERVER_GET_TEMPORARY_TOPIC:
254: arguments = null;
255: break;
256:
257: case OIL2Constants.SERVER_DELETE_TEMPORARY_DESTINATION:
258: arguments = new Object[] { in.readObject() };
259: break;
260:
261: case OIL2Constants.SERVER_RECEIVE:
262: arguments = new Object[] { new Integer(in.readInt()),
263: new Long(in.readLong()) };
264: break;
265:
266: case OIL2Constants.SERVER_SET_ENABLED:
267: arguments = new Object[] { new Boolean(in.readBoolean()) };
268: break;
269:
270: case OIL2Constants.SERVER_SUBSCRIBE:
271: arguments = new Object[] { in.readObject() };
272: break;
273:
274: case OIL2Constants.SERVER_TRANSACT:
275: TransactionRequest trans = new TransactionRequest();
276: trans.readExternal(in);
277: arguments = new Object[] { trans };
278: break;
279:
280: case OIL2Constants.SERVER_UNSUBSCRIBE:
281: arguments = new Object[] { new Integer(in.readInt()) };
282: break;
283:
284: case OIL2Constants.SERVER_DESTROY_SUBSCRIPTION:
285: arguments = new Object[] { in.readObject() };
286: break;
287:
288: case OIL2Constants.SERVER_CHECK_USER:
289: arguments = new Object[] { readString(in), readString(in) };
290: break;
291:
292: case OIL2Constants.SERVER_PING:
293: arguments = new Object[] { new Long(in.readLong()) };
294: break;
295:
296: case OIL2Constants.SERVER_AUTHENTICATE:
297: arguments = new Object[] { readString(in), readString(in) };
298: break;
299:
300: default:
301: throw new IOException("Protocol Error: Bad operation code.");
302: }
303: }
304:
305: private static void writeString(java.io.ObjectOutput out, String s)
306: throws java.io.IOException {
307: if (s == null) {
308: out.writeByte(0);
309: } else {
310: out.writeByte(1);
311: out.writeUTF(s);
312: }
313: }
314:
315: private static String readString(java.io.ObjectInput in)
316: throws java.io.IOException {
317: byte b = in.readByte();
318: if (b == 0)
319: return null;
320: else
321: return in.readUTF();
322: }
323:
324: public String toString() {
325: return "[operation:" + operation + "," + "requestId:"
326: + requestId + ",arguments:" + arguments + "]";
327: }
328: }
|