KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > corba > AnyImpl


1 /*
2  * @(#)AnyImpl.java 1.66 06/10/09
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package com.sun.corba.se.impl.corba;
17
18 import java.io.Serializable JavaDoc;
19 import java.math.BigDecimal JavaDoc;
20 import java.util.List JavaDoc ;
21 import java.util.ArrayList JavaDoc ;
22
23 import org.omg.CORBA.Principal JavaDoc ;
24 import org.omg.CORBA.TypeCode JavaDoc ;
25 import org.omg.CORBA.Any JavaDoc ;
26 import org.omg.CORBA.CompletionStatus JavaDoc ;
27 import org.omg.CORBA.TCKind JavaDoc ;
28
29 import org.omg.CORBA.portable.Streamable JavaDoc;
30 import org.omg.CORBA.portable.InputStream JavaDoc;
31 import org.omg.CORBA.portable.OutputStream JavaDoc;
32 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
33 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
34
35 import com.sun.corba.se.spi.orb.ORB;
36 import com.sun.corba.se.spi.orb.ORBVersionFactory;
37 import com.sun.corba.se.spi.logging.CORBALogDomains;
38 import com.sun.corba.se.spi.presentation.rmi.StubAdapter;
39
40 import com.sun.corba.se.impl.encoding.CDRInputStream;
41 import com.sun.corba.se.impl.encoding.EncapsInputStream;
42 import com.sun.corba.se.impl.encoding.EncapsOutputStream;
43 import com.sun.corba.se.impl.io.ValueUtility;
44 import com.sun.corba.se.impl.orbutil.RepositoryIdFactory;
45 import com.sun.corba.se.impl.orbutil.RepositoryIdStrings;
46 import com.sun.corba.se.impl.orbutil.ORBUtility;
47 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
48
49 // subclasses must provide a matching helper class
50
public class AnyImpl extends Any JavaDoc
51 {
52     private static final class AnyInputStream extends EncapsInputStream
53     {
54     public AnyInputStream(EncapsInputStream theStream )
55     {
56         super( theStream );
57     }
58     }
59
60     private static final class AnyOutputStream extends EncapsOutputStream
61     {
62     public AnyOutputStream(ORB orb)
63     {
64         super((ORB)orb);
65     }
66
67     public org.omg.CORBA.portable.InputStream JavaDoc create_input_stream()
68     {
69         return new AnyInputStream(
70         (com.sun.corba.se.impl.encoding.EncapsInputStream)
71             super.create_input_stream());
72     }
73     }
74
75     //
76
// Always valid.
77
//
78
private TypeCodeImpl typeCode;
79     protected ORB orb;
80     private ORBUtilSystemException wrapper ;
81
82     //
83
// Validity depends upon typecode. The 'value' and 'object' instance
84
// members are used to hold immutable types as defined by the
85
// isStreamed[] table below. Otherwise, 'stream' is non-null and
86
// holds the value in CDR marshaled format. As an optimization, the
87
// stream type is an Any extension of CDR stream that is used to
88
// detect an optimization in read_value().
89
//
90
private CDRInputStream stream;
91     private long value;
92     private java.lang.Object JavaDoc object;
93
94     // Setting the typecode via the type() accessor wipes out the value.
95
// An attempt to extract before the value is set will result
96
// in a BAD_OPERATION exception being raised.
97
private boolean isInitialized = false;
98
99     private static final int DEFAULT_BUFFER_SIZE = 32;
100
101     /*
102      * This boolean array tells us if a given typecode must be
103      * streamed. Objects that are immutable don't have to be streamed.
104      */

105     static boolean isStreamed[] = {
106     false, // null
107
false, // void
108
false, // short
109
false, // long
110
false, // ushort
111
false, // ulong
112
false, // float
113
false, // double
114
false, // boolean
115
false, // char
116
false, // octet
117
false, // any
118
false, // TypeCode
119
true, // Principal
120
false, // objref
121
true, // struct
122
true, // union
123
false, // enum
124
false, // string
125
true, // sequence
126
true, // array
127
true, // alias
128
true, // except
129
false, // longlong
130
false, // ulonglong
131
false, // longdouble
132
false, // wchar
133
false, // wstring
134
false, // fixed
135
false, // value
136
false, // value_box (used to be true)
137
false, // native
138
false // abstract interface
139
};
140
141     static AnyImpl convertToNative(ORB orb, Any JavaDoc any) {
142         if (any instanceof AnyImpl) {
143             return (AnyImpl)any;
144         } else {
145             AnyImpl anyImpl = new AnyImpl(orb, any);
146             anyImpl.typeCode = TypeCodeImpl.convertToNative(orb, anyImpl.typeCode);
147             return anyImpl;
148         }
149     }
150
151     ///////////////////////////////////////////////////////////////////////////
152
// Constructors
153

154     /**
155      * A constructor that sets the Any to contain a null. It also marks
156      * the value as being invalid so that extractions throw an exception
157      * until an insertion has been performed.
158      */

159     public AnyImpl(ORB orb)
160     {
161     this.orb = orb;
162     wrapper = ORBUtilSystemException.get( (com.sun.corba.se.spi.orb.ORB)orb,
163         CORBALogDomains.RPC_PRESENTATION ) ;
164
165     typeCode = orb.get_primitive_tc(TCKind._tk_null);
166     stream = null;
167     object = null;
168     value = 0;
169         // null is a valid value
170
isInitialized = true;
171     }
172
173     //
174
// Create a new AnyImpl which is a copy of obj.
175
//
176
public AnyImpl(ORB orb, Any JavaDoc obj) {
177     this(orb);
178
179     if ((obj instanceof AnyImpl)) {
180         AnyImpl objImpl = (AnyImpl)obj;
181         typeCode = objImpl.typeCode;
182         value = objImpl.value;
183         object = objImpl.object;
184             isInitialized = objImpl.isInitialized;
185
186         if (objImpl.stream != null)
187         stream = objImpl.stream.dup();
188
189     } else {
190         read_value(obj.create_input_stream(), obj.type());
191     }
192     }
193
194     ///////////////////////////////////////////////////////////////////////////
195
// basic accessors
196

