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.apache.avalon.framework.logger.Logger;
024: import org.jacorb.orb.Any;
025: import org.jacorb.orb.CDRInputStream;
026: import org.jacorb.orb.CDROutputStream;
027: import org.jacorb.orb.TypeCode;
028: import org.omg.DynamicAny.DynStructHelper;
029: import org.omg.DynamicAny.NameDynAnyPair;
030: import org.omg.DynamicAny.NameValuePair;
031: import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
032: import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
033:
034: /**
035: * CORBA DynStruct
036: *
037: * @author Gerald Brose
038: * @version $Id: DynStruct.java,v 1.24 2006/07/05 09:18:11 alphonse.bendt Exp $
039: */
040:
041: public final class DynStruct extends DynAny implements
042: org.omg.DynamicAny.DynStruct {
043: private org.omg.DynamicAny.NameValuePair[] members;
044:
045: /** only set if this represents an exception */
046: private String exceptionMsg;
047:
048: DynStruct(org.omg.DynamicAny.DynAnyFactory dynFactory,
049: org.omg.CORBA.TypeCode type, org.omg.CORBA.ORB orb,
050: Logger logger) throws TypeMismatch {
051: super (dynFactory, orb, logger);
052:
053: org.omg.CORBA.TypeCode _type = TypeCode.originalType(type);
054:
055: if (_type.kind().value() != org.omg.CORBA.TCKind._tk_except
056: && _type.kind().value() != org.omg.CORBA.TCKind._tk_struct) {
057: throw new TypeMismatch();
058: }
059:
060: typeCode = _type;
061:
062: try {
063: /* initialize position for all except empty exceptions */
064: if (!isEmptyEx()) {
065: pos = 0;
066: }
067: if (_type.kind().value() == org.omg.CORBA.TCKind._tk_except) {
068: exceptionMsg = typeCode.id();
069: }
070:
071: limit = typeCode.member_count();
072: members = new NameValuePair[limit];
073: for (int i = 0; i < limit; i++) {
074: org.omg.CORBA.TypeCode _tc = TypeCode
075: .originalType(typeCode.member_type(i));
076: members[i] = new NameValuePair(typeCode.member_name(i),
077: dynFactory.create_dyn_any_from_type_code(_tc)
078: .to_any());
079:
080: }
081: } catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode e) {
082: throw unexpectedException(e);
083: } catch (org.omg.CORBA.TypeCodePackage.BadKind e) {
084: throw unexpectedException(e);
085: } catch (org.omg.CORBA.TypeCodePackage.Bounds e) {
086: throw unexpectedException(e);
087: }
088: }
089:
090: public void from_any(org.omg.CORBA.Any value) throws InvalidValue,
091: TypeMismatch {
092: checkDestroyed();
093:
094: if (!value.type().equivalent(type())) {
095: throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch();
096: }
097:
098: typeCode = TypeCode.originalType(value.type());
099:
100: try {
101: limit = type().member_count();
102: members = new NameValuePair[limit];
103: org.omg.CORBA.portable.InputStream is = value
104: .create_input_stream();
105:
106: if (typeCode.kind().value() == org.omg.CORBA.TCKind._tk_except) {
107: exceptionMsg = is.read_string();
108: }
109:
110: for (int i = 0; i < limit; i++) {
111: try {
112: Any any = (org.jacorb.orb.Any) orb.create_any();
113: any.read_value(is, TypeCode.originalType(typeCode
114: .member_type(i)));
115:
116: members[i] = new NameValuePair(type()
117: .member_name(i), any);
118: } catch (org.omg.CORBA.TypeCodePackage.Bounds e) {
119: throw unexpectedException(e);
120: }
121: }
122: } catch (org.omg.CORBA.TypeCodePackage.BadKind e) {
123: throw unexpectedException(e);
124: }
125:
126: super .from_any(value);
127: }
128:
129: public org.omg.CORBA.Any to_any() {
130: checkDestroyed();
131: org.omg.CORBA.Any out_any = orb.create_any();
132: out_any.type(type());
133:
134: final CDROutputStream out = new CDROutputStream(orb);
135:
136: try {
137: if (type().kind().value() == org.omg.CORBA.TCKind._tk_except) {
138: out.write_string(exceptionMsg);
139: }
140:
141: for (int i = 0; i < members.length; i++) {
142: out.write_value(members[i].value.type(),
143: members[i].value.create_input_stream());
144: }
145:
146: final CDRInputStream in = new CDRInputStream(orb, out
147: .getBufferCopy());
148: try {
149: out_any.read_value(in, type());
150: return out_any;
151: } finally {
152: in.close();
153: }
154: } finally {
155: out.close();
156: }
157: }
158:
159: /**
160: * Overrides equal() in DynAny
161: */
162:
163: public boolean equal(org.omg.DynamicAny.DynAny dyn_any) {
164: checkDestroyed();
165: if (!type().equal(dyn_any.type())) {
166: return false;
167: }
168:
169: org.omg.DynamicAny.DynStruct other = DynStructHelper
170: .narrow(dyn_any);
171:
172: NameValuePair[] elements = get_members();
173: NameValuePair[] other_elements = other.get_members();
174:
175: for (int i = 0; i < elements.length; i++) {
176: if (!(elements[i].value.equal(other_elements[i].value))) {
177: return false;
178: }
179: }
180:
181: return true;
182: }
183:
184: /* DynStruct specials */
185:
186: public java.lang.String current_member_name() throws TypeMismatch,
187: InvalidValue {
188: checkDestroyed();
189:
190: if (isEmptyEx()) {
191: throw new TypeMismatch();
192: }
193: if (pos == -1) {
194: throw new InvalidValue();
195: }
196: return members[pos].id;
197: }
198:
199: public org.omg.CORBA.TCKind current_member_kind()
200: throws TypeMismatch, InvalidValue {
201: checkDestroyed();
202:
203: if (isEmptyEx()) {
204: throw new TypeMismatch();
205: }
206: if (pos == -1) {
207: throw new InvalidValue();
208: }
209: return members[pos].value.type().kind();
210: }
211:
212: public NameValuePair[] get_members() {
213: checkDestroyed();
214: return members;
215: }
216:
217: public void set_members(NameValuePair[] nvp) throws InvalidValue,
218: TypeMismatch {
219: checkDestroyed();
220: if (nvp.length != limit) {
221: throw new org.omg.DynamicAny.DynAnyPackage.InvalidValue();
222: }
223:
224: for (int i = 0; i < limit; i++) {
225: if (!nvp[i].value.type()
226: .equivalent(members[i].value.type())) {
227: throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch();
228: }
229:
230: if (!(nvp[i].id.equals("") || nvp[i].id
231: .equals(members[i].id))) {
232: throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch();
233: }
234: }
235: members = nvp;
236: }
237:
238: public org.omg.DynamicAny.NameDynAnyPair[] get_members_as_dyn_any() {
239: checkDestroyed();
240: NameDynAnyPair[] result = new NameDynAnyPair[limit];
241: try {
242: for (int i = 0; i < limit; i++) {
243: result[i] = new NameDynAnyPair(members[i].id,
244: dynFactory.create_dyn_any(members[i].value));
245: }
246:
247: return result;
248: } catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode e) {
249: throw unexpectedException(e);
250: }
251: }
252:
253: public void set_members_as_dyn_any(
254: org.omg.DynamicAny.NameDynAnyPair[] nvp)
255: throws TypeMismatch, InvalidValue {
256: checkDestroyed();
257: if (nvp.length != limit) {
258: throw new org.omg.DynamicAny.DynAnyPackage.InvalidValue();
259: }
260:
261: for (int i = 0; i < limit; i++) {
262: if (!nvp[i].value.type()
263: .equivalent(members[i].value.type())) {
264: throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch();
265: }
266:
267: if (!(nvp[i].id.equals("") || nvp[i].id
268: .equals(members[i].id))) {
269: throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch();
270: }
271:
272: }
273: members = new NameValuePair[nvp.length];
274: for (int i = 0; i < limit; i++) {
275: members[i] = new NameValuePair(nvp[i].id, nvp[i].value
276: .to_any());
277: }
278: }
279:
280: public void destroy() {
281: super .destroy();
282: members = null;
283: }
284:
285: /**
286: * returns the DynAny's internal any representation.
287: * <p>
288: * Overrides getRepresentation() in DynAny.
289: */
290:
291: protected org.omg.CORBA.Any getRepresentation() {
292: return members[pos].value;
293: }
294:
295: /* iteration interface */
296:
297: public org.omg.DynamicAny.DynAny current_component()
298: throws TypeMismatch {
299: checkDestroyed();
300: try {
301: /* special case for empty exceptions */
302: if (isEmptyEx()) {
303: throw new TypeMismatch();
304: }
305:
306: if (pos == -1) {
307: return null;
308: }
309: return dynFactory.create_dyn_any(members[pos].value);
310: } catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode e) {
311: throw unexpectedException(e);
312: }
313: }
314:
315: private boolean isEmptyEx() {
316: try {
317: return (typeCode.kind().value() == org.omg.CORBA.TCKind._tk_except && typeCode
318: .member_count() == 0);
319: } catch (org.omg.CORBA.TypeCodePackage.BadKind e) {
320: throw unexpectedException(e);
321: }
322: }
323: }
|