KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > dynamicany > DynAnyUtil


1 /*
2  * @(#)DynAnyUtil.java 1.12 03/12/19
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.dynamicany;
9
10 import org.omg.CORBA.Any JavaDoc;
11 import org.omg.CORBA.TypeCode JavaDoc;
12 import org.omg.CORBA.TCKind JavaDoc;
13 import org.omg.CORBA.portable.OutputStream JavaDoc;
14 //import org.omg.CORBA.ORBPackage.*;
15
import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
16 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
17 import org.omg.CORBA.portable.InputStream JavaDoc;
18 import org.omg.DynamicAny.*;
19 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc;
20 import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc;
21 import java.math.BigDecimal JavaDoc;
22 import com.sun.corba.se.impl.corba.AnyImpl;
23
24 import com.sun.corba.se.spi.orb.ORB ;
25 import com.sun.corba.se.spi.logging.CORBALogDomains ;
26 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
27
28 public class DynAnyUtil
29 {
30     static boolean isConsistentType(TypeCode JavaDoc typeCode) {
31         int kind = typeCode.kind().value();
32         return (kind != TCKind._tk_Principal &&
33                 kind != TCKind._tk_native &&
34                 kind != TCKind._tk_abstract_interface);
35     }
36
37     static boolean isConstructedDynAny(DynAny dynAny) {
38         // DynFixed is constructed but not a subclass of DynAnyConstructedImpl
39
//return (dynAny instanceof DynAnyConstructedImpl);
40
int kind = dynAny.type().kind().value();
41         return (kind == TCKind._tk_sequence ||
42                 kind == TCKind._tk_struct ||
43                 kind == TCKind._tk_array ||
44                 kind == TCKind._tk_union ||
45                 kind == TCKind._tk_enum ||
46                 kind == TCKind._tk_fixed ||
47                 kind == TCKind._tk_value ||
48                 kind == TCKind._tk_value_box);
49     }
50
51     static DynAny createMostDerivedDynAny(Any JavaDoc any, ORB orb, boolean copyValue)
52         throws org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc
53     {
54         if (any == null || ! DynAnyUtil.isConsistentType(any.type()))
55             throw new org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc();
56
57         switch (any.type().kind().value()) {
58             case TCKind._tk_sequence:
59                 return new DynSequenceImpl(orb, any, copyValue);
60             case TCKind._tk_struct:
61                 return new DynStructImpl(orb, any, copyValue);
62             case TCKind._tk_array:
63                 return new DynArrayImpl(orb, any, copyValue);
64             case TCKind._tk_union:
65                 return new DynUnionImpl(orb, any, copyValue);
66             case TCKind._tk_enum:
67                 return new DynEnumImpl(orb, any, copyValue);
68             case TCKind._tk_fixed:
69                 return new DynFixedImpl(orb, any, copyValue);
70             case TCKind._tk_value:
71                 return new DynValueImpl(orb, any, copyValue);
72             case TCKind._tk_value_box:
73                 return new DynValueBoxImpl(orb, any, copyValue);
74             default:
75                 return new DynAnyBasicImpl(orb, any, copyValue);
76         }
77     }
78
79     static DynAny createMostDerivedDynAny(TypeCode JavaDoc typeCode, ORB orb)
80         throws org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc
81     {
82         if (typeCode == null || ! DynAnyUtil.isConsistentType(typeCode))
83             throw new org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc();
84
85         switch (typeCode.kind().value()) {
86             case TCKind._tk_sequence:
87                 return new DynSequenceImpl(orb, typeCode);
88             case TCKind._tk_struct:
89                 return new DynStructImpl(orb, typeCode);
90             case TCKind._tk_array:
91                 return new DynArrayImpl(orb, typeCode);
92             case TCKind._tk_union:
93                 return new DynUnionImpl(orb, typeCode);
94             case TCKind._tk_enum:
95                 return new DynEnumImpl(orb, typeCode);
96             case TCKind._tk_fixed:
97                 return new DynFixedImpl(orb, typeCode);
98             case TCKind._tk_value:
99                 return new DynValueImpl(orb, typeCode);
100             case TCKind._tk_value_box:
101                 return new DynValueBoxImpl(orb, typeCode);
102             default:
103                 return new DynAnyBasicImpl(orb, typeCode);
104         }
105     }
106
107     // Extracts a member value according to the given TypeCode from the given complex Any
108
// (at the Anys current internal stream position, consuming the anys stream on the way)
109
// and returns it wrapped into a new Any
110
/*
111     static Any extractAnyFromAny(TypeCode memberType, Any any, ORB orb) {
112         // Moved this functionality into AnyImpl because it is needed for Any.equal()
113         return ((AnyImpl)any).extractAny(memberType, orb);
114     }
115 */

