KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > iiop > TypeCodeImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.iiop;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import org.omg.CORBA.Any JavaDoc;
35 import org.omg.CORBA.TCKind JavaDoc;
36 import org.omg.CORBA.TypeCode JavaDoc;
37 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
38 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
39
40 import java.io.IOException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 public class TypeCodeImpl extends TypeCode JavaDoc {
45   protected static final L10N L = new L10N(TypeCodeImpl.class);
46   protected static final Logger JavaDoc log = Log.open(TypeCodeImpl.class);
47
48   public static final TypeCode JavaDoc TK_NULL = new TypeCodeImpl(TCKind.tk_null);
49   
50   public static final int TK_VOID = 1;
51   public static final int TK_SHORT = 2;
52   
53   public static final TypeCode JavaDoc TK_LONG = new TypeCodeImpl(TCKind.tk_long);
54   
55   public static final int TK_USHORT = 4;
56   public static final int TK_ULONG = 5;
57   public static final int TK_FLOAT = 6;
58   public static final int TK_DOUBLE = 7;
59   public static final int TK_BOOLEAN = 8;
60   public static final int TK_CHAR = 9;
61   
62   public static final TypeCode JavaDoc TK_OCTET = new TypeCodeImpl(TCKind.tk_octet);
63
64   public static final int TK_ANY = 11;
65   public static final int TK_TYPE_CODE = 12;
66   public static final int TK_PRINCIPAL = 13;
67   public static final int TK_OBJREF = 14;
68   public static final int TK_UNION = 16;
69   public static final int TK_ENUM = 17;
70   public static final int TK_STRING = 18;
71   public static final int TK_SEQUENCE = 19;
72   public static final int TK_ARRAY = 20;
73   public static final int TK_ALIAS = 21;
74   public static final int TK_EXCEPT = 22;
75   public static final int TK_LONGLONG = 23;
76   public static final int TK_ULONGLONG = 24;
77   public static final int TK_LONGDOUBLE = 25;
78   public static final int TK_WCHAR = 26;
79   public static final int TK_WSTRING = 27;
80   public static final int TK_FIXED = 28;
81   public static final int TK_VALUE = 29;
82   public static final int TK_VALUE_BOX = 30;
83   public static final int TK_NATIVE = 31;
84   public static final int TK_ABSTRACT_INTERFACE = 32;
85
86   private TCKind JavaDoc _kind;
87
88   private String JavaDoc _id;
89   
90   private short _typeModifier;
91   private TypeCode JavaDoc _concreteBaseType;
92   private ArrayList JavaDoc<Member> _members = new ArrayList JavaDoc<Member>();
93
94   private TypeCode JavaDoc _contentType;
95   private int _length;
96
97   TypeCodeImpl(TCKind JavaDoc kind)
98   {
99     if (kind == null)
100       throw new NullPointerException JavaDoc();
101     
102     _kind = kind;
103   }
104
105   private TypeCodeImpl(TCKind JavaDoc kind, String JavaDoc repId, short modifier)
106   {
107     _kind = kind;
108     _id = repId;
109     _typeModifier = modifier;
110   }
111
112   private TypeCodeImpl(TCKind JavaDoc kind, String JavaDoc repId)
113   {
114     _kind = kind;
115     _id = repId;
116   }
117
118   static TypeCodeImpl createValue(String JavaDoc repId, String JavaDoc name, short modifier)
119   {
120     return new TypeCodeImpl(TCKind.tk_value, repId, modifier);
121   }
122
123   static TypeCodeImpl createValueBox(String JavaDoc repId, String JavaDoc name)
124   {
125     return new TypeCodeImpl(TCKind.tk_value_box, repId);
126   }
127
128   static TypeCodeImpl createAbstractInterface(String JavaDoc repId, String JavaDoc name)
129   {
130     return new TypeCodeImpl(TCKind.tk_abstract_interface, repId);
131   }
132
133   static TypeCodeImpl createSequence()
134   {
135     return new TypeCodeImpl(TCKind.tk_sequence);
136   }
137
138   /**
139    * Returns true if the type codes are equal.
140    */

141   public boolean equal(TypeCode JavaDoc tc)
142   {
143     return tc == this;
144   }
145
146   /**
147    * Returns true if the type codes are equivalent.
148    */

149   public boolean equivalent(TypeCode JavaDoc tc)
150   {
151     return tc == this;
152   }
153
154   /**
155    * Returns the compact typecode
156    */

157   public TypeCode JavaDoc get_compact_typecode()
158   {
159     return this;
160   }
161
162   /**
163    * Returns the kind of the type code.
164    */

165   public TCKind JavaDoc kind()
166   {
167     return _kind;
168   }
169
170   /**
171    * Returns the rep-id of the typecode.
172    */

173   public String JavaDoc id()
174     throws BadKind JavaDoc
175   {
176     return _id;
177   }
178
179   /**
180    * Returns the simple name withing the scope.
181    */

182   public String JavaDoc name()
183     throws BadKind JavaDoc
184   {
185     return "";
186   }
187   
188   /**
189    * Returns the type modifier.
190    */

191   public short type_modifier()
192     throws BadKind JavaDoc
193   {
194     return _typeModifier;
195   }
196   
197   /**
198    * Set the type code of the concrete base
199    */

200   void setConcreteBaseType(TypeCode JavaDoc baseType)
201   {
202     _concreteBaseType = baseType;
203   }
204   
205   /**
206    * Returns the type code of the concrete base
207    */

208   public TypeCode JavaDoc concrete_base_type()
209     throws BadKind JavaDoc
210   {
211     return _concreteBaseType;
212   }
213
214   
215   /**
216    * Returns the number of fields in the type code
217    */

218   public int member_count()
219     throws BadKind JavaDoc
220   {
221     return _members.size();
222   }
223
224   void addMember(String JavaDoc name, TypeCode JavaDoc type, short visibility)
225   {
226     _members.add(new Member(name, type, visibility));
227   }
228
229   /**
230    * Returns the field name for the given index.
231    */

232   public String JavaDoc member_name(int index)
233     throws BadKind JavaDoc, Bounds JavaDoc
234   {
235     return _members.get(index).getName();
236   }
237
238   /**
239    * Returns the typecode for the member type.
240    */

241   public TypeCode JavaDoc member_type(int index)
242     throws BadKind JavaDoc, Bounds JavaDoc
243   {
244     return _members.get(index).getType();
245   }
246
247   /**
248    * Returns the visibility status of the given member.
249    */

250   public short member_visibility(int index)
251     throws BadKind JavaDoc, Bounds JavaDoc
252   {
253     throw new UnsupportedOperationException JavaDoc();
254   }
255
256   /**
257    * Returns the label for the given index.
258    */

259   public Any JavaDoc member_label(int index)
260     throws BadKind JavaDoc, Bounds JavaDoc
261   {
262     return null;
263   }
264
265   /**
266    * Returns the discriminator type for the given index.
267    */

268   public TypeCode JavaDoc discriminator_type()
269     throws BadKind JavaDoc
270   {
271     throw new UnsupportedOperationException JavaDoc();
272   }
273
274   /**
275    * Returns the default member.
276    */

277   public int default_index()
278     throws BadKind JavaDoc
279   {
280     throw new UnsupportedOperationException JavaDoc();
281   }
282
283   void setLength(int length)
284   {
285     _length = length;
286   }
287   
288   /**
289    * Returns the number of elements.
290    */

291   public int length()
292     throws BadKind JavaDoc
293   {
294     return _length;
295   }
296
297   /**
298    * Returns the typecode for the content type.
299    */

300   void setContentType(TypeCode JavaDoc type)
301   {
302     _contentType = type;
303   }
304
305   /**
306    * Returns the typecode for the content type.
307    */

308   public TypeCode JavaDoc content_type()
309     throws BadKind JavaDoc
310   {
311     return _contentType;
312   }
313
314   /**
315    * Returns the number of digits in the fixed type.
316    */

317   public short fixed_digits()
318     throws BadKind JavaDoc
319   {
320     throw new UnsupportedOperationException JavaDoc();
321   }
322
323   /**
324    * Returns the scale of the fixed type.
325    */

326   public short fixed_scale()
327     throws BadKind JavaDoc
328   {
329     throw new UnsupportedOperationException JavaDoc();
330   }
331
332   /**
333    * Reads the type code.
334    */

335   public static TypeCodeImpl read(IiopReader reader)
336     throws IOException JavaDoc
337   {
338     int tcKind = reader.readInt();
339
340     // System.out.println("KIND: " + TCKind.from_int(tcKind));
341
switch (tcKind) {
342     case TK_VALUE_BOX:
343       {
344     int len = reader.read_sequence_length();
345     int endian = reader.read_octet();
346
347     String JavaDoc repId = reader.readString();
348     String JavaDoc name = reader.readString();
349     TypeCode JavaDoc typeCode = reader.read_TypeCode();
350
351     return new ValueBoxTypeCode(TCKind.from_int(tcKind), typeCode);
352       }
353       
354     case TK_ABSTRACT_INTERFACE:
355       {
356     int len = reader.read_sequence_length();
357     
358     int offset = reader.getOffset();
359     int tail = offset + len;
360
361     int endian = reader.read_octet();
362
363     String JavaDoc repId = reader.readString();
364     String JavaDoc name = reader.readString();
365     
366     /*
367     int direction = reader.readInt();
368     while (reader.getOffset() < tail) {
369       TypeCode subCode = reader.read_TypeCode();
370     }
371     */

372
373     return new AbstractBaseTypeCode(TCKind.from_int(tcKind));
374       }
375
376     case TK_STRING:
377       {
378     int maxLen = reader.read_ulong();
379     log.info("string len: " + maxLen);
380     return new TypeCodeImpl(TCKind.from_int(tcKind));
381       }
382       
383     case 0xffffffff:
384       throw new UnsupportedOperationException JavaDoc("INDIRECTION");
385       
386     default:
387       throw new UnsupportedOperationException JavaDoc();
388     }
389   }
390
391   /**
392    * Reads the value from the .
393    */

394   public Object JavaDoc readValue(IiopReader reader)
395     throws IOException JavaDoc
396   {
397     log.fine("READ: " + this);
398     
399     switch (_kind.value()) {
400     case TCKind._tk_null:
401     case TCKind._tk_void:
402       return null;
403       
404     case TCKind._tk_long:
405       return new Integer JavaDoc(reader.read_long());
406       
407     case TCKind._tk_ulong:
408       return new Integer JavaDoc(reader.read_ulong());
409       
410     case TCKind._tk_string:
411       return reader.read_string();
412       
413     case TCKind._tk_wstring:
414       return reader.read_wstring();
415       
416     case TK_ABSTRACT_INTERFACE:
417       return reader.read_abstract_interface();
418
419     case TCKind._tk_value:
420       return reader.read_value();
421
422     case TCKind._tk_value_box:
423       return reader.read_value();
424       
425     default:
426       throw new UnsupportedOperationException JavaDoc(String.valueOf(this));
427     }
428   }
429
430   public String JavaDoc toString()
431   {
432     return "TypeCodeImpl[" + _kind.value() + "]";
433   }
434   
435   static class Member {
436     private String JavaDoc _name;
437     private TypeCode JavaDoc _type;
438     private short _visibility;
439
440     Member(String JavaDoc name, TypeCode JavaDoc type, short visibility)
441     {
442       _name = name;
443       _type = type;
444       _visibility = visibility;
445     }
446
447     String JavaDoc getName()
448     {
449       return _name;
450     }
451
452     TypeCode JavaDoc getType()
453     {
454       return _type;
455     }
456
457     short getVisibility()
458     {
459       return _visibility;
460     }
461   }
462
463 }
464
Popular Tags