KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
24 import org.apache.avalon.framework.logger.Logger;
25
26 import org.omg.CORBA.MARSHAL JavaDoc;
27 import org.omg.GIOP.*;
28 import org.jacorb.orb.*;
29
30 /**
31  * @author Gerald Brose, FU Berlin 1999
32  * @version $Id: ReplyOutputStream.java,v 1.20 2004/05/06 12:40:00 nicolas Exp $
33  *
34  */

35 public class ReplyOutputStream
36     extends ServiceContextTransportingOutputStream
37 {
38     private boolean is_locate_reply = false;
39     private Logger logger;
40
41     public ReplyOutputStream ( int request_id,
42                                ReplyStatusType_1_2 reply_status,
43                                int giop_minor,
44                                boolean is_locate_reply,
45                                Logger logger)
46     {
47         super();
48
49         this.is_locate_reply = is_locate_reply;
50
51         setGIOPMinor( giop_minor );
52
53         writeGIOPMsgHeader( MsgType_1_1._Reply,
54                             giop_minor );
55
56         switch( giop_minor )
57         {
58             case 0 :
59             {
60                 // GIOP 1.0 Reply == GIOP 1.1 Reply, fall through
61
}
62             case 1 :
63             {
64                 //Technically, GIOP.idl only allows either
65
//ReplyStatusType_1_0 or ReplyStatusType_1_2, but not
66
//both. We go around this by compiling GIOP.idl two
67
//times
68

69                 //GIOP 1.1
70
// ReplyHeader_1_0 repl_hdr =
71
// new ReplyHeader_1_0( alignment_ctx,
72
// request_id,
73
// ReplyStatusType_1_0.from_int( reply_status.value() ));
74
// ReplyHeader_1_0Helper.write( this, repl_hdr );
75

76                 // inlining for performance
77

78                 org.omg.IOP.ServiceContextListHelper.write(this , Messages.service_context );
79                 write_ulong( request_id );
80                 org.omg.GIOP.ReplyStatusType_1_0Helper.write( this,
81                                                               ReplyStatusType_1_0.from_int( reply_status.value() ));
82
83                 break;
84             }
85             case 2 :
86             {
87                 //GIOP 1.2
88
// ReplyHeader_1_2 repl_hdr =
89
// new ReplyHeader_1_2( request_id,
90
// reply_status,
91
// alignment_ctx );
92

93 // ReplyHeader_1_2Helper.write( this, repl_hdr );
94

95                 // more inlining
96

97                 write_ulong( request_id );
98                 org.omg.GIOP.ReplyStatusType_1_2Helper.write(this,
99                                                              reply_status);
100
101
102                 org.omg.IOP.ServiceContextListHelper.write( this, Messages.service_context );
103
104                 markHeaderEnd(); //use padding if minor 2
105

106                 break;
107             }
108             default :
109             {
110                 throw new MARSHAL JavaDoc( "Unknown GIOP minor: " + giop_minor );
111             }
112         }
113     }
114
115     public void write_to( GIOPConnection conn )
116         throws IOException JavaDoc
117     {
118         if( is_locate_reply )
119         {
120             ReplyInputStream r_in =
121             new ReplyInputStream( null, getBufferCopy() );
122
123             LocateReplyOutputStream lr_out = null;
124
125             if( r_in.getGIOPMinor() < 2 )
126             {
127                 //GIOP 1.0 or 1.1
128
switch( r_in.rep_hdr.reply_status.value() )
129                 {
130                     case ReplyStatusType_1_2._NO_EXCEPTION :
131                     {
132                         int status;
133
134                         //_non_existent?
135
if (r_in.read_boolean())
136                         {
137                             //_non_existent == true
138
status = LocateStatusType_1_2._UNKNOWN_OBJECT;
139                         }
140                         else
141                         {
142                             //_non_existent == false
143
status = LocateStatusType_1_2._OBJECT_HERE;
144                         }
145
146                         lr_out = new LocateReplyOutputStream( r_in.rep_hdr.request_id,
147                                                               status,
148                                                               r_in.getGIOPMinor() );
149
150                         break;
151                     }
152                     case ReplyStatusType_1_2._USER_EXCEPTION :
153                     {
154                         //fall through
155
}
156                     case ReplyStatusType_1_2._SYSTEM_EXCEPTION :
157                     {
158                         //uh oh, can't reply with exception
159
if (logger.isErrorEnabled())
160                             logger.error("Received an exception when processing a LocateRequest" );
161
162                         // GIOP prior to 1.2 doesn't have the status
163
// LOC_SYSTEM_EXCEPTION, so we have to return
164
// OBJECT_UNKNOWN (even if it may not be unknown)
165
lr_out =
166                             new LocateReplyOutputStream( r_in.rep_hdr.request_id,
167                                                          LocateStatusType_1_2._UNKNOWN_OBJECT,
168                                                          r_in.getGIOPMinor() );
169                         break;
170                     }
171                     case ReplyStatusType_1_2._LOCATION_FORWARD :
172                     {
173
174                         lr_out =
175                             new LocateReplyOutputStream( r_in.rep_hdr.request_id,
176                                                          LocateStatusType_1_2._OBJECT_FORWARD,
177                                                          r_in.getGIOPMinor() );
178
179
180                         //FIXME: it would be more efficient to copy
181
//the body part of this buffer to the new
182
//buffer
183
lr_out.write_IOR( org.omg.IOP.IORHelper.read( r_in ));
184
185                         break;
186                     }
187                 }
188             }
189             else
190             {
191                 //GIOP 1.2
192
switch( r_in.rep_hdr.reply_status.value() )
193                 {
194                     case ReplyStatusType_1_2._NO_EXCEPTION :
195                     {
196                         int status;
197
198                         //_non_existent?
199
if( r_in.read_boolean() )
200                         {
201                             //_non_existent == true
202
status = LocateStatusType_1_2._UNKNOWN_OBJECT;
203                         }
204                         else
205                         {
206                             //_non_existent == false
207
status = LocateStatusType_1_2._OBJECT_HERE;
208                         }
209
210                         lr_out = new LocateReplyOutputStream( r_in.rep_hdr.request_id,
211                                                               status,
212                                                               r_in.getGIOPMinor() );
213
214                         break;
215                     }
216                     case ReplyStatusType_1_2._USER_EXCEPTION :
217                     {
218                         //uh oh, can't reply with user exception
219
if (logger.isErrorEnabled())
220                             logger.error("Received an exception when processing a LocateRequest - mapping to UNKNOWN system exception" );
221
222                         lr_out =
223                         new LocateReplyOutputStream( r_in.rep_hdr.request_id,
224                                                      LocateStatusType_1_2._LOC_SYSTEM_EXCEPTION,
225                                                      r_in.getGIOPMinor() );
226
227                         SystemExceptionHelper.write( lr_out,
228                                                      new org.omg.CORBA.UNKNOWN JavaDoc() );
229
230                     }
231                     case ReplyStatusType_1_2._SYSTEM_EXCEPTION :
232                     {
233
234
235                         lr_out =
236                         new LocateReplyOutputStream( r_in.rep_hdr.request_id,
237                                                      LocateStatusType_1_2._LOC_SYSTEM_EXCEPTION,
238                                                      r_in.getGIOPMinor() );
239
240                         //FIXME: inefficient, use copying
241
SystemExceptionHelper.write( lr_out,
242                                                      SystemExceptionHelper.read( r_in ));
243
244                         break;
245                     }
246                     case ReplyStatusType_1_2._LOCATION_FORWARD :
247                     {
248
249                         lr_out =
250                         new LocateReplyOutputStream( r_in.rep_hdr.request_id,
251                                                      LocateStatusType_1_2._OBJECT_FORWARD,
252                                                      r_in.getGIOPMinor() );
253
254
255                         //FIXME: it would be more efficient to copy
256
//the body part of this buffer to the new
257
//buffer
258
lr_out.write_IOR( org.omg.IOP.IORHelper.read( r_in ));
259
260                         break;
261                     }
262                 }
263             }
264
265             lr_out.write_to( conn );
266         }
267         else
268         {
269             super.write_to( conn );
270         }
271     }
272 }
273
Popular Tags