001: /*
002: * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.corba.se.impl.dynamicany;
027:
028: import org.omg.CORBA.TypeCode;
029: import org.omg.CORBA.Any;
030: import org.omg.CORBA.BAD_OPERATION;
031: import org.omg.CORBA.TypeCodePackage.BadKind;
032: import org.omg.CORBA.TypeCodePackage.Bounds;
033: import org.omg.CORBA.portable.InputStream;
034: import org.omg.CORBA.portable.OutputStream;
035: import org.omg.DynamicAny.*;
036: import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
037: import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
038: import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
039:
040: import com.sun.corba.se.spi.orb.ORB;
041: import com.sun.corba.se.spi.logging.CORBALogDomains;
042: import com.sun.corba.se.impl.logging.ORBUtilSystemException;
043:
044: // _REVIST_ Could make this a subclass of DynArrayImpl
045: // But that would mean that an object that implements DynSequence also implements DynArray
046: // which the spec doesn't mention (it also doesn't forbid it).
047: public class DynSequenceImpl extends DynAnyCollectionImpl implements
048: DynSequence {
049: //
050: // Constructors
051: //
052:
053: private DynSequenceImpl() {
054: this (null, (Any) null, false);
055: }
056:
057: protected DynSequenceImpl(ORB orb, Any any, boolean copyValue) {
058: super (orb, any, copyValue);
059: }
060:
061: // Sets the current position to -1 and creates an empty sequence.
062: protected DynSequenceImpl(ORB orb, TypeCode typeCode) {
063: super (orb, typeCode);
064: }
065:
066: // Initializes components and anys representation
067: // from the Any representation
068: protected boolean initializeComponentsFromAny() {
069: // This typeCode is of kind tk_sequence.
070: TypeCode typeCode = any.type();
071: int length;
072: TypeCode contentType = getContentType();
073: InputStream input;
074:
075: try {
076: input = any.create_input_stream();
077: } catch (BAD_OPERATION e) {
078: return false;
079: }
080:
081: length = input.read_long();
082: components = new DynAny[length];
083: anys = new Any[length];
084:
085: for (int i = 0; i < length; i++) {
086: // _REVISIT_ Could use read_xxx_array() methods on InputStream for efficiency
087: // but only for primitive types
088: anys[i] = DynAnyUtil.extractAnyFromStream(contentType,
089: input, orb);
090: try {
091: // Creates the appropriate subtype without copying the Any
092: components[i] = DynAnyUtil.createMostDerivedDynAny(
093: anys[i], orb, false);
094: } catch (InconsistentTypeCode itc) { // impossible
095: }
096: }
097: return true;
098: }
099:
100: // Sets the current position to -1 and creates an empty sequence.
101: protected boolean initializeComponentsFromTypeCode() {
102: // already done in the type code constructor
103: components = new DynAny[0];
104: anys = new Any[0];
105: return true;
106: }
107:
108: // Collapses the whole DynAny hierarchys values into one single streamed Any
109: protected boolean initializeAnyFromComponents() {
110: OutputStream out = any.create_output_stream();
111: // Writing the length first is the only difference to supers implementation
112: out.write_long(components.length);
113: for (int i = 0; i < components.length; i++) {
114: if (components[i] instanceof DynAnyImpl) {
115: ((DynAnyImpl) components[i]).writeAny(out);
116: } else {
117: // Not our implementation. Nothing we can do to prevent copying.
118: components[i].to_any().write_value(out);
119: }
120: }
121: any.read_value(out.create_input_stream(), any.type());
122: return true;
123: }
124:
125: //
126: // DynSequence interface methods
127: //
128:
129: // Returns the current length of the sequence
130: public int get_length() {
131: if (status == STATUS_DESTROYED) {
132: throw wrapper.dynAnyDestroyed();
133: }
134: return (checkInitComponents() ? components.length : 0);
135: }
136:
137: // Sets the length of the sequence. Increasing the length of a sequence
138: // adds new elements at the tail without affecting the values of already
139: // existing elements. Newly added elements are default-initialized.
140: //
141: // Increasing the length of a sequence sets the current position to the first
142: // newly-added element if the previous current position was -1.
143: // Otherwise, if the previous current position was not -1,
144: // the current position is not affected.
145: //
146: // Increasing the length of a bounded sequence to a value larger than the bound
147: // raises InvalidValue.
148: //
149: // Decreasing the length of a sequence removes elements from the tail
150: // without affecting the value of those elements that remain.
151: // The new current position after decreasing the length of a sequence is determined
152: // as follows:
153: // ?f the length of the sequence is set to zero, the current position is set to -1.
154: // ?f the current position is -1 before decreasing the length, it remains at -1.
155: // ?f the current position indicates a valid element and that element is not removed
156: // when the length is decreased, the current position remains unaffected.
157: // ?f the current position indicates a valid element and that element is removed, the
158: // current position is set to -1.
159: public void set_length(int len)
160: throws org.omg.DynamicAny.DynAnyPackage.InvalidValue {
161: if (status == STATUS_DESTROYED) {
162: throw wrapper.dynAnyDestroyed();
163: }
164: int bound = getBound();
165: if (bound > 0 && len > bound) {
166: throw new InvalidValue();
167: }
168:
169: checkInitComponents();
170:
171: int oldLength = components.length;
172: if (len > oldLength) {
173: // Increase length
174: DynAny[] newComponents = new DynAny[len];
175: Any[] newAnys = new Any[len];
176: System
177: .arraycopy(components, 0, newComponents, 0,
178: oldLength);
179: System.arraycopy(anys, 0, newAnys, 0, oldLength);
180: components = newComponents;
181: anys = newAnys;
182:
183: // Newly added elements are default-initialized
184: TypeCode contentType = getContentType();
185: for (int i = oldLength; i < len; i++) {
186: createDefaultComponentAt(i, contentType);
187: }
188:
189: // Increasing the length of a sequence sets the current position to the first
190: // newly-added element if the previous current position was -1.
191: if (index == NO_INDEX)
192: index = oldLength;
193: } else if (len < oldLength) {
194: // Decrease length
195: DynAny[] newComponents = new DynAny[len];
196: Any[] newAnys = new Any[len];
197: System.arraycopy(components, 0, newComponents, 0, len);
198: System.arraycopy(anys, 0, newAnys, 0, len);
199: // It is probably right not to destroy the released component DynAnys.
200: // Some other DynAny or a user variable might still hold onto them
201: // and if not then the garbage collector will take care of it.
202: //for (int i=len; i<oldLength; i++) {
203: // components[i].destroy();
204: //}
205: components = newComponents;
206: anys = newAnys;
207:
208: // ?f the length of the sequence is set to zero, the current position is set to -1.
209: // ?f the current position is -1 before decreasing the length, it remains at -1.
210: // ?f the current position indicates a valid element and that element is not removed
211: // when the length is decreased, the current position remains unaffected.
212: // ?f the current position indicates a valid element and that element is removed,
213: // the current position is set to -1.
214: if (len == 0 || index >= len) {
215: index = NO_INDEX;
216: }
217: } else {
218: // Length unchanged
219: // Maybe components is now default initialized from type code
220: if (index == NO_INDEX && len > 0) {
221: index = 0;
222: }
223: }
224: }
225:
226: // Initializes the elements of the sequence.
227: // The length of the DynSequence is set to the length of value.
228: // The current position is set to zero if value has non-zero length
229: // and to -1 if value is a zero-length sequence.
230: // If the length of value exceeds the bound of a bounded sequence,
231: // the operation raises InvalidValue.
232: // If value contains one or more elements whose TypeCode is not equivalent
233: // to the element TypeCode of the DynSequence, the operation raises TypeMismatch.
234: /*
235: public void set_elements(org.omg.CORBA.Any[] value)
236: throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
237: org.omg.DynamicAny.DynAnyPackage.InvalidValue;
238: */
239:
240: //
241: // Utility methods
242: //
243: protected void checkValue(Object[] value)
244: throws org.omg.DynamicAny.DynAnyPackage.InvalidValue {
245: if (value == null || value.length == 0) {
246: clearData();
247: index = NO_INDEX;
248: return;
249: } else {
250: index = 0;
251: }
252: int bound = getBound();
253: if (bound > 0 && value.length > bound) {
254: throw new InvalidValue();
255: }
256: }
257: }
|