KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
24 import java.util.*;
25 import org.jacorb.orb.CDROutputStream;
26 import org.jacorb.util.Time;
27 import org.omg.CONV_FRAME.*;
28 import org.omg.CORBA.MARSHAL JavaDoc;
29 import org.omg.CORBA.PrincipalHelper;
30 import org.omg.GIOP.MsgType_1_1;
31 import org.omg.GIOP.TargetAddress;
32 import org.omg.GIOP.TargetAddressHelper;
33 import org.omg.IOP.INVOCATION_POLICIES;
34 import org.omg.IOP.ServiceContext JavaDoc;
35 import org.omg.IOP.ServiceContextListHelper JavaDoc;
36 import org.omg.Messaging.PolicyValue;
37 import org.omg.Messaging.PolicyValueSeqHelper;
38 import org.omg.Messaging.REPLY_END_TIME_POLICY_TYPE;
39 import org.omg.Messaging.REQUEST_END_TIME_POLICY_TYPE;
40 import org.omg.Messaging.REQUEST_START_TIME_POLICY_TYPE;
41 import org.omg.Messaging.SYNC_NONE;
42 import org.omg.Messaging.SYNC_WITH_SERVER;
43 import org.omg.Messaging.SYNC_WITH_TARGET;
44 import org.omg.Messaging.SYNC_WITH_TRANSPORT JavaDoc;
45 import org.omg.TimeBase.UtcT;
46
47 /**
48  * @author Gerald Brose, FU Berlin 1999
49  * @version $Id: RequestOutputStream.java,v 1.29 2005/04/22 13:25:48 andre.spiegel Exp $
50  *
51  */

