001: package org.jacorb.orb.dynany;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import org.omg.DynamicAny.DynAnyPackage.*;
024: import org.omg.DynamicAny.*;
025: import org.apache.avalon.framework.logger.Logger;
026: import org.jacorb.orb.*;
027:
028: /**
029: * CORBA DynEnum
030: *
031: * @author Gerald Brose
032: * @version $Id: DynEnum.java,v 1.21 2006/07/05 09:18:11 alphonse.bendt Exp $
033: */
034:
035: public final class DynEnum extends DynAny implements
036: org.omg.DynamicAny.DynEnum {
037: private int enumValue;
038: private int max;
039: private String[] member_names;
040:
041: DynEnum(org.omg.DynamicAny.DynAnyFactory dynFactory,
042: org.omg.CORBA.TypeCode type, org.omg.CORBA.ORB orb,
043: Logger logger) throws TypeMismatch {
044: super (dynFactory, orb, logger);
045:
046: org.omg.CORBA.TypeCode _type = TypeCode.originalType(type);
047:
048: if (_type.kind().value() != org.omg.CORBA.TCKind._tk_enum) {
049: throw new TypeMismatch();
050: }
051:
052: typeCode = _type;
053: pos = -1;
054: enumValue = 0;
055:
056: try {
057: member_names = new String[type().member_count()];
058: max = member_names.length;
059: for (int i = 0; i < member_names.length; i++) {
060: member_names[i] = type().member_name(i);
061: }
062: } catch (org.omg.CORBA.TypeCodePackage.BadKind e) {
063: throw unexpectedException(e);
064: } catch (org.omg.CORBA.TypeCodePackage.Bounds e) {
065: throw unexpectedException(e);
066: }
067: }
068:
069: /**
070: * Overrides from_any() in DynAny
071: */
072:
073: public void from_any(org.omg.CORBA.Any value) throws InvalidValue,
074: TypeMismatch {
075: checkDestroyed();
076: if (!value.type().equivalent(type())) {
077: throw new TypeMismatch();
078: }
079:
080: typeCode = TypeCode.originalType(value.type());
081:
082: try {
083: enumValue = value.create_input_stream().read_long();
084: member_names = new String[type().member_count()];
085: max = member_names.length;
086: for (int i = 0; i < member_names.length; i++) {
087: member_names[i] = type().member_name(i);
088: }
089: } catch (org.omg.CORBA.TypeCodePackage.Bounds e) {
090: throw unexpectedException(e);
091: } catch (org.omg.CORBA.TypeCodePackage.BadKind e) {
092: throw unexpectedException(e);
093: }
094: }
095:
096: /**
097: * Overrides equal() in DynAny
098: */
099:
100: public boolean equal(org.omg.DynamicAny.DynAny dyn_any) {
101: checkDestroyed();
102: if (!type().equal(dyn_any.type())) {
103: return false;
104: }
105:
106: return DynEnumHelper.narrow(dyn_any).get_as_ulong() == get_as_ulong();
107: }
108:
109: public org.omg.CORBA.Any to_any() {
110: checkDestroyed();
111: final CDROutputStream out = new CDROutputStream(orb);
112: try {
113: out.write_long(enumValue);
114:
115: org.omg.CORBA.Any out_any = orb.create_any();
116: out_any.type(type());
117: final CDRInputStream in = new CDRInputStream(orb, out
118: .getBufferCopy());
119:
120: try {
121: out_any.read_value(in, type());
122: return out_any;
123: } finally {
124: in.close();
125: }
126: } finally {
127: out.close();
128: }
129: }
130:
131: public java.lang.String get_as_string() {
132: checkDestroyed();
133: return member_names[enumValue];
134: }
135:
136: public void set_as_string(java.lang.String arg) throws InvalidValue {
137: checkDestroyed();
138: int i = 0;
139: while (i < member_names.length
140: && !(arg.equals(member_names[i]))) {
141: i++;
142: }
143:
144: if (i < member_names.length) {
145: set_as_ulong(i);
146: } else {
147: throw new InvalidValue();
148: }
149: }
150:
151: public int get_as_ulong() {
152: checkDestroyed();
153: return enumValue;
154: }
155:
156: public void set_as_ulong(int arg) throws InvalidValue {
157: checkDestroyed();
158: if (arg < 0 || arg > max) {
159: throw new InvalidValue();
160: }
161:
162: enumValue = arg;
163: }
164: }
|