KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > encoding > CDROutputObject


1 /*
2  * @(#)CDROutputObject.java 1.54 04/06/21
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.encoding;
9
10 import java.io.IOException JavaDoc;
11 import java.nio.ByteBuffer JavaDoc;
12
13 import org.omg.CORBA.Any JavaDoc;
14 import org.omg.CORBA.Principal JavaDoc;
15 import org.omg.CORBA.TypeCode JavaDoc;
16 import org.omg.CORBA.portable.InputStream JavaDoc;
17
18 import com.sun.corba.se.pept.encoding.OutputObject;
19 import com.sun.corba.se.pept.protocol.MessageMediator;
20
21 import com.sun.corba.se.spi.encoding.CorbaOutputObject ;
22 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
23 import com.sun.corba.se.spi.orb.ORB;
24 import com.sun.corba.se.spi.protocol.CorbaMessageMediator;
25 import com.sun.corba.se.pept.transport.ByteBufferPool;
26 import com.sun.corba.se.spi.transport.CorbaConnection;
27 import com.sun.corba.se.spi.logging.CORBALogDomains;
28
29 import com.sun.corba.se.spi.servicecontext.ServiceContexts;
30 import com.sun.corba.se.impl.encoding.BufferManagerFactory;
31 import com.sun.corba.se.impl.encoding.ByteBufferWithInfo;
32 import com.sun.corba.se.impl.encoding.CDROutputStream;
33 import com.sun.corba.se.impl.encoding.CDROutputStream_1_0;
34 import com.sun.corba.se.impl.encoding.CodeSetConversion;
35 import com.sun.corba.se.impl.encoding.CodeSetComponentInfo;
36 import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry;
37 import com.sun.corba.se.impl.orbutil.ORBUtility;
38 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
39 import com.sun.corba.se.impl.protocol.giopmsgheaders.MessageBase;
40 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
41 import com.sun.corba.se.impl.logging.OMGSystemException;
42
43 /**
44  * @author Harold Carr
45  */

