KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
22 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
23
24 /**
25  * * An implementation of CORBA 'TypeCode' for use with RMI-IIOP.
26  */

27 public class TypeCode
28     extends org.omg.CORBA.TypeCode JavaDoc
29 {
30     // -----------------------------------------------------------------------
31
// public data
32
// -----------------------------------------------------------------------
33

34     public static final TypeCode NULL = new TypeCode(TCKind.tk_null);
35
36     public static final TypeCode VOID = new TypeCode(TCKind.tk_void);
37
38     public static final TypeCode ANY = new TypeCode(TCKind.tk_any);
39
40     public static final TypeCode BOOLEAN = new TypeCode(TCKind.tk_boolean);
41
42     public static final TypeCode CHAR = new TypeCode(TCKind.tk_char);
43
44     public static final TypeCode WCHAR = new TypeCode(TCKind.tk_wchar);
45
46     public static final TypeCode OCTET = new TypeCode(TCKind.tk_octet);
47
48     public static final TypeCode SHORT = new TypeCode(TCKind.tk_short);
49
50     public static final TypeCode USHORT = new TypeCode(TCKind.tk_ushort);
51
52     public static final TypeCode LONG = new TypeCode(TCKind.tk_long);
53
54     public static final TypeCode ULONG = new TypeCode(TCKind.tk_ulong);
55
56     public static final TypeCode LONGLONG = new TypeCode(TCKind.tk_longlong);
57
58     public static final TypeCode ULONGLONG = new TypeCode(TCKind.tk_ulonglong);
59
60     public static final TypeCode FLOAT = new TypeCode(TCKind.tk_float);
61
62     public static final TypeCode DOUBLE = new TypeCode(TCKind.tk_double);
63
64     public static final TypeCode LONGDOUBLE = new TypeCode(
65         TCKind.tk_longdouble);
66
67     public static final TypeCode STRING = new TypeCode(TCKind.tk_string);
68
69     public static final TypeCode WSTRING = new TypeCode(TCKind.tk_wstring);
70
71     public static final TypeCode OBJREF = new TypeCode(TCKind.tk_objref);
72
73     public static final TypeCode TYPECODE = new TypeCode(TCKind.tk_TypeCode);
74
75     // -----------------------------------------------------------------------
76
// private data
77
// -----------------------------------------------------------------------
78

79     private TCKind JavaDoc _kind;
80
81     private String JavaDoc _name;
82
83     private String JavaDoc _type;
84
85     private String JavaDoc _id;
86
87     private String JavaDoc _label;
88
89     // content type, discriminator type, concrete base type,
90
// or other TypeCode for indirection.
91
private org.omg.CORBA.TypeCode JavaDoc _ref;
92
93     private String JavaDoc[] _member_name;
94
95     private org.omg.CORBA.TypeCode JavaDoc[] _member_type;
96
97     private org.omg.CORBA.Any JavaDoc[] _member_label;
98
99     private short[] _member_visibility;
100
101     private int _default;
102
103     private int _length;
104
105     private short _digits;
106
107     private short _scale;
108
109     private short _type_modifier;
110
111     private boolean _indirection;
112
113     // -----------------------------------------------------------------------
114
// public methods
115
// -----------------------------------------------------------------------
116
/**
117      * @param kind
118      */

119     public TypeCode(TCKind JavaDoc kind)
120     {
121         _kind = kind;
122         _default = -1;
123         if (kind.value() == TCKind._tk_objref)
124         {
125             _type = "Object";
126         }
127     }
128
129     /**
130      * @param tc
131      * @return
132      */

133     public boolean equal(org.omg.CORBA.TypeCode JavaDoc tc)
134     {
135         if (_indirection)
136         {
137             return _ref.equal(tc);
138         }
139         try
140         {
141             int tk = _kind.value();
142             if (tk != tc.kind().value())
143             {
144                 return false;
145             }
146             // TODO: compare id()
147
if (_member_name != null)
148             {
149                 int n = _member_name.length;
150                 if (n != tc.member_count())
151                 {
152                     return false;
153                 }
154                 for (int i = 0; i < n; i++)
155                 {
156                     if (!equalIfNotEmpty(member_name(i), tc.member_name(i)))
157                     {
158                         return false;
159                     }
160                     if (!member_type(i).equal(tc.member_type(i)))
161                     {
162                         return false;
163                     }
164                 }
165             }
166             if (tk == TCKind._tk_union)
167             {
168                 if (!discriminator_type().equal(tc.discriminator_type()))
169                 {
170                     return false;
171                 }
172                 int n = _member_name.length;
173                 for (int i = 0; i < n; i++)
174                 {
175                     if (!member_label(i).equal(tc.member_label(i)))
176                     {
177                         return false;
178                     }
179                 }
180             }
181             if (tk == TCKind._tk_array
182                 ||
183                 tk == TCKind._tk_sequence
184                 ||
185                 tk == TCKind._tk_string
186                 || tk == TCKind._tk_wstring)
187             {
188                 if (length() != tc.length())
189                 {
190                     return false;
191                 }
192             }
193             if (tk == TCKind._tk_alias
194                 ||
195                 tk == TCKind._tk_array
196                 || tk == TCKind._tk_sequence)
197             {
198                 if (!content_type().equal(tc.content_type()))
199                 {
200                     return false;
201                 }
202             }
203             return true;
204         }
205         catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc ex)
206         {
207             throw new org.omg.CORBA.UNKNOWN JavaDoc(ex.toString());
208         }
209         catch (org.omg.CORBA.TypeCodePackage.Bounds JavaDoc ex)
210         {
211             throw new org.omg.CORBA.UNKNOWN JavaDoc(ex.toString());
212         }
213     }
214
215     /**
216      * @param tc
217      * @return
218      */