197     /**
198      * returns the type of the element contained in the Any.
199      *
200      * @result the TypeCode for the element in the Any
201      */

202     public TypeCode JavaDoc type() {
203     return typeCode;
204     }
205
206     private TypeCode JavaDoc realType() {
207         return realType(typeCode);
208     }
209
210     private TypeCode JavaDoc realType(TypeCode JavaDoc aType) {
211         TypeCode JavaDoc realType = aType;
212         try {
213             // Note: Indirect types are handled in kind() method
214
while (realType.kind().value() == TCKind._tk_alias) {
215                 realType = realType.content_type();
216             }
217         } catch (BadKind JavaDoc bad) { // impossible
218
throw wrapper.badkindCannotOccur( bad ) ;
219         }
220         return realType;
221     }
222
223     /**
224      * sets the type of the element to be contained in the Any.
225      *
226      * @param tc the TypeCode for the element in the Any
227      */

228     public void type(TypeCode JavaDoc tc)
229     {
230     //debug.log ("type2");
231
// set the typecode
232
typeCode = TypeCodeImpl.convertToNative(orb, tc);
233
234     stream = null;
235     value = 0;
236     object = null;
237     // null is the only legal value this Any can have after resetting the type code
238
isInitialized = (tc.kind().value() == TCKind._tk_null);
239     }
240
241     /**
242      * checks for equality between Anys.
243      *
244      * @param otherAny the Any to be compared with.
245      * @result true if the Anys are equal, false otherwise.
246      */

