KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > giop > ReplyInputStream


1 package org.jacorb.orb.giop;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.jacorb.orb.SystemExceptionHelper;
24 import org.jacorb.util.*;
25
26 import org.omg.GIOP.*;
27 import org.omg.IOP.*;
28 import org.omg.CORBA.MARSHAL JavaDoc;
29 import org.omg.CORBA.portable.ApplicationException JavaDoc;
30 import org.omg.PortableServer.ForwardRequest JavaDoc;
31
32 /**
33  * @author Gerald Brose, FU Berlin 1999
34  * @version $Id: ReplyInputStream.java,v 1.22 2004/11/18 23:27:20 andre.spiegel Exp $
35  *
36  */

37
38 public class ReplyInputStream
39     extends ServiceContextTransportingInputStream
40 {
41     public ReplyHeader_1_2 rep_hdr = null;
42     private int body_start;
43
44     public ReplyInputStream( org.omg.CORBA.ORB JavaDoc orb, byte[] buffer )
45     {
46         super( orb, buffer );
47
48         //check message type
49
if( Messages.getMsgType( buffer ) != MsgType_1_1._Reply )
50         {
51             throw new MARSHAL JavaDoc("Not a reply!");
52         }
53
54         switch( giop_minor )
55         {
56             case 0 :
57             {
58                 //GIOP 1.0 = GIOP 1.1, fall through
59
}
60             case 1 :
61             {
62                 //GIOP 1.1
63
ReplyHeader_1_0 hdr =
64                 ReplyHeader_1_0Helper.read( this );
65
66                 body_start = pos;
67
68                 rep_hdr =
69                 new ReplyHeader_1_2( hdr.request_id,
70                                      ReplyStatusType_1_2.from_int( hdr.reply_status.value() ),
71                                      hdr.service_context );
72                 break;
73             }
74             case 2 :
75             {
76                 //GIOP 1.2
77
rep_hdr = ReplyHeader_1_2Helper.read( this );
78
79                 skipHeaderPadding();
80
81                 body_start = pos;
82
83                 break;
84             }
85             default : {
86                 throw new MARSHAL JavaDoc("Unknown GIOP minor version: " + giop_minor);
87             }
88         }
89     }
90
91     /**
92      * Returns the reply status of this reply.
93      */

94     public ReplyStatusType_1_2 getStatus()
95     {
96         return rep_hdr.reply_status;
97     }
98
99     /**
100      * Returns any exception that is indicated by this reply. If
101      * the reply status is USER_EXCEPTION, SYSTEM_EXCEPTION, LOCATION_FORWARD,
102      * or LOCATION_FORWARD_PERM, an appropriate exception object is returned.
103      * For any other status, returns null.
104      */

105     public synchronized Exception JavaDoc getException()
106     {
107         switch( rep_hdr.reply_status.value() )
108         {
109             case ReplyStatusType_1_2._USER_EXCEPTION :
110             {
111                 mark( 0 );
112                 String JavaDoc id = read_string();
113
114                 try
115                 {
116                     reset();
117                 }
118                 catch( java.io.IOException JavaDoc ioe )
119                 {
120                     //should not happen anyway
121
// Debug.output( 1, ioe );
122
}
123                 return new ApplicationException JavaDoc( id, this );
124             }
125             case ReplyStatusType_1_2._SYSTEM_EXCEPTION:
126             {
127                 return SystemExceptionHelper.read( this );
128             }
129             case ReplyStatusType_1_2._LOCATION_FORWARD:
130             case ReplyStatusType_1_2._LOCATION_FORWARD_PERM:
131             {
132                 return new ForwardRequest JavaDoc( read_Object() );
133             }
134             default:
135             {
136                 return null;
137             }
138         }
139     }
140
141     /**
142      * Returns the ServiceContext with the given id, if one is present.
143      * If there is no such ServiceContext, returns null.
144      */

145     public ServiceContext getServiceContext(int id)
146     {
147         if (rep_hdr.service_context != null)
148         {
149             for (int i=0; i<rep_hdr.service_context.length; i++)
150             {
151                 if (rep_hdr.service_context[i].context_id == id)
152                     return rep_hdr.service_context[i];
153             }
154         }
155         return null;
156     }
157
158     /**
159      * Returns a copy of the body of this reply. This does not include
160      * the GIOP header and the reply header.
161      */

162     public byte[] getBody()
163     {
164         int body_length = msg_size - (body_start - Messages.MSG_HEADER_SIZE);
165         byte[] body = new byte[body_length];
166         System.arraycopy (buffer, body_start, body, 0, body_length);
167         return body;
168     }
169
170     protected void finalize() throws Throwable JavaDoc
171     {
172         try
173         {
174             close();
175         }
176         catch( java.io.IOException JavaDoc iox )
177         {
178             //ignore
179
}
180         finally
181         {
182             super.finalize();
183         }
184     }
185 }
186
Popular Tags