0001: package org.jacorb.orb;
0002:
0003: /*
0004: * JacORB - the free Java ORB
0005: *
0006: * Copyright (C) 1997-2004 Gerald Brose.
0007: *
0008: * This library is free software; you can redistribute it and/or
0009: * modify it under the terms of the GNU Library General Public
0010: * License as published by the Free Software Foundation; either
0011: * version 2 of the License, or (at your option) any later version.
0012: *
0013: * This library is distributed in the hope that it will be useful,
0014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0016: * Library General Public License for more details.
0017: *
0018: * You should have received a copy of the GNU Library General Public
0019: * License along with this library; if not, write to the Free
0020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
0021: */
0022:
0023: import org.jacorb.util.ObjectUtil;
0024: import org.omg.CORBA.*;
0025: import org.omg.CORBA.TypeCodePackage.BadKind;
0026: import org.omg.CORBA.portable.InputStream;
0027: import org.omg.CORBA.portable.Streamable;
0028: import org.omg.CORBA_2_3.portable.OutputStream;
0029:
0030: import java.io.Serializable;
0031: import java.lang.reflect.Field;
0032: import java.math.BigDecimal;
0033: import java.util.*;
0034:
0035: /**
0036: * CORBA any
0037: *
0038: * @author Gerald Brose
0039: * $Id: Any.java,v 1.56 2006/11/27 14:34:15 alphonse.bendt Exp $
0040: */
0041:
0042: public final class Any extends org.omg.CORBA.Any {
0043: private org.omg.CORBA.TypeCode typeCode;
0044: private java.lang.Object value;
0045: private final org.omg.CORBA.ORB orb;
0046:
0047: Any(org.omg.CORBA.ORB orb) {
0048: super ();
0049:
0050: this .orb = orb;
0051: typeCode = orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_null);
0052: }
0053:
0054: public TCKind kind() {
0055: return typeCode.kind();
0056: }
0057:
0058: public org.omg.CORBA.TypeCode type() {
0059: return typeCode;
0060: }
0061:
0062: public org.omg.CORBA.TypeCode originalType() {
0063: return TypeCode.originalType(typeCode);
0064: }
0065:
0066: public void type(org.omg.CORBA.TypeCode type) {
0067: typeCode = type;
0068: value = null;
0069: }
0070:
0071: public java.lang.Object value() {
0072: return value;
0073: }
0074:
0075: public int _get_TCKind() {
0076: return org.omg.CORBA.TCKind._tk_any;
0077: }
0078:
0079: private void tc_error(String cause) {
0080: throw new BAD_OPERATION(cause);
0081: }
0082:
0083: private void checkNull() {
0084: if (value == null) {
0085: throw new BAD_OPERATION(
0086: "No value has previously been inserted");
0087: }
0088: }
0089:
0090: private void checkExtract(int value, String cause) {
0091: if (originalType().kind().value() != value) {
0092: throw new BAD_OPERATION(cause);
0093: }
0094: }
0095:
0096: public boolean equal(org.omg.CORBA.Any other) {
0097: if (other == null) {
0098: throw new BAD_PARAM("Null passed to Any equal operation");
0099: }
0100:
0101: if (!typeCode.equivalent(other.type())) {
0102: return false;
0103: }
0104:
0105: // TODO
0106: // as this was changed from orgininalType().kind().value()
0107: // this is possibly an alias for a primitive
0108: // type. could prohably be optimized?
0109: int kind = kind().value();
0110:
0111: switch (kind) {
0112: case TCKind._tk_null: // 0
0113: // fallthrough
0114: case TCKind._tk_void: // 1
0115: {
0116: return true;
0117: }
0118: case TCKind._tk_short: // 2
0119: {
0120: return extract_short() == other.extract_short();
0121: }
0122: case TCKind._tk_long: // 3
0123: {
0124: return extract_long() == other.extract_long();
0125: }
0126: case TCKind._tk_ushort: // 4
0127: {
0128: return extract_ushort() == other.extract_ushort();
0129: }
0130: case TCKind._tk_ulong: // 5
0131: {
0132: return extract_ulong() == other.extract_ulong();
0133: }
0134: case TCKind._tk_float: // 6
0135: {
0136: return extract_float() == other.extract_float();
0137: }
0138: case TCKind._tk_double: // 7
0139: {
0140: return extract_double() == other.extract_double();
0141: }
0142: case TCKind._tk_boolean: // 8
0143: {
0144: return extract_boolean() == other.extract_boolean();
0145: }
0146: case TCKind._tk_char: // 9
0147: {
0148: return extract_char() == other.extract_char();
0149: }
0150: case TCKind._tk_octet: // 10
0151: {
0152: return extract_octet() == other.extract_octet();
0153: }
0154: case TCKind._tk_any: // 11
0155: {
0156: return extract_any().equal(other.extract_any());
0157: }
0158: case TCKind._tk_TypeCode: // 12
0159: {
0160: return extract_TypeCode().equal(other.extract_TypeCode());
0161: }
0162: case TCKind._tk_Principal: // 13
0163: {
0164: throw new org.omg.CORBA.NO_IMPLEMENT("Principal deprecated");
0165: }
0166: case TCKind._tk_objref: // 14
0167: {
0168: java.lang.Object myValue = extract_Object();
0169: java.lang.Object otherValue = other.extract_Object();
0170: if (myValue == null && otherValue == null) {
0171: return true;
0172: } else if (myValue != null) {
0173: return myValue.equals(otherValue);
0174: } else //if (otherValue != null)
0175: {
0176: // For this case otherValue must be null. Can there
0177: // be a case where an actual object instance represents
0178: // a null object reference? Ignore the FindBugs complaint
0179: // here.
0180: return otherValue.equals(myValue);
0181: }
0182: }
0183: case TCKind._tk_struct: // 15
0184: // fallthrough
0185: case TCKind._tk_union: // 16
0186: // falltrough
0187: case TCKind._tk_enum: // 17
0188: {
0189: return this .compareComplexValue(other);
0190: }
0191: case TCKind._tk_string: // 18
0192: {
0193: return extract_string().equals(other.extract_string());
0194: }
0195: case TCKind._tk_sequence: // 19
0196: // fallthrough
0197: case TCKind._tk_array: // 20
0198: // fallthrough
0199: case TCKind._tk_alias: // 21
0200: // fallthrough
0201: case TCKind._tk_except: // 22
0202: {
0203: return this .compareComplexValue(other);
0204: }
0205: case TCKind._tk_longlong: // 23
0206: {
0207: return extract_longlong() == other.extract_longlong();
0208: }
0209: case TCKind._tk_ulonglong: // 24
0210: {
0211: return extract_ulonglong() == other.extract_ulonglong();
0212: }
0213: case TCKind._tk_longdouble: // 25
0214: {
0215: throw new org.omg.CORBA.BAD_TYPECODE(
0216: "type longdouble not supported in java");
0217: }
0218: case TCKind._tk_wchar: // 26
0219: {
0220: return extract_wchar() == other.extract_wchar();
0221: }
0222: case TCKind._tk_wstring: // 27
0223: {
0224: return extract_wstring().equals(other.extract_wstring());
0225: }
0226: case TCKind._tk_fixed: // 28
0227: {
0228: return extract_fixed().equals(other.extract_fixed());
0229: }
0230: case TCKind._tk_value: // 29
0231: // fallthrough
0232: case TCKind._tk_value_box: // 30
0233: {
0234: return compareComplexValue(other);
0235: }
0236: // These typecodes are not currently supported.
0237: //case TCKind._tk_native: // 31
0238: //case TCKind._tk_abstract_interface: // 32
0239: //case TCKind._tk_local_interface: // 33
0240: default: {
0241: throw new BAD_TYPECODE(
0242: "Cannot compare anys with TypeCode kind " + kind);
0243: }
0244: }
0245: }
0246:
0247: public boolean equals(java.lang.Object obj) {
0248: if (obj instanceof org.omg.CORBA.Any) {
0249: return equal((org.omg.CORBA.Any) obj);
0250: }
0251: return false;
0252: }
0253:
0254: public String toString() {
0255: if (value != null) {
0256: return value.toString();
0257: }
0258: return "null";
0259: }
0260:
0261: // short
0262:
0263: public void insert_short(short s) {
0264: value = new Short(s);
0265: typeCode = orb.get_primitive_tc(TCKind.tk_short);
0266: }
0267:
0268: public short extract_short() throws org.omg.CORBA.BAD_OPERATION {
0269: checkExtract(TCKind._tk_short, "Cannot extract short");
0270:
0271: checkNull();
0272:
0273: if (value instanceof Short) {
0274: return ((Short) value).shortValue();
0275: } else if (value instanceof ShortHolder) {
0276: return ((ShortHolder) value).value;
0277: } else if (value instanceof CDROutputStream) {
0278: return create_input_stream().read_short();
0279: } else {
0280: throw new INTERNAL("Encountered unexpected type of value: "
0281: + value.getClass());
0282: }
0283: }
0284:
0285: // ushort
0286:
0287: public void insert_ushort(short s) {
0288: value = new Short(s);
0289: typeCode = orb.get_primitive_tc(TCKind.tk_ushort);
0290: }
0291:
0292: public short extract_ushort() {
0293: checkExtract(TCKind._tk_ushort, "Cannot extract ushort");
0294:
0295: checkNull();
0296:
0297: if (value instanceof Short) {
0298: return ((Short) value).shortValue();
0299: } else if (value instanceof CDROutputStream) {
0300: return create_input_stream().read_ushort();
0301: } else {
0302: throw new INTERNAL("Encountered unexpected type of value: "
0303: + value.getClass());
0304: }
0305: }
0306:
0307: // long
0308:
0309: public void insert_long(int i) {
0310: value = ObjectUtil.newInteger(i);
0311: typeCode = orb.get_primitive_tc(TCKind.tk_long);
0312: }
0313:
0314: public int extract_long() {
0315: checkExtract(TCKind._tk_long, "Cannot extract long");
0316:
0317: checkNull();
0318:
0319: if (value instanceof Integer) {
0320: return ((Integer) value).intValue();
0321: } else if (value instanceof IntHolder) {
0322: return ((IntHolder) value).value;
0323: } else if (value instanceof CDROutputStream) {
0324: return create_input_stream().read_long();
0325: } else {
0326: throw new INTERNAL("Encountered unexpected type of value: "
0327: + value.getClass());
0328: }
0329: }
0330:
0331: // ulong
0332:
0333: public void insert_ulong(int i) {
0334: value = ObjectUtil.newInteger(i);
0335: typeCode = orb.get_primitive_tc(TCKind.tk_ulong);
0336: }
0337:
0338: public int extract_ulong() {
0339: checkExtract(TCKind._tk_ulong, "Cannot extract ulong");
0340:
0341: checkNull();
0342:
0343: if (value instanceof Integer) {
0344: return ((Integer) value).intValue();
0345: } else if (value instanceof CDROutputStream) {
0346: return create_input_stream().read_ulong();
0347: } else {
0348: throw new INTERNAL("Encountered unexpected type of value: "
0349: + value.getClass());
0350: }
0351: }
0352:
0353: // longlong
0354:
0355: public void insert_longlong(long l) {
0356: value = new Long(l);
0357: typeCode = orb.get_primitive_tc(TCKind.tk_longlong);
0358: }
0359:
0360: public long extract_longlong() {
0361: checkExtract(TCKind._tk_longlong, "Cannot extract longlong");
0362:
0363: checkNull();
0364:
0365: if (value instanceof Long) {
0366: return ((Long) value).longValue();
0367: } else if (value instanceof LongHolder) {
0368: return ((LongHolder) value).value;
0369: } else if (value instanceof CDROutputStream) {
0370: return create_input_stream().read_longlong();
0371: } else {
0372: throw new INTERNAL("Encountered unexpected type of value: "
0373: + value.getClass());
0374: }
0375: }
0376:
0377: // ulonglong
0378:
0379: public void insert_ulonglong(long l) {
0380: value = new Long(l);
0381: typeCode = orb.get_primitive_tc(TCKind.tk_ulonglong);
0382: }
0383:
0384: public long extract_ulonglong() {
0385: checkExtract(TCKind._tk_ulonglong, "Cannot extract ulonglong");
0386:
0387: checkNull();
0388:
0389: if (value instanceof Long) {
0390: return ((Long) value).longValue();
0391: } else if (value instanceof CDROutputStream) {
0392: return create_input_stream().read_ulonglong();
0393: } else {
0394: throw new INTERNAL("Encountered unexpected type of value: "
0395: + value.getClass());
0396: }
0397: }
0398:
0399: // float
0400:
0401: public void insert_float(float f) {
0402: value = new Float(f);
0403: typeCode = orb.get_primitive_tc(TCKind.tk_float);
0404: }
0405:
0406: public float extract_float() {
0407: checkExtract(TCKind._tk_float, "Cannot extract float");
0408:
0409: checkNull();
0410:
0411: if (value instanceof Float) {
0412: return ((Float) value).floatValue();
0413: } else if (value instanceof FloatHolder) {
0414: return ((FloatHolder) value).value;
0415: } else if (value instanceof CDROutputStream) {
0416: return create_input_stream().read_float();
0417: } else {
0418: throw new INTERNAL("Encountered unexpected type of value: "
0419: + value.getClass());
0420: }
0421: }
0422:
0423: // double
0424:
0425: public void insert_double(double d) {
0426: value = new Double(d);
0427: typeCode = orb.get_primitive_tc(TCKind.tk_double);
0428: }
0429:
0430: public double extract_double() {
0431: checkExtract(TCKind._tk_double, "Cannot extract double");
0432:
0433: checkNull();
0434:
0435: if (value instanceof Double) {
0436: return ((Double) value).doubleValue();
0437: } else if (value instanceof DoubleHolder) {
0438: return ((DoubleHolder) value).value;
0439: } else if (value instanceof CDROutputStream) {
0440: return create_input_stream().read_double();
0441: } else {
0442: throw new INTERNAL("Encountered unexpected type of value: "
0443: + value.getClass());
0444: }
0445: }
0446:
0447: /**
0448: * <code>insert_boolean</code> inserts a Boolean into this Any.
0449: *
0450: * @param bool a <code>boolean</code> value
0451: */
0452: public void insert_boolean(boolean bool) {
0453: // Equivilant to the static valueOf factory which is only
0454: // available post 1.4.
0455: value = (bool ? Boolean.TRUE : Boolean.FALSE);
0456: typeCode = orb.get_primitive_tc(TCKind.tk_boolean);
0457: }
0458:
0459: public boolean extract_boolean() {
0460: checkExtract(TCKind._tk_boolean, "Cannot extract boolean");
0461:
0462: checkNull();
0463:
0464: if (value instanceof Boolean) {
0465: return ((Boolean) value).booleanValue();
0466: } else if (value instanceof BooleanHolder) {
0467: return ((BooleanHolder) value).value;
0468: } else if (value instanceof CDROutputStream) {
0469: return create_input_stream().read_boolean();
0470: } else {
0471: throw new INTERNAL("Encountered unexpected type of value: "
0472: + value.getClass());
0473: }
0474: }
0475:
0476: // char
0477:
0478: public void insert_char(char c) {
0479: value = new Character(c);
0480: typeCode = orb.get_primitive_tc(TCKind.tk_char);
0481: }
0482:
0483: public char extract_char() {
0484: checkExtract(TCKind._tk_char, "Cannot extract char");
0485:
0486: checkNull();
0487:
0488: if (value instanceof Character) {
0489: return ((Character) value).charValue();
0490: } else if (value instanceof CharHolder) {
0491: return ((CharHolder) value).value;
0492: } else if (value instanceof CDROutputStream) {
0493: return create_input_stream().read_char();
0494: } else {
0495: throw new INTERNAL("Encountered unexpected type of value: "
0496: + value.getClass());
0497: }
0498: }
0499:
0500: public void insert_wchar(char c) {
0501: value = new Character(c);
0502: typeCode = orb.get_primitive_tc(TCKind.tk_wchar);
0503: }
0504:
0505: public char extract_wchar() {
0506: checkExtract(TCKind._tk_wchar, "Cannot extract wchar");
0507:
0508: checkNull();
0509:
0510: if (value instanceof Character) {
0511: return ((Character) value).charValue();
0512: } else if (value instanceof CDROutputStream) {
0513: return create_input_stream().read_wchar();
0514: } else {
0515: throw new INTERNAL("Encountered unexpected type of value: "
0516: + value.getClass());
0517: }
0518: }
0519:
0520: // octet
0521:
0522: public void insert_octet(byte b) {
0523: value = new Byte(b);
0524: typeCode = orb.get_primitive_tc(TCKind.tk_octet);
0525: }
0526:
0527: public byte extract_octet() {
0528: checkExtract(TCKind._tk_octet, "Cannot extract octet");
0529:
0530: checkNull();
0531:
0532: if (value instanceof Byte) {
0533: return ((Byte) value).byteValue();
0534: } else if (value instanceof ByteHolder) {
0535: return ((ByteHolder) value).value;
0536: } else if (value instanceof CDROutputStream) {
0537: return create_input_stream().read_octet();
0538: } else {
0539: throw new INTERNAL("Encountered unexpected type of value: "
0540: + value.getClass());
0541: }
0542: }
0543:
0544: // any
0545:
0546: public void insert_any(org.omg.CORBA.Any a) {
0547: value = a;
0548: typeCode = orb.get_primitive_tc(TCKind.tk_any);
0549: }
0550:
0551: public org.omg.CORBA.Any extract_any() {
0552: checkExtract(TCKind._tk_any, "Cannot extract any");
0553:
0554: checkNull();
0555:
0556: if (value instanceof Any) {
0557: return (Any) value;
0558: } else if (value instanceof AnyHolder) {
0559: return ((AnyHolder) value).value;
0560: } else if (value instanceof CDROutputStream) {
0561: return create_input_stream().read_any();
0562: } else {
0563: throw new INTERNAL("Encountered unexpected type of value: "
0564: + value.getClass());
0565: }
0566: }
0567:
0568: // TypeCode
0569:
0570: public void insert_TypeCode(org.omg.CORBA.TypeCode tc) {
0571: value = tc;
0572: typeCode = orb.get_primitive_tc(TCKind.tk_TypeCode);
0573: }
0574:
0575: public org.omg.CORBA.TypeCode extract_TypeCode() {
0576: checkExtract(TCKind._tk_TypeCode, "Cannot extract TypeCode");
0577:
0578: checkNull();
0579:
0580: if (value instanceof TypeCode) {
0581: return (TypeCode) value;
0582: } else if (value instanceof TypeCodeHolder) {
0583: return ((TypeCodeHolder) value).value;
0584: } else if (value instanceof CDROutputStream) {
0585: return create_input_stream().read_TypeCode();
0586: } else {
0587: throw new INTERNAL("Encountered unexpected type of value: "
0588: + value.getClass());
0589: }
0590: }
0591:
0592: // string
0593:
0594: public void insert_string(String s) {
0595: value = s;
0596: typeCode = orb.create_string_tc(0);
0597: }
0598:
0599: public String extract_string() {
0600: checkExtract(TCKind._tk_string, "Cannot extract string");
0601:
0602: checkNull();
0603:
0604: if (value instanceof String) {
0605: return (String) value;
0606: } else if (value instanceof StringHolder) {
0607: return ((StringHolder) value).value;
0608: } else if (value instanceof CDROutputStream) {
0609: return create_input_stream().read_string();
0610: } else {
0611: throw new INTERNAL("Encountered unexpected type of value: "
0612: + value.getClass());
0613: }
0614: }
0615:
0616: public void insert_wstring(String s) {
0617: value = s;
0618: typeCode = orb.create_wstring_tc(0);
0619: }
0620:
0621: public String extract_wstring() {
0622: checkExtract(TCKind._tk_wstring, "Cannot extract wstring");
0623:
0624: checkNull();
0625:
0626: if (value instanceof String) {
0627: return (String) value;
0628: } else if (value instanceof StringHolder) {
0629: return ((StringHolder) value).value;
0630: } else if (value instanceof CDROutputStream) {
0631: return create_input_stream().read_wstring();
0632: } else {
0633: throw new INTERNAL("Encountered unexpected type of value: "
0634: + value.getClass());
0635: }
0636: }
0637:
0638: // fixed
0639:
0640: public void insert_fixed(BigDecimal fixed) {
0641: insert_fixed(fixed, new FixedHolder(fixed)._type());
0642: }
0643:
0644: public void insert_fixed(BigDecimal fixed,
0645: org.omg.CORBA.TypeCode type) {
0646: try {
0647: String val = fixed.toString();
0648: int extra = fixed.scale() - type.fixed_scale();
0649: if (extra > 0) {
0650: // truncate the value to fit the scale of the typecode
0651: val = val.substring(0, val.length() - extra);
0652: } else if (extra < 0) {
0653: StringBuffer sb = new StringBuffer(val);
0654:
0655: // add the decimal point if necessary
0656: if (val.indexOf('.') == -1) {
0657: sb.append('.');
0658: }
0659:
0660: // pad the value with zeros to fit the scale of the typecode
0661: for (int i = extra; i < 0; i++) {
0662: sb.append('0');
0663: }
0664: val = sb.toString();
0665: }
0666: BigDecimal tmp = new BigDecimal(val);
0667:
0668: org.omg.CORBA.FixedHolder holder = new org.omg.CORBA.FixedHolder(
0669: tmp);
0670: org.omg.CORBA.TypeCode tc = holder._type();
0671:
0672: if (tc.fixed_digits() > type.fixed_digits()) {
0673: throw new org.omg.CORBA.BAD_TYPECODE();
0674: }
0675:
0676: value = tmp;
0677: typeCode = type;
0678: } catch (org.omg.CORBA.TypeCodePackage.BadKind bk) {
0679: throw new org.omg.CORBA.BAD_TYPECODE();
0680: }
0681: }
0682:
0683: public java.math.BigDecimal extract_fixed() {
0684: checkExtract(TCKind._tk_fixed, "Cannot extract fixed");
0685:
0686: checkNull();
0687:
0688: if (value instanceof BigDecimal) {
0689: return (BigDecimal) value;
0690: } else if (value instanceof FixedHolder) {
0691: return ((FixedHolder) value).value;
0692: } else if (value instanceof CDROutputStream) {
0693: final CDRInputStream inputStream = (CDRInputStream) create_input_stream();
0694: try {
0695: return inputStream.read_fixed(typeCode.fixed_digits(),
0696: typeCode.fixed_scale());
0697: } catch (BadKind e) {
0698: // shouldn't happen due to initial check above
0699: throw new INTERNAL("should not happen");
0700: } finally {
0701: inputStream.close();
0702: }
0703: } else {
0704: throw new INTERNAL("Encountered unexpected type of value: "
0705: + value.getClass());
0706: }
0707: }
0708:
0709: // obj refs
0710:
0711: public void insert_Object(org.omg.CORBA.Object obj) {
0712: String typeId = null;
0713: String name = "";
0714:
0715: if (obj == null) {
0716: typeId = "IDL:omg.org/CORBA/Object:1.0";
0717: name = "Object";
0718: } else {
0719: typeId = ((org.omg.CORBA.portable.ObjectImpl) obj)._ids()[0];
0720:
0721: // check if the repository Id is in IDL format
0722: if (typeId.startsWith("IDL:")) {
0723: // parse the interface name from a repository Id string
0724: // like "IDL:some.prefix/Some/Module/TheInterfaceName"
0725: name = typeId.substring(4, typeId.lastIndexOf(':'));
0726: name = name.substring(name.lastIndexOf('/') + 1);
0727: } else if (typeId.startsWith("RMI:")) {
0728: // parse the interface name from a repository Id string
0729: // like "RMI:some.java.package.TheInterfaceName"
0730: name = typeId.substring(4, typeId.lastIndexOf(':'));
0731: name = name.substring(name.lastIndexOf('.') + 1);
0732: } else {
0733: throw new org.omg.CORBA.BAD_PARAM(
0734: "Unknown repository id format");
0735: }
0736: }
0737: typeCode = orb.create_interface_tc(typeId, name);
0738: value = obj;
0739: }
0740:
0741: public void insert_Object(org.omg.CORBA.Object obj,
0742: org.omg.CORBA.TypeCode type) {
0743: if (type.kind().value() != TCKind._tk_objref) {
0744: tc_error("Illegal, non-object TypeCode!");
0745: }
0746:
0747: value = obj;
0748: typeCode = type;
0749: }
0750:
0751: public org.omg.CORBA.Object extract_Object() {
0752: checkExtract(TCKind._tk_objref, "Cannot extract object");
0753:
0754: if (value == null) {
0755: //return null directly, saves cast
0756: return null;
0757: }
0758: if (value instanceof org.omg.CORBA.Object) {
0759: return (org.omg.CORBA.Object) value;
0760: } else if (value instanceof Streamable) {
0761: Class valueClass = value.getClass();
0762: try {
0763: Field field = valueClass.getDeclaredField("value");
0764: return (org.omg.CORBA.Object) field.get(value);
0765: } catch (Exception e) {
0766: throw new INTERNAL(
0767: "Failed to retrieve value from Holder via reflection: "
0768: + e);
0769: }
0770: } else if (value instanceof CDROutputStream) {
0771: return create_input_stream().read_Object();
0772: } else {
0773: throw new INTERNAL("Encountered unexpected type of value: "
0774: + value.getClass());
0775: }
0776: }
0777:
0778: // workaround: as long as local objects don't have stubs, we need to
0779: // return *Java* objects
0780:
0781: public java.lang.Object extract_objref() {
0782: checkExtract(TCKind._tk_objref, "Cannot extract object");
0783: return value;
0784: }
0785:
0786: // Principal (deprecated)
0787:
0788: public void insert_Principal(org.omg.CORBA.Principal p) {
0789: throw new org.omg.CORBA.NO_IMPLEMENT("Principal deprecated");
0790: }
0791:
0792: public org.omg.CORBA.Principal extract_Principal() {
0793: throw new org.omg.CORBA.NO_IMPLEMENT("Principal deprecated");
0794: }
0795:
0796: public void insert_Streamable(org.omg.CORBA.portable.Streamable s) {
0797: int kind = s._type().kind().value();
0798: if (kind == TCKind._tk_value || kind == TCKind._tk_value_box
0799: || kind == TCKind._tk_abstract_interface
0800: || kind == TCKind._tk_null) {
0801: throw new NO_IMPLEMENT(
0802: "No support for valuetypes through streamable interface");
0803: }
0804:
0805: value = s;
0806: typeCode = s._type();
0807: }
0808:
0809: public org.omg.CORBA.portable.Streamable extract_Streamable()
0810: throws org.omg.CORBA.BAD_INV_ORDER {
0811: if (value instanceof org.omg.CORBA.portable.Streamable) {
0812: return (org.omg.CORBA.portable.Streamable) value;
0813: } else if (value == null) {
0814: throw new BAD_OPERATION(
0815: "No value has previously been inserted");
0816: } else {
0817: throw new org.omg.CORBA.BAD_INV_ORDER(
0818: "Any value is not a Streamable, but a "
0819: + value.getClass());
0820: }
0821: }
0822:
0823: public java.io.Serializable extract_Value()
0824: throws org.omg.CORBA.BAD_OPERATION {
0825: int kind = typeCode.kind().value();
0826: if (kind != TCKind._tk_value && kind != TCKind._tk_value_box
0827: && kind != TCKind._tk_abstract_interface
0828: && kind != TCKind._tk_null) {
0829: tc_error("Cannot extract value!");
0830: }
0831:
0832: if (value == null) {
0833: //return null directly, saves cast
0834: return null;
0835: } else if (value instanceof Serializable) {
0836: return (Serializable) value;
0837: } else if (value instanceof Streamable) {
0838: Class valueClass = value.getClass();
0839: try {
0840: Field field = valueClass.getDeclaredField("value");
0841: return (Serializable) field.get(value);
0842: } catch (Exception e) {
0843: throw new INTERNAL(
0844: "Failed to retrieve value from Holder via reflection: "
0845: + e);
0846: }
0847: } else {
0848: throw new INTERNAL("Encountered unexpected type of value: "
0849: + value.getClass());
0850: }
0851: }
0852:
0853: public void insert_Value(Serializable value) {
0854: if (value != null) {
0855: this .value = value;
0856: this .typeCode = TypeCode.create_tc(value.getClass());
0857: } else {
0858: this .value = null;
0859: this .typeCode = new TypeCode(TCKind._tk_null);
0860: }
0861: }
0862:
0863: public void insert_Value(Serializable value,
0864: org.omg.CORBA.TypeCode type) throws org.omg.CORBA.MARSHAL {
0865: this .value = value;
0866: this .typeCode = type;
0867: }
0868:
0869: // portable
0870:
0871: public org.omg.CORBA.portable.OutputStream create_output_stream() {
0872: if (orb instanceof org.jacorb.orb.ORB) {
0873: value = new CDROutputStream(orb);
0874: } else {
0875: value = new CDROutputStream();
0876: }
0877:
0878: return (CDROutputStream) value;
0879: }
0880:
0881: public org.omg.CORBA.portable.InputStream create_input_stream() {
0882: if (value instanceof org.jacorb.orb.CDROutputStream) {
0883: return new org.jacorb.orb.CDRInputStream(orb,
0884: ((CDROutputStream) value).getBufferCopy());
0885: }
0886:
0887: final org.jacorb.orb.CDROutputStream out;
0888:
0889: if (orb instanceof org.jacorb.orb.ORB) {
0890: out = new org.jacorb.orb.CDROutputStream(orb);
0891: } else {
0892: out = new org.jacorb.orb.CDROutputStream();
0893: }
0894:
0895: try {
0896: write_value(out);
0897: return new org.jacorb.orb.CDRInputStream(orb, out
0898: .getBufferCopy());
0899: } finally {
0900: out.close();
0901: }
0902: }
0903:
0904: public void read_value(org.omg.CORBA.portable.InputStream input,
0905: org.omg.CORBA.TypeCode type) throws org.omg.CORBA.MARSHAL {
0906: if (type == null) {
0907: throw new org.omg.CORBA.BAD_PARAM("TypeCode is null");
0908: }
0909: typeCode = type;
0910:
0911: int kind = type.kind().value();
0912: switch (kind) {
0913: case TCKind._tk_null: // 0
0914: {
0915: break;
0916: }
0917: case TCKind._tk_void: // 1
0918: {
0919: break;
0920: }
0921: case TCKind._tk_short: // 2
0922: {
0923: insert_short(input.read_short());
0924: break;
0925: }
0926: case TCKind._tk_long: // 3
0927: {
0928: insert_long(input.read_long());
0929: break;
0930: }
0931: case TCKind._tk_ushort: // 4
0932: {
0933: insert_ushort(input.read_ushort());
0934: break;
0935: }
0936: case TCKind._tk_ulong: // 5
0937: {
0938: insert_ulong(input.read_ulong());
0939: break;
0940: }
0941: case TCKind._tk_float: // 6
0942: {
0943: insert_float(input.read_float());
0944: break;
0945: }
0946: case TCKind._tk_double: // 7
0947: {
0948: insert_double(input.read_double());
0949: break;
0950: }
0951: case TCKind._tk_boolean: // 8
0952: {
0953: insert_boolean(input.read_boolean());
0954: break;
0955: }
0956: case TCKind._tk_char: // 9
0957: {
0958: insert_char(input.read_char());
0959: break;
0960: }
0961: case TCKind._tk_octet: // 10
0962: {
0963: insert_octet(input.read_octet());
0964: break;
0965: }
0966: case TCKind._tk_any: // 11
0967: {
0968: insert_any(input.read_any());
0969: break;
0970: }
0971: case TCKind._tk_TypeCode: // 12
0972: {
0973: insert_TypeCode(input.read_TypeCode());
0974: break;
0975: }
0976: case TCKind._tk_Principal: // 13
0977: {
0978: throw new org.omg.CORBA.NO_IMPLEMENT("Principal deprecated");
0979: }
0980: case TCKind._tk_objref: // 14
0981: {
0982: insert_Object(input.read_Object());
0983: break;
0984: }
0985: case TCKind._tk_struct: // 15
0986: // fallthrough
0987: case TCKind._tk_union: // 16
0988: // fallthrough
0989: case TCKind._tk_enum: // 17
0990: {
0991: CDROutputStream out = new CDROutputStream(orb);
0992: out.write_value(type, input);
0993: value = out;
0994: break;
0995: }
0996: case TCKind._tk_string: // 18
0997: {
0998: insert_string(input.read_string());
0999: break;
1000: }
1001: case TCKind._tk_sequence: // 19
1002: // fallthrough
1003: case TCKind._tk_array: // 20
1004: // fallthrough
1005: case TCKind._tk_alias: // 21
1006: // fallthrough
1007: case TCKind._tk_except: // 22
1008: {
1009: CDROutputStream out = new CDROutputStream(orb);
1010: out.write_value(type, input);
1011: value = out;
1012: break;
1013: }
1014: case TCKind._tk_longlong: // 23
1015: {
1016: insert_longlong(input.read_longlong());
1017: break;
1018: }
1019: case TCKind._tk_ulonglong: // 24
1020: {
1021: insert_ulonglong(input.read_ulonglong());
1022: break;
1023: }
1024: case TCKind._tk_longdouble: // 25
1025: {
1026: throw new org.omg.CORBA.BAD_TYPECODE(
1027: "type longdouble not supported in java");
1028: }
1029: case TCKind._tk_wchar: // 26
1030: {
1031: insert_wchar(input.read_wchar());
1032: break;
1033: }
1034: case TCKind._tk_wstring: // 27
1035: {
1036: insert_wstring(input.read_wstring());
1037: break;
1038: }
1039: case TCKind._tk_fixed: // 28
1040: {
1041: try {
1042: // move the decimal based on the scale
1043: java.math.BigDecimal fixed = ((CDRInputStream) input)
1044: .read_fixed(type.fixed_digits(), type
1045: .fixed_scale());
1046: insert_fixed(fixed, type);
1047: } catch (org.omg.CORBA.TypeCodePackage.BadKind bk) {
1048: throw new INTERNAL("should never happen");
1049: }
1050: break;
1051: }
1052: case TCKind._tk_value: // 29
1053: case TCKind._tk_value_box: // 30
1054: {
1055: insert_Value(
1056: ((org.omg.CORBA_2_3.portable.InputStream) input)
1057: .read_value(), type);
1058: break;
1059: }
1060: case TCKind._tk_native: //31
1061: {
1062: throw new BAD_TYPECODE("Cannot handle TypeCode with kind "
1063: + kind);
1064: }
1065: case TCKind._tk_abstract_interface: // 32
1066: {
1067: java.lang.Object obj = ((org.omg.CORBA_2_3.portable.InputStream) input)
1068: .read_abstract_interface();
1069: if (obj instanceof org.omg.CORBA.Object) {
1070: insert_Object((org.omg.CORBA.Object) obj);
1071: } else {
1072: insert_Value((java.io.Serializable) obj);
1073: }
1074: break;
1075: }
1076: default: {
1077: throw new BAD_TYPECODE("Cannot handle TypeCode with kind "
1078: + kind);
1079: }
1080: }
1081: }
1082:
1083: public void write_value(org.omg.CORBA.portable.OutputStream output) {
1084: final int kind = typeCode.kind().value();
1085:
1086: if (value instanceof Streamable && kind != TCKind._tk_value
1087: && kind != TCKind._tk_value_box
1088: && kind != TCKind._tk_abstract_interface
1089: && kind != TCKind._tk_null) {
1090: ((Streamable) value)._write(output);
1091: } else {
1092: switch (kind) {
1093: case TCKind._tk_null: // 0
1094: case TCKind._tk_void: // 1
1095: {
1096: break;
1097: }
1098: case TCKind._tk_short: // 2
1099: {
1100: output.write_short(extract_short());
1101: break;
1102: }
1103: case TCKind._tk_long: // 3
1104: {
1105: output.write_long(extract_long());
1106: break;
1107: }
1108: case TCKind._tk_ushort: // 4
1109: {
1110: output.write_ushort(extract_ushort());
1111: break;
1112: }
1113: case TCKind._tk_ulong: // 5
1114: {
1115: output.write_ulong(extract_ulong());
1116: break;
1117: }
1118: case TCKind._tk_float: // 6
1119: {
1120: output.write_float(extract_float());
1121: break;
1122: }
1123: case TCKind._tk_double: // 7
1124: {
1125: output.write_double(extract_double());
1126: break;
1127: }
1128: case TCKind._tk_boolean: // 8
1129: {
1130: output.write_boolean(extract_boolean());
1131: break;
1132: }
1133: case TCKind._tk_char: // 9
1134: {
1135: output.write_char(extract_char());
1136: break;
1137: }
1138: case TCKind._tk_octet: // 10
1139: {
1140: output.write_octet(extract_octet());
1141: break;
1142: }
1143: case TCKind._tk_any: // 11
1144: {
1145: output.write_any(extract_any());
1146: break;
1147: }
1148: case TCKind._tk_TypeCode: // 12
1149: {
1150: output.write_TypeCode(extract_TypeCode());
1151: break;
1152: }
1153: case TCKind._tk_Principal: // 13
1154: {
1155: throw new org.omg.CORBA.NO_IMPLEMENT(
1156: "Principal deprecated");
1157: }
1158: case TCKind._tk_objref: // 14
1159: {
1160: output.write_Object(extract_Object());
1161: break;
1162: }
1163: case TCKind._tk_struct: // 15
1164: case TCKind._tk_union: // 16
1165: case TCKind._tk_enum: // 17
1166: {
1167: this .writeComplexValue(output);
1168: break;
1169: }
1170: case TCKind._tk_string: // 18
1171: {
1172: output.write_string(extract_string());
1173: break;
1174: }
1175: case TCKind._tk_sequence: // 19
1176: case TCKind._tk_array: // 20
1177: case TCKind._tk_alias: // 21
1178: case TCKind._tk_except: // 22
1179: {
1180: this .writeComplexValue(output);
1181: break;
1182: }
1183: case TCKind._tk_longlong: // 23
1184: {
1185: output.write_longlong(extract_longlong());
1186: break;
1187: }
1188: case TCKind._tk_ulonglong: // 24
1189: {
1190: output.write_ulonglong(extract_ulonglong());
1191: break;
1192: }
1193: case TCKind._tk_longdouble: // 25
1194: {
1195: throw new org.omg.CORBA.BAD_TYPECODE(
1196: "type longdouble not supported in java");
1197: }
1198: case TCKind._tk_wchar: // 26
1199: {
1200: output.write_wchar(extract_wchar());
1201: break;
1202: }
1203: case TCKind._tk_wstring: // 27
1204: {
1205: output.write_wstring(extract_wstring());
1206: break;
1207: }
1208: case TCKind._tk_fixed: // 28
1209: {
1210: output.write_fixed(extract_fixed());
1211: break;
1212: }
1213: case TCKind._tk_value: // 29
1214: case TCKind._tk_value_box: // 30
1215: {
1216: final OutputStream outputStream = ((org.omg.CORBA_2_3.portable.OutputStream) output);
1217: final Serializable serializable = (Serializable) value;
1218: outputStream.write_value(serializable);
1219: break;
1220: }
1221: case TCKind._tk_native: //31
1222: {
1223: throw new BAD_TYPECODE(
1224: "Cannot handle TypeCode with kind " + kind);
1225: }
1226: case TCKind._tk_abstract_interface: //32
1227: {
1228: ((org.omg.CORBA_2_3.portable.OutputStream) output)
1229: .write_abstract_interface(value);
1230: break;
1231: }
1232: default: {
1233: throw new BAD_TYPECODE(
1234: "Cannot handle TypeCode with kind " + kind);
1235: }
1236: }
1237: }
1238: }
1239:
1240: // other, proprietary
1241:
1242: public void insert_void() {
1243: typeCode = orb.get_primitive_tc(TCKind.tk_void);
1244: value = null;
1245: }
1246:
1247: /**
1248: * Convenience method for making a shallow copy of an Any.
1249: */
1250: public void insert_object(org.omg.CORBA.TypeCode typeCode,
1251: java.lang.Object object) {
1252: this .typeCode = typeCode;
1253: this .value = object;
1254: }
1255:
1256: private void writeComplexValue(
1257: org.omg.CORBA.portable.OutputStream output) {
1258: if (value instanceof org.omg.CORBA.portable.Streamable) {
1259: org.omg.CORBA.portable.Streamable streamable = (org.omg.CORBA.portable.Streamable) value;
1260: streamable._write(output);
1261: } else if (value instanceof org.omg.CORBA.portable.OutputStream) {
1262: // Use ORB from CDROutputStream if Any has been created from
1263: // ORBSingleton.
1264: org.omg.CORBA.ORB toUse = orb;
1265:
1266: if (!(toUse instanceof org.jacorb.orb.ORB)) {
1267: checkStreamClass(output);
1268: toUse = ((CDROutputStream) output).orb();
1269: }
1270: checkStreamClass((org.omg.CORBA.portable.OutputStream) value);
1271: CDROutputStream out = (CDROutputStream) value;
1272: final CDRInputStream in = new CDRInputStream(toUse, out
1273: .getBufferCopy());
1274:
1275: try {
1276: in.read_value(typeCode, output);
1277: } finally {
1278: in.close();
1279: }
1280: } else {
1281: throw new org.omg.CORBA.INTERNAL(
1282: "Encountered unexpected type for any value: "
1283: + value.getClass());
1284: }
1285: }
1286:
1287: /**
1288: * <code>checkStreamClass</code> is used to provide a sanity check and some
1289: * debugging on the type of stream class.
1290: *
1291: * @param stream an <code>org.omg.CORBA.portable.OutputStream</code> value
1292: */
1293: private void checkStreamClass(
1294: org.omg.CORBA.portable.OutputStream stream) {
1295: if (!(stream instanceof CDROutputStream)) {
1296: throw new INTERNAL("Output class not CDROutputStream "
1297: + stream.getClass().getName());
1298: }
1299: }
1300:
1301: private boolean compareComplexValue(org.omg.CORBA.Any other) {
1302: final CDROutputStream this Stream;
1303: if (value instanceof CDROutputStream) {
1304: this Stream = (CDROutputStream) value;
1305: } else {
1306: this Stream = new CDROutputStream(orb);
1307: write_value(this Stream);
1308: }
1309:
1310: final CDROutputStream otherStream;
1311: if (other instanceof Any
1312: && ((Any) other).value instanceof CDROutputStream) {
1313: otherStream = (CDROutputStream) ((Any) other).value;
1314: } else {
1315: otherStream = new CDROutputStream(orb);
1316: other.write_value(otherStream);
1317: }
1318:
1319: return Arrays.equals(thisStream.getBufferCopy(), otherStream
1320: .getBufferCopy());
1321: }
1322: }
|