247     public boolean equal(Any JavaDoc otherAny)
248     {
249     //debug.log ("equal");
250

251     if (otherAny == this)
252         return true;
253
254     // first check for typecode equality.
255
// note that this will take aliases into account
256
if (!typeCode.equal(otherAny.type()))
257         return false;
258
259     // Resolve aliases here
260
TypeCode JavaDoc realType = realType();
261
262     // _REVISIT_ Possible optimization for the case where
263
// otherAny is a AnyImpl and the endianesses match.
264
// Need implementation of CDRInputStream.equals()
265
// For now we disable this to encourage testing the generic,
266
// unoptimized code below.
267
// Unfortunately this generic code needs to copy the whole stream
268
// at least once.
269
// if (AnyImpl.isStreamed[realType.kind().value()]) {
270
// if (otherAny instanceof AnyImpl) {
271
// return ((AnyImpl)otherAny).stream.equals(stream);
272
// }
273
// }
274
switch (realType.kind().value()) {
275         // handle primitive types
276
case TCKind._tk_null:
277         case TCKind._tk_void:
278         return true;
279         case TCKind._tk_short:
280         return (extract_short() == otherAny.extract_short());
281         case TCKind._tk_long:
282         return (extract_long() == otherAny.extract_long());
283         case TCKind._tk_ushort:
284         return (extract_ushort() == otherAny.extract_ushort());
285         case TCKind._tk_ulong:
286         return (extract_ulong() == otherAny.extract_ulong());
287         case TCKind._tk_float:
288         return (extract_float() == otherAny.extract_float());
289         case TCKind._tk_double:
290         return (extract_double() == otherAny.extract_double());
291         case TCKind._tk_boolean:
292         return (extract_boolean() == otherAny.extract_boolean());
293         case TCKind._tk_char:
294         return (extract_char() == otherAny.extract_char());
295         case TCKind._tk_wchar:
296         return (extract_wchar() == otherAny.extract_wchar());
297         case TCKind._tk_octet:
298         return (extract_octet() == otherAny.extract_octet());
299         case TCKind._tk_any:
300         return extract_any().equal(otherAny.extract_any());
301         case TCKind._tk_TypeCode:
302         return extract_TypeCode().equal(otherAny.extract_TypeCode());
303         case TCKind._tk_string:
304         return extract_string().equals(otherAny.extract_string());
305         case TCKind._tk_wstring:
306         return (extract_wstring().equals(otherAny.extract_wstring()));
307         case TCKind._tk_longlong:
308         return (extract_longlong() == otherAny.extract_longlong());
309         case TCKind._tk_ulonglong:
310         return (extract_ulonglong() == otherAny.extract_ulonglong());
311
312         case TCKind._tk_objref:
313         return (extract_Object().equals(otherAny.extract_Object()));
314         case TCKind._tk_Principal:
315         return (extract_Principal().equals(otherAny.extract_Principal()));
316
317         case TCKind._tk_enum:
318         return (extract_long() == otherAny.extract_long());
319         case TCKind._tk_fixed:
320         return (extract_fixed().compareTo(otherAny.extract_fixed()) == 0);
321         case TCKind._tk_except:
322         case TCKind._tk_struct:
323         case TCKind._tk_union:
324         case TCKind._tk_sequence:
325         case TCKind._tk_array:
326         InputStream JavaDoc copyOfMyStream = this.create_input_stream();
327         InputStream JavaDoc copyOfOtherStream = otherAny.create_input_stream();
328         return equalMember(realType, copyOfMyStream, copyOfOtherStream);
329
330         // Too complicated to handle value types the way we handle
331
// other complex types above. Don't try to decompose it here
332
// for faster comparison, just use Object.equals().
333
case TCKind._tk_value:
334         case TCKind._tk_value_box:
335         return extract_Value().equals(otherAny.extract_Value());
336
337         case TCKind._tk_alias:
338         throw wrapper.errorResolvingAlias() ;
339
340         case TCKind._tk_longdouble:
341         // Unspecified for Java
342
throw wrapper.tkLongDoubleNotSupported() ;
343
344         default:
345         throw wrapper.typecodeNotSupported() ;
346     }
347     }
348
349     // Needed for equal() in order to achieve linear performance for complex types.
350
// Uses up (recursively) copies of the InputStream in both Anys that got created in equal().
351
private boolean equalMember(TypeCode JavaDoc memberType, InputStream JavaDoc myStream, InputStream JavaDoc otherStream) {
352     // Resolve aliases here
353
TypeCode JavaDoc realType = realType(memberType);
354
355     try {
356         switch (realType.kind().value()) {
357         // handle primitive types
358
case TCKind._tk_null:
359         case TCKind._tk_void:
360             return true;
361         case TCKind._tk_short:
362             return (myStream.read_short() == otherStream.read_short());
363         case TCKind._tk_long:
364             return (myStream.read_long() == otherStream.read_long());
365         case TCKind._tk_ushort:
366             return (myStream.read_ushort() == otherStream.read_ushort());
367         case TCKind._tk_ulong:
368             return (myStream.read_ulong() == otherStream.read_ulong());
369         case TCKind._tk_float:
370             return (myStream.read_float() == otherStream.read_float());
371         case TCKind._tk_double:
372             return (myStream.read_double() == otherStream.read_double());
373         case TCKind._tk_boolean:
374             return (myStream.read_boolean() == otherStream.read_boolean());
375         case TCKind._tk_char:
376             return (myStream.read_char() == otherStream.read_char());
377         case TCKind._tk_wchar:
378             return (myStream.read_wchar() == otherStream.read_wchar());
379         case TCKind._tk_octet:
380             return (myStream.read_octet() == otherStream.read_octet());
381         case TCKind._tk_any:
382             return myStream.read_any().equal(otherStream.read_any());
383         case TCKind._tk_TypeCode:
384             return myStream.read_TypeCode().equal(otherStream.read_TypeCode());
385         case TCKind._tk_string:
386             return myStream.read_string().equals(otherStream.read_string());
387         case TCKind._tk_wstring:
388             return (myStream.read_wstring().equals(otherStream.read_wstring()));
389         case TCKind._tk_longlong:
390             return (myStream.read_longlong() == otherStream.read_longlong());
391         case TCKind._tk_ulonglong:
392             return (myStream.read_ulonglong() == otherStream.read_ulonglong());
393
394         case TCKind._tk_objref:
395             return (myStream.read_Object().equals(otherStream.read_Object()));
396         case TCKind._tk_Principal:
397             return (myStream.read_Principal().equals(otherStream.read_Principal()));
398
399         case TCKind._tk_enum:
400             return (myStream.read_long() == otherStream.read_long());
401         case TCKind._tk_fixed:
402             return (myStream.read_fixed().compareTo(otherStream.read_fixed()) == 0);
403         case TCKind._tk_except:
404         case TCKind._tk_struct: {
405             int length = realType.member_count();
406             for (int i=0; i<length; i++) {
407             if ( ! equalMember(realType.member_type(i), myStream, otherStream)) {
408                 return false;
409             }
410             }
411             return true;
412         }
413         case TCKind._tk_union: {
414             Any JavaDoc myDiscriminator = orb.create_any();
415             Any JavaDoc otherDiscriminator = orb.create_any();
416             myDiscriminator.read_value(myStream, realType.discriminator_type());
417             otherDiscriminator.read_value(otherStream, realType.discriminator_type());
418
419             if ( ! myDiscriminator.equal(otherDiscriminator)) {
420             return false;
421             }
422             TypeCodeImpl realTypeCodeImpl = TypeCodeImpl.convertToNative(orb, realType);
423             int memberIndex = realTypeCodeImpl.currentUnionMemberIndex(myDiscriminator);
424             if (memberIndex == -1)
425             throw wrapper.unionDiscriminatorError() ;
426
427             if ( ! equalMember(realType.member_type(memberIndex), myStream, otherStream)) {
428             return false;
429             }
430             return true;
431         }
432         case TCKind._tk_sequence: {
433             int length = myStream.read_long();
434             otherStream.read_long(); // just so that the two stream are in sync
435
for (int i=0; i<length; i++) {
436             if ( ! equalMember(realType.content_type(), myStream, otherStream)) {
437                 return false;
438             }
439             }
440             return true;
441         }
442         case TCKind._tk_array: {
443             int length = realType.member_count();
444             for (int i=0; i<length; i++) {
445             if ( ! equalMember(realType.content_type(), myStream, otherStream)) {
446                 return false;
447             }
448             }
449             return true;
450         }
451
452         // Too complicated to handle value types the way we handle
453
// other complex types above. Don't try to decompose it here
454
// for faster comparison, just use Object.equals().
455
case TCKind._tk_value:
456         case TCKind._tk_value_box:
457             org.omg.CORBA_2_3.portable.InputStream JavaDoc mine =
458             (org.omg.CORBA_2_3.portable.InputStream JavaDoc)myStream;
459             org.omg.CORBA_2_3.portable.InputStream JavaDoc other =
460             (org.omg.CORBA_2_3.portable.InputStream JavaDoc)otherStream;
461             return mine.read_value().equals(other.read_value());
462
463         case TCKind._tk_alias:
464             // error resolving alias above
465
throw wrapper.errorResolvingAlias() ;
466
467         case TCKind._tk_longdouble:
468             throw wrapper.tkLongDoubleNotSupported() ;
469
470         default:
471             throw wrapper.typecodeNotSupported() ;
472         }
473     } catch (BadKind JavaDoc badKind) { // impossible
474
throw wrapper.badkindCannotOccur() ;
475     } catch (Bounds JavaDoc bounds) { // impossible
476
throw wrapper.boundsCannotOccur() ;
477     }
478     }
479
480     ///////////////////////////////////////////////////////////////////////////
481
// accessors for marshaling/unmarshaling
482

483     /**
484      * returns an output stream that an Any value can be marshaled into.
485      *
486      * @result the OutputStream to marshal value of Any into
487      */

488     public org.omg.CORBA.portable.OutputStream JavaDoc create_output_stream()
489     {
490     //debug.log ("create_output_stream");
491
return new AnyOutputStream(orb);
492     }
493
494     /**
495      * returns an input stream that an Any value can be marshaled out of.
496      *
497      * @result the InputStream to marshal value of Any out of.
498      */

