1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62:
63: import ;
64:
65:
70: public class gnuDynValue extends RecordAny implements DynValue,
71: Serializable
72: {
73:
76: private static final long serialVersionUID = 1;
77:
78:
81: boolean isNull;
82:
83:
86: public gnuDynValue(TypeCode oType, TypeCode aType,
87: gnuDynAnyFactory aFactory, ORB anOrb
88: )
89: {
90: super(oType, aType, aFactory, anOrb);
91:
92:
93:
94: try
95: {
96: array = new DynAny[ final_type.member_count() ];
97: fNames = new String[ array.length ];
98: for (int i = 0; i < array.length; i++)
99: {
100: array [ i ] =
101: factory.create_dyn_any_from_type_code(final_type.member_type(i));
102: fNames [ i ] = final_type.member_name(i);
103: }
104:
105:
106: if (final_type.type_modifier() == VM_TRUNCATABLE.value)
107: {
108: TypeCode parent = final_type.concrete_base_type();
109: DynAny ancestor = factory.create_dyn_any_from_type_code(parent);
110:
111: if (ancestor instanceof DynValue)
112: {
113:
114: DynValue anc = (DynValue) ancestor;
115: anc.set_to_value();
116:
117: NameDynAnyPair[] aar = anc.get_members_as_dyn_any();
118: inheritFields(aar);
119: }
120: else if (ancestor instanceof DynStruct)
121: {
122:
123: DynStruct anc = (DynStruct) ancestor;
124: NameDynAnyPair[] aar = anc.get_members_as_dyn_any();
125: inheritFields(aar);
126: }
127: else
128: throw new BAD_PARAM("The parent of " + final_type.id() + ", " +
129: parent.id() + ", is not structure nor value."
130: );
131: }
132: }
133: catch (Exception e)
134: {
135: throw new Unexpected(e);
136: }
137:
138: set_to_null();
139: }
140:
141:
144: private void inheritFields(NameDynAnyPair[] aar)
145: {
146: DynAny[] nArray = new DynAny[ array.length + aar.length ];
147: String[] nNames = new String[ array.length + aar.length ];
148: int p = 0;
149: for (int i = 0; i < aar.length; i++)
150: {
151: nArray [ p ] = aar [ i ].value;
152: nNames [ p ] = aar [ i ].id;
153: p++;
154: }
155:
156: for (int i = 0; i < array.length; i++)
157: {
158: nArray [ p ] = array [ i ];
159: nNames [ p ] = fNames [ i ];
160: p++;
161: }
162:
163: array = nArray;
164: fNames = nNames;
165: }
166:
167:
168: public TCKind current_member_kind() throws TypeMismatch, InvalidValue
169: {
170: if (isNull)
171: throw new TypeMismatch(ISNULL);
172: else
173: return super.current_member_kind();
174: }
175: ;
176:
177:
178: public String current_member_name() throws TypeMismatch, InvalidValue
179: {
180: if (isNull)
181: throw new TypeMismatch(ISNULL);
182: else
183: return super.current_member_name();
184: }
185: ;
186:
187:
188: public NameDynAnyPair[] get_members_as_dyn_any() throws InvalidValue
189: {
190: if (isNull)
191: throw new InvalidValue(ISNULL);
192: return super.gnu_get_members_as_dyn_any();
193: }
194: ;
195:
196:
197: public NameValuePair[] get_members() throws InvalidValue
198: {
199: if (isNull)
200: throw new InvalidValue(ISNULL);
201: else
202: return super.gnu_get_members();
203: }
204: ;
205:
206:
207: public void set_members_as_dyn_any(NameDynAnyPair[] value)
208: throws TypeMismatch, InvalidValue
209: {
210: super.set_members_as_dyn_any(value);
211: isNull = false;
212: }
213: ;
214:
215:
216: public void set_members(NameValuePair[] value)
217: throws TypeMismatch, InvalidValue
218: {
219: super.set_members(value);
220: isNull = false;
221: }
222: ;
223:
224:
225: public boolean is_null()
226: {
227: return isNull;
228: }
229:
230:
231: public void set_to_null()
232: {
233: isNull = true;
234: valueChanged();
235: }
236:
237:
238: public void set_to_value()
239: {
240: isNull = false;
241: valueChanged();
242: }
243:
244:
247: protected RecordAny newInstance(TypeCode oType, TypeCode aType,
248: gnuDynAnyFactory aFactory, ORB anOrb
249: )
250: {
251: gnuDynValue v = new gnuDynValue(oType, aType, aFactory, anOrb);
252: if (isNull)
253: v.set_to_null();
254: else
255: v.set_to_value();
256: return v;
257: }
258:
259:
262: public boolean equal(DynAny other)
263: {
264: if (other instanceof DynValueOperations)
265: {
266: DynValueCommon o = (DynValueCommon) other;
267: if (isNull)
268: return o.is_null() && o.type().equal(official_type);
269: else
270: return !o.is_null() && record_equal(other);
271: }
272: else
273: return false;
274: }
275:
276:
279: protected DynAny focused() throws InvalidValue, TypeMismatch
280: {
281: if (isNull)
282: throw new TypeMismatch(ISNULL);
283: else
284: return super.focused();
285: }
286:
287:
290: public Any to_any()
291: {
292: if (isNull)
293: {
294: Any a0 = createAny();
295: a0.type(orb.get_primitive_tc(TCKind.tk_null));
296: return a0;
297: }
298: else
299: {
300: try
301: {
302: ValueFactory factory =
303: ((org.omg.CORBA_2_3.ORB) orb).lookup_value_factory(official_type.id());
304: if (factory == null)
305: {
306: MARSHAL m = new MARSHAL("Factory for " + official_type.id() +
307: " not registered.");
308: m.minor = Minor.Factory;
309: throw m;
310: }
311:
312: OutputStream out = orb.create_output_stream();
313:
314: for (int i = 0; i < array.length; i++)
315: array [ i ].to_any().write_value(out);
316:
317: org.omg.CORBA_2_3.portable.InputStream in =
318: (org.omg.CORBA_2_3.portable.InputStream) out.create_input_stream();
319: Serializable v = factory.read_value(in);
320:
321: Any g = createAny();
322: g.type(official_type);
323: g.insert_Value(v, official_type);
324:
325: return g;
326: }
327: catch (Exception e)
328: {
329: throw new Unexpected(e);
330: }
331: }
332: }
333:
334:
335: public void assign(DynAny from) throws TypeMismatch
336: {
337: checkType(official_type, from.type());
338:
339: if (from instanceof DynValue)
340: {
341: DynValue other = (DynValue) from;
342: if (other.is_null())
343: set_to_null();
344: else
345: {
346: set_to_value();
347: try
348: {
349: DynValueOperations src = (DynValueOperations) from;
350: set_members_as_dyn_any(src.get_members_as_dyn_any());
351: }
352: catch (InvalidValue e)
353: {
354: TypeMismatch t = new TypeMismatch("Invalid value");
355: t.initCause(e);
356: throw t;
357: }
358: }
359: }
360: else
361: throw new TypeMismatch("Not a DynValue");
362: }
363:
364:
367: public int component_count()
368: {
369: return isNull ? 0 : record_component_count();
370: }
371:
372:
373: public Serializable get_val() throws TypeMismatch, InvalidValue
374: {
375: return to_any().extract_Value();
376: }
377:
378:
379: public void insert_val(Serializable a_x) throws InvalidValue, TypeMismatch
380: {
381: Any a = to_any();
382: a.insert_Value(a_x);
383: from_any(a);
384: valueChanged();
385: }
386: }