001: package org.jacorb.orb.giop;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import org.jacorb.orb.SystemExceptionHelper;
024: import org.omg.CORBA.INTERNAL;
025: import org.omg.CORBA.MARSHAL;
026: import org.omg.CORBA.portable.ApplicationException;
027: import org.omg.GIOP.MsgType_1_1;
028: import org.omg.GIOP.ReplyHeader_1_0;
029: import org.omg.GIOP.ReplyHeader_1_0Helper;
030: import org.omg.GIOP.ReplyHeader_1_2;
031: import org.omg.GIOP.ReplyHeader_1_2Helper;
032: import org.omg.GIOP.ReplyStatusType_1_2;
033: import org.omg.IOP.ServiceContext;
034: import org.omg.PortableServer.ForwardRequest;
035:
036: /**
037: * @author Gerald Brose, FU Berlin 1999
038: * @version $Id: ReplyInputStream.java,v 1.24 2006/06/28 12:41:43 alphonse.bendt Exp $
039: *
040: */
041:
042: public class ReplyInputStream extends
043: ServiceContextTransportingInputStream {
044: public final ReplyHeader_1_2 rep_hdr;
045: private final int body_start;
046:
047: public ReplyInputStream(org.omg.CORBA.ORB orb, byte[] buffer) {
048: super (orb, buffer);
049:
050: //check message type
051: if (Messages.getMsgType(buffer) != MsgType_1_1._Reply) {
052: throw new MARSHAL("Not a reply!");
053: }
054:
055: switch (giop_minor) {
056: case 0: {
057: //GIOP 1.0 = GIOP 1.1, fall through
058: }
059: case 1: {
060: //GIOP 1.1
061: ReplyHeader_1_0 hdr = ReplyHeader_1_0Helper.read(this );
062:
063: body_start = pos;
064:
065: rep_hdr = new ReplyHeader_1_2(hdr.request_id,
066: ReplyStatusType_1_2.from_int(hdr.reply_status
067: .value()), hdr.service_context);
068: break;
069: }
070: case 2: {
071: //GIOP 1.2
072: rep_hdr = ReplyHeader_1_2Helper.read(this );
073:
074: skipHeaderPadding();
075:
076: body_start = pos;
077:
078: break;
079: }
080: default: {
081: throw new MARSHAL("Unknown GIOP minor version: "
082: + giop_minor);
083: }
084: }
085: }
086:
087: /**
088: * Returns the reply status of this reply.
089: */
090: public ReplyStatusType_1_2 getStatus() {
091: return rep_hdr.reply_status;
092: }
093:
094: /**
095: * Returns any exception that is indicated by this reply. If
096: * the reply status is USER_EXCEPTION, SYSTEM_EXCEPTION, LOCATION_FORWARD,
097: * or LOCATION_FORWARD_PERM, an appropriate exception object is returned.
098: * For any other status, returns null.
099: */
100: public synchronized Exception getException() {
101: switch (rep_hdr.reply_status.value()) {
102: case ReplyStatusType_1_2._USER_EXCEPTION: {
103: mark(0);
104: String id = read_string();
105:
106: try {
107: reset();
108: } catch (java.io.IOException e) {
109: throw new INTERNAL("should never happen: "
110: + e.toString());
111: }
112: return new ApplicationException(id, this );
113: }
114: case ReplyStatusType_1_2._SYSTEM_EXCEPTION: {
115: return SystemExceptionHelper.read(this );
116: }
117: case ReplyStatusType_1_2._LOCATION_FORWARD:
118: case ReplyStatusType_1_2._LOCATION_FORWARD_PERM: {
119: return new ForwardRequest(read_Object());
120: }
121: default: {
122: return null;
123: }
124: }
125: }
126:
127: /**
128: * Returns the ServiceContext with the given id, if one is present.
129: * If there is no such ServiceContext, returns null.
130: */
131: public ServiceContext getServiceContext(int id) {
132: if (rep_hdr.service_context != null) {
133: for (int i = 0; i < rep_hdr.service_context.length; i++) {
134: if (rep_hdr.service_context[i].context_id == id) {
135: return rep_hdr.service_context[i];
136: }
137: }
138: }
139: return null;
140: }
141:
142: /**
143: * Returns a copy of the body of this reply. This does not include
144: * the GIOP header and the reply header.
145: */
146: public byte[] getBody() {
147: int body_length = msg_size
148: - (body_start - Messages.MSG_HEADER_SIZE);
149: byte[] body = new byte[body_length];
150: System.arraycopy(buffer, body_start, body, 0, body_length);
151: return body;
152: }
153:
154: protected void finalize() throws Throwable {
155: try {
156: close();
157: } finally {
158: super.finalize();
159: }
160: }
161: }
|