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.IOException;
025:
026: import javax.jms.JMSException;
027:
028: /**
029: *
030: *
031: * @author <a href="mailto:hiram.chirino@jboss.org">Hiram Chirino</a>
032: * @version $Revision: 1.2 $
033: */
034: public class OIL2Response {
035: Integer correlationRequestId;
036: byte operation;
037: Object result;
038: Throwable exception;
039:
040: public OIL2Response() {
041: }
042:
043: public OIL2Response(OIL2Request request) {
044: correlationRequestId = request.requestId;
045: operation = request.operation;
046: }
047:
048: public Object evalThrowsJMSException() throws JMSException,
049: IOException {
050: if (exception != null) {
051: if (exception instanceof JMSException) {
052: throw (JMSException) exception;
053: } else {
054: throw new IOException(
055: "Protocol violation: unexpected exception found in response: "
056: + exception);
057: }
058: }
059: return result;
060: }
061:
062: public Object evalThrowsException() throws Exception {
063: if (exception != null) {
064: if (exception instanceof Exception) {
065: throw (Exception) exception;
066: } else {
067: throw new IOException(
068: "Protocol violation: unexpected exception found in response: "
069: + exception);
070: }
071: }
072: return result;
073: }
074:
075: public Object evalThrowsThrowable() throws Throwable {
076: if (exception != null) {
077: throw exception;
078: }
079: return result;
080: }
081:
082: public void writeExternal(java.io.ObjectOutput out)
083: throws IOException {
084: out.writeByte(operation);
085:
086: if (correlationRequestId == null) {
087: out.writeByte(0);
088: } else {
089: out.writeByte(1);
090: out.writeInt(correlationRequestId.intValue());
091: }
092:
093: if (exception != null) {
094: out.writeByte(OIL2Constants.RESULT_EXCEPTION);
095: out.writeObject(exception);
096: return;
097: }
098: if (result == null) {
099: out.writeByte(OIL2Constants.RESULT_VOID);
100: return;
101: }
102: switch (operation) {
103: default:
104: out.writeByte(OIL2Constants.RESULT_OBJECT);
105: out.writeObject(result);
106: return;
107: }
108: }
109:
110: public void readExternal(java.io.ObjectInput in)
111: throws IOException, ClassNotFoundException {
112: operation = in.readByte();
113: if (in.readByte() == 1)
114: correlationRequestId = new Integer(in.readInt());
115:
116: byte responseType = in.readByte();
117: switch (responseType) {
118: case OIL2Constants.RESULT_VOID:
119: result = null;
120: exception = null;
121: break;
122: case OIL2Constants.RESULT_EXCEPTION:
123: result = null;
124: exception = (Throwable) in.readObject();
125: break;
126: case OIL2Constants.RESULT_OBJECT:
127: exception = null;
128: switch (operation) {
129: default:
130: result = in.readObject();
131: }
132: break;
133: default:
134: throw new IOException(
135: "Protocol Error: Bad response type code '"
136: + responseType + "' ");
137:
138: }
139:
140: }
141:
142: public String toString() {
143: return "[operation:" + operation + ","
144: + "correlationRequestId:" + correlationRequestId
145: + ",result:" + result + ",exception:" + exception + "]";
146: }
147:
148: }
|