219     public boolean equivalent
220         (org.omg.CORBA.TypeCode JavaDoc tc)
221     {
222         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
223     }
224
225     /**
226      * @return
227      */

228     public org.omg.CORBA.TypeCode JavaDoc get_compact_typecode()
229     {
230         throw new org.omg.CORBA.NO_IMPLEMENT JavaDoc();
231     }
232
233     /**
234      * @param a
235      * @param b
236      * @return
237      */

238     private boolean equalIfNotEmpty(String JavaDoc a, String JavaDoc b)
239     {
240         if (a.length() == 0 || b.length() == 0)
241         {
242             return true;
243         }
244         else
245         {
246             return a.equals(b);
247         }
248     }
249
250     /**
251      * @return
252      */

253     public TCKind JavaDoc kind()
254     {
255         if (_indirection)
256         {
257             return _ref.kind();
258         }
259         return _kind;
260     }
261
262     /**
263      * @return
264      * @throws BadKind
265      */

266     public String JavaDoc id()
267         throws BadKind JavaDoc
268     {
269         if (_indirection)
270         {
271             return _ref.id();
272         }
273         if (_id != null)
274         {
275             return _id;
276         }
277         if (_type != null && _type.equals("Object"))
278         {
279             return "";
280         }
281         return default_id();
282     }
283
284     /**
285      * @param id
286      */

287     public void id(String JavaDoc id)
288     {
289         if (!id.equals(""))
290         {
291             _id = id;
292             if (id.startsWith("IDL:") && id.endsWith(":1.0"))
293             {
294                 // Infer _type field from standard IDL format _id
295
id = id.substring(4, id.length() - 4);
296                 if (id.startsWith("omg.org/"))
297                 {
298                     id = id.substring(8);
299                 }
300                 _type = "";
301                 for (; ;)
302                 {
303                     int slash = id.indexOf('/');
304                     if (slash == -1)
305                     {
306                         break;
307                     }
308                     _type = _type + id.substring(0, slash) + "::";
309                     id = id.substring(slash + 1);
310                 }
311                 _type = _type + id;
312             }
313         }
314     }
315
316     /**
317      * @return
318      * @throws BadKind
319      */