499     public org.omg.CORBA.portable.InputStream JavaDoc create_input_stream()
500     {
501     //
502
// We create a new InputStream so that multiple threads can call here
503
// and read the streams in parallel without thread safety problems.
504
//
505
//debug.log ("create_input_stream");
506
if (AnyImpl.isStreamed[realType().kind().value()]) {
507         return stream.dup();
508     } else {
509         OutputStream JavaDoc os = (OutputStream JavaDoc)orb.create_output_stream();
510         TCUtility.marshalIn(os, realType(), value, object);
511
512         return os.create_input_stream();
513     }
514     }
515
516     ///////////////////////////////////////////////////////////////////////////
517
// marshaling/unmarshaling routines
518

519     //
520
// If the InputStream is a CDRInputStream then we can copy the bytes
521
// since it is in our format and does not have alignment issues.
522
//
523
public void read_value(org.omg.CORBA.portable.InputStream JavaDoc in, TypeCode JavaDoc tc)
524     {
525     //debug.log ("read_value");
526
//
527
// Assume that someone isn't going to think they can keep reading
528
// from this stream after calling us. That would be likely for
529
// an IIOPInputStream but if it is an AnyInputStream then they
530
// presumably obtained it via our create_output_stream() so they could
531
// write the contents of an IDL data type to it and then call
532
// create_input_stream() for us to read it. This is how Helper classes
533
// typically implement the insert() method.
534
// We should probably document this behavior in the 1.1 revision
535
// task force.
536
//
537

538     typeCode = TypeCodeImpl.convertToNative(orb, tc);
539     int kind = realType().kind().value();
540     if (kind >= isStreamed.length) {
541         throw wrapper.invalidIsstreamedTckind( CompletionStatus.COMPLETED_MAYBE,
542         new Integer JavaDoc(kind)) ;
543     }
544
545     if (AnyImpl.isStreamed[kind]) {
546         if ( in instanceof AnyInputStream ) {
547         // could only have been created here
548
stream = (CDRInputStream)in;
549         } else {
550         org.omg.CORBA_2_3.portable.OutputStream JavaDoc out =
551             (org.omg.CORBA_2_3.portable.OutputStream JavaDoc)orb.create_output_stream();
552         typeCode.copy((org.omg.CORBA_2_3.portable.InputStream JavaDoc)in, out);
553         stream = (CDRInputStream)out.create_input_stream();
554         }
555     } else {
556         java.lang.Object JavaDoc[] objholder = new java.lang.Object JavaDoc[1];
557         objholder[0] = object;
558         long[] longholder = new long[1];
559         TCUtility.unmarshalIn(in, realType(), longholder, objholder);
560         value = longholder[0];
561         object = objholder[0];
562         stream = null;
563     }
564     isInitialized = true;
565     }
566
567
568     //
569
// We could optimize this by noticing whether the target stream
570
// has ever had anything marshaled on it that required an
571
// alignment of greater than 4 (was write_double() ever called on it).
572
// If not, then we can just do a byte array copy without having to
573
// drive the remarshaling through typecode interpretation.
574
//
575
public void write_value(OutputStream JavaDoc out)
576     {
577     //debug.log ("write_value");
578
if (AnyImpl.isStreamed[realType().kind().value()]) {
579         typeCode.copy(stream.dup(), out);
580     } else {
581         // _REVISIT_ check isInitialized whether all we write is TypeCode!
582
TCUtility.marshalIn(out, realType(), value, object);
583     }
584     }
585
586     /**
587      * takes a streamable and inserts its reference into the any
588      *
589      * @param s the streamable to insert
590      */

591     public void insert_Streamable(Streamable JavaDoc s)
592     {
593     //debug.log ("insert_Streamable");
594
typeCode = TypeCodeImpl.convertToNative(orb, s._type());
595     object = s;
596     isInitialized = true;
597     }
598      
599     public Streamable JavaDoc extract_Streamable()
600     {
601     //debug.log( "extract_Streamable" ) ;
602
return (Streamable JavaDoc)object;
603     }
604
605     ///////////////////////////////////////////////////////////////////////////
606
// insertion/extraction/replacement for all basic types
607

608     /**
609      * See the description of the <a HREF="#anyOps">general Any operations.</a>
610      */

611     public void insert_short(short s)
612     {
613     //debug.log ("insert_short");
614
typeCode = orb.get_primitive_tc(TCKind._tk_short);
615     value = s;
616     isInitialized = true;
617     }
618
619     private String JavaDoc getTCKindName( int tc )
620     {
621     if ((tc >= 0) && (tc < TypeCodeImpl.kindNames.length))
622         return TypeCodeImpl.kindNames[tc] ;
623     else
624         return "UNKNOWN(" + tc + ")" ;
625     }
626
627     private void checkExtractBadOperation( int expected )
628     {
629     if (!isInitialized)
630         throw wrapper.extractNotInitialized() ;
631
632     int tc = realType().kind().value() ;
633     if (tc != expected) {
634         String JavaDoc tcName = getTCKindName( tc ) ;
635         String JavaDoc expectedName = getTCKindName( expected ) ;
636         throw wrapper.extractWrongType( expectedName, tcName ) ;
637     }
638     }
639
640     private void checkExtractBadOperationList( int[] expected )
641     {
642     if (!isInitialized)
643         throw wrapper.extractNotInitialized() ;
644
645     int tc = realType().kind().value() ;
646     for (int ctr=0; ctr<expected.length; ctr++)
647         if (tc == expected[ctr])
648         return ;
649
650     List JavaDoc list = new ArrayList JavaDoc() ;
651     for (int ctr=0; ctr<expected.length; ctr++)
652         list.add( getTCKindName( expected[ctr] ) ) ;
653
654     String JavaDoc tcName = getTCKindName( tc ) ;
655     throw wrapper.extractWrongTypeList( list, tcName ) ;
656     }
657
658     /**
659      * See the description of the <a HREF="#anyOps">general Any operations.</a>
660      */

