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:
026: import org.apache.avalon.framework.logger.Logger;
027: import org.jacorb.orb.*;
028:
029: /**
030: * CORBA DynArray
031: *
032: * @author Gerald Brose
033: * @version $Id: DynArray.java,v 1.22 2006/07/05 09:18:11 alphonse.bendt Exp $
034: */
035:
036: public final class DynArray extends DynAny implements
037: org.omg.DynamicAny.DynArray {
038: private org.omg.CORBA.TypeCode elementType;
039: private org.omg.CORBA.Any[] members;
040:
041: DynArray(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() != org.omg.CORBA.TCKind.tk_array) {
049: throw new TypeMismatch();
050: }
051:
052: try {
053: typeCode = _type;
054: elementType = TypeCode
055: .originalType(typeCode.content_type());
056:
057: limit = typeCode.length();
058: members = new Any[limit];
059: try {
060: for (int i = limit; i-- > 0;) {
061: members[i] = dynFactory
062: .create_dyn_any_from_type_code(elementType)
063: .to_any();
064: }
065: } catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode e) {
066: throw unexpectedException(e);
067: }
068: } catch (org.omg.CORBA.TypeCodePackage.BadKind e) {
069: throw unexpectedException(e);
070: }
071: }
072:
073: public void from_any(org.omg.CORBA.Any value) throws InvalidValue,
074: TypeMismatch {
075: checkDestroyed();
076: if (!typeCode.equivalent(value.type())) {
077: throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch();
078: }
079:
080: typeCode = TypeCode.originalType(value.type());
081:
082: try {
083: limit = type().length();
084: elementType = TypeCode
085: .originalType(typeCode.content_type());
086:
087: if (limit > 0) {
088: pos = 0;
089: }
090:
091: org.omg.CORBA.portable.InputStream in = value
092: .create_input_stream();
093: members = new org.omg.CORBA.Any[limit];
094:
095: for (int i = 0; i < limit; i++) {
096: members[i] = orb.create_any();
097: members[i].read_value(in, elementType);
098: }
099: } catch (org.omg.CORBA.TypeCodePackage.BadKind e) {
100: throw unexpectedException(e);
101: }
102: }
103:
104: public org.omg.CORBA.Any to_any() {
105: checkDestroyed();
106: final org.omg.CORBA.Any out_any = orb.create_any();
107: out_any.type(type());
108:
109: final CDROutputStream out = new CDROutputStream(orb);
110:
111: try {
112: for (int i = 0; i < limit; i++) {
113: out.write_value(elementType, members[i]
114: .create_input_stream());
115: }
116:
117: final CDRInputStream in = new CDRInputStream(orb, out
118: .getBufferCopy());
119: try {
120: out_any.read_value(in, type());
121: return out_any;
122: } finally {
123: in.close();
124: }
125: } finally {
126: out.close();
127: }
128: }
129:
130: /**
131: * Overrides equal() in DynAny
132: */
133:
134: public boolean equal(org.omg.DynamicAny.DynAny dyn_any) {
135: checkDestroyed();
136: if (!type().equal(dyn_any.type())) {
137: return false;
138: }
139:
140: org.omg.DynamicAny.DynArray other = DynArrayHelper
141: .narrow(dyn_any);
142:
143: org.omg.CORBA.Any[] elements = get_elements();
144: org.omg.CORBA.Any[] other_elements = other.get_elements();
145:
146: for (int i = 0; i < elements.length; i++) {
147: if (!(elements[i].equal(other_elements[i]))) {
148: return false;
149: }
150: }
151:
152: return true;
153: }
154:
155: public org.omg.CORBA.Any[] get_elements() {
156: checkDestroyed();
157: return members;
158: }
159:
160: public void set_elements(org.omg.CORBA.Any[] value)
161: throws TypeMismatch, InvalidValue {
162: checkDestroyed();
163: if (value.length != limit) {
164: throw new InvalidValue();
165: }
166:
167: for (int i = value.length; i-- > 0;) {
168: org.omg.CORBA.TypeCode tc = TypeCode.originalType(value[i]
169: .type());
170:
171: if (tc.kind() != elementType.kind()) {
172: throw new TypeMismatch();
173: }
174: }
175:
176: /** ok now */
177: members = value;
178: }
179:
180: public org.omg.DynamicAny.DynAny[] get_elements_as_dyn_any() {
181: checkDestroyed();
182: org.omg.DynamicAny.DynAny[] result = new org.omg.DynamicAny.DynAny[members.length];
183: try {
184: for (int i = members.length; i-- > 0;) {
185: result[i] = dynFactory.create_dyn_any(members[i]);
186: }
187: return result;
188: } catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode e) {
189: throw unexpectedException(e);
190: }
191: }
192:
193: public void set_elements_as_dyn_any(
194: org.omg.DynamicAny.DynAny[] value) throws TypeMismatch,
195: InvalidValue {
196: checkDestroyed();
197: org.omg.CORBA.Any[] any_seq = new org.omg.CORBA.Any[value.length];
198: for (int i = value.length; i-- > 0;) {
199: any_seq[i] = value[i].to_any();
200: }
201:
202: set_elements(any_seq);
203: }
204:
205: public void destroy() {
206: super .destroy();
207: members = null;
208: elementType = null;
209: }
210:
211: /**
212: * Returns the DynAny's internal any representation.
213: * <p>
214: * Overrides getRepresentation() in DynAny
215: */
216:
217: protected org.omg.CORBA.Any getRepresentation() {
218: return members[pos];
219: }
220:
221: /* iteration interface */
222:
223: public org.omg.DynamicAny.DynAny current_component() {
224: checkDestroyed();
225: if (pos == -1) {
226: return null;
227: }
228:
229: try {
230: return dynFactory.create_dyn_any(members[pos]);
231: } catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode e) {
232: throw unexpectedException(e);
233: }
234: }
235: }
|