KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > protocol > giopmsgheaders > MessageBase


1 /*
2  * @(#)MessageBase.java 1.19 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.protocol.giopmsgheaders;
9
10 import java.io.IOException JavaDoc;
11 import java.lang.Class JavaDoc;
12 import java.lang.reflect.Constructor JavaDoc;
13 import java.nio.ByteBuffer JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.omg.CORBA.CompletionStatus JavaDoc;
17 import org.omg.CORBA.INTERNAL JavaDoc;
18 import org.omg.CORBA.MARSHAL JavaDoc;
19 import org.omg.CORBA.Principal JavaDoc;
20 import org.omg.CORBA.SystemException JavaDoc;
21 import org.omg.IOP.TaggedProfile JavaDoc;
22
23 import com.sun.corba.se.pept.transport.ByteBufferPool;
24
25 import com.sun.corba.se.spi.ior.ObjectKey;
26 import com.sun.corba.se.spi.ior.ObjectId;
27 import com.sun.corba.se.spi.ior.IOR;
28 import com.sun.corba.se.spi.ior.ObjectKeyFactory;
29 import com.sun.corba.se.spi.ior.iiop.IIOPProfile;
30 import com.sun.corba.se.spi.ior.iiop.IIOPFactories;
31 import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;
32 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
33 import com.sun.corba.se.spi.ior.iiop.RequestPartitioningComponent;
34 import com.sun.corba.se.spi.logging.CORBALogDomains ;
35 import com.sun.corba.se.spi.orb.ORB;
36 import com.sun.corba.se.spi.transport.CorbaConnection;
37 import com.sun.corba.se.spi.transport.ReadTimeouts;
38
39 import com.sun.corba.se.spi.servicecontext.ServiceContexts;
40 import com.sun.corba.se.impl.encoding.ByteBufferWithInfo;
41 import com.sun.corba.se.impl.encoding.CDRInputStream_1_0;
42 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
43 import com.sun.corba.se.impl.orbutil.ORBUtility;
44 import com.sun.corba.se.impl.orbutil.ORBConstants;
45 import com.sun.corba.se.impl.orbutil.ORBClassLoader;
46 import com.sun.corba.se.impl.protocol.AddressingDispositionException;
47
48 /**
49  * This class acts as the base class for the various GIOP message types. This
50  * also serves as a factory to create various message types. We currently
51  * support GIOP 1.0, 1.1 and 1.2 message types.
52  *
53  * @author Ram Jeyaraman 05/14/2000
54  * @version 1.0
55  */

