KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > db > simple > offers > AnyValue


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.db.simple.offers;
15
16 import java.io.*;
17 import java.util.*;
18 import org.omg.CORBA.*;
19 import org.omg.DynamicAny.*;
20 import org.omg.DynamicAny.DynSequence JavaDoc;
21
22 public class AnyValue implements Serializable
23 {
24     private TypeCodeValue m_type;
25     private java.lang.Object JavaDoc m_value;
26     static final long serialVersionUID = 3945922728443512828L;
27
28     private transient org.omg.DynamicAny.DynAnyFactory JavaDoc factory;
29     private transient org.omg.CORBA.ORB JavaDoc orb;
30     private transient boolean initialized = false;
31
32     private AnyValue()
33     {
34     }
35
36
37     public AnyValue(org.omg.CORBA.ORB JavaDoc orb, Any any)
38     {
39     init( orb );
40     setValue(any);
41     }
42
43     public void init(org.omg.CORBA.ORB JavaDoc orb)
44     {
45     this.orb = orb;
46     try
47     {
48         factory = DynAnyFactoryHelper.narrow(orb.resolve_initial_references("DynAnyFactory"));
49     }
50     catch( Exception JavaDoc e )
51     {
52         e.printStackTrace();
53     }
54     initialized = true;
55     }
56
57
58     /** Returns true if we can store a value of the given type */
59     public static boolean isTypeSupported(org.omg.CORBA.TypeCode JavaDoc type)
60     {
61     boolean result = false;
62
63     try {
64         TypeCode tc = type;
65
66         while (tc.kind() == TCKind.tk_alias)
67         tc = tc.content_type();
68
69         TCKind kind = tc.kind();
70
71         if (kind == TCKind.tk_null || kind == TCKind.tk_void)
72         result = true;
73         else {
74         if (kind == TCKind.tk_sequence) {
75             TypeCode contentType = tc.content_type();
76             kind = contentType.kind();
77         }
78
79         switch (kind.value()) {
80         case TCKind._tk_short:
81         case TCKind._tk_long:
82         case TCKind._tk_ushort:
83         case TCKind._tk_ulong:
84         case TCKind._tk_float:
85         case TCKind._tk_double:
86         case TCKind._tk_boolean:
87         case TCKind._tk_char:
88         case TCKind._tk_string:
89         case TCKind._tk_octet:
90             result = true;
91             break;
92         }
93         }
94     }
95     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e)
96     {
97         // ignore
98
}
99     catch (BAD_OPERATION e)
100     {
101         // ignore
102
}
103     return result;
104     }
105
106
107     public Any getValue()
108     {
109     if( !initialized )
110         throw new RuntimeException JavaDoc("AnyVale not initialized!");
111     Any result = null;
112     TypeCode origType = m_type.getValue();
113
114     boolean isSequence = false;
115
116     try {
117         TypeCode tc = origType;
118         while (tc.kind() == TCKind.tk_alias)
119         tc = tc.content_type();
120
121         TCKind kind = tc.kind();
122
123         if (kind == TCKind.tk_sequence) {
124         TypeCode content = tc.content_type();
125         kind = content.kind();
126         isSequence = true;
127         }
128
129         switch (kind.value()) {
130         case TCKind._tk_null:
131         case TCKind._tk_void:
132         result = orb.create_any();
133         result.type(origType);
134         break;
135
136         case TCKind._tk_short: {
137         if (isSequence) {
138             short[] arr = (short[])m_value;
139             org.omg.DynamicAny.DynSequence JavaDoc ds = (DynSequence JavaDoc)factory.create_dyn_any_from_type_code(origType);
140             ds.set_length(arr.length);
141             ds.rewind();
142             for (int i = 0; i < arr.length; ds.next(),i++)
143             ds.current_component().insert_short(arr[i]);
144             result = ds.to_any();
145         }
146         else {
147             Short JavaDoc val = (Short JavaDoc)m_value;
148             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
149             da.insert_short(val.shortValue());
150             result = da.to_any();
151         }
152         }
153         break;
154
155         case TCKind._tk_long: {
156         if (isSequence) {
157             int[] arr = (int[])m_value;
158             org.omg.DynamicAny.DynSequence JavaDoc ds = (DynSequence JavaDoc)factory.create_dyn_any_from_type_code(origType);
159             ds.set_length(arr.length);
160             ds.rewind();
161             for (int i = 0; i < arr.length;ds.next(), i++)
162             ds.current_component().insert_long(arr[i]);
163             result = ds.to_any();
164         }
165         else {
166             Integer JavaDoc val = (Integer JavaDoc)m_value;
167             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
168             da.insert_long(val.intValue());
169             result = da.to_any();
170         }
171         }
172         break;
173
174         case TCKind._tk_ushort: {
175         if (isSequence) {
176             short[] arr = (short[])m_value;
177             org.omg.DynamicAny.DynSequence JavaDoc ds = (DynSequence JavaDoc)factory.create_dyn_any_from_type_code(origType);
178             ds.set_length(arr.length);
179             ds.rewind();
180             for (int i = 0; i < arr.length; ds.next(),i++)
181             ds.current_component().insert_ushort(arr[i]);
182             result = ds.to_any();
183         }
184         else {
185             Short JavaDoc val = (Short JavaDoc)m_value;
186             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
187             da.insert_ushort(val.shortValue());
188             result = da.to_any();
189         }
190         }
191         break;
192
193         case TCKind._tk_ulong: {
194         if (isSequence) {
195             int[] arr = (int[])m_value;
196             org.omg.DynamicAny.DynSequence JavaDoc ds = (DynSequence JavaDoc)factory.create_dyn_any_from_type_code(origType);
197             ds.set_length(arr.length);
198             ds.rewind();
199             for (int i = 0; i < arr.length; ds.next(),i++)
200             ds.current_component().insert_ulong(arr[i]);
201             result = ds.to_any();
202         }
203         else {
204             Integer JavaDoc val = (Integer JavaDoc)m_value;
205             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
206             da.insert_ulong(val.intValue());
207             result = da.to_any();
208         }
209         }
210         break;
211
212         case TCKind._tk_float: {
213         if (isSequence) {
214             float[] arr = (float[])m_value;
215             org.omg.DynamicAny.DynSequence JavaDoc ds = (DynSequence JavaDoc)factory.create_dyn_any_from_type_code(origType);
216             ds.set_length(arr.length);
217             ds.rewind();
218             for (int i = 0; i < arr.length;ds.next(), i++)
219             ds.current_component().insert_float(arr[i]);
220             result = ds.to_any();
221         }
222         else {
223             Float JavaDoc val = (Float JavaDoc)m_value;
224             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
225             da.insert_float(val.floatValue());
226             result = da.to_any();
227         }
228         }
229         break;
230
231         case TCKind._tk_double: {
232         if (isSequence) {
233             double[] arr = (double[])m_value;
234             org.omg.DynamicAny.DynSequence JavaDoc ds = (DynSequence JavaDoc)factory.create_dyn_any_from_type_code(origType);
235             ds.set_length(arr.length);
236             ds.rewind();
237             for (int i = 0; i < arr.length;ds.next(), i++)
238             ds.current_component().insert_double(arr[i]);
239             result = ds.to_any();
240         }
241         else {
242             Double JavaDoc val = (Double JavaDoc)m_value;
243             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
244             da.insert_double(val.doubleValue());
245             result = da.to_any();
246         }
247         }
248         break;
249
250         case TCKind._tk_boolean: {
251         if (isSequence) {
252             boolean[] arr = (boolean[])m_value;
253             org.omg.DynamicAny.DynSequence JavaDoc ds = (DynSequence JavaDoc)factory.create_dyn_any_from_type_code(origType);
254             ds.set_length(arr.length);
255             ds.rewind();
256             for (int i = 0; i < arr.length;ds.next(), i++)
257             ds.current_component().insert_boolean(arr[i]);
258             result = ds.to_any();
259         }
260         else {
261             Boolean JavaDoc val = (Boolean JavaDoc)m_value;
262             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
263             da.insert_boolean(val.booleanValue());
264             result = da.to_any();
265         }
266         }
267         break;
268
269         case TCKind._tk_char: {
270         if (isSequence) {
271             char[] arr = (char[])m_value;
272             org.omg.DynamicAny.DynSequence JavaDoc ds =(DynSequence JavaDoc) factory.create_dyn_any_from_type_code(origType);
273             ds.set_length(arr.length);
274             ds.rewind();
275             for (int i = 0; i < arr.length;ds.next(), i++)
276             ds.current_component().insert_char(arr[i]);
277             result = ds.to_any();
278         }
279         else {
280             Character JavaDoc val = (Character JavaDoc)m_value;
281             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
282             da.insert_char(val.charValue());
283             result = da.to_any();
284         }
285         }
286         break;
287
288         case TCKind._tk_string: {
289         if (isSequence) {
290             String JavaDoc[] arr = (String JavaDoc[])m_value;
291             org.omg.DynamicAny.DynSequence JavaDoc ds = (DynSequence JavaDoc)factory.create_dyn_any_from_type_code(origType);
292             ds.set_length(arr.length);
293             ds.rewind();
294             for (int i = 0; i < arr.length;ds.next(), i++)
295             ds.current_component().insert_string(arr[i]);
296             result = ds.to_any();
297         }
298         else {
299             String JavaDoc val = (String JavaDoc)m_value;
300             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
301             da.insert_string(val);
302             result = da.to_any();
303         }
304         }
305         break;
306
307         case TCKind._tk_octet: {
308         if (isSequence) {
309             byte[] arr = (byte[])m_value;
310             org.omg.DynamicAny.DynSequence JavaDoc ds = (DynSequence JavaDoc)factory.create_dyn_any_from_type_code(origType);
311             ds.set_length(arr.length);
312             ds.rewind();
313             for (int i = 0; i < arr.length; ds.next(), i++)
314             ds.current_component().insert_octet(arr[i]);
315             result = ds.to_any();
316         }
317         else {
318             Byte JavaDoc val = (Byte JavaDoc)m_value;
319             org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any_from_type_code(origType);
320             da.insert_octet(val.byteValue());
321             result = da.to_any();
322         }
323         }
324         break;
325         }
326     }
327     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
328         throw new RuntimeException JavaDoc(e.getMessage());
329     }
330     catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc e) {
331         throw new RuntimeException JavaDoc(e.getMessage());
332     }
333     catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc e) {
334         throw new RuntimeException JavaDoc(e.getMessage());
335     }
336     catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc e) {
337         throw new RuntimeException JavaDoc(e.getMessage());
338     }
339     //**//
340
return result;
341     }
342
343
344     protected void setValue(Any val)
345     {
346     if( !initialized )
347         throw new RuntimeException JavaDoc("AnyVale not initialized!");
348
349     TypeCode origType = val.type();
350     m_type = new TypeCodeValue(origType);
351
352     boolean isSequence = false;
353
354     try {
355         TypeCode tc = origType;
356         while (tc.kind() == TCKind.tk_alias)
357         tc = tc.content_type();
358
359         TCKind kind = tc.kind();
360
361         if (kind == TCKind.tk_sequence) {
362         TypeCode content = tc.content_type();
363         kind = content.kind();
364         isSequence = true;
365         }
366
367         org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any(val);
368
369         switch (kind.value()) {
370         case TCKind._tk_null:
371         m_value = null;
372         break;
373
374         case TCKind._tk_void:
375         m_value = null;
376         break;
377
378         case TCKind._tk_short: {
379         if (isSequence) {
380             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
381             int len = ds.get_length();
382             short[] arr = new short[len];
383             for (int i = 0; i < len; i++, ds.next())
384             arr[i] = ds.current_component().get_short();
385             m_value = arr;
386         }
387         else
388             m_value = new Short JavaDoc(da.get_short());
389         }
390         break;
391
392         case TCKind._tk_long: {
393         if (isSequence) {
394             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
395             int len = ds.get_length();
396             int[] arr = new int[len];
397             for (int i = 0; i < len; i++, ds.next())
398             arr[i] = ds.current_component().get_long();
399             m_value = arr;
400         }
401         else
402             m_value = new Integer JavaDoc(da.get_long());
403         }
404         break;
405
406         case TCKind._tk_ushort: {
407         if (isSequence) {
408             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
409             int len = ds.get_length();
410             short[] arr = new short[len];
411             for (int i = 0; i < len; i++, ds.next())
412             arr[i] = ds.current_component().get_ushort();
413             m_value = arr;
414         }
415         else
416             m_value = new Short JavaDoc(da.get_ushort());
417         }
418         break;
419
420         case TCKind._tk_ulong: {
421         if (isSequence) {
422             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
423             int len = ds.get_length();
424             int[] arr = new int[len];
425             for (int i = 0; i < len; i++, ds.next())
426             arr[i] = ds.current_component().get_ulong();
427             m_value = arr;
428         }
429         else
430             m_value = new Integer JavaDoc(da.get_ulong());
431         }
432         break;
433
434         case TCKind._tk_float: {
435         if (isSequence) {
436             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
437             int len = ds.get_length();
438             float[] arr = new float[len];
439             for (int i = 0; i < len; i++, ds.next())
440             arr[i] = ds.current_component().get_float();
441             m_value = arr;
442         }
443         else
444             m_value = new Float JavaDoc(da.get_float());
445         }
446         break;
447
448         case TCKind._tk_double: {
449         if (isSequence) {
450             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
451             int len = ds.get_length();
452             double[] arr = new double[len];
453             for (int i = 0; i < len; i++, ds.next())
454             arr[i] = ds.current_component().get_double();
455             m_value = arr;
456         }
457         else
458             m_value = new Double JavaDoc(da.get_double());
459         }
460         break;
461
462         case TCKind._tk_boolean: {
463         if (isSequence) {
464             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
465             int len = ds.get_length();
466             boolean[] arr = new boolean[len];
467             for (int i = 0; i < len; i++, ds.next())
468             arr[i] = ds.current_component().get_boolean();
469             m_value = arr;
470         }
471         else
472             m_value = new Boolean JavaDoc(da.get_boolean());
473         }
474         break;
475
476         case TCKind._tk_char: {
477         if (isSequence) {
478             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
479             int len = ds.get_length();
480             char[] arr = new char[len];
481             for (int i = 0; i < len; i++, ds.next())
482             arr[i] = ds.current_component().get_char();
483             m_value = arr;
484         }
485         else
486             m_value = new Character JavaDoc(da.get_char());
487         }
488         break;
489
490         case TCKind._tk_string: {
491         if (isSequence) {
492             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
493             int len = ds.get_length();
494             String JavaDoc[] arr = new String JavaDoc[len];
495             for (int i = 0; i < len; i++, ds.next())
496             arr[i] = ds.current_component().get_string();
497             m_value = arr;
498         }
499         else
500             m_value = da.get_string();
501         }
502         break;
503
504         case TCKind._tk_octet: {
505         if (isSequence) {
506             org.omg.DynamicAny.DynSequence JavaDoc ds = DynSequenceHelper.narrow(da);
507             int len = ds.get_length();
508             byte[] arr = new byte[len];
509             for (int i = 0; i < len; i++, ds.next())
510             arr[i] = ds.current_component().get_octet();
511             m_value = arr;
512         }
513         else
514             m_value = new Byte JavaDoc(da.get_octet());
515         }
516         break;
517
518         default:
519         throw new RuntimeException JavaDoc("Unsupported type");
520         }
521
522         da.destroy();
523     }
524     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
525         throw new RuntimeException JavaDoc(e.getMessage());
526     }
527     // GB: name changes
528
catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc e) {
529         throw new RuntimeException JavaDoc(e.getMessage());
530     }
531     catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc e) {
532         throw new RuntimeException JavaDoc(e.getMessage());
533     }
534     catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc e) {
535         throw new RuntimeException JavaDoc(e.getMessage());
536     }
537     catch (BAD_OPERATION e) {
538         throw new RuntimeException JavaDoc();
539     }
540     }
541
542     private void readObject(ObjectInputStream in)
543     throws IOException, ClassNotFoundException JavaDoc
544     {
545     in.defaultReadObject();
546     init( org.jacorb.trading.TradingService.getORB());
547     }
548
549
550 }
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
Popular Tags