661     public short extract_short()
662     {
663     //debug.log ("extract_short");
664
checkExtractBadOperation( TCKind._tk_short ) ;
665     return (short)value;
666     }
667
668     /**
669      * See the description of the <a HREF="#anyOps">general Any operations.</a>
670      */

671     public void insert_long(int l)
672     {
673     //debug.log ("insert_long");
674
// A long value is applicable to enums as well, so don't erase the enum type code
675
// in case it was initialized that way before.
676
int kind = realType().kind().value();
677     if (kind != TCKind._tk_long && kind != TCKind._tk_enum) {
678         typeCode = orb.get_primitive_tc(TCKind._tk_long);
679     }
680     value = l;
681     isInitialized = true;
682     }
683
684     /**
685      * See the description of the <a HREF="#anyOps">general Any operations.</a>
686      */

687     public int extract_long()
688     {
689     //debug.log ("extract_long");
690
checkExtractBadOperationList( new int[] { TCKind._tk_long, TCKind._tk_enum } ) ;
691     return (int)value;
692     }
693
694     /**
695      * See the description of the <a HREF="#anyOps">general Any operations.</a>
696      */

697     public void insert_ushort(short s)
698     {
699     //debug.log ("insert_ushort");
700
typeCode = orb.get_primitive_tc(TCKind._tk_ushort);
701     value = s;
702     isInitialized = true;
703     }
704
705     /**
706      * See the description of the <a HREF="#anyOps">general Any operations.</a>
707      */

708     public short extract_ushort()
709     {
710     //debug.log ("extract_ushort");
711
checkExtractBadOperation( TCKind._tk_ushort ) ;
712     return (short)value;
713     }
714
715     /**
716      * See the description of the <a HREF="#anyOps">general Any operations.</a>
717      */

718     public void insert_ulong(int l)
719     {
720     //debug.log ("insert_ulong");
721
typeCode = orb.get_primitive_tc(TCKind._tk_ulong);
722     value = l;
723     isInitialized = true;
724     }
725
726     /**
727      * See the description of the <a HREF="#anyOps">general Any operations.</a>
728      */

729     public int extract_ulong()
730     {
731     //debug.log ("extract_ulong");
732
checkExtractBadOperation( TCKind._tk_ulong ) ;
733     return (int)value;
734     }
735
736     /**
737      * See the description of the <a HREF="#anyOps">general Any operations.</a>
738      */

739     public void insert_float(float f)
740     {
741     //debug.log ("insert_float");
742
typeCode = orb.get_primitive_tc(TCKind._tk_float);
743     value = Float.floatToIntBits(f);
744     isInitialized = true;
745     }
746
747     /**
748      * See the description of the <a HREF="#anyOps">general Any operations.</a>
749      */

750     public float extract_float()
751     {
752     //debug.log ("extract_float");
753
checkExtractBadOperation( TCKind._tk_float ) ;
754     return Float.intBitsToFloat((int)value);
755     }
756
757     /**
758      * See the description of the <a HREF="#anyOps">general Any operations.</a>
759      */

760     public void insert_double(double d)
761     {
762     //debug.log ("insert_double");
763
typeCode = orb.get_primitive_tc(TCKind._tk_double);
764     value = Double.doubleToLongBits(d);
765     isInitialized = true;
766     }
767
768     /**
769      * See the description of the <a HREF="#anyOps">general Any operations.</a>
770      */

771     public double extract_double()
772     {
773     //debug.log ("extract_double");
774
checkExtractBadOperation( TCKind._tk_double ) ;
775     return Double.longBitsToDouble(value);
776     }
777
778     /**
779      * See the description of the <a HREF="#anyOps">general Any operations.</a>
780      */

781     public void insert_longlong(long l)
782     {
783     //debug.log ("insert_longlong");
784
typeCode = orb.get_primitive_tc(TCKind._tk_longlong);
785     value = l;
786     isInitialized = true;
787     }
788
789     /**
790      * See the description of the <a HREF="#anyOps">general Any operations.</a>
791      */

792     public long extract_longlong()
793     {
794     //debug.log ("extract_longlong");
795
checkExtractBadOperation( TCKind._tk_longlong ) ;
796     return value;
797     }
798
799     /**
800      * See the description of the <a HREF="#anyOps">general Any operations.</a>
801      */

802     public void insert_ulonglong(long l)
803     {
804     //debug.log ("insert_ulonglong");
805
typeCode = orb.get_primitive_tc(TCKind._tk_ulonglong);
806     value = l;
807     isInitialized = true;
808     }
809
810     /**
811      * See the description of the <a HREF="#anyOps">general Any operations.</a>
812      */

813     public long extract_ulonglong()
814     {
815     //debug.log ("extract_ulonglong");
816
checkExtractBadOperation( TCKind._tk_ulonglong ) ;
817     return value;
818     }
819
820     /**
821      * See the description of the <a HREF="#anyOps">general Any operations.</a>
822      */

823     public void insert_boolean(boolean b)
824     {
825     //debug.log ("insert_boolean");
826
typeCode = orb.get_primitive_tc(TCKind._tk_boolean);
827     value = (b)? 1:0;
828     isInitialized = true;
829     }
830
831     /**
832      * See the description of the <a HREF="#anyOps">general Any operations.</a>
833      */

834     public boolean extract_boolean()
835     {
836     //debug.log ("extract_boolean");
837
checkExtractBadOperation( TCKind._tk_boolean ) ;
838     return (value == 0)? false: true;
839     }
840
841     /**
842      * See the description of the <a HREF="#anyOps">general Any operations.</a>
843      */

844     public void insert_char(char c)
845     {
846     //debug.log ("insert_char");
847
typeCode = orb.get_primitive_tc(TCKind._tk_char);
848     value = c;
849     isInitialized = true;
850     }
851
852     /**
853      * See the description of the <a HREF="#anyOps">general Any operations.</a>
854      */