116
117     // Extracts a member value according to the given TypeCode from the given complex Any
118
// (at the Anys current internal stream position, consuming the anys stream on the way)
119
// and returns it wrapped into a new Any
120
static Any JavaDoc extractAnyFromStream(TypeCode JavaDoc memberType, InputStream JavaDoc input, ORB orb) {
121         return AnyImpl.extractAnyFromStream(memberType, input, orb);
122     }
123
124     // Creates a default Any of the given type.
125
static Any JavaDoc createDefaultAnyOfType(TypeCode JavaDoc typeCode, ORB orb) {
126     ORBUtilSystemException wrapper = ORBUtilSystemException.get( orb,
127         CORBALogDomains.RPC_PRESENTATION ) ;
128
129         Any JavaDoc returnValue = orb.create_any();
130         // The spec for DynAny differs from Any on initialization via type code:
131
// - false for boolean
132
// - zero for numeric types
133
// - zero for types octet, char, and wchar
134
// - the empty string for string and wstring
135
// - nil for object references
136
// - a type code with a TCKind value of tk_null for type codes
137
// - for Any values, an Any containing a type code with a TCKind value of tk_null
138
// type and no value
139
switch (typeCode.kind().value()) {
140             case TCKind._tk_boolean:
141                 // false for boolean
142
returnValue.insert_boolean(false);
143                 break;
144             case TCKind._tk_short:
145                 // zero for numeric types
146
returnValue.insert_short((short)0);
147                 break;
148             case TCKind._tk_ushort:
149                 // zero for numeric types
150
returnValue.insert_ushort((short)0);
151                 break;
152             case TCKind._tk_long:
153                 // zero for numeric types
154
returnValue.insert_long(0);
155                 break;
156             case TCKind._tk_ulong:
157                 // zero for numeric types
158
returnValue.insert_ulong(0);
159                 break;
160             case TCKind._tk_longlong:
161                 // zero for numeric types
162
returnValue.insert_longlong((long)0);
163                 break;
164             case TCKind._tk_ulonglong:
165                 // zero for numeric types
166
returnValue.insert_ulonglong((long)0);
167                 break;
168             case TCKind._tk_float:
169                 // zero for numeric types
170
returnValue.insert_float((float)0.0);
171                 break;
172             case TCKind._tk_double:
173                 // zero for numeric types
174
returnValue.insert_double((double)0.0);
175                 break;
176             case TCKind._tk_octet:
177                 // zero for types octet, char, and wchar
178
returnValue.insert_octet((byte)0);
179                 break;
180             case TCKind._tk_char:
181                 // zero for types octet, char, and wchar
182
returnValue.insert_char((char)0);
183                 break;
184             case TCKind._tk_wchar:
185                 // zero for types octet, char, and wchar
186
returnValue.insert_wchar((char)0);
187                 break;
188             case TCKind._tk_string:
189                 // the empty string for string and wstring
190
// Make sure that type code for bounded strings gets respected
191
returnValue.type(typeCode);
192                 // Doesn't erase the type of bounded string
193
returnValue.insert_string("");
194                 break;
195             case TCKind._tk_wstring:
196                 // the empty string for string and wstring
197
// Make sure that type code for bounded strings gets respected
198
returnValue.type(typeCode);
199                 // Doesn't erase the type of bounded string
200
returnValue.insert_wstring("");
201                 break;
202             case TCKind._tk_objref:
203                 // nil for object references
204
returnValue.insert_Object(null);
205                 break;
206             case TCKind._tk_TypeCode:
207                 // a type code with a TCKind value of tk_null for type codes
208
// We can reuse the type code that's already in the any.
209
returnValue.insert_TypeCode(returnValue.type());
210                 break;
211             case TCKind._tk_any:
212                 // for Any values, an Any containing a type code with a TCKind value
213
// of tk_null type and no value.
214
// This is exactly what the default AnyImpl constructor provides.
215
// _REVISIT_ Note that this inner Any is considered uninitialized.
216
returnValue.insert_any(orb.create_any());
217                 break;
218             case TCKind._tk_struct:
219             case TCKind._tk_union:
220             case TCKind._tk_enum:
221             case TCKind._tk_sequence:
222             case TCKind._tk_array:
223             case TCKind._tk_except:
224             case TCKind._tk_value:
225             case TCKind._tk_value_box:
226                 // There are no default value for complex types since there is no
227
// concept of a hierarchy of Anys. Only DynAnys can be arrange in
228
// a hierarchy to mirror the TypeCode hierarchy.
229
// See DynAnyConstructedImpl.initializeComponentsFromTypeCode()
230
// on how this DynAny hierarchy is created from TypeCodes.
231
returnValue.type(typeCode);
232                 break;
233             case TCKind._tk_fixed:
234                 returnValue.insert_fixed(new BigDecimal JavaDoc("0.0"), typeCode);
235                 break;
236             case TCKind._tk_native:
237             case TCKind._tk_alias:
238             case TCKind._tk_void:
239             case TCKind._tk_Principal:
240             case TCKind._tk_abstract_interface:
241                 returnValue.type(typeCode);
242                 break;
243             case TCKind._tk_null:
244                 // Any is already initialized to null
245
break;
246             case TCKind._tk_longdouble:
247                 // Unspecified for Java
248
throw wrapper.tkLongDoubleNotSupported() ;
249             default:
250         throw wrapper.typecodeNotSupported() ;
251         }
252         return returnValue;
253     }
254 /*
255     static Any setTypeOfAny(TypeCode typeCode, Any value) {
256         if (value != null) {
257             value.read_value(value.create_input_stream(), typeCode);
258         }
259         return value;
260     }
261 */