52 public class RequestOutputStream
53     extends ServiceContextTransportingOutputStream
54 {
55     private static byte[] principal = new byte[ 0 ];
56     private static byte[] reserved = new byte[ 3 ];
57
58     private int request_id = -1;
59     private boolean response_expected = true;
60     private short syncScope = SYNC_WITH_SERVER.value;
61     private String JavaDoc operation = null;
62
63     /**
64      * Absolute time after which this request may be delivered to its target.
65      * (CORBA 3.0, 22.2.4.1)
66      */

67     private UtcT requestStartTime = null;
68
69     /**
70      * Absolute time after which this request may no longer be delivered
71      * to its target. (CORBA 3.0, 22.2.4.2/5)
72      */

73     private UtcT requestEndTime = null;
74
75     /**
76      * Absolute time after which a reply may no longer be obtained
77      * or returned to the client. (CORBA 3.0, 22.2.4.4/6)
78      */

79     private UtcT replyEndTime = null;
80
81     private org.jacorb.orb.dii.Request request = null;
82
83     private ClientConnection connection = null;
84
85     public RequestOutputStream( ClientConnection connection,
86                                 int request_id,
87                                 String JavaDoc operation,
88                                 boolean response_expected,
89                                 short syncScope,
90                                 UtcT requestStartTime,
91                                 UtcT requestEndTime,
92                                 UtcT replyEndTime,
93                                 byte[] object_key,
94                                 int giop_minor )
95     {
96         super();
97
98         setGIOPMinor( giop_minor );
99
100         this.request_id = request_id;
101         this.response_expected = response_expected;
102         this.syncScope = syncScope;
103         this.operation = operation;
104         this.connection = connection;
105
106         this.requestStartTime = requestStartTime;
107         this.requestEndTime = requestEndTime;
108         this.replyEndTime = replyEndTime;
109
110         if (requestStartTime != null ||
111             requestEndTime != null ||
112             replyEndTime != null)
113         {
114             addServiceContext (createInvocationPolicies());
115         }
116
117         writeGIOPMsgHeader( MsgType_1_1._Request,
118                             giop_minor );
119
120         switch( giop_minor )
121         {
122             case 0 :
123             {
124                 // GIOP 1.0 inlining
125
ServiceContextListHelper.write( this , Messages.service_context );
126                 write_ulong( request_id);
127                 write_boolean( response_expected );
128                 write_long( object_key.length );
129                 write_octet_array( object_key, 0, object_key.length);
130                 write_string( operation);
131                 PrincipalHelper.write( this, principal);
132
133                 break;
134             }
135             case 1 :
136             {
137                 //GIOP 1.1
138
ServiceContextListHelper.write( this , Messages.service_context );
139                 write_ulong( request_id);
140                 write_boolean( response_expected );
141                 write_long( object_key.length );
142                 write_octet_array( object_key, 0, object_key.length);
143                 write_string( operation);
144                 PrincipalHelper.write( this, principal);
145
146                 break;
147             }
148             case 2 :
149             {
150                 //GIOP 1.2
151
TargetAddress addr = new TargetAddress();
152                 addr.object_key( object_key );
153
154                 // inlined RequestHeader_1_2Helper.write method
155

156                 write_ulong( request_id);
157                 if (response_expected)
158                 {
159                     write_octet ((byte)0x03);
160                 }
161                 else
162                 {
163                     switch (syncScope)
164                     {
165                         case SYNC_NONE.value:
166                         case SYNC_WITH_TRANSPORT.value:
167                         write_octet ((byte)0x00);
168                         break;
169                         case SYNC_WITH_SERVER.value:
170                         write_octet ((byte)0x01);
171                         break;
172                         case SYNC_WITH_TARGET.value:
173                         write_octet ((byte)0x03);
174                         break;
175                         default:
176                         throw new MARSHAL JavaDoc ("Invalid SYNC_SCOPE: " + syncScope);
177                     }
178                 }
179
180                 write_octet_array( reserved,0,3 );
181                 TargetAddressHelper.write( this, addr );
182                 write_string( operation );
183                 ServiceContextListHelper.write( this, Messages.service_context );
184
185                 markHeaderEnd(); //use padding if GIOP minor == 2
186

187                 break;
188             }
189             default :
190             {
191                 throw new MARSHAL JavaDoc( "Unknown GIOP minor: " + giop_minor );
192             }
193         }
194     }
195
196     public int requestId()
197     {
198         return request_id;
199     }
200
201     public boolean response_expected()
202     {
203         return response_expected;
204     }
205
206     public short syncScope()
207     {
208         return syncScope;
209     }
210
211     public String JavaDoc operation()
212     {
213         return operation;
214     }
215
216     public UtcT getReplyEndTime()
217     {
218         return replyEndTime;
219     }
220
221     public void setRequest(org.jacorb.orb.dii.Request request)
222     {
223         this.request = request;
224     }
225
226     public org.jacorb.orb.dii.Request getRequest()
227     {
228         return request;
229     }
230
231     public ClientConnection getConnection()
232     {
233         return connection;
234     }
235
236     /**
237      * Overridden to add a codeset service context if this
238      * is the first request on the connection.
239      */

240     public void write_to(GIOPConnection conn) throws IOException
241     {
242         if (!conn.isTCSNegotiated())
243         {
244             // encapsulate context
245
CDROutputStream os = new CDROutputStream();
246             os.beginEncapsulatedArray();
247             CodeSetContextHelper.write
248             (
249                 os,
250                 new CodeSetContext(conn.getTCS(), conn.getTCSW())
251             );
252             addServiceContext(new ServiceContext JavaDoc
253             (
254                 org.omg.IOP.CodeSets.value,
255                 os.getBufferCopy()
256             ));
257             conn.markTCSNegotiated();
258         }
259         super.write_to(conn);
260     }
261     
262     /**
263      * Returns the timing policies for this request as an array
264      * of PolicyValues that can be propagated in a ServiceContext.
265      */

266     private PolicyValue[] getTimingPolicyValues()
267     {
268         List l = new ArrayList();
269         if (requestStartTime != null)
270             l.add (new PolicyValue (REQUEST_START_TIME_POLICY_TYPE.value,
271                                     Time.toCDR (requestStartTime)));
272         if (requestEndTime != null)
273             l.add (new PolicyValue (REQUEST_END_TIME_POLICY_TYPE.value,
274                                     Time.toCDR (requestEndTime)));
275         if (replyEndTime != null)
276             l.add (new PolicyValue (REPLY_END_TIME_POLICY_TYPE.value,
277                                     Time.toCDR (replyEndTime)));
278         return (PolicyValue[])l.toArray (new PolicyValue[0]);
279     }
280
281     private ServiceContext JavaDoc createInvocationPolicies()
282     {
283         CDROutputStream out = new CDROutputStream();
284         out.beginEncapsulatedArray();
285         PolicyValueSeqHelper.write(out, getTimingPolicyValues());
286         return new ServiceContext JavaDoc (INVOCATION_POLICIES.value,
287                                    out.getBufferCopy());
288     }
289 }
290
Popular Tags