855     public char extract_char()
856     {
857     //debug.log ("extract_char");
858
checkExtractBadOperation( TCKind._tk_char ) ;
859     return (char)value;
860     }
861
862     /**
863      * See the description of the <a HREF="#anyOps">general Any operations.</a>
864      */

865     public void insert_wchar(char c)
866     {
867     //debug.log ("insert_wchar");
868
typeCode = orb.get_primitive_tc(TCKind._tk_wchar);
869     value = c;
870     isInitialized = true;
871     }
872
873     /**
874      * See the description of the <a HREF="#anyOps">general Any operations.</a>
875      */

876     public char extract_wchar()
877     {
878     //debug.log ("extract_wchar");
879
checkExtractBadOperation( TCKind._tk_wchar ) ;
880     return (char)value;
881     }
882
883
884     /**
885      * See the description of the <a HREF="#anyOps">general Any operations.</a>
886      */

887     public void insert_octet(byte b)
888     {
889     //debug.log ("insert_octet");
890
typeCode = orb.get_primitive_tc(TCKind._tk_octet);
891     value = b;
892     isInitialized = true;
893     }
894
895     /**
896      * See the description of the <a HREF="#anyOps">general Any operations.</a>
897      */

898     public byte extract_octet()
899     {
900     //debug.log ("extract_octet");
901
checkExtractBadOperation( TCKind._tk_octet ) ;
902     return (byte)value;
903     }
904
905     /**
906      * See the description of the <a HREF="#anyOps">general Any operations.</a>
907      */

908     public void insert_string(String JavaDoc s)
909     {
910     //debug.log ("insert_string");
911
// Make sure type code information for bounded strings is not erased
912
if (typeCode.kind() == TCKind.tk_string) {
913         int length = 0;
914         try {
915         length = typeCode.length();
916         } catch (BadKind JavaDoc bad) {
917         throw wrapper.badkindCannotOccur() ;
918         }
919
920         // Check if bounded strings length is not exceeded
921
if (length != 0 && s != null && s.length() > length) {
922         throw wrapper.badStringBounds( new Integer JavaDoc(s.length()),
923             new Integer JavaDoc(length) ) ;
924         }
925     } else {
926         typeCode = orb.get_primitive_tc(TCKind._tk_string);
927     }
928     object = s;
929     isInitialized = true;
930     }
931
932     /**
933      * See the description of the <a HREF="#anyOps">general Any operations.</a>
934      */

935     public String JavaDoc extract_string()
936     {
937     //debug.log ("extract_string");
938
checkExtractBadOperation( TCKind._tk_string ) ;
939     return (String JavaDoc)object;
940     }
941
942     /**
943      * See the description of the <a HREF="#anyOps">general Any operations.</a>
944      */

945     public void insert_wstring(String JavaDoc s)
946     {
947     //debug.log ("insert_wstring");
948
// Make sure type code information for bounded strings is not erased
949
if (typeCode.kind() == TCKind.tk_wstring) {
950         int length = 0;
951         try {
952         length = typeCode.length();
953         } catch (BadKind JavaDoc bad) {
954         throw wrapper.badkindCannotOccur() ;
955         }
956
957         // Check if bounded strings length is not exceeded
958
if (length != 0 && s != null && s.length() > length) {
959         throw wrapper.badStringBounds( new Integer JavaDoc(s.length()),
960             new Integer JavaDoc(length) ) ;
961         }
962     } else {
963         typeCode = orb.get_primitive_tc(TCKind._tk_wstring);
964     }
965     object = s;
966     isInitialized = true;
967     }
968
969     /**
970      * See the description of the <a HREF="#anyOps">general Any operations.</a>
971      */

972     public String JavaDoc extract_wstring()
973     {
974     //debug.log ("extract_wstring");
975
checkExtractBadOperation( TCKind._tk_wstring ) ;
976     return (String JavaDoc)object;
977     }
978
979     /**
980      * See the description of the <a HREF="#anyOps">general Any operations.</a>
981      */

982     public void insert_any(Any JavaDoc a)
983     {
984     //debug.log ("insert_any");
985
typeCode = orb.get_primitive_tc(TCKind._tk_any);
986     object = a;
987     stream = null;
988     isInitialized = true;
989     }
990
991     /**
992      * See the description of the <a HREF="#anyOps">general Any operations.</a>
993      */

994     public Any JavaDoc extract_any()
995     {
996     //debug.log ("extract_any");
997
checkExtractBadOperation( TCKind._tk_any ) ;
998     return (Any JavaDoc)object;
999     }
1000
1001    /**
1002     * See the description of the <a HREF="#anyOps">general Any operations.</a>
1003     */

1004    public void insert_Object(org.omg.CORBA.Object JavaDoc o)
1005    {
1006    //debug.log ("insert_Object");
1007
if ( o == null ) {
1008        typeCode = orb.get_primitive_tc(TCKind._tk_objref);
1009    } else {
1010        if (StubAdapter.isStub(o)) {
1011        String JavaDoc[] ids = StubAdapter.getTypeIds( o ) ;
1012        typeCode = new TypeCodeImpl(orb, TCKind._tk_objref, ids[0], "");
1013        } else {
1014        throw wrapper.badInsertobjParam(
1015            CompletionStatus.COMPLETED_MAYBE, o.getClass().getName() ) ;
1016        }
1017    }
1018    
1019    object = o;
1020    isInitialized = true;
1021    }
1022
1023    /**
1024     * A variant of the insertion operation that takes a typecode
1025     * argument as well.
1026     */

1027    public void insert_Object(org.omg.CORBA.Object JavaDoc o, TypeCode JavaDoc tc)
1028    {
1029    //debug.log ("insert_Object2");
1030
try {
1031        if ( tc.id().equals("IDL:omg.org/CORBA/Object:1.0") || o._is_a(tc.id()) )
1032        {
1033            typeCode = TypeCodeImpl.convertToNative(orb, tc);
1034            object = o;
1035        }
1036        else {
1037        throw wrapper.insertObjectIncompatible() ;
1038        }
1039    } catch ( Exception JavaDoc ex ) {
1040        throw wrapper.insertObjectFailed(ex) ;
1041    }
1042    isInitialized = true;
1043    }
1044
1045    /**
1046     * See the description of the <a HREF="#anyOps">general Any operations.</a>
1047     */

