KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > rmi > iiop > Any


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.rmi.iiop;
19
20 import org.omg.CORBA.TCKind JavaDoc;
21
22 /**
23  * * An implementation of CORBA 'any'.
24  */

25 public class Any
26     extends org.omg.CORBA.Any JavaDoc
27 {
28     private org.omg.CORBA.TypeCode JavaDoc _type;
29
30     private byte[] _data;
31
32     /**
33      * @param tk
34      * @param what
35      */

36     private void extract_type(TCKind JavaDoc tk, String JavaDoc what)
37     {
38         if (_type.kind().value() != tk.value())
39         {
40             throw new org.omg.CORBA.BAD_TYPECODE JavaDoc("org.apache.geronimo.interop.rmi.iiop.Any.extract_"
41                 + what + ": type = " + _type);
42         }
43     }
44
45     /**
46      *
47      */

48     public Any()
49     {
50         _type = TypeCode.NULL;
51     }
52
53     /**
54      * @param a
55      * @return
56      */

57     public boolean equal(org.omg.CORBA.Any JavaDoc a)
58     {
59         if (!_type.equal(a.type()))
60         {
61             return false;
62         }
63         if (a instanceof org.apache.geronimo.interop.rmi.iiop.Any)
64         {
65             org.apache.geronimo.interop.rmi.iiop.Any _that = (org.apache.geronimo.interop.rmi.iiop.Any) a;
66             String JavaDoc x = org.apache.geronimo.interop.util.Base16Binary.toString(this._data);
67             String JavaDoc y = org.apache.geronimo.interop.util.Base16Binary.toString(_that._data);
68             return x.equals(y);
69         }
70         else
71         {
72             // TODO: implement equality testing with other ORB's 'any' values
73
throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc("org.apache.geronimo.interop.rmi.iiop.Any.equal("
74                 + a.getClass().getName() + ")");
75         }
76     }
77
78     /**
79      * @return
80      */

81     public org.omg.CORBA.TypeCode JavaDoc type()
82     {
83         return _type;
84     }
85
86     /**
87      * @param type
88      */

89     public void type(org.omg.CORBA.TypeCode JavaDoc type)
90     {
91         _type = type;
92     }
93
94     /**
95      * @param input
96      * @param type
97      */

98     public void read_value(org.omg.CORBA.portable.InputStream JavaDoc input,
99                            org.omg.CORBA.TypeCode JavaDoc type)
100     {
101         byte[] buffer = ((CdrInputStream) input)._buffer;
102         int length = ((CdrInputStream) input)._length;
103         _type = type;
104         _data = new byte[length];
105         System.arraycopy(buffer, 0, _data, 0, length);
106     }
107
108     /**
109      * @param output
110      */

111     public void write_value(org.omg.CORBA.portable.OutputStream JavaDoc output)
112     {
113         // A no-op in this implementation.
114
}
115
116     /**
117      * @return
118      */

119     public org.omg.CORBA.portable.OutputStream JavaDoc create_output_stream()
120     {
121         _data = null;
122         return CdrOutputStream.getInstance();
123     }
124
125     /**
126      * @return
127      */

128     public org.omg.CORBA.portable.InputStream JavaDoc create_input_stream()
129     {
130         if (_data == null)
131         {
132             throw new org.omg.CORBA.BAD_OPERATION JavaDoc(
133                 "org.apache.geronimo.interop.rmi.Any.create_input_stream");
134         }
135         return CdrInputStream.getInstance();
136     }
137
138     /**
139      * @return
140      */

141     public short extract_short()
142     {
143         extract_type(TCKind.tk_short, "short");
144         return create_input_stream().read_short();
145     }
146
147     /**
148      * @param value
149      */

150     public void insert_short(short value)
151     {
152         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
153         output.write_short(value);
154         read_value(null, TypeCode.SHORT);
155     }
156
157     /**
158      * @return
159      */

160     public int extract_long()
161     {
162         extract_type(TCKind.tk_long, "long");
163         return create_input_stream().read_long();
164     }
165
166     /**
167      * @param value
168      */

169     public void insert_long(int value)
170     {
171         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
172         output.write_long(value);
173         read_value(null, TypeCode.LONG);
174     }
175
176     /**
177      * @return
178      */

179     public long extract_longlong()
180     {
181         extract_type(TCKind.tk_longlong, "longlong");
182         return create_input_stream().read_longlong();
183     }
184
185     /**
186      * @param value
187      */

188     public void insert_longlong(long value)
189     {
190         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
191         output.write_longlong(value);
192         read_value(null, TypeCode.LONGLONG);
193     }
194
195     /**
196      * @return
197      */

198     public short extract_ushort()
199     {
200         extract_type(TCKind.tk_ushort, "ushort");
201         return create_input_stream().read_ushort();
202     }
203
204     /**
205      * @param value
206      */

207     public void insert_ushort(short value)
208     {
209         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
210         output.write_ushort(value);
211         read_value(null, TypeCode.USHORT);
212     }
213
214     /**
215      * @return
216      */

217     public int extract_ulong()
218     {
219         extract_type(TCKind.tk_ulong, "ulong");
220         return create_input_stream().read_ulong();
221     }
222
223     /**
224      * @param value
225      */

226     public void insert_ulong(int value)
227     {
228         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
229         output.write_ulong(value);
230         read_value(null, TypeCode.ULONG);
231     }
232
233     /**
234      * @return
235      */

236     public long extract_ulonglong()
237     {
238         extract_type(TCKind.tk_ulonglong, "ulonglong");
239         return create_input_stream().read_ulonglong();
240     }
241
242     /**
243      * @param value
244      */

245     public void insert_ulonglong(long value)
246     {
247         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
248         output.write_ulonglong(value);
249         read_value(null, TypeCode.ULONGLONG);
250     }
251
252     /**
253      * @return
254      */

255     public float extract_float()
256     {
257         extract_type(TCKind.tk_float, "float");
258         return create_input_stream().read_float();
259     }
260
261     /**
262      * @param value
263      */

264     public void insert_float(float value)
265     {
266         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
267         output.write_float(value);
268         read_value(null, TypeCode.FLOAT);
269     }
270
271     /**
272      * @return
273      */

274     public double extract_double()
275     {
276         extract_type(TCKind.tk_double, "double");
277         return create_input_stream().read_double();
278     }
279
280     /**
281      * @param value
282      */

283     public void insert_double(double value)
284     {
285         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
286         output.write_double(value);
287         read_value(null, TypeCode.DOUBLE);
288     }
289
290     /**
291      * @return
292      */

293     public boolean extract_boolean()
294     {
295         extract_type(TCKind.tk_boolean, "boolean");
296         return create_input_stream().read_boolean();
297     }
298
299     /**
300      * @param value
301      */

302     public void insert_boolean(boolean value)
303     {
304         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
305         output.write_boolean(value);
306         read_value(null, TypeCode.BOOLEAN);
307     }
308
309     /**
310      * @return
311      */

312     public char extract_char()
313     {
314         extract_type(TCKind.tk_char, "char");
315         return create_input_stream().read_char();
316     }
317
318     /**
319      * @param value
320      */

321     public void insert_char(char value)
322     {
323         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
324         output.write_char(value);
325         read_value(null, TypeCode.CHAR);
326     }
327
328     /**
329      * @return
330      */

331     public char extract_wchar()
332     {
333         extract_type(TCKind.tk_wchar, "wchar");
334         return create_input_stream().read_wchar();
335     }
336
337     /**
338      * @param value
339      */

340     public void insert_wchar(char value)
341     {
342         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
343         output.write_wchar(value);
344         read_value(null, TypeCode.WCHAR);
345     }
346
347     /**
348      * @return
349      */

350     public byte extract_octet()
351     {
352         extract_type(TCKind.tk_octet, "octet");
353         return create_input_stream().read_octet();
354     }
355
356     /**
357      * @param value
358      */

359     public void insert_octet(byte value)
360     {
361         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
362         output.write_octet(value);
363         read_value(null, TypeCode.OCTET);
364     }
365
366     /**
367      * @return
368      */

369     public org.omg.CORBA.Any JavaDoc extract_any()
370     {
371         extract_type(TCKind.tk_any, "any");
372         return create_input_stream().read_any();
373     }
374
375     /**
376      * @param value
377      */

378     public void insert_any(org.omg.CORBA.Any JavaDoc value)
379     {
380         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
381         output.write_any(value);
382         read_value(null, TypeCode.ANY);
383     }
384
385     /**
386      * @return
387      */

388     public org.omg.CORBA.Object JavaDoc extract_Object()
389     {
390         extract_type(TCKind.tk_objref, "Object");
391         org.omg.CORBA.Object JavaDoc obj = create_input_stream().read_Object();
392         return obj;
393     }
394
395     /**
396      * @param value
397      */

398     public void insert_Object(org.omg.CORBA.Object JavaDoc value)
399     {
400         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
401         output.write_Object(value);
402         read_value(null, TypeCode.OBJREF);
403     }
404
405     /**
406      * @param value
407      * @param type
408      */

409     public void insert_Object(org.omg.CORBA.Object JavaDoc value,
410                               org.omg.CORBA.TypeCode JavaDoc type)
411     {
412         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
413         output.write_Object(value);
414         read_value(null, type);
415     }
416
417     /**
418      * @return
419      */

420     public java.io.Serializable JavaDoc extract_Value()
421     {
422         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
423     }
424
425     /**
426      * @param v
427      */

428     public void insert_Value(java.io.Serializable JavaDoc v)
429     {
430         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
431     }
432
433     /**
434      * @param v
435      * @param t
436      */

437     public void insert_Value(java.io.Serializable JavaDoc v, org.omg.CORBA.TypeCode JavaDoc t)
438     {
439         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
440     }
441
442     /**
443      * @return
444      */

445     public String JavaDoc extract_string()
446     {
447         extract_type(TCKind.tk_string, "string");
448         return create_input_stream().read_string();
449     }
450
451     /**
452      * @param value
453      */

454     public void insert_string(String JavaDoc value)
455     {
456         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
457         output.write_string(value);
458         read_value(null, TypeCode.STRING);
459     }
460
461     /**
462      * @return
463      */

464     public String JavaDoc extract_wstring()
465     {
466         extract_type(TCKind.tk_wstring, "wstring");
467         return create_input_stream().read_wstring();
468     }
469
470     /**
471      * @param value
472      */

473     public void insert_wstring(String JavaDoc value)
474     {
475         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
476         output.write_wstring(value);
477         read_value(null, TypeCode.WSTRING);
478     }
479
480     /**
481      * @return
482      */

483     public org.omg.CORBA.TypeCode JavaDoc extract_TypeCode()
484     {
485         extract_type(TCKind.tk_TypeCode, "TypeCode");
486         return create_input_stream().read_TypeCode();
487     }
488
489     /**
490      * @param value
491      */

492     public void insert_TypeCode(org.omg.CORBA.TypeCode JavaDoc value)
493     {
494         org.omg.CORBA.portable.OutputStream JavaDoc output = create_output_stream();
495         output.write_TypeCode(value);
496         read_value(null, TypeCode.TYPECODE);
497     }
498
499     /**
500      * @return
501      */

502     public org.omg.CORBA.Principal JavaDoc extract_Principal()
503     {
504         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc(
505             "org.apache.geronimo.interop.rmi.iiop.Any.extract_Principal");
506     }
507
508     public void insert_Principal(org.omg.CORBA.Principal JavaDoc value)
509     {
510         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc(
511             "org.apache.geronimo.interop.rmi.iiop.Any.insert_Principal");
512     }
513
514     // Don't implement insert_Streamable and extract_Streamable since from
515
// a TypeCode it appears to be impossible to determine the holder class
516
// name (in the general case) in order to construct a Streamable object
517
// for return from extract_Streamable.
518
/**
519      * @return
520      * @throws org.omg.CORBA.BAD_INV_ORDER
521      */

522     public org.omg.CORBA.portable.Streamable JavaDoc extract_Streamable()
523         throws org.omg.CORBA.BAD_INV_ORDER JavaDoc
524     {
525         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
526     }
527
528     /**
529      * @param s
530      */

531     public void insert_Streamable(org.omg.CORBA.portable.Streamable JavaDoc s)
532     {
533         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
534     }
535
536     /**
537      * * Construct an Any from a TypeCode and a String value
538      * * (supported for boolean and numeric primitive IDL types only).
539      */

540     /**
541      * @param type
542      * @param value
543      */

544     public Any(org.omg.CORBA.TypeCode JavaDoc type, String JavaDoc value)
545     {
546         try
547         {
548             _type = type;
549             switch (_type.kind().value())
550             {
551                 case TCKind._tk_boolean:
552                     if (value.equals("0"))
553                     {
554                         insert_boolean(false);
555                     }
556                     else if (value.equals("1"))
557                     {
558                         insert_boolean(true);
559                     }
560                     else
561                     {
562                         insert_boolean(Boolean.valueOf(value).booleanValue());
563                     }
564                     break;
565                 case TCKind._tk_octet:
566                     // Don't use class Byte as it isn't present in JDK 1.0.2
567
insert_octet((byte) parse(value, 0, 255));
568                     break;
569                 case TCKind._tk_short:
570                     // Don't use class Short as it isn't present in JDK 1.0.2
571
insert_short((short) parse(value, -32768, 32767));
572                     break;
573                 case TCKind._tk_ushort:
574                     // Don't use class Short as it isn't present in JDK 1.0.2
575
insert_ushort((short) parse(value, 0, 65535));
576                     break;
577                 case TCKind._tk_long:
578                     insert_long((int) parse(value, -2147483648, 2147483647));
579                     break;
580                 case TCKind._tk_ulong:
581                     insert_ulong((int) parse(value, 0, 4294967295L));
582                     break;
583                 case TCKind._tk_longlong:
584                     insert_longlong(Long.parseLong(value));
585                     break;
586                 case TCKind._tk_ulonglong:
587                     // Note: doesn't handle "unsigned long" values >= 2^63. Fix
588
// this if reported by customers.
589
insert_ulonglong(Long.parseLong(value));
590                     break;
591                 case TCKind._tk_float:
592                     insert_float(Float.valueOf(value).floatValue());
593                     break;
594                 case TCKind._tk_double:
595                     insert_double(Double.valueOf(value).doubleValue());
596                     break;
597                 default:
598                     throw new org.omg.CORBA.BAD_PARAM JavaDoc(value);
599             }
600         }
601         catch (NumberFormatException JavaDoc nfe)
602         {
603             throw new org.omg.CORBA.BAD_PARAM JavaDoc(value + " - " + nfe.toString());
604         }
605     }
606
607     /**
608      * @param value
609      * @param min
610      * @param max
611      * @return
612      * @throws NumberFormatException
613      */

614     private long parse(String JavaDoc value, long min, long max)
615         throws NumberFormatException JavaDoc
616     {
617         long n = Long.parseLong(value);
618         if (n < min || n > max)
619         {
620             throw new NumberFormatException JavaDoc(value +
621                 " is not in range ["
622                 + min + ".." + max + "]");
623         }
624         return n;
625     }
626
627     /**
628      * @return
629      */

630     public String JavaDoc toString()
631     {
632         switch (_type.kind().value())
633         {
634             case TCKind._tk_any:
635             case TCKind._tk_boolean:
636             case TCKind._tk_char:
637             case TCKind._tk_wchar:
638             case TCKind._tk_octet:
639             case TCKind._tk_short:
640             case TCKind._tk_ushort:
641             case TCKind._tk_long:
642             case TCKind._tk_ulong:
643             case TCKind._tk_longlong:
644             case TCKind._tk_ulonglong:
645             case TCKind._tk_float:
646             case TCKind._tk_double:
647             case TCKind._tk_string:
648             case TCKind._tk_wstring:
649             case TCKind._tk_objref:
650                 return value();
651             default:
652                 // TODO: traverse structure to produce printable output
653
return _type.toString();
654         }
655     }
656
657     /**
658      * @return
659      */

660     private String JavaDoc value()
661     {
662         switch (_type.kind().value())
663         {
664             case TCKind._tk_any:
665                 return "" + extract_any();
666             case TCKind._tk_boolean:
667                 return extract_boolean() ? "TRUE" : "FALSE";
668             case TCKind._tk_char:
669                 return "'" + extract_char() + "'";
670             case TCKind._tk_wchar:
671                 return "'" + extract_wchar() + "'";
672             case TCKind._tk_octet:
673                 return "" + extract_octet();
674             case TCKind._tk_short:
675                 return "" + extract_short();
676             case TCKind._tk_ushort:
677                 return "" + extract_ushort();
678             case TCKind._tk_long:
679                 return "" + extract_long();
680             case TCKind._tk_ulong:
681                 return "" + extract_ulong();
682             case TCKind._tk_longlong:
683                 return "" + extract_longlong();
684             case TCKind._tk_ulonglong:
685                 return "" + extract_ulonglong();
686             case TCKind._tk_float:
687                 return "" + extract_float();
688             case TCKind._tk_double:
689                 return "" + extract_double();
690             case TCKind._tk_string:
691                 return "\"" + extract_string() + "\"";
692             case TCKind._tk_wstring:
693                 return "\"" + extract_wstring() + "\"";
694             case TCKind._tk_objref:
695                 return extract_Object().toString();
696             case TCKind._tk_TypeCode:
697                 return "" + extract_TypeCode();
698             default:
699                 return "?";
700         }
701     }
702 }
703
Popular Tags