56
57 public abstract class MessageBase implements Message{
58
59     // This is only used when the giopDebug flag is
60
// turned on.
61
public byte[] giopHeader;
62     private ByteBuffer JavaDoc byteBuffer;
63     private int threadPoolToUse;
64
65     // (encodingVersion == 0x00) implies CDR encoding,
66
// (encodingVersion > 0x00) implies Java serialization version.
67
byte encodingVersion = (byte) Message.CDR_ENC_VERSION;
68
69     private static ORBUtilSystemException wrapper =
70     ORBUtilSystemException.get( CORBALogDomains.RPC_PROTOCOL ) ;
71
72     // Static methods
73

74     public static String JavaDoc typeToString(int type)
75     {
76     return typeToString((byte)type);
77     }
78
79     public static String JavaDoc typeToString(byte type)
80     {
81     String JavaDoc result = type + "/";
82     switch (type) {
83     case GIOPRequest : result += "GIOPRequest"; break;
84     case GIOPReply : result += "GIOPReply"; break;
85     case GIOPCancelRequest : result += "GIOPCancelRequest"; break;
86     case GIOPLocateRequest : result += "GIOPLocateRequest"; break;
87     case GIOPLocateReply : result += "GIOPLocateReply"; break;
88     case GIOPCloseConnection : result += "GIOPCloseConnection"; break;
89     case GIOPMessageError : result += "GIOPMessageError"; break;
90     case GIOPFragment : result += "GIOPFragment"; break;
91     default : result += "Unknown"; break;
92     }
93     return result;
94     }
95
96     public static MessageBase readGIOPMessage(ORB orb, CorbaConnection connection)
97     {
98     MessageBase msg = readGIOPHeader(orb, connection);
99     msg = (MessageBase)readGIOPBody(orb, connection, (Message)msg);
100     return msg;
101     }
102
103     public static MessageBase readGIOPHeader(ORB orb, CorbaConnection connection)
104     {
105         MessageBase msg = null;
106         ReadTimeouts readTimeouts =
107                        orb.getORBData().getTransportTCPReadTimeouts();
108
109     ByteBuffer JavaDoc buf = null;
110
111     try {
112         buf = connection.read(GIOPMessageHeaderLength,
113               0, GIOPMessageHeaderLength,
114               readTimeouts.get_max_giop_header_time_to_wait());
115     } catch (IOException JavaDoc e) {
116         throw wrapper.ioexceptionWhenReadingConnection(e);
117     }
118
119         if (orb.giopDebugFlag) {
120             // Since this is executed in debug mode only the overhead of
121
// using a View Buffer is not an issue. We'll also use a
122
// read-only View Buffer so we don't disturb the state of
123
// byteBuffer.
124
dprint(".readGIOPHeader: " + typeToString(buf.get(7)));
125         dprint(".readGIOPHeader: GIOP header is: ");
126             ByteBuffer JavaDoc viewBuffer = buf.asReadOnlyBuffer();
127             viewBuffer.position(0).limit(GIOPMessageHeaderLength);
128         ByteBufferWithInfo bbwi = new ByteBufferWithInfo(orb,viewBuffer);
129         bbwi.buflen = GIOPMessageHeaderLength;
130         CDRInputStream_1_0.printBuffer(bbwi);
131         }
132
133         // Sanity checks
134

135         /*
136          * check for magic corruption
137          * check for version incompatibility
138          * check if fragmentation is allowed based on mesg type.
139             . 1.0 fragmentation disallowed; FragmentMessage is non-existent.
140             . 1.1 only {Request, Reply} msgs maybe fragmented.
141             . 1.2 only {Request, Reply, LocateRequest, LocateReply} msgs
142               maybe fragmented.
143         */

144
145         int b1, b2, b3, b4;
146
147         b1 = (buf.get(0) << 24) & 0xFF000000;
148         b2 = (buf.get(1) << 16) & 0x00FF0000;
149         b3 = (buf.get(2) << 8) & 0x0000FF00;
150         b4 = (buf.get(3) << 0) & 0x000000FF;
151         int magic = (b1 | b2 | b3 | b4);
152
153         if (magic != GIOPBigMagic) {
154             // If Magic is incorrect, it is an error.
155
// ACTION : send MessageError and close the connection.
156
throw wrapper.giopMagicError( CompletionStatus.COMPLETED_MAYBE);
157         }
158
159     // Extract the encoding version from the request GIOP Version,
160
// if it contains an encoding, and set GIOP version appropriately.
161
// For Java serialization, we use GIOP Version 1.2 message format.
162
byte requestEncodingVersion = Message.CDR_ENC_VERSION;
163     if ((buf.get(4) == 0x0D) &&
164         (buf.get(5) <= Message.JAVA_ENC_VERSION) &&
165         (buf.get(5) > Message.CDR_ENC_VERSION) &&
166         orb.getORBData().isJavaSerializationEnabled()) {
167         // Entering this block means the request is using Java encoding,
168
// and the encoding version is <= this ORB's Java encoding version.
169
requestEncodingVersion = buf.get(5);
170         buf.put(4, (byte) 0x01);
171         buf.put(5, (byte) 0x02);
172     }
173
174         GIOPVersion orbVersion = orb.getORBData().getGIOPVersion();
175
176         if (orb.giopDebugFlag) {
177             dprint(".readGIOPHeader: Message GIOP version: "
178                               + buf.get(4) + '.' + buf.get(5));
179             dprint(".readGIOPHeader: ORB Max GIOP Version: "
180                               + orbVersion);
181         }
182
183         if ( (buf.get(4) > orbVersion.getMajor()) ||
184              ( (buf.get(4) == orbVersion.getMajor()) && (buf.get(5) > orbVersion.getMinor()) )
185             ) {
186             // For requests, sending ORB should use the version info
187
// published in the IOR or may choose to use a <= version
188
// for requests. If the version is greater than published version,
189
// it is an error.
190

191             // For replies, the ORB should always receive a version it supports
192
// or less, but never greater (except for MessageError)
193

194             // ACTION : Send back a MessageError() with the the highest version
195
// the server ORB supports, and close the connection.
196
if ( buf.get(7) != GIOPMessageError ) {
197         throw wrapper.giopVersionError( CompletionStatus.COMPLETED_MAYBE);
198             }
199         }
200
201         AreFragmentsAllowed(buf.get(4), buf.get(5), buf.get(6), buf.get(7));
202
203         // create appropriate messages types
204

205         switch (buf.get(7)) {
206
207         case GIOPRequest:
208             if (orb.giopDebugFlag) {
209                 dprint(".readGIOPHeader: creating RequestMessage");
210             }
211             //msg = new RequestMessage(orb.giopDebugFlag);
212
if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0
213
msg = new RequestMessage_1_0(orb);
214             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1
215
msg = new RequestMessage_1_1(orb);
216             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2
217
msg = new RequestMessage_1_2(orb);
218             } else {
219         throw wrapper.giopVersionError(
220             CompletionStatus.COMPLETED_MAYBE);
221             }
222             break;
223
224         case GIOPLocateRequest:
225             if (orb.giopDebugFlag) {
226                 dprint(".readGIOPHeader: creating LocateRequestMessage");
227             }
228             //msg = new LocateRequestMessage(orb.giopDebugFlag);
229
if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0
230
msg = new LocateRequestMessage_1_0(orb);
231             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1
232
msg = new LocateRequestMessage_1_1(orb);
233             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2
234
msg = new LocateRequestMessage_1_2(orb);
235             } else {
236         throw wrapper.giopVersionError(
237             CompletionStatus.COMPLETED_MAYBE);
238             }
239             break;
240
241         case GIOPCancelRequest:
242             if (orb.giopDebugFlag) {
243                 dprint(".readGIOPHeader: creating CancelRequestMessage");
244             }
245             //msg = new CancelRequestMessage(orb.giopDebugFlag);
246
if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0
247
msg = new CancelRequestMessage_1_0();
248             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1
249
msg = new CancelRequestMessage_1_1();
250             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2
251
msg = new CancelRequestMessage_1_2();
252             } else {
253         throw wrapper.giopVersionError(
254             CompletionStatus.COMPLETED_MAYBE);
255             }
256             break;
257
258         case GIOPReply:
259             if (orb.giopDebugFlag) {
260                 dprint(".readGIOPHeader: creating ReplyMessage");
261             }
262             //msg = new ReplyMessage(orb.giopDebugFlag);
263
if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0
264
msg = new ReplyMessage_1_0(orb);
265             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1
266
msg = new ReplyMessage_1_1(orb);
267             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2
268
msg = new ReplyMessage_1_2(orb);
269             } else {
270         throw wrapper.giopVersionError(
271             CompletionStatus.COMPLETED_MAYBE);
272             }
273             break;
274
275         case GIOPLocateReply:
276             if (orb.giopDebugFlag) {
277                 dprint(".readGIOPHeader: creating LocateReplyMessage");
278             }
279             //msg = new LocateReplyMessage(orb.giopDebugFlag);
280
if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0
281
msg = new LocateReplyMessage_1_0(orb);
282             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1
283
msg = new LocateReplyMessage_1_1(orb);
284             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2
285
msg = new LocateReplyMessage_1_2(orb);
286             } else {
287         throw wrapper.giopVersionError(
288             CompletionStatus.COMPLETED_MAYBE);
289             }
290             break;
291
292         case GIOPCloseConnection:
293         case GIOPMessageError:
294             if (orb.giopDebugFlag) {
295                 dprint(".readGIOPHeader: creating Message for CloseConnection or MessageError");
296             }
297             // REVISIT a MessageError may contain the highest version server
298
// can support. In such a case, a new request may be made with the
299
// correct version or the connection be simply closed. Note the
300
// connection may have been closed by the server.
301
//msg = new Message(orb.giopDebugFlag);
302
if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0
303
msg = new Message_1_0();
304             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1
305
msg = new Message_1_1();
306             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2
307
msg = new Message_1_1();
308             } else {
309         throw wrapper.giopVersionError(
310             CompletionStatus.COMPLETED_MAYBE);
311             }
312             break;
313
314         case GIOPFragment:
315             if (orb.giopDebugFlag) {
316                 dprint(".readGIOPHeader: creating FragmentMessage");
317             }
318             //msg = new FragmentMessage(orb.giopDebugFlag);
319
if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0
320
// not possible (error checking done already)
321
} else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x01) ) { // 1.1
322
msg = new FragmentMessage_1_1();
323             } else if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x02) ) { // 1.2
324
msg = new FragmentMessage_1_2();
325             } else {
326         throw wrapper.giopVersionError(
327             CompletionStatus.COMPLETED_MAYBE);
328             }
329             break;
330
331         default:
332             if (orb.giopDebugFlag)
333                 dprint(".readGIOPHeader: UNKNOWN MESSAGE TYPE: "
334                + buf.get(7));
335             // unknown message type ?
336
// ACTION : send MessageError and close the connection
337
throw wrapper.giopVersionError(
338         CompletionStatus.COMPLETED_MAYBE);
339         }
340
341         //
342
// Initialize the generic GIOP header instance variables.
343
//
344