1048    public org.omg.CORBA.Object JavaDoc extract_Object()
1049    {
1050    //debug.log ("extract_Object");
1051
if (!isInitialized)
1052        throw wrapper.extractNotInitialized() ;
1053
1054    // Check if the object contained here is of the type in typeCode
1055
org.omg.CORBA.Object JavaDoc obj = null;
1056    try {
1057        obj = (org.omg.CORBA.Object JavaDoc) object;
1058        if (typeCode.id().equals("IDL:omg.org/CORBA/Object:1.0") || obj._is_a(typeCode.id())) {
1059        return obj;
1060        } else {
1061        throw wrapper.extractObjectIncompatible() ;
1062        }
1063    } catch ( Exception JavaDoc ex ) {
1064        throw wrapper.extractObjectFailed(ex);
1065    }
1066    }
1067
1068    /**
1069     * See the description of the <a HREF="#anyOps">general Any operations.</a>
1070     */

1071    public void insert_TypeCode(TypeCode JavaDoc tc)
1072    {
1073    //debug.log ("insert_TypeCode");
1074
typeCode = orb.get_primitive_tc(TCKind._tk_TypeCode);
1075    object = tc;
1076    isInitialized = true;
1077    }
1078
1079    /**
1080     * See the description of the <a HREF="#anyOps">general Any operations.</a>
1081     */

1082    public TypeCode JavaDoc extract_TypeCode()
1083    {
1084    //debug.log ("extract_TypeCode");
1085
checkExtractBadOperation( TCKind._tk_TypeCode ) ;
1086    return (TypeCode JavaDoc)object;
1087    }
1088
1089    /**
1090     * @deprecated
1091     */

1092    @Deprecated JavaDoc
1093    public void insert_Principal(Principal JavaDoc p)
1094    {
1095    typeCode = orb.get_primitive_tc(TCKind._tk_Principal);
1096    object = p;
1097    isInitialized = true;
1098    }
1099
1100    /**
1101     * @deprecated
1102     */

1103    @Deprecated JavaDoc
1104    public Principal JavaDoc extract_Principal()
1105    {
1106    checkExtractBadOperation( TCKind._tk_Principal ) ;
1107    return (Principal JavaDoc)object;
1108    }
1109
1110    /**
1111     * Note that the Serializable really should be an IDLEntity of
1112     * some kind. It shouldn't just be an RMI-IIOP type. Currently,
1113     * we accept and will produce RMI repIds with the latest
1114     * calculations if given a non-IDLEntity Serializable.
1115     */

1116    public Serializable JavaDoc extract_Value()
1117    {
1118    //debug.log ("extract_Value");
1119
checkExtractBadOperationList( new int[] { TCKind._tk_value,
1120        TCKind._tk_value_box, TCKind._tk_abstract_interface } ) ;
1121    return (Serializable JavaDoc)object;
1122    }
1123
1124    public void insert_Value(Serializable JavaDoc v)
1125    {
1126    //debug.log ("insert_Value");
1127
object = v;
1128
1129    TypeCode JavaDoc tc;
1130
1131    if ( v == null ) {
1132        tc = orb.get_primitive_tc (TCKind.tk_value);
1133    } else {
1134        // See note in getPrimitiveTypeCodeForClass. We
1135
// have to use the latest type code fixes in this
1136
// case since there is no way to know what ORB will
1137
// actually send this Any. In RMI-IIOP, when using
1138
// Util.writeAny, we can do the versioning correctly,
1139
// and use the insert_Value(Serializable, TypeCode)
1140
// method.
1141
//
1142
// The ORB singleton uses the latest version.
1143
tc = createTypeCodeForClass (v.getClass(), (ORB)ORB.init());
1144    }
1145
1146    typeCode = TypeCodeImpl.convertToNative(orb, tc);
1147    isInitialized = true;
1148    }
1149      
1150    public void insert_Value(Serializable JavaDoc v, org.omg.CORBA.TypeCode JavaDoc t)
1151    {
1152    //debug.log ("insert_Value2");
1153
object = v;
1154    typeCode = TypeCodeImpl.convertToNative(orb, t);
1155    isInitialized = true;
1156    }
1157
1158    public void insert_fixed(java.math.BigDecimal JavaDoc value) {
1159    typeCode = TypeCodeImpl.convertToNative(orb,
1160        orb.create_fixed_tc(TypeCodeImpl.digits(value), TypeCodeImpl.scale(value)));
1161    object = value;
1162    isInitialized = true;
1163    }
1164
1165    public void insert_fixed(java.math.BigDecimal JavaDoc value, org.omg.CORBA.TypeCode JavaDoc type)
1166    {
1167    try {
1168        if (TypeCodeImpl.digits(value) > type.fixed_digits() ||
1169        TypeCodeImpl.scale(value) > type.fixed_scale())
1170        {
1171        throw wrapper.fixedNotMatch() ;
1172        }
1173    } catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk) {
1174        // type isn't even of kind fixed
1175
throw wrapper.fixedBadTypecode( bk ) ;
1176    }
1177    typeCode = TypeCodeImpl.convertToNative(orb, type);
1178    object = value;
1179    isInitialized = true;
1180    }
1181
1182    public java.math.BigDecimal JavaDoc extract_fixed() {
1183    checkExtractBadOperation( TCKind._tk_fixed ) ;
1184    return (BigDecimal JavaDoc)object;
1185    }
1186
1187    /**
1188     * Utility method for insert_Value and Util.writeAny.
1189     *
1190     * The ORB passed in should have the desired ORBVersion. It
1191     * is used to generate the type codes.
1192     */

