KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > db > pse > 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.pse.offers;
15
16
17 import org.omg.CORBA.*;
18 import jtport.ORBLayer;
19
20
21 /**
22  * TypeCodeValue represents a persistent CORBA TypeCode object
23  */

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

309 }
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
Popular Tags