KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > dynany > DynAny


1 package org.jacorb.orb.dynany;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.omg.CORBA.*;
24 import org.omg.DynamicAny.*;
25 import org.omg.DynamicAny.DynAnyPackage.*;
26 import org.jacorb.orb.TypeCode;
27
28 /**
29  * CORBA DynAny
30  *
31  * @author (c) Gerald Brose, FU Berlin 1999
32  * $Id: DynAny.java,v 1.22 2005/03/25 13:15:55 andre.spiegel Exp $
33  *
34  */

35 public class DynAny
36    extends org.omg.CORBA.LocalObject JavaDoc
37    implements org.omg.DynamicAny.DynAny JavaDoc
38 {
39    protected org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory;
40    protected org.omg.CORBA.TypeCode JavaDoc type;
41    protected int pos = -1;
42    protected int limit = 0;
43    protected org.omg.CORBA.ORB JavaDoc orb;
44
45    /* our representation of a primitive type any is the any itself */
46    private org.omg.CORBA.Any JavaDoc anyRepresentation = null;
47
48
49    protected DynAny() {}
50
51    DynAny( org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory,
52            org.omg.CORBA.TypeCode JavaDoc _type)
53       throws TypeMismatch
54    {
55       this.orb = org.omg.CORBA.ORB.init();
56       this.dynFactory = dynFactory;
57       type = TypeCode.originalType( _type );
58       anyRepresentation = defaultValue( type );
59    }
60    
61    DynAny( org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory,
62            org.omg.CORBA.TypeCode JavaDoc _type,
63            org.omg.CORBA.ORB JavaDoc orb)
64       throws TypeMismatch
65    {
66       this.orb = orb;
67       this.dynFactory = dynFactory;
68       type = TypeCode.originalType( _type );
69       anyRepresentation = defaultValue( type );
70    }
71    
72    public org.omg.CORBA.TypeCode JavaDoc type()
73    {
74       checkDestroyed ();
75       return type;
76    }
77
78    public void assign(org.omg.DynamicAny.DynAny JavaDoc dyn_any)
79       throws TypeMismatch
80    {
81       checkDestroyed ();
82       if( dyn_any.type().equivalent( this.type()))
83       {
84          try
85          {
86             from_any( dyn_any.to_any() );
87          }
88          catch( InvalidValue iv )
89          {
90             // should not happen
91
}
92       }
93       else
94          throw new TypeMismatch();
95    }
96
97    public boolean equal( org.omg.DynamicAny.DynAny JavaDoc dyn_any )
98    {
99       checkDestroyed ();
100
101       if ( getRepresentation() == null )
102       {
103          throw new BAD_INV_ORDER ("DynAny not initialized");
104       }
105
106       return dyn_any.to_any().equal( getRepresentation() );
107    }
108
109    public void from_any(org.omg.CORBA.Any JavaDoc value)
110       throws InvalidValue, TypeMismatch
111    {
112       checkDestroyed ();
113       if( ! value.type().equivalent( type()) )
114          throw new TypeMismatch();
115
116       type = TypeCode.originalType( value.type() );
117
118       try
119       {
120          anyRepresentation = (org.jacorb.orb.Any)orb.create_any();
121          anyRepresentation.read_value( value.create_input_stream(), type());
122       }
123       catch( Exception JavaDoc e)
124       {
125          e.printStackTrace();
126          throw new InvalidValue();
127       }
128    }
129
130    public org.omg.CORBA.Any JavaDoc to_any()
131    {
132       checkDestroyed ();
133       org.jacorb.orb.Any out_any = (org.jacorb.orb.Any)orb.create_any();
134       out_any.type( type());
135       out_any.read_value( getRepresentation().create_input_stream(), type());
136       return out_any;
137    }
138
139    public void destroy()
140    {
141       checkDestroyed ();
142       anyRepresentation = null;
143       type = null;
144    }
145
146    public org.omg.DynamicAny.DynAny JavaDoc copy()
147    {
148       checkDestroyed ();
149       try
150       {
151          return dynFactory.create_dyn_any( to_any() );
152       }
153       catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc tm )
154       {
155          tm.printStackTrace();
156       }
157       return null;
158    }
159
160    /**
161     * returns the DynAny's internal any representation,
162     * overwritten in subclasses that represent constructed
163     * types and need to traverse structures.
164     */

