1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
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: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69:
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75:
76:
90: public class gnuAny
91: extends Any
92: {
93:
96: private static final long serialVersionUID = 1;
97:
98:
102: protected static final TypeCode nullType =
103: new PrimitiveTypeCode(TCKind.tk_null);
104:
105:
108: protected Streamable has;
109:
110:
113: protected TypeCode typecode;
114:
115:
118: protected int xKind = -1;
119:
120:
123: private ORB orb;
124:
125:
128: public void setOrb(ORB an_orb)
129: {
130: orb = an_orb;
131: }
132:
133:
140: public gnuAny Clone()
141: {
142: BufferedCdrOutput out = new BufferedCdrOutput();
143: out.setOrb(orb);
144: out.write_any(this);
145:
146: BufferredCdrInput in = new BufferredCdrInput(out.buffer.toByteArray());
147: in.setOrb(orb);
148: return (gnuAny) in.read_any();
149: }
150:
151:
155: public org.omg.CORBA.portable.InputStream create_input_stream()
156: {
157: if (has instanceof GeneralHolder)
158: {
159: GeneralHolder u = (GeneralHolder) has;
160: return u.getInputStream();
161: }
162: else
163: {
164: BufferedCdrOutput out = new BufferedCdrOutput();
165: out.setOrb(orb);
166: write_value(out);
167:
168: BufferredCdrInput in = new BufferredCdrInput(out.buffer.toByteArray());
169: in.setOrb(orb);
170: return in;
171: }
172: }
173:
174:
177: public org.omg.CORBA.portable.OutputStream create_output_stream()
178: {
179: BufferedCdrOutput stream = new BufferedCdrOutput();
180: stream.setOrb(orb);
181: return stream;
182: }
183:
184:
188: public boolean equal(Any other)
189: {
190: if (other == this)
191: return true;
192: if (type().kind() != other.type().kind())
193: return false;
194:
195: if (has != null && other instanceof gnuAny)
196: if (has.equals(((gnuAny) other).has))
197: return true;
198:
199: BufferedCdrOutput a = new BufferedCdrOutput();
200: a.setOrb(orb);
201: write_value(a);
202:
203: BufferedCdrOutput b = new BufferedCdrOutput();
204: b.setOrb(orb);
205: other.write_value(b);
206:
207: byte[] ba = a.buffer.toByteArray();
208: byte[] bb = b.buffer.toByteArray();
209:
210: return Arrays.equals(ba, bb);
211: }
212:
213:
216: public int hashCode()
217: {
218: if (has == null)
219: return type().kind().value();
220: else
221: {
222: Adler32 adler = new Adler32();
223:
224: BufferedCdrOutput a = new BufferedCdrOutput();
225: a.setOrb(orb);
226: write_value(a);
227:
228: adler.update(a.buffer.toByteArray());
229: adler.update(type().kind().value());
230:
231: return (int) adler.getValue() & Integer.MAX_VALUE;
232: }
233: }
234:
235:
238: public boolean equals(java.lang.Object other)
239: {
240: if (other == this)
241: return true;
242: if (!(other instanceof Any))
243: return false;
244:
245: return equal((Any) other);
246: }
247:
248:
251: public org.omg.CORBA.Object extract_Object()
252: {
253: try
254: {
255: return ((ObjectHolder) has).value;
256: }
257: catch (ClassCastException ex)
258: {
259: BAD_OPERATION bad = new BAD_OPERATION();
260: bad.initCause(ex);
261: bad.minor = Minor.Any;
262: throw bad;
263: }
264: }
265:
266:
275: public Principal extract_Principal()
276: {
277: check(TCKind._tk_Principal);
278: return ((PrincipalHolder) has).value;
279: }
280:
281:
287: public Streamable extract_Streamable()
288: {
289: return has;
290: }
291:
292: public TypeCode extract_TypeCode()
293: throws BAD_OPERATION
294: {
295: check(TCKind._tk_TypeCode);
296: return ((TypeCodeHolder) has).value;
297: }
298:
299:
308: public Serializable extract_Value()
309: throws BAD_OPERATION
310: {
311: try
312: {
313: if (has instanceof ValueBaseHolder)
314: return ((ValueBaseHolder) has).value;
315: else
316: {
317:
318:
319:
320:
321: Field f = has.getClass().getField("value");
322: return (Serializable) f.get(has);
323: }
324: }
325: catch (Exception ex)
326: {
327: BAD_OPERATION bad = new BAD_OPERATION("Value type expected");
328: bad.minor = Minor.Any;
329: bad.initCause(ex);
330: throw bad;
331: }
332: }
333:
334:
335: public Any extract_any()
336: throws BAD_OPERATION
337: {
338: check(TCKind._tk_any);
339: return ((AnyHolder) has).value;
340: }
341:
342:
343: public boolean extract_boolean()
344: throws BAD_OPERATION
345: {
346: check(TCKind._tk_boolean);
347: return ((BooleanHolder) has).value;
348: }
349:
350:
351: public char extract_char()
352: throws BAD_OPERATION
353: {
354: check(TCKind._tk_char);
355: return ((CharHolder) has).value;
356: }
357:
358:
359: public double extract_double()
360: throws BAD_OPERATION
361: {
362: check(TCKind._tk_double);
363: return ((DoubleHolder) has).value;
364: }
365:
366:
373: public BigDecimal extract_fixed()
374: throws org.omg.CORBA.BAD_OPERATION
375: {
376: check(TCKind._tk_fixed);
377: return ((FixedHolder) has).value;
378: }
379:
380:
381: public float extract_float()
382: throws BAD_OPERATION
383: {
384: check(TCKind._tk_float);
385: return ((FloatHolder) has).value;
386: }
387:
388:
389: public int extract_long()
390: throws BAD_OPERATION
391: {
392:
393: check(TCKind._tk_long);
394: return ((IntHolder) has).value;
395: }
396:
397:
398: public long extract_longlong()
399: throws BAD_OPERATION
400: {
401: check(TCKind._tk_longlong);
402: return ((LongHolder) has).value;
403: }
404:
405:
406: public byte extract_octet()
407: throws BAD_OPERATION
408: {
409:
410: check(TCKind._tk_octet);
411: return (byte) ((OctetHolder) has).value;
412: }
413:
414:
415: public short extract_short()
416: throws BAD_OPERATION
417: {
418: check(TCKind._tk_short);
419: return ((ShortHolder) has).value;
420: }
421:
422:
423: public String extract_string()
424: throws BAD_OPERATION
425: {
426: check(TCKind._tk_string);
427: return ((StringHolder) has).value;
428: }
429:
430:
431: public int extract_ulong()
432: throws BAD_OPERATION
433: {
434:
435: check(TCKind._tk_ulong);
436: return ((IntHolder) has).value;
437: }
438:
439:
440: public long extract_ulonglong()
441: throws BAD_OPERATION
442: {
443:
444: check(TCKind._tk_ulonglong);
445: return ((LongHolder) has).value;
446: }
447:
448:
449: public short extract_ushort()
450: throws BAD_OPERATION
451: {
452:
453: check(TCKind._tk_ushort);
454: return ((ShortHolder) has).value;
455: }
456:
457:
458: public char extract_wchar()
459: throws BAD_OPERATION
460: {
461: check(TCKind._tk_wchar);
462: return ((WCharHolder) has).value;
463: }
464:
465:
466: public String extract_wstring()
467: throws BAD_OPERATION
468: {
469:
470: check(TCKind._tk_wstring);
471: return ((WStringHolder) has).value;
472: }
473:
474:
477: public void insert_Object(org.omg.CORBA.Object x, TypeCode typecode)
478: {
479: has = new ObjectHolder(x);
480: type(typecode);
481: }
482:
483:
486: public void insert_Object(org.omg.CORBA.Object x)
487: {
488: has = new ObjectHolder(x);
489: }
490:
491:
499: public void insert_Principal(Principal x)
500: {
501: resetTypes();
502: if (has instanceof PrincipalHolder)
503: ((PrincipalHolder) has).value = x;
504: else
505: has = new PrincipalHolder(x);
506: }
507:
508:
514: public void insert_Streamable(Streamable x)
515: {
516: resetTypes();
517: has = x;
518: }
519:
520:
524: public void insert_TypeCode(TypeCode typecode)
525: {
526: resetTypes();
527: if (has instanceof TypeCodeHolder)
528: ((TypeCodeHolder) has).value = typecode;
529: else
530: has = new TypeCodeHolder(typecode);
531: }
532:
533:
534: public void insert_Value(Serializable x, TypeCode c_typecode)
535: {
536: if (typecode != null && typecode.kind() == TCKind.tk_value_box)
537: {
538: has = new gnuValueHolder(x, typecode);
539: }
540: else
541: {
542: type(typecode);
543: insert_Value(x);
544: }
545: }
546:
547:
548: public void insert_Value(Serializable x)
549: {
550: if (typecode != null && typecode.kind() == TCKind.tk_value_box)
551: {
552: has = new gnuValueHolder(x, typecode);
553: }
554: else
555: {
556: if (has instanceof ValueBaseHolder)
557: ((ValueBaseHolder) has).value = x;
558: else
559: has = new ValueBaseHolder(x);
560: }
561: }
562:
563:
569: public void insert_any(Any an_any)
570: {
571: resetTypes();
572: if (has instanceof AnyHolder)
573: ((AnyHolder) has).value = an_any;
574: else
575: has = new AnyHolder(an_any);
576: }
577:
578:
579: public void insert_boolean(boolean x)
580: {
581: resetTypes();
582: if (has instanceof BooleanHolder)
583: ((BooleanHolder) has).value = x;
584: else
585: has = new BooleanHolder(x);
586: }
587:
588:
589: public void insert_char(char x)
590: {
591: resetTypes();
592: if (has instanceof CharHolder)
593: ((CharHolder) has).value = x;
594: else
595: has = new CharHolder(x);
596: }
597:
598:
599: public void insert_double(double x)
600: {
601: resetTypes();
602: if (has instanceof DoubleHolder)
603: ((DoubleHolder) has).value = x;
604: else
605: has = new DoubleHolder(x);
606: }
607:
608:
615: public void insert_fixed(BigDecimal x, TypeCode x_typecode)
616: {
617: resetTypes();
618: insert_fixed(x);
619: typecode = x_typecode;
620: }
621:
622:
629: public void insert_fixed(BigDecimal x)
630: {
631: resetTypes();
632: if (has instanceof FixedHolder)
633: ((FixedHolder) has).value = x;
634: else
635: has = new FixedHolder(x);
636: }
637:
638:
639: public void insert_float(float x)
640: {
641: resetTypes();
642: if (has instanceof FloatHolder)
643: ((FloatHolder) has).value = x;
644: else
645: has = new FloatHolder(x);
646: }
647:
648:
649: public void insert_long(int x)
650: {
651: resetTypes();
652: if (has instanceof IntHolder)
653: ((IntHolder) has).value = x;
654: else
655: has = new IntHolder(x);
656: }
657:
658:
659: public void insert_longlong(long x)
660: {
661: resetTypes();
662: if (has instanceof LongHolder)
663: ((LongHolder) has).value = x;
664: else
665: has = new LongHolder(x);
666: }
667:
668:
669: public void insert_octet(byte x)
670: {
671: resetTypes();
672: if (has instanceof OctetHolder)
673: ((OctetHolder) has).value = x;
674: else
675: has = new OctetHolder(x);
676: }
677:
678:
679: public void insert_short(short x)
680: {
681: resetTypes();
682: if (has instanceof ShortHolder)
683: ((ShortHolder) has).value = x;
684: else
685: has = new ShortHolder(x);
686: }
687:
688:
689: public void insert_string(String x)
690: {
691: resetTypes();
692: if (has instanceof StringHolder)
693: ((StringHolder) has).value = x;
694: else
695: has = new StringHolder(x);
696:
697: typecode = new StringTypeCode(TCKind.tk_string);
698: }
699:
700:
701: public void insert_ulong(int x)
702: {
703: resetTypes();
704: if (has instanceof IntHolder)
705: ((IntHolder) has).value = x;
706: else
707: has = new IntHolder(x);
708: xKind = TCKind._tk_ulong;
709: }
710:
711:
712: public void insert_ulonglong(long x)
713: {
714: resetTypes();
715: if (has instanceof LongHolder)
716: ((LongHolder) has).value = x;
717: else
718: has = new LongHolder(x);
719: xKind = TCKind._tk_ulonglong;
720: }
721:
722:
723: public void insert_ushort(short x)
724: {
725: resetTypes();
726: if (has instanceof ShortHolder)
727: ((ShortHolder) has).value = x;
728: else
729: has = new ShortHolder(x);
730: xKind = TCKind._tk_ushort;
731: }
732:
733:
734: public void insert_wchar(char x)
735: {
736: resetTypes();
737: if (has instanceof WCharHolder)
738: ((WCharHolder) has).value = x;
739: else
740: has = new WCharHolder(x);
741: }
742:
743:
744: public void insert_wstring(String x)
745: {
746: resetTypes();
747: if (has instanceof WStringHolder)
748: ((WStringHolder) has).value = x;
749: else
750: has = new WStringHolder(x);
751: }
752:
753:
756: public ORB orb()
757: {
758: return orb;
759: }
760:
761:
767: public void read_value(org.omg.CORBA.portable.InputStream input,
768: TypeCode a_type
769: )
770: throws MARSHAL
771: {
772: try
773: {
774: int kind = a_type.kind().value();
775:
776:
777: if (kind == TCKind._tk_fixed)
778: {
779: BigDecimal dec = BigDecimalHelper.read(input, a_type.fixed_scale());
780: has = new FixedHolder(dec);
781: }
782: else
783: {
784: has = HolderLocator.createHolder(a_type);
785: if (has == null)
786: {
787:
788:
789:
790: BufferedCdrOutput buffer = new BufferedCdrOutput();
791: buffer.setOrb(orb);
792: has = new GeneralHolder(buffer);
793: }
794: }
795: type(a_type);
796:
797: if (!(has instanceof GeneralHolder) &&
798: (kind == TCKind._tk_value_box))
799: {
800:
801:
802: Field vField = has.getClass().getField("value");
803:
804: Object content = Vio.read(input, a_type.id());
805: vField.set(has, content);
806: }
807: else
808: has._read(input);
809: }
810: catch (Exception ex)
811: {
812: MARSHAL m = new MARSHAL();
813: m.minor = Minor.Any;
814: m.initCause(ex);
815: throw m;
816: }
817: }
818:
819:
820: public TypeCode type()
821: {
822: if (typecode != null)
823: return typecode;
824: else if (xKind >= 0)
825: {
826: typecode = new PrimitiveTypeCode(TCKind.from_int(xKind));
827: return typecode;
828: }
829: else
830: return has != null ? has._type() : nullType;
831: }
832:
833:
838: public void type(TypeCode valueTypeCode)
839: {
840: xKind = valueTypeCode.kind().value();
841: typecode = valueTypeCode;
842: }
843:
844:
845: public void write_value(org.omg.CORBA.portable.OutputStream output)
846: {
847: if (has != null)
848: has._write(output);
849: else
850:
851: if (xKind == TCKind._tk_null || xKind == TCKind._tk_objref ||
852: xKind == TCKind._tk_value || xKind == TCKind._tk_value_box
853: )
854: output.write_long(0);
855: }
856:
857:
863: protected void check(int kind)
864: throws BAD_OPERATION
865: {
866: if (has == null)
867: {
868: BAD_OPERATION bad = new BAD_OPERATION("value not set");
869: bad.minor = Minor.Any;
870: throw bad;
871: }
872:
873: if (xKind >= 0)
874: {
875: if (xKind != kind)
876: if (!(xKind == TCKind._tk_alias && has._type().kind().value() == kind))
877: {
878: BAD_OPERATION bad = new BAD_OPERATION("Extracting "
879: + TypeKindNamer.nameIt(kind) + " when stored "
880: + TypeKindNamer.nameIt(xKind));
881: bad.minor = Minor.Any;
882: throw bad;
883: }
884: }
885: else
886: {
887: if (type().kind().value() != kind)
888: if (!(type().kind().value() == TCKind._tk_alias && has._type().kind().value() == kind))
889: {
890: BAD_OPERATION bad = new BAD_OPERATION("Extracting "
891: + TypeKindNamer.nameIt(kind) + " stored "
892: + TypeKindNamer.nameIt(type()));
893: bad.minor = Minor.Any;
894: throw bad;
895: }
896: }
897: }
898:
899:
902: private final void resetTypes()
903: {
904: typecode = null;
905: xKind = -1;
906: }
907: }