KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > dynamicany > DynAnyComplexImpl


1 /*
2  * @(#)DynAnyComplexImpl.java 1.10 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.dynamicany;
9
10 import org.omg.CORBA.TypeCode JavaDoc;
11 import org.omg.CORBA.TCKind JavaDoc;
12 import org.omg.CORBA.Any JavaDoc;
13 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
14 import org.omg.CORBA.TypeCodePackage.Bounds JavaDoc;
15 import org.omg.CORBA.portable.InputStream JavaDoc;
16 import org.omg.DynamicAny.*;
17 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc;
18 import org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc;
19 import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc;
20
21 import com.sun.corba.se.spi.orb.ORB ;
22 import com.sun.corba.se.spi.logging.CORBALogDomains ;
23 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
24
25 abstract class DynAnyComplexImpl extends DynAnyConstructedImpl
26 {
27     //
28
// Instance variables
29
//
30

31     String JavaDoc[] names = null;
32     // Instance variables components and names above are kept in sync
33
// with these two arrays at all times.
34
NameValuePair[] nameValuePairs = null;
35     NameDynAnyPair[] nameDynAnyPairs = null;
36
37     //
38
// Constructors
39
//
40

41     private DynAnyComplexImpl() {
42         this(null, (Any JavaDoc)null, false);
43     }
44
45     protected DynAnyComplexImpl(ORB orb, Any JavaDoc any, boolean copyValue) {
46         // We can be sure that typeCode is of kind tk_struct
47
super(orb, any, copyValue);
48         // Initialize components lazily, on demand.
49
// This is an optimization in case the user is only interested in storing Anys.
50
}
51
52     protected DynAnyComplexImpl(ORB orb, TypeCode JavaDoc typeCode) {
53         // We can be sure that typeCode is of kind tk_struct
54
super(orb, typeCode);
55         // For DynAnyComplex, the operation sets the current position to -1
56
// for empty exceptions and to zero for all other TypeCodes.
57
// The members (if any) are (recursively) initialized to their default values.
58
index = 0;
59     }
60
61     //
62
// DynAny interface methods
63
//
64

65     // _REVISIT_ Overridden to provide more efficient copying.
66
// Copies all the internal representations which is faster than reconstructing them.
67
/*
68     public org.omg.DynamicAny.DynAny copy() {
69         if (status == STATUS_DESTROYED) {
70             throw new OBJECT_NOT_EXIST();
71         }
72         DynAnyComplexImpl returnValue = null;
73         if ((representations & REPRESENTATION_ANY) != 0) {
74             // The flag "true" indicates copying the Any value
75             returnValue = (DynAnyComplexImpl)DynAnyUtil.createMostDerivedDynAny(any, orb, true);
76         }
77         if ((representations & REPRESENTATION_COMPONENTS) != 0) {
78         }
79         return returnValue;
80     }
81 */

82
83     //
84
// Complex methods
85
//
86