165
166    protected org.omg.CORBA.Any JavaDoc getRepresentation()
167    {
168       return anyRepresentation;
169    }
170
171
172    public void insert_boolean( boolean value )
173       throws TypeMismatch
174    {
175       checkDestroyed ();
176       org.omg.CORBA.Any JavaDoc any = getRepresentation();
177       if( any.type().kind() != org.omg.CORBA.TCKind.tk_boolean)
178          throw new TypeMismatch ();
179       any.insert_boolean(value);
180    }
181
182
183    public void insert_octet( byte value )
184       throws TypeMismatch
185    {
186       checkDestroyed ();
187       org.omg.CORBA.Any JavaDoc any = getRepresentation();
188       if( any.type().kind() != org.omg.CORBA.TCKind.tk_octet)
189          throw new TypeMismatch ();
190       any.insert_octet(value);
191    }
192
193
194    public void insert_char(char value)
195       throws TypeMismatch
196    {
197       checkDestroyed ();
198       org.omg.CORBA.Any JavaDoc any = getRepresentation();
199       if( any.type().kind() != org.omg.CORBA.TCKind.tk_char)
200          throw new TypeMismatch ();
201       any.insert_char(value);
202    }
203
204
205    public void insert_short(short value)
206       throws TypeMismatch
207    {
208       checkDestroyed ();
209       org.omg.CORBA.Any JavaDoc any = getRepresentation();
210       if( any.type().kind() != org.omg.CORBA.TCKind.tk_short)
211          throw new TypeMismatch ();
212       any.insert_short(value);
213    }
214
215
216    public void insert_ushort(short value)
217       throws TypeMismatch
218    {
219       checkDestroyed ();
220       org.omg.CORBA.Any JavaDoc any = getRepresentation();
221       if( any.type().kind() != org.omg.CORBA.TCKind.tk_ushort)
222          throw new TypeMismatch ();
223       any.insert_ushort(value);
224    }
225
226
227    public void insert_long(int value)
228       throws TypeMismatch
229    {
230       checkDestroyed ();
231       org.omg.CORBA.Any JavaDoc any = getRepresentation();
232       if( any.type().kind() != org.omg.CORBA.TCKind.tk_long)
233          throw new TypeMismatch ();
234       any.insert_long(value);
235    }
236
237
238    public void insert_ulong(int value)
239       throws TypeMismatch
240    {
241       checkDestroyed ();
242       org.omg.CORBA.Any JavaDoc any = getRepresentation();
243       if( any.type().kind() != org.omg.CORBA.TCKind.tk_ulong)
244          throw new TypeMismatch ();
245       any.insert_ulong(value);
246    }
247
248
249    public void insert_float(float value)
250       throws TypeMismatch
251    {
252       checkDestroyed ();
253       org.omg.CORBA.Any JavaDoc any = getRepresentation();
254       if( any.type().kind() != org.omg.CORBA.TCKind.tk_float)
255          throw new TypeMismatch ();
256       any.insert_float(value);
257    }
258
259
260    public void insert_double(double value)
261       throws TypeMismatch
262    {
263       checkDestroyed ();
264       org.omg.CORBA.Any JavaDoc any = getRepresentation();
265       if( any.type().kind() != org.omg.CORBA.TCKind.tk_double)
266          throw new TypeMismatch ();
267       any.insert_double(value);
268    }
269
270
271    public void insert_string(java.lang.String JavaDoc value)
272       throws TypeMismatch
273    {
274       checkDestroyed ();
275       org.omg.CORBA.Any JavaDoc any = getRepresentation();
276       if( any.type().kind() != org.omg.CORBA.TCKind.tk_string)
277          throw new TypeMismatch ();
278       any.insert_string(value);
279    }
280
281
282    public void insert_reference(org.omg.CORBA.Object JavaDoc value)
283       throws TypeMismatch
284    {
285       checkDestroyed ();
286       org.omg.CORBA.Any JavaDoc any = getRepresentation();
287       if( any.type().kind() != org.omg.CORBA.TCKind.tk_objref)
288          throw new TypeMismatch ();
289       any.insert_Object(value);
290    }
291
292
293    public void insert_typecode(org.omg.CORBA.TypeCode JavaDoc value)
294       throws TypeMismatch
295    {
296       checkDestroyed ();
297       org.omg.CORBA.Any JavaDoc any = getRepresentation();
298       if( any.type().kind() != org.omg.CORBA.TCKind.tk_TypeCode)
299          throw new TypeMismatch ();
300       any.insert_TypeCode(value);
301    }
302
303    public void insert_longlong(long value)
304       throws TypeMismatch
305    {
306       checkDestroyed ();
307       org.omg.CORBA.Any JavaDoc any = getRepresentation();
308       if( any.type().kind() != org.omg.CORBA.TCKind.tk_longlong)
309          throw new TypeMismatch ();
310       any.insert_longlong(value);
311    }
312
313    public void insert_ulonglong(long value)
314       throws TypeMismatch
315    {
316       checkDestroyed ();
317       org.omg.CORBA.Any JavaDoc any = getRepresentation();
318       if( any.type().kind() != org.omg.CORBA.TCKind.tk_ulonglong)
319          throw new TypeMismatch ();
320       any.insert_ulonglong(value);
321    }
322
323    public void insert_wchar(char value)
324       throws TypeMismatch
325    {
326       checkDestroyed ();
327       org.omg.CORBA.Any JavaDoc any = getRepresentation();
328       if( any.type().kind() != org.omg.CORBA.TCKind.tk_wchar)
329          throw new TypeMismatch ();
330       any.insert_wchar(value);
331    }
332
333    public void insert_wstring(java.lang.String JavaDoc value)
334       throws TypeMismatch
335    {
336       checkDestroyed ();
337       org.omg.CORBA.Any JavaDoc any = getRepresentation();
338       if( any.type().kind() != org.omg.CORBA.TCKind.tk_wstring)
339          throw new TypeMismatch ();
340       any.insert_wstring(value);
341    }
342
343    public void insert_any(org.omg.CORBA.Any JavaDoc value)
344       throws TypeMismatch
345    {
346       checkDestroyed ();
347       org.omg.CORBA.Any JavaDoc any = getRepresentation();
348       if( any.type().kind() != org.omg.CORBA.TCKind.tk_any)
349          throw new TypeMismatch ();
350       any.insert_any(value);
351    }
352
353    public void insert_dyn_any(org.omg.DynamicAny.DynAny JavaDoc value)
354       throws TypeMismatch
355    {
356       checkDestroyed ();
357       insert_any (value.to_any ());
358    }
359
360    public boolean get_boolean() throws TypeMismatch
361    {
362       checkDestroyed ();
363       org.omg.CORBA.Any JavaDoc any = getRepresentation();
364       try
365       {
366          return any.extract_boolean();
367       }
368       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
369       {
370          throw new TypeMismatch();
371       }
372    }
373
374    public byte get_octet() throws TypeMismatch
375    {
376       checkDestroyed ();
377       org.omg.CORBA.Any JavaDoc any = getRepresentation();
378       try
379       {
380          return any.extract_octet();
381       }
382       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
383       {
384          throw new TypeMismatch();
385       }
386    }
387
388    public char get_char() throws TypeMismatch
389    {
390       checkDestroyed ();
391       org.omg.CORBA.Any JavaDoc any = getRepresentation();
392       try
393       {
394          return any.extract_char();
395       }
396       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
397       {
398          throw new TypeMismatch();
399       }
400    }
401
402    public short get_short() throws TypeMismatch
403    {
404       checkDestroyed ();
405       org.omg.CORBA.Any JavaDoc any = getRepresentation();
406       try
407       {
408          return any.extract_short();
409       }
410       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
411       {
412          throw new TypeMismatch();
413       }
414    }
415
416    public short get_ushort() throws TypeMismatch
417    {
418       checkDestroyed ();
419       org.omg.CORBA.Any JavaDoc any = getRepresentation();
420       try
421       {
422          return any.extract_ushort();
423       }
424       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
425       {
426          throw new TypeMismatch();
427       }
428    }
429
430    public int get_long() throws TypeMismatch
431    {
432       checkDestroyed ();
433       org.omg.CORBA.Any JavaDoc any = getRepresentation();
434       try
435       {
436          return any.extract_long();
437       }
438       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
439       {
440          throw new TypeMismatch();
441       }
442    }
443
444    public int get_ulong() throws TypeMismatch
445    {
446       checkDestroyed ();
447       org.omg.CORBA.Any JavaDoc any = getRepresentation();
448       try
449       {
450          return any.extract_ulong();
451       }
452       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
453       {
454          throw new TypeMismatch();
455       }
456    }
457    public float get_float() throws TypeMismatch
458    {
459       checkDestroyed ();
460       org.omg.CORBA.Any JavaDoc any = getRepresentation();
461       try
462       {
463          return any.extract_float();
464       }
465       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
466       {
467          throw new TypeMismatch();
468       }
469    }
470
471    public double get_double() throws TypeMismatch
472    {
473       checkDestroyed ();
474       org.omg.CORBA.Any JavaDoc any = getRepresentation();
475       try
476       {
477          return any.extract_double();
478       }
479       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
480       {
481          throw new TypeMismatch();
482       }
483    }
484
485    public java.lang.String JavaDoc get_string() throws TypeMismatch
486    {
487       checkDestroyed ();
488       org.omg.CORBA.Any JavaDoc any = getRepresentation();
489       try
490       {
491          return any.extract_string();
492       }
493       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
494       {
495          throw new TypeMismatch();
496       }
497    }
498
499    public org.omg.CORBA.Object JavaDoc get_reference() throws TypeMismatch
500    {
501       checkDestroyed ();
502       org.omg.CORBA.Any JavaDoc any = getRepresentation();
503       try
504       {
505          return any.extract_Object();
506       }
507       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
508       {
509          throw new TypeMismatch();
510       }
511    }
512
513    public org.omg.CORBA.TypeCode JavaDoc get_typecode() throws TypeMismatch
514    {
515       checkDestroyed ();
516       org.omg.CORBA.Any JavaDoc any = getRepresentation();
517       try
518       {
519          return any.extract_TypeCode();
520       }
521       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
522       {
523          throw new TypeMismatch();
524       }
525    }
526
527    public long get_longlong() throws TypeMismatch
528    {
529       checkDestroyed ();
530       org.omg.CORBA.Any JavaDoc any = getRepresentation();
531       try
532       {
533          return any.extract_longlong();
534       }
535       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
536       {
537          throw new TypeMismatch();
538       }
539    }
540
541    public long get_ulonglong() throws TypeMismatch
542    {
543       checkDestroyed ();
544       org.omg.CORBA.Any JavaDoc any = getRepresentation();
545       try
546       {
547          return any.extract_ulonglong();
548       }
549       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
550       {
551          throw new TypeMismatch();
552       }
553    }
554
555    public char get_wchar() throws TypeMismatch
556    {
557       checkDestroyed ();
558       org.omg.CORBA.Any JavaDoc any = getRepresentation();
559       try
560       {
561          return any.extract_wchar();
562       }
563       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
564       {
565          throw new TypeMismatch();
566       }
567    }
568
569    public java.lang.String JavaDoc get_wstring() throws TypeMismatch
570    {
571       checkDestroyed ();
572       org.omg.CORBA.Any JavaDoc any = getRepresentation();
573       try
574       {
575          return any.extract_wstring();
576       }
577       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
578       {
579          throw new TypeMismatch();
580       }
581    }
582
583    public org.omg.CORBA.Any JavaDoc get_any() throws TypeMismatch
584    {
585       checkDestroyed ();
586       org.omg.CORBA.Any JavaDoc any = getRepresentation();
587       try
588       {
589          return any.extract_any();
590       }
591       catch( org.omg.CORBA.BAD_OPERATION JavaDoc b )
592       {
593          throw new TypeMismatch();
594       }
595    }
596
597    public org.omg.DynamicAny.DynAny JavaDoc get_dyn_any() throws TypeMismatch
598    {
599       checkDestroyed ();
600       try
601       {
602          return dynFactory.create_dyn_any( get_any () );
603       }
604       catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc tm )
605       {
606          tm.printStackTrace();
607       }
608       return null;
609    }
610
611    public int component_count()
612    {
613       checkDestroyed ();
614       return limit;
615    }
616
617    public org.omg.DynamicAny.DynAny JavaDoc current_component()
618       throws TypeMismatch
619    {
620       checkDestroyed ();
621       throw new TypeMismatch();
622    }
623
624    public boolean next()
625    {
626       checkDestroyed ();
627       if( pos < limit-1 )
628       {
629          pos++;
630          return true;
631       }
632       pos = -1;
633       return false;
634    }
635
636    public boolean seek(int index)
637    {
638       checkDestroyed ();
639       if( index < 0 )
640       {
641          pos = -1;
642          return false;
643       }
644       if( index < limit )
645       {
646          pos = index;
647          return true;
648       }
649       pos = -1;
650
651       return false;
652    }
653
654    public void rewind()
655    {
656       checkDestroyed ();
657       seek(0);
658    }
659
660
661    protected void checkDestroyed ()
662    {
663       if (anyRepresentation == null && type == null)
664       {
665          throw new OBJECT_NOT_EXIST ();
666       }
667    }
668
669    private org.omg.CORBA.Any JavaDoc defaultValue(org.omg.CORBA.TypeCode JavaDoc tc)
670       throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc
671    {
672       org.omg.CORBA.Any JavaDoc _any = orb.create_any();
673       _any.type( tc );
674       switch( tc.kind().value() )
675       {
676       case TCKind._tk_boolean :
677          _any.insert_boolean(false);
678          break;
679       case TCKind._tk_short:
680          _any.insert_short( (short)0 );
681          break;
682       case TCKind._tk_ushort:
683          _any.insert_ushort( (short)0 );
684          break;
685       case TCKind._tk_long:
686          _any.insert_long( 0 );
687          break;
688       case TCKind._tk_double:
689          _any.insert_double( 0 );
690          break;
691       case TCKind._tk_ulong:
692          _any.insert_ulong( 0 );
693          break;
694       case TCKind._tk_longlong:
695          _any.insert_longlong(0);
696          break;
697       case TCKind._tk_ulonglong:
698          _any.insert_ulonglong(0);
699          break;
700       case TCKind._tk_float:
701          _any.insert_float(0);
702          break;
703       case TCKind._tk_char:
704          _any.insert_char((char)0);
705          break;
706       case TCKind._tk_wchar:
707          _any.insert_wchar((char)0);
708          break;
709       case TCKind._tk_octet:
710          _any.insert_octet((byte)0);
711          break;
712       case TCKind._tk_string:
713          _any.insert_string("");
714          break;
715       case TCKind._tk_wstring:
716          _any.insert_wstring("");
717          break;
718       case TCKind._tk_objref:
719          _any.insert_Object(null);
720          break;
721       case TCKind._tk_TypeCode:
722          _any.insert_TypeCode( orb.get_primitive_tc( TCKind.tk_null ) );
723          break;
724       case TCKind._tk_any:
725          org.jacorb.orb.Any a = (org.jacorb.orb.Any)orb.create_any();
726          a.type( orb.get_primitive_tc( TCKind.tk_null ));
727          _any.insert_any(a);
728          break;
729       case TCKind._tk_null:
730       case TCKind._tk_void:
731          // legal TypeCodes that have no associated value
732
break;
733       default:
734          throw new TypeMismatch();
735       }
736       return _any;
737    }
738
739    // methods below suggested by <Philippe.Merle@lifl.fr>,
740
// to allow compilation on J2SDK 1.4.2
741

742    public void insert_val(java.io.Serializable JavaDoc value)
743         throws TypeMismatch
744    {
745        throw new NO_IMPLEMENT("DynAny::insert_val not implemented.");
746    }
747
748    public java.io.Serializable JavaDoc get_val()
749         throws TypeMismatch
750    {
751         throw new NO_IMPLEMENT("DynAny::get_val not implemented");
752    }
753 }
754
Popular Tags