262     static Any JavaDoc copy(Any JavaDoc inAny, ORB orb) {
263         return new AnyImpl(orb, inAny);
264     }
265
266 /*
267     static Any copy(Any inAny, ORB orb) {
268         Any outAny = null;
269         if (inAny != null && orb != null) {
270             outAny = orb.create_any();
271             outAny.read_value(inAny.create_input_stream(), inAny.type());
272             // isInitialized is set to true
273         }
274         return outAny;
275     }
276 */

277
278     static DynAny convertToNative(DynAny dynAny, ORB orb) {
279         if (dynAny instanceof DynAnyImpl) {
280             return dynAny;
281         } else {
282             // if copy flag wasn't true we would be using our DynAny with
283
// a foreign Any in it.
284
try {
285                 return createMostDerivedDynAny(dynAny.to_any(), orb, true);
286             } catch (InconsistentTypeCode JavaDoc ictc) {
287                 return null;
288             }
289         }
290     }
291
292     static boolean isInitialized(Any JavaDoc any) {
293         // Returning simply the value of Any.isInitialized() is not enough.
294
// The DynAny spec says that Anys containing null strings do not contain
295
// a "legal value" (see ptc 99-10-07, 9.2.3.3)
296
boolean isInitialized = ((AnyImpl)any).isInitialized();
297         switch (any.type().kind().value()) {
298             case TCKind._tk_string:
299                 return (isInitialized && (any.extract_string() != null));
300             case TCKind._tk_wstring:
301                 return (isInitialized && (any.extract_wstring() != null));
302         }
303         return isInitialized;
304     }
305
306     // This is a convenient method to reset the current component to where it was
307
// before we changed it. See DynAnyConstructedImpl.equal for use.
308
static boolean set_current_component(DynAny dynAny, DynAny currentComponent) {
309         if (currentComponent != null) {
310             try {
311                 dynAny.rewind();
312                 do {
313                     if (dynAny.current_component() == currentComponent)
314                         return true;
315                 } while (dynAny.next());
316             } catch (TypeMismatch JavaDoc tm) { /* impossible */ }
317         }
318         return false;
319     }
320 }
321
Popular Tags