46 public class CDROutputObject extends CorbaOutputObject
47 {
48     private Message header;
49     private ORB orb;
50     private ORBUtilSystemException wrapper;
51     private OMGSystemException omgWrapper;
52
53     // REVISIT - only used on sendCancelRequest.
54
private CorbaConnection connection;
55
56     private CDROutputObject(
57         ORB orb, GIOPVersion giopVersion, Message header,
58     BufferManagerWrite manager, byte streamFormatVersion,
59     CorbaMessageMediator mediator)
60     {
61     super(orb, giopVersion, header.getEncodingVersion(),
62           false, manager, streamFormatVersion,
63           ((mediator != null && mediator.getConnection() != null) ?
64            ((CorbaConnection)mediator.getConnection()).
65                  shouldUseDirectByteBuffers() : false));
66
67     this.header = header;
68         this.orb = orb;
69     this.wrapper = ORBUtilSystemException.get( orb, CORBALogDomains.RPC_ENCODING ) ;
70     this.omgWrapper = OMGSystemException.get( orb, CORBALogDomains.RPC_ENCODING ) ;
71
72         getBufferManager().setOutputObject(this);
73     this.corbaMessageMediator = mediator;
74     }
75
76     public CDROutputObject(ORB orb,
77                MessageMediator messageMediator,
78                Message header,
79                byte streamFormatVersion)
80     {
81         this(
82         orb,
83         ((CorbaMessageMediator)messageMediator).getGIOPVersion(),
84         header,
85         BufferManagerFactory.newBufferManagerWrite(
86         ((CorbaMessageMediator)messageMediator).getGIOPVersion(),
87         header.getEncodingVersion(),
88         orb),
89         streamFormatVersion,
90         (CorbaMessageMediator)messageMediator);
91     }
92
93     // NOTE:
94
// Used in SharedCDR (i.e., must be grow).
95
// Used in msgtypes test.
96
public CDROutputObject(ORB orb,
97                MessageMediator messageMediator,
98                Message header,
99                byte streamFormatVersion,
100                int strategy)
101     {
102         this(
103         orb,
104         ((CorbaMessageMediator)messageMediator).getGIOPVersion(),
105         header,
106             BufferManagerFactory.
107             newBufferManagerWrite(strategy,
108                       header.getEncodingVersion(),
109                       orb),
110         streamFormatVersion,
111         (CorbaMessageMediator)messageMediator);
112     }
113
114     // REVISIT
115
// Used on sendCancelRequest.
116
// Used for needs addressing mode.
117
public CDROutputObject(ORB orb, CorbaMessageMediator mediator,
118                GIOPVersion giopVersion,
119                CorbaConnection connection, Message header,
120                byte streamFormatVersion)
121     {
122         this(
123         orb,
124         giopVersion,
125         header,
126             BufferManagerFactory.
127         newBufferManagerWrite(giopVersion,
128                   header.getEncodingVersion(),
129                   orb),
130         streamFormatVersion,
131         mediator);
132     this.connection = connection ;
133     }
134
135     // XREVISIT
136
// Header should only be in message mediator.
137
// Another possibility: merge header and message mediator.
138
// REVISIT - make protected once all encoding together
139
public Message getMessageHeader() {
140         return header;
141     }
142
143     public final void finishSendingMessage() {
144         getBufferManager().sendMessage();
145     }
146
147     /**
148      * Write the contents of the CDROutputStream to the specified
149      * output stream. Has the side-effect of pushing any current
150      * Message onto the Message list.
151      * @param s The output stream to write to.
152      */

153     public void writeTo(CorbaConnection connection)
154     throws java.io.IOException JavaDoc
155     {
156
157         //
158
// Update the GIOP MessageHeader size field.
159
//
160

161         ByteBufferWithInfo bbwi = getByteBufferWithInfo();
162
163         getMessageHeader().setSize(bbwi.byteBuffer, bbwi.getSize());
164
165         if (orb() != null) {
166         if (((ORB)orb()).transportDebugFlag) {
167         dprint(".writeTo: " + connection);
168         }
169         if (((ORB)orb()).giopDebugFlag) {
170         CDROutputStream_1_0.printBuffer(bbwi);
171         }
172         }
173     bbwi.byteBuffer.position(0).limit(bbwi.getSize());
174     connection.write(bbwi.byteBuffer);
175     }
176
177     /** overrides create_input_stream from CDROutputStream */
178     public org.omg.CORBA.portable.InputStream JavaDoc create_input_stream()
179     {
180         // XREVISIT
181
return null;
182         //return new XIIOPInputStream(orb(), getByteBuffer(), getIndex(),
183
//isLittleEndian(), getMessageHeader(), conn);
184
}
185
186     public CorbaConnection getConnection()
187     {
188     // REVISIT - only set when doing sendCancelRequest.
189
if (connection != null) {
190         return connection;
191     }
192     return (CorbaConnection) corbaMessageMediator.getConnection();
193     }
194
195     // XREVISIT - If CDROutputObject doesn't live in the iiop
196
// package, it will need this, here, to give package access
197
// to xgiop.
198
// REVISIT - make protected once all encoding together
199
public final ByteBufferWithInfo getByteBufferWithInfo() {
200         return super.getByteBufferWithInfo();
201     }
202
203     // REVISIT - make protected once all encoding together
204
public final void setByteBufferWithInfo(ByteBufferWithInfo bbwi) {
205         super.setByteBufferWithInfo(bbwi);
206     }
207
208     /**
209      * Override the default CDR factory behavior to get the
210      * negotiated code sets from the connection.
211      *
212      * These are only called once per message, the first time needed.
213      *
214      * In the local case, there is no Connection, so use the
215      * local code sets.
216      */

217     protected CodeSetConversion.CTBConverter createCharCTBConverter() {
218         CodeSetComponentInfo.CodeSetContext codesets = getCodeSets();
219
220         // If the connection doesn't have its negotiated
221
// code sets by now, fall back on the defaults defined
222
// in CDRInputStream.
223
if (codesets == null)
224             return super.createCharCTBConverter();
225         
226         OSFCodeSetRegistry.Entry charSet
227             = OSFCodeSetRegistry.lookupEntry(codesets.getCharCodeSet());
228
229         if (charSet == null)
230         throw wrapper.unknownCodeset( charSet ) ;
231
232         return CodeSetConversion.impl().getCTBConverter(charSet,
233                                                         isLittleEndian(),
234                                                         false);
235     }
236
237     protected CodeSetConversion.CTBConverter createWCharCTBConverter() {
238
239         CodeSetComponentInfo.CodeSetContext codesets = getCodeSets();
240
241         // If the connection doesn't have its negotiated
242
// code sets by now, we have to throw an exception.
243
// See CORBA formal 00-11-03 13.9.2.6.
244
if (codesets == null) {
245             if (getConnection().isServer())
246         throw omgWrapper.noClientWcharCodesetCtx() ;
247             else
248         throw omgWrapper.noServerWcharCodesetCmp() ;
249         }
250
251         OSFCodeSetRegistry.Entry wcharSet
252             = OSFCodeSetRegistry.lookupEntry(codesets.getWCharCodeSet());
253
254         if (wcharSet == null)
255         throw wrapper.unknownCodeset( wcharSet ) ;
256
257         boolean useByteOrderMarkers
258             = ((ORB)orb()).getORBData().useByteOrderMarkers();
259
260         // With UTF-16:
261
//
262
// For GIOP 1.2, we can put byte order markers if we want to, and
263
// use the default of big endian otherwise. (See issue 3405b)
264
//
265
// For GIOP 1.1, we don't use BOMs and use the endianness of
266
// the stream.
267
if (wcharSet == OSFCodeSetRegistry.UTF_16) {
268             if (getGIOPVersion().equals(GIOPVersion.V1_2)) {
269                 return CodeSetConversion.impl().getCTBConverter(wcharSet,
270                                                                 false,
271                                                                 useByteOrderMarkers);
272             }
273
274             if (getGIOPVersion().equals(GIOPVersion.V1_1)) {
275                 return CodeSetConversion.impl().getCTBConverter(wcharSet,
276                                                                 isLittleEndian(),
277                                                                 false);
278             }
279         }
280
281         // In the normal case, let the converter system handle it
282
return CodeSetConversion.impl().getCTBConverter(wcharSet,
283                                                         isLittleEndian(),
284                                                         useByteOrderMarkers);
285     }
286
287     // If we're local and don't have a Connection, use the
288
// local code sets, otherwise get them from the connection.
289
// If the connection doesn't have negotiated code sets
290
// yet, then we use ISO8859-1 for char/string and wchar/wstring
291
// are illegal.
292
private CodeSetComponentInfo.CodeSetContext getCodeSets() {
293         if (getConnection() == null)
294             return CodeSetComponentInfo.LOCAL_CODE_SETS;
295         else
296             return getConnection().getCodeSetContext();
297     }
298
299     protected void dprint(String JavaDoc msg)
300     {
301     ORBUtility.dprint("CDROutputObject", msg);
302     }
303 }
304
305 // End of file.
306
Popular Tags