KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > Any


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop;
20
21 import org.omg.CORBA.TCKind;
22
23 /**
24  ** An implementation of CORBA 'any' for the Sybase ORB.
25  **/

26 public class Any extends org.omg.CORBA.Any
27 {
28     private org.omg.CORBA.TypeCode _type;
29
30     private byte[] _data;
31
32     private void extract_type(TCKind tk, String what)
33     {
34         if (_type.kind().value() != tk.value())
35         {
36             throw new org.omg.CORBA.BAD_TYPECODE("com.sybase.CORBA.Any.extract_"
37                 + what + ": type = " + _type);
38         }
39     }
40
41     public Any()
42     {
43         _type = TypeCode.NULL;
44     }
45
46     public boolean equal(org.omg.CORBA.Any a)
47     {
48         if (! _type.equal(a.type()))
49         {
50             return false;
51         }
52         if (a instanceof gcc.rmi.iiop.Any)
53         {
54             gcc.rmi.iiop.Any _that = (gcc.rmi.iiop.Any)a;
55             String x = gcc.util.Base16Binary.toString(this._data);
56             String y = gcc.util.Base16Binary.toString(_that._data);
57             return x.equals(y);
58         }
59         else
60         {
61             // TODO: implement equality testing with other ORB's 'any' values
62
throw new org.omg.CORBA.NO_IMPLEMENT("gcc.rmi.iiop.Any.equal("
63                 + a.getClass().getName() + ")");
64         }
65     }
66
67     public org.omg.CORBA.TypeCode type()
68     {
69         return _type;
70     }
71
72     public void type(org.omg.CORBA.TypeCode type)
73     {
74         _type = type;
75     }
76
77     public void read_value(org.omg.CORBA.portable.InputStream input, org.omg.CORBA.TypeCode type)
78     {
79         byte[] buffer = ((CdrInputStream)input)._buffer;
80         int length = ((CdrInputStream)input)._length;
81         _type = type;
82         _data = new byte[length];
83         System.arraycopy(buffer, 0, _data, 0, length);
84     }
85
86     public void write_value(org.omg.CORBA.portable.OutputStream output)
87     {
88         // A no-op in this implementation.
89
}
90
91     public org.omg.CORBA.portable.OutputStream create_output_stream()
92     {
93         _data = null;
94         return CdrOutputStream.getInstance();
95     }
96
97     public org.omg.CORBA.portable.InputStream create_input_stream()
98     {
99         if (_data == null)
100         {
101             throw new org.omg.CORBA.BAD_OPERATION("com.sybase.CORBA.Any.create_input_stream");
102         }
103         return CdrInputStream.getInstance();
104     }
105
106     public short extract_short()
107     {
108         extract_type(TCKind.tk_short, "short");
109         return create_input_stream().read_short();
110     }
111
112     public void insert_short(short value)
113     {
114         org.omg.CORBA.portable.OutputStream output = create_output_stream();
115         output.write_short(value);
116         read_value(null, TypeCode.SHORT);
117     }
118
119     public int extract_long()
120     {
121         extract_type(TCKind.tk_long, "long");
122         return create_input_stream().read_long();
123     }
124
125     public void insert_long(int value)
126     {
127         org.omg.CORBA.portable.OutputStream output = create_output_stream();
128         output.write_long(value);
129         read_value(null, TypeCode.LONG);
130     }
131
132     public long extract_longlong()
133     {
134         extract_type(TCKind.tk_longlong, "longlong");
135         return create_input_stream().read_longlong();
136     }
137
138     public void insert_longlong(long value)
139     {
140         org.omg.CORBA.portable.OutputStream output = create_output_stream();
141         output.write_longlong(value);
142         read_value(null, TypeCode.LONGLONG);
143     }
144
145     public short extract_ushort()
146     {
147         extract_type(TCKind.tk_ushort, "ushort");
148         return create_input_stream().read_ushort();
149     }
150
151     public void insert_ushort(short value)
152     {
153         org.omg.CORBA.portable.OutputStream output = create_output_stream();
154         output.write_ushort(value);
155         read_value(null, TypeCode.USHORT);
156     }
157
158     public int extract_ulong()
159     {
160         extract_type(TCKind.tk_ulong, "ulong");
161         return create_input_stream().read_ulong();
162     }
163
164     public void insert_ulong(int value)
165     {
166         org.omg.CORBA.portable.OutputStream output = create_output_stream();
167         output.write_ulong(value);
168         read_value(null, TypeCode.ULONG);
169     }
170
171     public long extract_ulonglong()
172     {
173         extract_type(TCKind.tk_ulonglong, "ulonglong");
174         return create_input_stream().read_ulonglong();
175     }
176
177     public void insert_ulonglong(long value)
178     {
179         org.omg.CORBA.portable.OutputStream output = create_output_stream();
180         output.write_ulonglong(value);
181         read_value(null, TypeCode.ULONGLONG);
182     }
183
184     public float extract_float()
185     {
186         extract_type(TCKind.tk_float, "float");
187         return create_input_stream().read_float();
188     }
189
190     public void insert_float(float value)
191     {
192         org.omg.CORBA.portable.OutputStream output = create_output_stream();
193         output.write_float(value);
194         read_value(null, TypeCode.FLOAT);
195     }
196
197     public double extract_double()
198     {
199         extract_type(TCKind.tk_double, "double");
200         return create_input_stream().read_double();
201     }
202
203     public void insert_double(double value)
204     {
205         org.omg.CORBA.portable.OutputStream output = create_output_stream();
206         output.write_double(value);
207         read_value(null, TypeCode.DOUBLE);
208     }
209
210     public boolean extract_boolean()
211     {
212         extract_type(TCKind.tk_boolean, "boolean");
213         return create_input_stream().read_boolean();
214     }
215
216     public void insert_boolean(boolean value)
217     {
218         org.omg.CORBA.portable.OutputStream output = create_output_stream();
219         output.write_boolean(value);
220         read_value(null, TypeCode.BOOLEAN);
221     }
222
223     public char extract_char()
224     {
225         extract_type(TCKind.tk_char, "char");
226         return create_input_stream().read_char();
227     }
228
229     public void insert_char(char value)
230     {
231         org.omg.CORBA.portable.OutputStream output = create_output_stream();
232         output.write_char(value);
233         read_value(null, TypeCode.CHAR);
234     }
235
236     public char extract_wchar()
237     {
238         extract_type(TCKind.tk_wchar, "wchar");
239         return create_input_stream().read_wchar();
240     }
241
242     public void insert_wchar(char value)
243     {
244         org.omg.CORBA.portable.OutputStream output = create_output_stream();
245         output.write_wchar(value);
246         read_value(null, TypeCode.WCHAR);
247     }
248
249     public byte extract_octet()
250     {
251         extract_type(TCKind.tk_octet, "octet");
252         return create_input_stream().read_octet();
253     }
254
255     public void insert_octet(byte value)
256     {
257         org.omg.CORBA.portable.OutputStream output = create_output_stream();
258         output.write_octet(value);
259         read_value(null, TypeCode.OCTET);
260     }
261
262     public org.omg.CORBA.Any extract_any()
263     {
264         extract_type(TCKind.tk_any, "any");
265         return create_input_stream().read_any();
266     }
267
268     public void insert_any(org.omg.CORBA.Any value)
269     {
270         org.omg.CORBA.portable.OutputStream output = create_output_stream();
271         output.write_any(value);
272         read_value(null, TypeCode.ANY);
273     }
274
275     public org.omg.CORBA.Object extract_Object()
276     {
277         extract_type(TCKind.tk_objref, "Object");
278         org.omg.CORBA.Object obj = create_input_stream().read_Object();
279         return obj;
280     }
281
282     public void insert_Object(org.omg.CORBA.Object value)
283     {
284         org.omg.CORBA.portable.OutputStream output = create_output_stream();
285         output.write_Object(value);
286         read_value(null, TypeCode.OBJREF);
287     }
288
289     public void insert_Object(org.omg.CORBA.Object value, org.omg.CORBA.TypeCode type)
290     {
291         org.omg.CORBA.portable.OutputStream output = create_output_stream();
292         output.write_Object(value);
293         read_value(null, type);
294     }
295
296     public java.io.Serializable extract_Value()
297     {
298         throw new org.omg.CORBA.NO_IMPLEMENT();
299     }
300
301     public void insert_Value(java.io.Serializable v)
302     {
303         throw new org.omg.CORBA.NO_IMPLEMENT();
304     }
305
306     public void insert_Value(java.io.Serializable v, org.omg.CORBA.TypeCode t)
307     {
308         throw new org.omg.CORBA.NO_IMPLEMENT();
309     }
310
311     public String extract_string()
312     {
313         extract_type(TCKind.tk_string, "string");
314         return create_input_stream().read_string();
315     }
316
317     public void insert_string(String value)
318     {
319         org.omg.CORBA.portable.OutputStream output = create_output_stream();
320         output.write_string(value);
321         read_value(null, TypeCode.STRING);
322     }
323
324     public String extract_wstring()
325     {
326         extract_type(TCKind.tk_wstring, "wstring");
327         return create_input_stream().read_wstring();
328     }
329
330     public void insert_wstring(String value)
331     {
332         org.omg.CORBA.portable.OutputStream output = create_output_stream();
333         output.write_wstring(value);
334         read_value(null, TypeCode.WSTRING);
335     }
336
337     public org.omg.CORBA.TypeCode extract_TypeCode()
338     {
339         extract_type(TCKind.tk_TypeCode, "TypeCode");
340         return create_input_stream().read_TypeCode();
341     }
342
343     public void insert_TypeCode(org.omg.CORBA.TypeCode value)
344     {
345         org.omg.CORBA.portable.OutputStream output = create_output_stream();
346         output.write_TypeCode(value);
347         read_value(null, TypeCode.TYPECODE);
348     }
349
350     public org.omg.CORBA.Principal extract_Principal()
351     {
352         throw new org.omg.CORBA.NO_IMPLEMENT("gcc.rmi.iiop.Any.extract_Principal");
353     }
354
355     public void insert_Principal(org.omg.CORBA.Principal value)
356     {
357         throw new org.omg.CORBA.NO_IMPLEMENT("gcc.rmi.iiop.Any.insert_Principal");
358     }
359
360     // Don't implement insert_Streamable and extract_Streamable since from
361
// a TypeCode it appears to be impossible to determine the holder class
362
// name (in the general case) in order to construct a Streamable object
363
// for return from extract_Streamable.
364

365     public org.omg.CORBA.portable.Streamable extract_Streamable()
366         throws org.omg.CORBA.BAD_INV_ORDER
367     {
368         throw new org.omg.CORBA.NO_IMPLEMENT();
369     }
370
371     public void insert_Streamable(org.omg.CORBA.portable.Streamable s)
372     {
373         throw new org.omg.CORBA.NO_IMPLEMENT();
374     }
375
376     // -----------------------------------------------------------------------
377
// Sybase-internal constructors and methods
378
// -----------------------------------------------------------------------
379

380     /**
381      ** Construct an Any from a TypeCode and a String value
382      ** (supported for boolean and numeric primitive IDL types only).
383      **/

384     public Any(org.omg.CORBA.TypeCode type, String value)
385     {
386         try
387         {
388             _type = type;
389             switch (_type.kind().value())
390             {
391                 case TCKind._tk_boolean:
392                     if (value.equals("0"))
393                     {
394                         insert_boolean(false);
395                     }
396                     else if (value.equals("1"))
397                     {
398                         insert_boolean(true);
399                     }
400                     else
401                     {
402                         insert_boolean(Boolean.valueOf(value).booleanValue());
403                     }
404                     break;
405                 case TCKind._tk_octet:
406                     // Don't use class Byte as it isn't present in JDK 1.0.2
407
insert_octet((byte)parse(value, 0, 255));
408                     break;
409                 case TCKind._tk_short:
410                     // Don't use class Short as it isn't present in JDK 1.0.2
411
insert_short((short)parse(value, -32768, 32767));
412                     break;
413                 case TCKind._tk_ushort:
414                     // Don't use class Short as it isn't present in JDK 1.0.2
415
insert_ushort((short)parse(value, 0, 65535));
416                     break;
417                 case TCKind._tk_long:
418                     insert_long((int)parse(value, -2147483648, 2147483647));
419                     break;
420                 case TCKind._tk_ulong:
421                     insert_ulong((int)parse(value, 0, 4294967295L));
422                     break;
423                 case TCKind._tk_longlong:
424                     insert_longlong(Long.parseLong(value));
425                     break;
426                 case TCKind._tk_ulonglong:
427                     // Note: doesn't handle "unsigned long" values >= 2^63. Fix
428
// this if reported by customers.
429
insert_ulonglong(Long.parseLong(value));
430                     break;
431                 case TCKind._tk_float:
432                     insert_float(Float.valueOf(value).floatValue());
433                     break;
434                 case TCKind._tk_double:
435                     insert_double(Double.valueOf(value).doubleValue());
436                     break;
437                 default:
438                     throw new org.omg.CORBA.BAD_PARAM(value);
439             }
440         }
441         catch (NumberFormatException nfe)
442         {
443             throw new org.omg.CORBA.BAD_PARAM(value + " - " + nfe.toString());
444         }
445     }
446
447     private long parse(String value, long min, long max) throws NumberFormatException
448     {
449         long n = Long.parseLong(value);
450         if (n < min || n > max)
451         {
452             throw new NumberFormatException(value + " is not in range ["
453                 + min + ".." + max + "]");
454         }
455         return n;
456     }
457
458     public String toString()
459     {
460         switch (_type.kind().value())
461         {
462             case TCKind._tk_any:
463             case TCKind._tk_boolean:
464             case TCKind._tk_char:
465             case TCKind._tk_wchar:
466             case TCKind._tk_octet:
467             case TCKind._tk_short:
468             case TCKind._tk_ushort:
469             case TCKind._tk_long:
470             case TCKind._tk_ulong:
471             case TCKind._tk_longlong:
472             case TCKind._tk_ulonglong:
473             case TCKind._tk_float:
474             case TCKind._tk_double:
475             case TCKind._tk_string:
476             case TCKind._tk_wstring:
477             case TCKind._tk_objref:
478                 return value();
479             default:
480                 // TODO: traverse structure to produce printable output
481
return _type.toString();
482         }
483     }
484
485     private String value()
486     {
487         switch (_type.kind().value())
488         {
489             case TCKind._tk_any:
490                 return "" + extract_any();
491             case TCKind._tk_boolean:
492                 return extract_boolean() ? "TRUE" : "FALSE";
493             case TCKind._tk_char:
494                 return "'" + extract_char() + "'";
495             case TCKind._tk_wchar:
496                 return "'" + extract_wchar() + "'";
497             case TCKind._tk_octet:
498                 return "" + extract_octet();
499             case TCKind._tk_short:
500                 return "" + extract_short();
501             case TCKind._tk_ushort:
502                 return "" + extract_ushort();
503             case TCKind._tk_long:
504                 return "" + extract_long();
505             case TCKind._tk_ulong:
506                 return "" + extract_ulong();
507             case TCKind._tk_longlong:
508                 return "" + extract_longlong();
509             case TCKind._tk_ulonglong:
510                 return "" + extract_ulonglong();
511             case TCKind._tk_float:
512                 return "" + extract_float();
513             case TCKind._tk_double:
514                 return "" + extract_double();
515             case TCKind._tk_string:
516                 return "\"" + extract_string() + "\"";
517             case TCKind._tk_wstring:
518                 return "\"" + extract_wstring() + "\"";
519             case TCKind._tk_objref:
520                 return extract_Object().toString();
521             case TCKind._tk_TypeCode:
522                 return "" + extract_TypeCode();
523             default:
524                 return "?";
525         }
526     }
527 }
528
Popular Tags