87     public String JavaDoc current_member_name ()
88         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc,
89                org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
90     {
91         if (status == STATUS_DESTROYED) {
92         throw wrapper.dynAnyDestroyed() ;
93         }
94         if( ! checkInitComponents() || index < 0 || index >= names.length) {
95             throw new InvalidValue JavaDoc();
96         }
97         return names[index];
98     }
99
100     public TCKind JavaDoc current_member_kind ()
101         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc,
102                org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
103     {
104         if (status == STATUS_DESTROYED) {
105         throw wrapper.dynAnyDestroyed() ;
106         }
107         if( ! checkInitComponents() || index < 0 || index >= components.length) {
108             throw new InvalidValue JavaDoc();
109         }
110         return components[index].type().kind();
111     }
112
113     // Creates references to the parameter instead of copying it.
114
public void set_members (org.omg.DynamicAny.NameValuePair JavaDoc[] value)
115         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc,
116                org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
117     {
118         if (status == STATUS_DESTROYED) {
119         throw wrapper.dynAnyDestroyed() ;
120         }
121         if (value == null || value.length == 0) {
122             clearData();
123             return;
124         }
125
126         Any JavaDoc memberAny;
127         DynAny memberDynAny = null;
128         String JavaDoc memberName;
129         // We know that this is of kind tk_struct
130
TypeCode JavaDoc expectedTypeCode = any.type();
131
132         int expectedMemberCount = 0;
133         try {
134             expectedMemberCount = expectedTypeCode.member_count();
135         } catch (BadKind JavaDoc badKind) { // impossible
136
}
137         if (expectedMemberCount != value.length) {
138             clearData();
139             throw new InvalidValue JavaDoc();
140         }
141
142         allocComponents(value);
143
144         for (int i=0; i<value.length; i++) {
145             if (value[i] != null) {
146                 memberName = value[i].id;
147                 String JavaDoc expectedMemberName = null;
148                 try {
149                     expectedMemberName = expectedTypeCode.member_name(i);
150                 } catch (BadKind JavaDoc badKind) { // impossible
151
} catch (Bounds JavaDoc bounds) { // impossible
152
}
153                 if ( ! (expectedMemberName.equals(memberName) || memberName.equals(""))) {
154                     clearData();
155                     // _REVISIT_ More info
156
throw new TypeMismatch JavaDoc();
157                 }
158                 memberAny = value[i].value;
159                 TypeCode JavaDoc expectedMemberType = null;
160                 try {
161                     expectedMemberType = expectedTypeCode.member_type(i);
162                 } catch (BadKind JavaDoc badKind) { // impossible
163
} catch (Bounds JavaDoc bounds) { // impossible
164
}
165                 if (! expectedMemberType.equal(memberAny.type())) {
166                     clearData();
167                     // _REVISIT_ More info
168
throw new TypeMismatch JavaDoc();
169                 }
170                 try {
171                     // Creates the appropriate subtype without copying the Any
172
memberDynAny = DynAnyUtil.createMostDerivedDynAny(memberAny, orb, false);
173                 } catch (InconsistentTypeCode JavaDoc itc) {
174                     throw new InvalidValue JavaDoc();
175                 }
176                 addComponent(i, memberName, memberAny, memberDynAny);
177             } else {
178                 clearData();
179                 // _REVISIT_ More info
180
throw new InvalidValue JavaDoc();
181             }
182         }
183         index = (value.length == 0 ? NO_INDEX : 0);
184         representations = REPRESENTATION_COMPONENTS;
185     }
186
187     // Creates references to the parameter instead of copying it.
188
public void set_members_as_dyn_any (org.omg.DynamicAny.NameDynAnyPair JavaDoc[] value)
189         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc,
190                org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
191     {
192         if (status == STATUS_DESTROYED) {
193         throw wrapper.dynAnyDestroyed() ;
194         }
195         if (value == null || value.length == 0) {
196             clearData();
197             return;
198         }
199
200         Any JavaDoc memberAny;
201         DynAny memberDynAny;
202         String JavaDoc memberName;
203         // We know that this is of kind tk_struct
204
TypeCode JavaDoc expectedTypeCode = any.type();
205
206         int expectedMemberCount = 0;
207         try {
208             expectedMemberCount = expectedTypeCode.member_count();
209         } catch (BadKind JavaDoc badKind) { // impossible
210
}
211         if (expectedMemberCount != value.length) {
212             clearData();
213             throw new InvalidValue JavaDoc();
214         }
215
216         allocComponents(value);
217
218         for (int i=0; i<value.length; i++) {
219             if (value[i] != null) {
220                 memberName = value[i].id;
221                 String JavaDoc expectedMemberName = null;
222                 try {
223                     expectedMemberName = expectedTypeCode.member_name(i);
224                 } catch (BadKind JavaDoc badKind) { // impossible
225
} catch (Bounds JavaDoc bounds) { // impossible
226
}
227                 if ( ! (expectedMemberName.equals(memberName) || memberName.equals(""))) {
228                     clearData();
229                     // _REVISIT_ More info
230
throw new TypeMismatch JavaDoc();
231                 }
232                 memberDynAny = value[i].value;
233                 memberAny = getAny(memberDynAny);
234                 TypeCode JavaDoc expectedMemberType = null;
235                 try {
236                     expectedMemberType = expectedTypeCode.member_type(i);
237                 } catch (BadKind JavaDoc badKind) { // impossible
238
} catch (Bounds JavaDoc bounds) { // impossible
239
}
240                 if (! expectedMemberType.equal(memberAny.type())) {
241                     clearData();
242                     // _REVISIT_ More info
243
throw new TypeMismatch JavaDoc();
244                 }
245
246                 addComponent(i, memberName, memberAny, memberDynAny);
247             } else {
248                 clearData();
249                 // _REVISIT_ More info
250
throw new InvalidValue JavaDoc();
251             }
252         }
253         index = (value.length == 0 ? NO_INDEX : 0);
254         representations = REPRESENTATION_COMPONENTS;
255     }
256
257     //
258
// Utility methods
259
//
260