345         if ( (buf.get(4) == 0x01) && (buf.get(5) == 0x00) ) { // 1.0
346
Message_1_0 msg10 = (Message_1_0) msg;
347             msg10.magic = magic;
348             msg10.GIOP_version = new GIOPVersion(buf.get(4), buf.get(5));
349             msg10.byte_order = (buf.get(6) == LITTLE_ENDIAN_BIT);
350         // 'request partitioning' not supported on GIOP version 1.0
351
// so just use the default thread pool, 0.
352
msg.threadPoolToUse = 0;
353             msg10.message_type = buf.get(7);
354             msg10.message_size = readSize(buf.get(8), buf.get(9), buf.get(10), buf.get(11),
355                                           msg10.isLittleEndian()) +
356                                  GIOPMessageHeaderLength;
357         } else { // 1.1 & 1.2
358
Message_1_1 msg11 = (Message_1_1) msg;
359             msg11.magic = magic;
360             msg11.GIOP_version = new GIOPVersion(buf.get(4), buf.get(5));
361             msg11.flags = (byte)(buf.get(6) & TRAILING_TWO_BIT_BYTE_MASK);
362         // IMPORTANT: For 'request partitioning', the thread pool to use
363
// information is stored in the leading 6 bits of byte 6.
364
//
365
// IMPORTANT: Request partitioning is a PROPRIETARY EXTENSION !!!
366
//
367
// NOTE: Bitwise operators will promote a byte to an int before
368
// performing a bitwise operation and bytes, ints, longs, etc
369
// are signed types in Java. Thus, the need for the
370
// THREAD_POOL_TO_USE_MASK operation.
371
msg.threadPoolToUse = (buf.get(6) >>> 2) & THREAD_POOL_TO_USE_MASK;
372             msg11.message_type = buf.get(7);
373             msg11.message_size =
374                       readSize(buf.get(8), buf.get(9), buf.get(10), buf.get(11),
375                               msg11.isLittleEndian()) + GIOPMessageHeaderLength;
376         }
377
378
379         if (orb.giopDebugFlag) {
380             // Since this is executed in debug mode only the overhead of
381
// using a View Buffer is not an issue. We'll also use a
382
// read-only View Buffer so we don't disturb the state of
383
// byteBuffer.
384
dprint(".readGIOPHeader: header construction complete.");
385
386             // For debugging purposes, save the 12 bytes of the header
387
ByteBuffer JavaDoc viewBuf = buf.asReadOnlyBuffer();
388             byte[] msgBuf = new byte[GIOPMessageHeaderLength];
389             viewBuf.position(0).limit(GIOPMessageHeaderLength);
390             viewBuf.get(msgBuf,0,msgBuf.length);
391         // REVISIT: is giopHeader still used?
392
((MessageBase)msg).giopHeader = msgBuf;
393         }
394
395     msg.setByteBuffer(buf);
396     msg.setEncodingVersion(requestEncodingVersion);
397
398     return msg;
399     }
400
401     public static Message readGIOPBody(ORB orb,
402                            CorbaConnection connection,
403                        Message msg)
404     {
405         ReadTimeouts readTimeouts =
406                        orb.getORBData().getTransportTCPReadTimeouts();
407     ByteBuffer JavaDoc buf = msg.getByteBuffer();
408
409     buf.position(MessageBase.GIOPMessageHeaderLength);
410     int msgSizeMinusHeader =
411         msg.getSize() - MessageBase.GIOPMessageHeaderLength;
412     try {
413         buf = connection.read(buf,
414               GIOPMessageHeaderLength, msgSizeMinusHeader,
415               readTimeouts.get_max_time_to_wait());
416     } catch (IOException JavaDoc e) {
417         throw wrapper.ioexceptionWhenReadingConnection(e);
418     }
419
420     msg.setByteBuffer(buf);
421
422     if (orb.giopDebugFlag) {
423         dprint(".readGIOPBody: received message:");
424         ByteBuffer JavaDoc viewBuffer = buf.asReadOnlyBuffer();
425         viewBuffer.position(0).limit(msg.getSize());
426         ByteBufferWithInfo bbwi = new ByteBufferWithInfo(orb, viewBuffer);
427         CDRInputStream_1_0.printBuffer(bbwi);
428     }
429
430         return msg;
431     }
432
433     private static RequestMessage createRequest(
434             ORB orb, GIOPVersion gv, byte encodingVersion, int request_id,
435             boolean response_expected, byte[] object_key, String JavaDoc operation,
436             ServiceContexts service_contexts, Principal JavaDoc requesting_principal) {
437
438         if (gv.equals(GIOPVersion.V1_0)) { // 1.0
439
return new RequestMessage_1_0(orb, service_contexts, request_id,
440                      response_expected, object_key,
441                      operation, requesting_principal);
442         } else if (gv.equals(GIOPVersion.V1_1)) { // 1.1
443
return new RequestMessage_1_1(orb, service_contexts, request_id,
444                 response_expected, new byte[] { 0x00, 0x00, 0x00 },
445                 object_key, operation, requesting_principal);
446         } else if (gv.equals(GIOPVersion.V1_2)) { // 1.2
447
// Note: Currently we use response_expected flag to decide if the
448
// call is oneway or not. Ideally, it is possible to expect a
449
// response on a oneway call too, but we do not support it now.
450
byte response_flags = 0x03;
451             if (response_expected) {
452                 response_flags = 0x03;
453             } else {
454                 response_flags = 0x00;
455             }
456             /*
457             // REVISIT The following is the correct way to do it. This gives
458             // more flexibility.
459             if ((DII::INV_NO_RESPONSE == false) && response_expected) {
460                 response_flags = 0x03; // regular two-way
461             } else if ((DII::INV_NO_RESPONSE == false) && !response_expected) {
462                 // this condition is not possible
463             } else if ((DII::INV_NO_RESPONSE == true) && response_expected) {
464                 // oneway, but we need response for LocationForwards or
465                 // SystemExceptions.
466                 response_flags = 0x01;
467             } else if ((DII::INV_NO_RESPONSE == true) && !response_expected) {
468                 // oneway, no response required
469                 response_flags = 0x00;
470             }
471             */

472             TargetAddress target = new TargetAddress();
473             target.object_key(object_key);
474             RequestMessage msg =
475         new RequestMessage_1_2(orb, request_id, response_flags,
476                        new byte[] { 0x00, 0x00, 0x00 },
477                        target, operation, service_contexts);
478         msg.setEncodingVersion(encodingVersion);
479         return msg;
480         } else {
481         throw wrapper.giopVersionError(
482         CompletionStatus.COMPLETED_MAYBE);
483         }
484     }
485
486     public static RequestMessage createRequest(
487             ORB orb, GIOPVersion gv, byte encodingVersion, int request_id,
488         boolean response_expected, IOR ior,
489         short addrDisp, String JavaDoc operation,
490             ServiceContexts service_contexts, Principal JavaDoc requesting_principal) {
491
492     RequestMessage requestMessage = null;
493         IIOPProfile profile = ior.getProfile();
494             
495         if (addrDisp == KeyAddr.value) {
496             // object key will be used for target addressing
497
profile = ior.getProfile();
498             ObjectKey objKey = profile.getObjectKey();
499         byte[] object_key = objKey.getBytes(orb);
500         requestMessage =
501            createRequest(orb, gv, encodingVersion, request_id,
502                  response_expected, object_key,
503                  operation, service_contexts,
504                                  requesting_principal);
505         } else {
506         
507             if (!(gv.equals(GIOPVersion.V1_2))) {
508                 // only object_key based target addressing is allowed for
509
// GIOP 1.0 & 1.1
510
throw wrapper.giopVersionError(
511             CompletionStatus.COMPLETED_MAYBE);
512             }
513     
514             // Note: Currently we use response_expected flag to decide if the
515
// call is oneway or not. Ideally, it is possible to expect a
516
// response on a oneway call too, but we do not support it now.
517
byte response_flags = 0x03;
518             if (response_expected) {
519                 response_flags = 0x03;
520             } else {
521                 response_flags = 0x00;
522             }
523             
524             TargetAddress target = new TargetAddress();
525             if (addrDisp == ProfileAddr.value) { // iop profile will be used
526
profile = ior.getProfile();
527                 target.profile(profile.getIOPProfile());
528             } else if (addrDisp == ReferenceAddr.value) { // ior will be used
529
IORAddressingInfo iorInfo =
530                     new IORAddressingInfo( 0, // profile index
531
ior.getIOPIOR());
532                 target.ior(iorInfo);
533             } else {
534                 // invalid target addressing disposition value
535
throw wrapper.illegalTargetAddressDisposition(
536             CompletionStatus.COMPLETED_NO);
537             }
538         
539         requestMessage =
540                    new RequestMessage_1_2(orb, request_id, response_flags,
541                                   new byte[] { 0x00, 0x00, 0x00 }, target,
542                                   operation, service_contexts);
543         requestMessage.setEncodingVersion(encodingVersion);
544     }
545
546     if (gv.supportsIORIIOPProfileComponents()) {
547         // add request partitioning thread pool to use info
548
int poolToUse = 0; // default pool
549
IIOPProfileTemplate temp =
550         (IIOPProfileTemplate)profile.getTaggedProfileTemplate();
551         Iterator JavaDoc iter =
552         temp.iteratorById(ORBConstants.TAG_REQUEST_PARTITIONING_ID);
553         if (iter.hasNext()) {
554         poolToUse =
555             ((RequestPartitioningComponent)iter.next()).getRequestPartitioningId();
556         }
557
558         if (poolToUse < ORBConstants.REQUEST_PARTITIONING_MIN_THREAD_POOL_ID ||
559         poolToUse > ORBConstants.REQUEST_PARTITIONING_MAX_THREAD_POOL_ID) {
560         throw wrapper.invalidRequestPartitioningId(new Integer JavaDoc(poolToUse),
561               new Integer JavaDoc(ORBConstants.REQUEST_PARTITIONING_MIN_THREAD_POOL_ID),
562                   new Integer JavaDoc(ORBConstants.REQUEST_PARTITIONING_MAX_THREAD_POOL_ID));
563         }
564         requestMessage.setThreadPoolToUse(poolToUse);
565     }
566
567     return requestMessage;
568     }
569                     
570     public static ReplyMessage createReply(
571             ORB orb, GIOPVersion gv, byte encodingVersion, int request_id,
572             int reply_status, ServiceContexts service_contexts, IOR ior) {
573
574         if (gv.equals(GIOPVersion.V1_0)) { // 1.0
575
return new ReplyMessage_1_0(orb, service_contexts, request_id,
576                                         reply_status, ior);
577         } else if (gv.equals(GIOPVersion.V1_1)) { // 1.1
578
return new ReplyMessage_1_1(orb, service_contexts, request_id,
579                                         reply_status, ior);
580         } else if (gv.equals(GIOPVersion.V1_2)) { // 1.2
581
ReplyMessage msg =
582         new ReplyMessage_1_2(orb, request_id, reply_status,
583                      service_contexts, ior);
584         msg.setEncodingVersion(encodingVersion);
585         return msg;
586         } else {
587         throw wrapper.giopVersionError(
588         CompletionStatus.COMPLETED_MAYBE);
589         }
590     }
591
592     public static LocateRequestMessage createLocateRequest(
593             ORB orb, GIOPVersion gv, byte encodingVersion,
594             int request_id, byte[] object_key) {
595
596         if (gv.equals(GIOPVersion.V1_0)) { // 1.0
597
return new LocateRequestMessage_1_0(orb, request_id, object_key);
598         } else if (gv.equals(GIOPVersion.V1_1)) { // 1.1
599
return new LocateRequestMessage_1_1(orb, request_id, object_key);
600         } else if (gv.equals(GIOPVersion.V1_2)) { // 1.2
601
TargetAddress target = new TargetAddress();
602             target.object_key(object_key);
603             LocateRequestMessage msg =
604         new LocateRequestMessage_1_2(orb, request_id, target);
605         msg.setEncodingVersion(encodingVersion);
606         return msg;
607         } else {
608         throw wrapper.giopVersionError(
609         CompletionStatus.COMPLETED_MAYBE);
610         }
611     }
612
613     public static LocateReplyMessage createLocateReply(
614         ORB orb, GIOPVersion gv, byte encodingVersion,
615             int request_id, int locate_status, IOR ior) {
616
617         if (gv.equals(GIOPVersion.V1_0)) { // 1.0
618
return new LocateReplyMessage_1_0(orb, request_id,
619                                               locate_status, ior);
620         } else if (gv.equals(GIOPVersion.V1_1)) { // 1.1
621
return new LocateReplyMessage_1_1(orb, request_id,
622                                               locate_status, ior);
623         } else if (gv.equals(GIOPVersion.V1_2)) { // 1.2
624
LocateReplyMessage msg =
625         new LocateReplyMessage_1_2(orb, request_id,
626                        locate_status, ior);
627         msg.setEncodingVersion(encodingVersion);
628         return msg;
629         } else {
630         throw wrapper.giopVersionError(
631         CompletionStatus.COMPLETED_MAYBE);
632         }
633     }
634
635     public static CancelRequestMessage createCancelRequest(
636             GIOPVersion gv, int request_id) {
637
638         if (gv.equals(GIOPVersion.V1_0)) { // 1.0
639
return new CancelRequestMessage_1_0(request_id);
640         } else if (gv.equals(GIOPVersion.V1_1)) { // 1.1
641
return new CancelRequestMessage_1_1(request_id);
642         } else if (gv.equals(GIOPVersion.V1_2)) { // 1.2
643
return new CancelRequestMessage_1_2(request_id);
644         } else {
645         throw wrapper.giopVersionError(
646         CompletionStatus.COMPLETED_MAYBE);
647         }
648     }
649
650     public static Message createCloseConnection(GIOPVersion gv) {
651         if (gv.equals(GIOPVersion.V1_0)) { // 1.0
652
return new Message_1_0(Message.GIOPBigMagic, false,
653                                    Message.GIOPCloseConnection, 0);
654         } else if (gv.equals(GIOPVersion.V1_1)) { // 1.1
655
return new Message_1_1(Message.GIOPBigMagic, GIOPVersion.V1_1,
656                                    FLAG_NO_FRAG_BIG_ENDIAN,
657                                    Message.GIOPCloseConnection, 0);
658         } else if (gv.equals(GIOPVersion.V1_2)) { // 1.2
659
return new Message_1_1(Message.GIOPBigMagic, GIOPVersion.V1_2,
660                                    FLAG_NO_FRAG_BIG_ENDIAN,
661                                    Message.GIOPCloseConnection, 0);
662         } else {
663         throw wrapper.giopVersionError(
664         CompletionStatus.COMPLETED_MAYBE);
665         }
666     }
667
668     public static Message createMessageError(GIOPVersion gv) {
669         if (gv.equals(GIOPVersion.V1_0)) { // 1.0
670
return new Message_1_0(Message.GIOPBigMagic, false,
671                                    Message.GIOPMessageError, 0);
672         } else if (gv.equals(GIOPVersion.V1_1)) { // 1.1
673
return new Message_1_1(Message.GIOPBigMagic, GIOPVersion.V1_1,
674                                    FLAG_NO_FRAG_BIG_ENDIAN,
675                                    Message.GIOPMessageError, 0);
676         } else if (gv.equals(GIOPVersion.V1_2)) { // 1.2
677
return new Message_1_1(Message.GIOPBigMagic, GIOPVersion.V1_2,
678                                    FLAG_NO_FRAG_BIG_ENDIAN,
679                                    Message.GIOPMessageError, 0);
680         } else {
681         throw wrapper.giopVersionError(
682         CompletionStatus.COMPLETED_MAYBE);
683         }
684     }
685
686     public static FragmentMessage createFragmentMessage(GIOPVersion gv) {
687         // This method is not currently used.
688
// New fragment messages are always created from existing messages.
689
// Creating a FragmentMessage from InputStream is done in
690
// createFromStream(..)
691
return null;
692     }
693
694     public static int getRequestId(Message msg) {
695         switch (msg.getType()) {
696         case GIOPRequest :
697             return ((RequestMessage) msg).getRequestId();
698         case GIOPReply :
699             return ((ReplyMessage) msg).getRequestId();
700         case GIOPLocateRequest :
701             return ((LocateRequestMessage) msg).getRequestId();
702         case GIOPLocateReply :
703             return ((LocateReplyMessage) msg).getRequestId();
704         case GIOPCancelRequest :
705             return ((CancelRequestMessage) msg).getRequestId();
706         case GIOPFragment :
707             return ((FragmentMessage) msg).getRequestId();
708         }
709
710     throw wrapper.illegalGiopMsgType(
711         CompletionStatus.COMPLETED_MAYBE);
712     }
713
714     /**
715      * Set a flag in the given buffer (fragment bit, byte order bit, etc)
716      */

