KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > db > simple > offers > TypeCodeValue


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

14 package org.jacorb.trading.db.simple.offers;
15
16 /**
17  * TypeCodeValue represents a serialized CORBA TypeCode object
18  */

19
20 import java.io.*;
21 import java.util.*;
22 import org.omg.CORBA.*;
23
24 public class TypeCodeValue implements Serializable
25 {
26     // not the most space-efficient representation, but the alternative
27
// is a bunch of subclasses
28
private int m_kind;
29     private String JavaDoc m_id;
30     private String JavaDoc m_name;
31     private int m_length;
32     private TypeCodeValue m_content;
33     private java.lang.Object JavaDoc m_arg;
34     private transient TypeCode m_typeCode;
35
36     static final long serialVersionUID = -2067446388881696087L;
37
38
39     private TypeCodeValue()
40     {
41     }
42
43
44     public TypeCodeValue(TypeCode tc)
45     {
46     setValue(tc);
47     }
48
49
50     public TypeCode getValue()
51     {
52     TypeCode result = m_typeCode;
53
54     if (result == null) {
55         ORB orb = org.omg.CORBA.ORB.init();
56
57         switch (m_kind) {
58         // the primitive types
59
case TCKind._tk_null:
60         case TCKind._tk_void:
61         case TCKind._tk_short:
62         case TCKind._tk_ushort:
63         case TCKind._tk_long:
64         case TCKind._tk_ulong:
65         case TCKind._tk_float:
66         case TCKind._tk_double:
67         case TCKind._tk_boolean:
68         case TCKind._tk_char:
69         case TCKind._tk_octet:
70         case TCKind._tk_any:
71         case TCKind._tk_TypeCode:
72         case TCKind._tk_Principal:
73         result = orb.get_primitive_tc(TCKind.from_int(m_kind));
74         break;
75
76         case TCKind._tk_string:
77         result = orb.create_string_tc(m_length);
78         break;
79
80         case TCKind._tk_sequence:
81         result = orb.create_sequence_tc(m_length, m_content.getValue());
82         break;
83
84         case TCKind._tk_array:
85         result = orb.create_array_tc(m_length, m_content.getValue());
86         break;
87
88         case TCKind._tk_alias:
89         result = orb.create_alias_tc(m_id, m_name, m_content.getValue());
90         break;
91
92         case TCKind._tk_objref:
93         result = orb.create_interface_tc(m_id, m_name);
94         break;
95
96         case TCKind._tk_longlong:
97         case TCKind._tk_ulonglong:
98         case TCKind._tk_longdouble:
99         case TCKind._tk_wstring:
100         case TCKind._tk_wchar:
101         case TCKind._tk_fixed:
102         case TCKind._tk_except:
103         case TCKind._tk_struct:
104         case TCKind._tk_union:
105         throw new RuntimeException JavaDoc("Unsupported type");
106
107         default:
108         throw new RuntimeException JavaDoc("Unexpected type");
109         }
110
111         // keep it for next time
112
m_typeCode = result;
113     }
114
115     return result;
116     }
117
118
119     protected void setValue(TypeCode tc)
120     {
121     TCKind kind = tc.kind();
122
123     // initialize members
124
m_kind = kind.value();
125     m_id = null;
126     m_name = null;
127     m_length = 0;
128     m_content = null;
129     m_arg = null;
130     m_typeCode = tc;
131
132
133     try {
134         switch (kind.value()) {
135         // the primitive types
136
case TCKind._tk_null:
137         case TCKind._tk_void:
138         case TCKind._tk_short:
139         case TCKind._tk_ushort:
140         case TCKind._tk_long:
141         case TCKind._tk_ulong:
142         case TCKind._tk_float:
143         case TCKind._tk_double:
144         case TCKind._tk_boolean:
145         case TCKind._tk_char:
146         case TCKind._tk_octet:
147         case TCKind._tk_any:
148         case TCKind._tk_TypeCode:
149         case TCKind._tk_Principal:
150         // nothing to do
151
break;
152
153         case TCKind._tk_string:
154         m_length = tc.length();
155         break;
156
157         case TCKind._tk_sequence:
158         m_length = tc.length();
159         m_content = new TypeCodeValue(tc.content_type());
160         break;
161
162         case TCKind._tk_array:
163         m_length = tc.length();
164         m_content = new TypeCodeValue(tc.content_type());
165         break;
166
167         case TCKind._tk_alias:
168         m_id = tc.id();
169         m_name = tc.name();
170         m_content = new TypeCodeValue(tc.content_type());
171         break;
172
173         case TCKind._tk_objref:
174         m_id = tc.id();
175         m_name = tc.name();
176         break;
177
178         case TCKind._tk_longlong:
179         case TCKind._tk_ulonglong:
180         case TCKind._tk_longdouble:
181         case TCKind._tk_wstring:
182         case TCKind._tk_wchar:
183         case TCKind._tk_fixed:
184         case TCKind._tk_except:
185         case TCKind._tk_struct:
186         case TCKind._tk_union:
187         throw new RuntimeException JavaDoc("Unsupported type");
188
189         default:
190         throw new RuntimeException JavaDoc("Unexpected type");
191         }
192     }
193     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
194         throw new RuntimeException JavaDoc(e.getMessage());
195     }
196     }
197
198
199     private void readObject(ObjectInputStream in)
200     throws IOException, ClassNotFoundException JavaDoc
201     {
202     in.defaultReadObject();
203     m_typeCode = null;
204     }
205
206
207     /*********************** comment out to enable main()
208
209                  public static void main(String[] args)
210                  {
211                  ORB orb = ORBLayer.instance().init(args);
212
213                  // test the primitives
214
215                  testType("null", orb.get_primitive_tc(TCKind.tk_null));
216                  testType("void", orb.get_primitive_tc(TCKind.tk_void));
217                  testType("short", orb.get_primitive_tc(TCKind.tk_short));
218                  testType("ushort", orb.get_primitive_tc(TCKind.tk_ushort));
219                  testType("long", orb.get_primitive_tc(TCKind.tk_long));
220                  testType("ulong", orb.get_primitive_tc(TCKind.tk_ulong));
221                  testType("float", orb.get_primitive_tc(TCKind.tk_float));
222                  testType("double", orb.get_primitive_tc(TCKind.tk_double));
223                  testType("boolean", orb.get_primitive_tc(TCKind.tk_boolean));
224                  testType("char", orb.get_primitive_tc(TCKind.tk_char));
225                  testType("octet", orb.get_primitive_tc(TCKind.tk_octet));
226                  //testType("longlong", orb.get_primitive_tc(TCKind.tk_longlong));
227                  //testType("ulonglong", orb.get_primitive_tc(TCKind.tk_ulonglong));
228                  //testType("longdouble", orb.get_primitive_tc(TCKind.tk_longdouble));
229                  //testType("wchar", orb.get_primitive_tc(TCKind.tk_wchar));
230                  //testType("fixed", orb.get_primitive_tc(TCKind.tk_fixed));
231                  testType("any", orb.get_primitive_tc(TCKind.tk_any));
232                  testType("TypeCode", orb.get_primitive_tc(TCKind.tk_TypeCode));
233                  testType("Principal", orb.get_primitive_tc(TCKind.tk_Principal));
234
235                  TypeCode tc;
236
237                  // test alias
238
239                  tc = orb.create_alias_tc("ID", "Name",
240                  orb.get_primitive_tc(TCKind.tk_double));
241                  testType("alias", tc);
242
243                  // test interface
244
245                  tc = orb.create_interface_tc("ID", "Name");
246                  testType("objref", tc);
247
248                  // test string
249
250                  tc = orb.create_string_tc(10);
251                  testType("string", tc);
252
253                  // test sequence
254
255                  tc = orb.create_sequence_tc(20, orb.get_primitive_tc(TCKind.tk_long));
256                  testType("sequence", tc);
257
258                  // test array
259
260                  tc = orb.create_array_tc(20, orb.get_primitive_tc(TCKind.tk_ulong));
261                  testType("array", tc);
262                  }
263
264
265                  protected static void testType(String name, TypeCode tc)
266                  {
267                  // test a TypeCodeValue by writing it out, reading it back in,
268                  // and comparing its TypeCode to the one we've been given
269
270                  System.out.println("Testing " + name + "...");
271
272                  try {
273                  TypeCodeValue val = new TypeCodeValue(tc);
274                  File f = new File("tctest.dat");
275
276                  FileOutputStream fileOut = new FileOutputStream(f);
277                  ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
278                  objOut.writeObject(val);
279                  fileOut.close();
280
281                  FileInputStream fileIn = new FileInputStream(f);
282                  ObjectInputStream objIn = new ObjectInputStream(fileIn);
283                  val = (TypeCodeValue)objIn.readObject();
284                  fileIn.close();
285
286                  f.delete();
287
288                  TypeCode newTC = val.getValue();
289                  if (! newTC.equal(tc)) {
290                  System.out.println("Read/write mismatch");
291                  System.exit(1);
292                  }
293                  }
294                  catch (IOException e) {
295                  System.err.println("I/O error: " + e.getMessage());
296                  System.exit(1);
297                  }
298                  catch (ClassNotFoundException e) {
299                  System.err.println("Class not found: " + e.getMessage());
300                  System.exit(1);
301                  }
302                  }
303
304                  /*********************** comment out to enable main() */

305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
Popular Tags