320     public String JavaDoc name()
321         throws BadKind JavaDoc
322     {
323         if (_indirection)
324         {
325             return _ref.name();
326         }
327         /* TODO?
328         if (_name == null)
329         {
330             _name = (String)_names.get(new Integer(_kind.value()));
331         }
332         */

333         if (_name == null)
334         {
335             throw new BadKind JavaDoc();
336         }
337         return _name;
338     }
339
340     /**
341      * @param name
342      */

343     public void name(String JavaDoc name)
344     {
345         _name = name;
346     }
347
348     /**
349      * @return
350      * @throws BadKind
351      */

352     public int member_count()
353         throws BadKind JavaDoc
354     {
355         if (_indirection)
356         {
357             return _ref.member_count();
358         }
359         if (_member_name == null)
360         {
361             throw new BadKind JavaDoc();
362         }
363         return _member_name.length;
364     }
365
366     /**
367      * @param count
368      */

369     public void member_count(int count)
370     {
371         _member_name = new String JavaDoc[count];
372         _member_type = new org.omg.CORBA.TypeCode JavaDoc[count];
373         if (_kind.value() == TCKind._tk_union)
374         {
375             _member_label = new org.omg.CORBA.Any JavaDoc[count];
376         }
377         if (_kind.value() == TCKind._tk_value)
378         {
379             _member_visibility = new short[count];
380         }
381     }
382
383     /**
384      * @param index
385      * @return
386      * @throws BadKind
387      * @throws Bounds
388      */

389     public String JavaDoc member_name(int index)
390         throws BadKind JavaDoc, Bounds JavaDoc
391     {
392         if (_indirection)
393         {
394             return _ref.member_name(index);
395         }
396         if (_member_name == null)
397         {
398             throw new BadKind JavaDoc();
399         }
400         if (index < 0 || index >= _member_name.length)
401         {
402             throw new Bounds JavaDoc();
403         }
404         return _member_name[index];
405     }
406
407     /**
408      * @param index
409      * @param name
410      */

411     public void member_name(int index, String JavaDoc name)
412     {
413         _member_name[index] = name;
414     }
415
416     /**
417      * @param index
418      * @return
419      * @throws BadKind
420      * @throws Bounds
421      */

422     public org.omg.CORBA.TypeCode JavaDoc member_type(int index)
423         throws BadKind JavaDoc, Bounds JavaDoc
424     {
425         if (_indirection)
426         {
427             return _ref.member_type(index);
428         }
429         if (_member_type == null)
430         {
431             throw new BadKind JavaDoc();
432         }
433         if (index < 0 || index >= _member_type.length)
434         {
435             throw new Bounds JavaDoc();
436         }
437         return _member_type[index];
438     }
439
440     /**
441      * @param index
442      * @param type
443      */

444     public void member_type(int index, org.omg.CORBA.TypeCode JavaDoc type)
445     {
446         _member_type[index] = type;
447     }
448
449     /**
450      * @param index
451      * @return
452      * @throws BadKind
453      * @throws Bounds
454      */

455     public org.omg.CORBA.Any JavaDoc member_label(int index)
456         throws BadKind JavaDoc, Bounds JavaDoc
457     {
458         if (_indirection)
459         {
460             return _ref.member_label(index);
461         }
462         if (_member_label == null)
463         {
464             throw new BadKind JavaDoc();
465         }
466         if (index < 0 || index >= _member_label.length)
467         {
468             throw new Bounds JavaDoc();
469         }
470         return _member_label[index];
471     }
472
473     /**
474      * @param index
475      * @param label
476      */