717     public static void setFlag(ByteBuffer JavaDoc byteBuffer, int flag) {
718         byte b = byteBuffer.get(6);
719         b |= flag;
720         byteBuffer.put(6,b);
721     }
722
723     /**
724      * Clears a flag in the given buffer
725      */

726     public static void clearFlag(byte[] buf, int flag) {
727         buf[6] &= (0xFF ^ flag);
728     }
729
730     private static void AreFragmentsAllowed(byte major, byte minor, byte flag,
731             byte msgType) {
732
733         if ( (major == 0x01) && (minor == 0x00) ) { // 1.0
734
if (msgType == GIOPFragment) {
735         throw wrapper.fragmentationDisallowed(
736             CompletionStatus.COMPLETED_MAYBE);
737             }
738         }
739
740         if ( (flag & MORE_FRAGMENTS_BIT) == MORE_FRAGMENTS_BIT ) {
741             switch (msgType) {
742             case GIOPCancelRequest :
743             case GIOPCloseConnection :
744             case GIOPMessageError :
745         throw wrapper.fragmentationDisallowed(
746             CompletionStatus.COMPLETED_MAYBE);
747             case GIOPLocateRequest :
748             case GIOPLocateReply :
749                 if ( (major == 0x01) && (minor == 0x01) ) { // 1.1
750
throw wrapper.fragmentationDisallowed(
751             CompletionStatus.COMPLETED_MAYBE);
752                 }
753                 break;
754             }
755         }
756     }
757
758     /**
759      * Construct an ObjectKey from a byte[].
760      *
761      * @return ObjectKey the object key.
762      */

