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