477     public void member_label(int index, org.omg.CORBA.Any JavaDoc label)
478     {
479         _member_label[index] = label;
480     }
481
482     /**
483      * @return
484      * @throws BadKind
485      */

486     public org.omg.CORBA.TypeCode JavaDoc discriminator_type()
487         throws BadKind JavaDoc
488     {
489         if (_indirection)
490         {
491             return _ref.discriminator_type();
492         }
493         if (_ref == null
494             || _kind.value() != TCKind._tk_union)
495         {
496             throw new BadKind JavaDoc();
497         }
498         return _ref;
499     }
500
501     /**
502      * @param disc
503      */

504     public void discriminator_type(org.omg.CORBA.TypeCode JavaDoc disc)
505     {
506         _ref = disc;
507     }
508
509     /**
510      * @return
511      * @throws BadKind
512      */

513     public int default_index()
514         throws BadKind JavaDoc
515     {
516         if (_indirection)
517         {
518             return _ref.default_index();
519         }
520         if (_kind.value() != TCKind._tk_union)
521         {
522             throw new BadKind JavaDoc();
523         }
524         return _default;
525     }
526
527     /**
528      * @param index
529      */

530     public void default_index(int index)
531     {
532         _default = index;
533         if (index != -1)
534         {
535             // We must store the member label for the discriminator type
536
// as some legal value, so we use zero.
537
_member_label[index] = new Any(_ref, "0");
538         }
539     }
540
541     /**
542      * @return
543      * @throws BadKind
544      */

545     public int length()
546         throws BadKind JavaDoc
547     {
548         if (_indirection)
549         {
550             return _ref.length();
551         }
552         int tk = _kind.value();
553         if (tk != TCKind._tk_string &&
554             tk != TCKind._tk_wstring
555             && tk != TCKind._tk_sequence && tk != TCKind._tk_array)
556         {
557             throw new BadKind JavaDoc();
558         }
559         return _length;
560     }
561
562     /**
563      * @param length
564      */

565     public void length(int length)
566     {
567         _length = length;
568     }
569
570     /**
571      * @return
572      * @throws BadKind
573      */

574     public org.omg.CORBA.TypeCode JavaDoc content_type()
575         throws BadKind JavaDoc
576     {
577         if (_indirection)
578         {
579             return _ref.content_type();
580         }
581         int tk = _kind.value();
582         if (_ref == null
583             || (tk != TCKind._tk_alias
584             &&
585             tk != TCKind._tk_array
586             &&
587             tk != TCKind._tk_sequence
588             && tk != TCKind._tk_value_box))
589         {
590             throw new BadKind JavaDoc();
591         }
592         return _ref;
593     }
594
595     /**
596      * @param type
597      */

598     public void content_type(org.omg.CORBA.TypeCode JavaDoc type)
599     {
600         _ref = type;
601     }
602
603     /**
604      * @return
605      * @throws BadKind
606      */

607     public short fixed_digits()
608         throws BadKind JavaDoc
609     {
610         if (_indirection)
611         {
612             return _ref.fixed_digits();
613         }
614         int tk = _kind.value();
615         if (tk != TCKind._tk_fixed)
616         {
617             throw new BadKind JavaDoc();
618         }
619         return _digits;
620     }
621
622     /**
623      * @param digits
624      */

625     public void fixed_digits(short digits)
626     {
627         _digits = digits;
628     }
629
630     /**
631      * @return
632      * @throws BadKind
633      */

634     public short fixed_scale()
635         throws BadKind JavaDoc
636     {
637         if (_indirection)
638         {
639             return _ref.fixed_scale();
640         }
641         int tk = _kind.value();
642         if (tk != TCKind._tk_fixed)
643         {
644             throw new BadKind JavaDoc();
645         }
646         return _scale;
647     }
648
649     /**
650      * @param scale
651      */

652     public void fixed_scale(short scale)
653     {
654         _scale = scale;
655     }
656
657     /**
658      * @param index
659      * @return
660      * @throws BadKind
661      * @throws Bounds
662      */