763     static ObjectKey extractObjectKey(byte[] objKey, ORB orb) {
764
765         try {
766             if (objKey != null) {
767                 ObjectKey objectKey =
768             orb.getObjectKeyFactory().create(objKey);
769                 if (objectKey != null) {
770                     return objectKey;
771                 }
772             }
773         } catch (Exception JavaDoc e) {
774         // XXX log this exception
775
}
776
777     // This exception is thrown if any exceptions are raised while
778
// extracting the object key or if the object key is empty.
779
throw wrapper.invalidObjectKey();
780     }
781
782     /**
783      * Extract the object key from TargetAddress.
784      *
785      * @return ObjectKey the object key.
786      */

787     static ObjectKey extractObjectKey(TargetAddress target, ORB orb) {
788     
789         short orbTargetAddrPref = orb.getORBData().getGIOPTargetAddressPreference();
790         short reqAddrDisp = target.discriminator();
791             
792         switch (orbTargetAddrPref) {
793         case ORBConstants.ADDR_DISP_OBJKEY :
794             if (reqAddrDisp != KeyAddr.value) {
795                 throw new AddressingDispositionException(KeyAddr.value);
796             }
797             break;
798         case ORBConstants.ADDR_DISP_PROFILE :
799             if (reqAddrDisp != ProfileAddr.value) {
800                 throw new AddressingDispositionException(ProfileAddr.value);
801             }
802             break;
803         case ORBConstants.ADDR_DISP_IOR :
804             if (reqAddrDisp != ReferenceAddr.value) {
805                 throw new AddressingDispositionException(ReferenceAddr.value);
806             }
807             break;
808         case ORBConstants.ADDR_DISP_HANDLE_ALL :
809             break;
810         default :
811         throw wrapper.orbTargetAddrPreferenceInExtractObjectkeyInvalid() ;
812         }
813         
814         try {
815             switch (reqAddrDisp) {
816             case KeyAddr.value :
817                 byte[] objKey = target.object_key();
818                 if (objKey != null) { // AddressingDisposition::KeyAddr
819
ObjectKey objectKey =
820             orb.getObjectKeyFactory().create(objKey);
821                     if (objectKey != null) {
822                        return objectKey;
823                    }
824                 }
825                 break;
826             case ProfileAddr.value :
827                 IIOPProfile iiopProfile = null;
828                 TaggedProfile JavaDoc profile = target.profile();
829                 if (profile != null) { // AddressingDisposition::ProfileAddr
830
iiopProfile = IIOPFactories.makeIIOPProfile(orb, profile);
831                    ObjectKey objectKey = iiopProfile.getObjectKey();
832                    if (objectKey != null) {
833                        return objectKey;
834                    }
835                 }
836                 break;
837             case ReferenceAddr.value :
838                 IORAddressingInfo iorInfo = target.ior();
839                 if (iorInfo != null) { // AddressingDisposition::IORAddr
840
profile = iorInfo.ior.profiles[iorInfo.selected_profile_index];
841                     iiopProfile = IIOPFactories.makeIIOPProfile(orb, profile);
842                     ObjectKey objectKey = iiopProfile.getObjectKey();
843                     if (objectKey != null) {
844                        return objectKey;
845                    }
846                 }
847                 break;
848             default : // this cannot happen
849
// There is no need for a explicit exception, since the
850
// TargetAddressHelper.read() would have raised a BAD_OPERATION
851
// exception by now.
852
break;
853             }
854         } catch (Exception JavaDoc e) {}
855
856     // This exception is thrown if any exceptions are raised while
857
// extracting the object key from the TargetAddress or if all the
858
// the valid TargetAddress::AddressingDispositions are empty.
859
throw wrapper.invalidObjectKey() ;
860     }
861
862     private static int readSize(byte b1, byte b2, byte b3, byte b4,
863             boolean littleEndian) {
864
865         int a1, a2, a3, a4;
866
867         if (!littleEndian) {
868             a1 = (b1 << 24) & 0xFF000000;
869             a2 = (b2 << 16) & 0x00FF0000;
870             a3 = (b3 << 8) & 0x0000FF00;
871             a4 = (b4 << 0) & 0x000000FF;
872         } else {
873             a1 = (b4 << 24) & 0xFF000000;
874             a2 = (b3 << 16) & 0x00FF0000;
875             a3 = (b2 << 8) & 0x0000FF00;
876             a4 = (b1 << 0) & 0x000000FF;
877         }
878
879         return (a1 | a2 | a3 | a4);
880     }
881
882     static void nullCheck(Object JavaDoc obj) {
883         if (obj == null) {
884         throw wrapper.nullNotAllowed() ;
885         }
886     }
887
888     static SystemException JavaDoc getSystemException(
889         String JavaDoc exClassName, int minorCode, CompletionStatus JavaDoc completionStatus,
890     String JavaDoc message, ORBUtilSystemException wrapper)
891     {
892     SystemException JavaDoc sysEx = null;
893
894         try {
895         Class JavaDoc clazz = ORBClassLoader.loadClass(exClassName);
896         if (message == null) {
897         sysEx = (SystemException JavaDoc) clazz.newInstance();
898         } else {
899         Class JavaDoc[] types = { String JavaDoc.class };
900         Constructor JavaDoc constructor = clazz.getConstructor(types);
901         Object JavaDoc[] args = { message };
902         sysEx = (SystemException JavaDoc)constructor.newInstance(args);
903         }
904         } catch (Exception JavaDoc someEx) {
905         throw wrapper.badSystemExceptionInReply(
906         CompletionStatus.COMPLETED_MAYBE, someEx );
907         }
908
909         sysEx.minor = minorCode;
910         sysEx.completed = completionStatus;
911
912         return sysEx;
913     }
914
915     public void callback(MessageHandler handler)
916         throws java.io.IOException JavaDoc
917     {
918         handler.handleInput(this);
919     }
920
921     public ByteBuffer JavaDoc getByteBuffer()
922     {
923     return byteBuffer;
924     }
925
926     public void setByteBuffer(ByteBuffer JavaDoc byteBuffer)
927     {
928     this.byteBuffer = byteBuffer;
929     }
930
931     public int getThreadPoolToUse()
932     {
933     return threadPoolToUse;
934     }
935
936     public byte getEncodingVersion() {
937     return this.encodingVersion;
938     }
939
940     public void setEncodingVersion(byte version) {
941     this.encodingVersion = version;
942     }
943
944     private static void dprint(String JavaDoc msg)
945     {
946     ORBUtility.dprint("MessageBase", msg);
947     }
948 }
949
Popular Tags