1193    public TypeCode JavaDoc createTypeCodeForClass (java.lang.Class JavaDoc c, ORB tcORB)
1194    {
1195    // Look in the cache first
1196
TypeCodeImpl classTC = tcORB.getTypeCodeForClass(c);
1197    if (classTC != null)
1198        return classTC;
1199
1200    // All cases need to be able to create repository IDs.
1201
//
1202
// See bug 4391648 for more info about the tcORB in this
1203
// case.
1204
RepositoryIdStrings repStrs
1205        = RepositoryIdFactory.getRepIdStringsFactory(tcORB);
1206
1207
1208    // Assertion: c instanceof Serializable?
1209

1210    if ( c.isArray() ) {
1211        // Arrays - may recurse for multi-dimensional arrays
1212
Class JavaDoc componentClass = c.getComponentType();
1213        TypeCode JavaDoc embeddedType;
1214        if ( componentClass.isPrimitive() ) {
1215        embeddedType = getPrimitiveTypeCodeForClass(componentClass,
1216                                tcORB);
1217        } else {
1218        embeddedType = createTypeCodeForClass (componentClass,
1219                               tcORB);
1220        }
1221        TypeCode JavaDoc t = tcORB.create_sequence_tc (0, embeddedType);
1222
1223        String JavaDoc id = repStrs.createForJavaType(c);
1224
1225        return tcORB.create_value_box_tc (id, "Sequence", t);
1226    } else if ( c == java.lang.String JavaDoc.class ) {
1227        // Strings
1228
TypeCode JavaDoc t = tcORB.create_string_tc (0);
1229
1230        String JavaDoc id = repStrs.createForJavaType(c);
1231
1232        return tcORB.create_value_box_tc (id, "StringValue", t);
1233    }
1234
1235    // Anything else
1236
// We know that this is a TypeCodeImpl since it is our ORB
1237
classTC = (TypeCodeImpl)ValueUtility.createTypeCodeForClass(
1238        tcORB, c, ORBUtility.createValueHandler(tcORB));
1239    // Intruct classTC to store its buffer
1240
classTC.setCaching(true);
1241    // Update the cache
1242
tcORB.setTypeCodeForClass(c, classTC);
1243    return classTC;
1244    }
1245
1246    /**
1247     * It looks like this was copied from io.ValueUtility at some
1248     * point.
1249     *
1250     * It's used by createTypeCodeForClass. The tcORB passed in
1251     * should have the desired ORB version, and is used to
1252     * create the type codes.
1253     */

1254    private TypeCode JavaDoc getPrimitiveTypeCodeForClass (Class JavaDoc c, ORB tcORB)
1255    {
1256    //debug.log ("getPrimitiveTypeCodeForClass");
1257

1258    if (c == Integer.TYPE) {
1259        return tcORB.get_primitive_tc (TCKind.tk_long);
1260    } else if (c == Byte.TYPE) {
1261        return tcORB.get_primitive_tc (TCKind.tk_octet);
1262    } else if (c == Long.TYPE) {
1263        return tcORB.get_primitive_tc (TCKind.tk_longlong);
1264    } else if (c == Float.TYPE) {
1265        return tcORB.get_primitive_tc (TCKind.tk_float);
1266    } else if (c == Double.TYPE) {
1267        return tcORB.get_primitive_tc (TCKind.tk_double);
1268    } else if (c == Short.TYPE) {
1269        return tcORB.get_primitive_tc (TCKind.tk_short);
1270    } else if (c == Character.TYPE) {
1271        // For Merlin or later JDKs, or for foreign ORBs,
1272
// we correctly say that a Java char maps to a
1273
// CORBA wchar. For backwards compatibility
1274
// with our older ORBs, we say it maps to a
1275
// CORBA char. This is only used in RMI-IIOP
1276
// in our javax.rmi.CORBA.Util delegate's
1277
// writeAny method. In Java IDL, there's no way
1278
// to know the ORB version that the Any will be
1279
// sent out with -- it could be different than
1280
// the one used to create the Any -- so we use the
1281
// most recent version (see insert_Value).
1282
if (ORBVersionFactory.getFOREIGN().compareTo(tcORB.getORBVersion()) == 0 ||
1283        ORBVersionFactory.getNEWER().compareTo(tcORB.getORBVersion()) <= 0)
1284        return tcORB.get_primitive_tc(TCKind.tk_wchar);
1285        else
1286        return tcORB.get_primitive_tc(TCKind.tk_char);
1287    } else if (c == Boolean.TYPE) {
1288        return tcORB.get_primitive_tc (TCKind.tk_boolean);
1289    } else {
1290        // _REVISIT_ Not sure if this is right.
1291
return tcORB.get_primitive_tc (TCKind.tk_any);
1292    }
1293    }
1294
1295    // Extracts a member value according to the given TypeCode from the given complex Any
1296
// (at the Anys current internal stream position, consuming the anys stream on the way)
1297
// and returns it wrapped into a new Any
1298
public Any JavaDoc extractAny(TypeCode JavaDoc memberType, ORB orb) {
1299        Any JavaDoc returnValue = orb.create_any();
1300        OutputStream JavaDoc out = returnValue.create_output_stream();
1301        TypeCodeImpl.convertToNative(orb, memberType).copy((InputStream JavaDoc)stream, out);
1302        returnValue.read_value(out.create_input_stream(), memberType);
1303        return returnValue;
1304    }
1305
1306    // This method could very well be moved into TypeCodeImpl or a common utility class,
1307
// but is has to be in this package.
1308
static public Any JavaDoc extractAnyFromStream(TypeCode JavaDoc memberType, InputStream JavaDoc input, ORB orb) {
1309        Any JavaDoc returnValue = orb.create_any();
1310        OutputStream JavaDoc out = returnValue.create_output_stream();
1311        TypeCodeImpl.convertToNative(orb, memberType).copy(input, out);
1312        returnValue.read_value(out.create_input_stream(), memberType);
1313        return returnValue;
1314    }
1315
1316    // There is no other way for DynAnys to find out whether the Any is initialized.
1317
public boolean isInitialized() {
1318        return isInitialized;
1319    }
1320}
1321
1322
Popular Tags