663     public short member_visibility
664         (int index)
665         throws BadKind JavaDoc, Bounds JavaDoc
666     {
667         if (_indirection)
668         {
669             return _ref.member_visibility(index);
670         }
671         if (_member_type == null)
672         {
673             throw new BadKind JavaDoc();
674         }
675         if (index < 0 || index >= _member_visibility.length)
676         {
677             throw new Bounds JavaDoc();
678         }
679         return _member_visibility[index];
680     }
681
682     /**
683      * @param index
684      * @param visibility
685      */

686     public void member_visibility(int index, short visibility)
687     {
688         _member_visibility[index] = visibility;
689     }
690
691     /**
692      * @return
693      * @throws BadKind
694      */

695     public short type_modifier()
696         throws BadKind JavaDoc
697     {
698         if (_indirection)
699         {
700             return _ref.type_modifier();
701         }
702         int tk = _kind.value();
703         if (tk != TCKind._tk_value)
704         {
705             throw new BadKind JavaDoc();
706         }
707         return _type_modifier;
708     }
709
710     /**
711      * @param modifier
712      */

713     public void type_modifier(short modifier)
714     {
715         _type_modifier = modifier;
716     }
717
718     /**
719      * @return
720      * @throws BadKind
721      */

722     public org.omg.CORBA.TypeCode JavaDoc concrete_base_type()
723         throws BadKind JavaDoc
724     {
725         if (_indirection)
726         {
727             return _ref.concrete_base_type();
728         }
729         int tk = _kind.value();
730         if (tk != TCKind._tk_value)
731         {
732             throw new BadKind JavaDoc();
733         }
734         return _ref;
735     }
736
737     /**
738      * @param base
739      */

740     public void concrete_base_type(org.omg.CORBA.TypeCode JavaDoc base)
741     {
742         _ref = base;
743     }
744
745     /**
746      * @param ref
747      */

748     public void indirection(org.omg.CORBA.TypeCode JavaDoc ref)
749     {
750         _ref = ref;
751         _indirection = true;
752     }
753
754     /**
755      * @param id
756      */

757     public void recursive(String JavaDoc id)
758     {
759         _id = id;
760         _ref = null;
761         _indirection = true;
762     }
763
764     /**
765      *
766      */

767     public void fix_recursive_members()
768     {
769         String JavaDoc id = _id == null ? default_id() : _id;
770         int n = _member_type.length;
771         for (int i = 0; i < n; i++)
772         {
773             TypeCode mt = (TypeCode) _member_type[i];
774             if (mt._kind.value() == TCKind._tk_sequence)
775             {
776                 TypeCode ct = (TypeCode) mt._ref;
777                 if (ct._indirection
778                     &&
779                     ct._ref == null
780                     && ct._id.equals(id))
781                 {
782                     ct._ref = this;
783                 }
784             }
785         }
786     }
787
788     // -----------------------------------------------------------------------
789
// private methods
790
// -----------------------------------------------------------------------
791
/**
792      * @return
793      */

794     private String JavaDoc default_id()
795     {
796         // Take _type, and generate _id, e.g.
797
// if _type = "SessionManager::Manager",
798
// then _id = "IDL:SessionManager/Manager:1.0".
799
if (_type == null)
800         {
801             return "";
802         }
803         StringBuffer JavaDoc id = new StringBuffer JavaDoc(_type.length() + 10);
804         id.append("IDL:");
805         int n = _type.length();
806         for (int i = 0; i < n; i++)
807         {
808             char c = _type.charAt(i);
809             if (c == ':' && i + 1 < n && _type.charAt(i + 1) == ':')
810             {
811                 i++;
812             }
813             id.append(c == ':' ? '/' : c);
814         }
815         id.append(":1.0");
816         return id.toString();
817     }
818 }
819
Popular Tags