261     private void allocComponents(int length) {
262         components = new DynAny[length];
263         names = new String JavaDoc[length];
264         nameValuePairs = new NameValuePair[length];
265         nameDynAnyPairs = new NameDynAnyPair[length];
266         for (int i=0; i<length; i++) {
267             nameValuePairs[i] = new NameValuePair();
268             nameDynAnyPairs[i] = new NameDynAnyPair();
269         }
270     }
271
272     private void allocComponents(org.omg.DynamicAny.NameValuePair JavaDoc[] value) {
273         components = new DynAny[value.length];
274         names = new String JavaDoc[value.length];
275         nameValuePairs = value;
276         nameDynAnyPairs = new NameDynAnyPair[value.length];
277         for (int i=0; i<value.length; i++) {
278             nameDynAnyPairs[i] = new NameDynAnyPair();
279         }
280     }
281
282     private void allocComponents(org.omg.DynamicAny.NameDynAnyPair JavaDoc[] value) {
283         components = new DynAny[value.length];
284         names = new String JavaDoc[value.length];
285         nameValuePairs = new NameValuePair[value.length];
286         for (int i=0; i<value.length; i++) {
287             nameValuePairs[i] = new NameValuePair();
288         }
289         nameDynAnyPairs = value;
290     }
291
292     private void addComponent(int i, String JavaDoc memberName, Any JavaDoc memberAny, DynAny memberDynAny) {
293         components[i] = memberDynAny;
294         names[i] = (memberName != null ? memberName : "");
295         nameValuePairs[i].id = memberName;
296         nameValuePairs[i].value = memberAny;
297         nameDynAnyPairs[i].id = memberName;
298         nameDynAnyPairs[i].value = memberDynAny;
299         if (memberDynAny instanceof DynAnyImpl)
300             ((DynAnyImpl)memberDynAny).setStatus(STATUS_UNDESTROYABLE);
301     }
302
303     // Initializes components, names, nameValuePairs and nameDynAnyPairs representation
304
// from the Any representation
305
protected boolean initializeComponentsFromAny() {
306         // This typeCode is of kind tk_struct.
307
TypeCode JavaDoc typeCode = any.type();
308         TypeCode JavaDoc memberType = null;
309         Any JavaDoc memberAny;
310         DynAny memberDynAny = null;
311         String JavaDoc memberName = null;
312         int length = 0;
313
314         try {
315             length = typeCode.member_count();
316         } catch (BadKind JavaDoc badKind) { // impossible
317
}
318
319         InputStream JavaDoc input = any.create_input_stream();
320
321         allocComponents(length);
322
323         for (int i=0; i<length; i++) {
324             try {
325                 memberName = typeCode.member_name(i);
326                 memberType = typeCode.member_type(i);
327             } catch (BadKind JavaDoc badKind) { // impossible
328
} catch (Bounds JavaDoc bounds) { // impossible
329
}
330             memberAny = DynAnyUtil.extractAnyFromStream(memberType, input, orb);
331             try {
332                 // Creates the appropriate subtype without copying the Any
333
memberDynAny = DynAnyUtil.createMostDerivedDynAny(memberAny, orb, false);
334                 // _DEBUG_
335
//System.out.println("Created DynAny for " + memberName +
336
// ", type " + memberType.kind().value());
337
} catch (InconsistentTypeCode JavaDoc itc) { // impossible
338
}
339             addComponent(i, memberName, memberAny, memberDynAny);
340         }
341         return true;
342     }
343
344     // Initializes components, names, nameValuePairs and nameDynAnyPairs representation
345
// from the internal TypeCode information with default values
346
// This is not done recursively, only one level.
347
// More levels are initialized lazily, on demand.
348
protected boolean initializeComponentsFromTypeCode() {
349         // This typeCode is of kind tk_struct.
350
TypeCode JavaDoc typeCode = any.type();
351         TypeCode JavaDoc memberType = null;
352         Any JavaDoc memberAny;
353         DynAny memberDynAny = null;
354         String JavaDoc memberName;
355         int length = 0;
356
357         try {
358             length = typeCode.member_count();
359         } catch (BadKind JavaDoc badKind) { // impossible
360
}
361
362         allocComponents(length);
363
364         for (int i=0; i<length; i++) {
365             memberName = null;
366             try {
367                 memberName = typeCode.member_name(i);
368                 memberType = typeCode.member_type(i);
369             } catch (BadKind JavaDoc badKind) { // impossible
370
} catch (Bounds JavaDoc bounds) { // impossible
371
}
372             try {
373                 memberDynAny = DynAnyUtil.createMostDerivedDynAny(memberType, orb);
374                 // _DEBUG_
375
//System.out.println("Created DynAny for " + memberName +
376
// ", type " + memberType.kind().value());
377
/*
378                 if (memberDynAny instanceof DynAnyConstructedImpl) {
379                     if ( ! ((DynAnyConstructedImpl)memberDynAny).isRecursive()) {
380                         // This is the recursive part
381                         ((DynAnyConstructedImpl)memberDynAny).initializeComponentsFromTypeCode();
382                     }
383                 } // Other implementations have their own way of dealing with implementing the spec.
384 */

385             } catch (InconsistentTypeCode JavaDoc itc) { // impossible
386
}
387             // get a hold of the default initialized Any without copying
388
memberAny = getAny(memberDynAny);
389             addComponent(i, memberName, memberAny, memberDynAny);
390         }
391         return true;
392     }
393
394     // It is probably right not to destroy the released component DynAnys.
395
// Some other DynAny or a user variable might still hold onto them
396
// and if not then the garbage collector will take care of it.
397
protected void clearData() {
398         super.clearData();
399         names = null;
400         nameValuePairs = null;
401         nameDynAnyPairs = null;
402     }
403 }
404
Popular Tags