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.DynamicAny.*;
035: import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
036: import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
037: import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
038:
039: import com.sun.corba.se.spi.orb.ORB;
040: import com.sun.corba.se.spi.logging.CORBALogDomains;
041: import com.sun.corba.se.impl.logging.ORBUtilSystemException;
042:
043: public class DynArrayImpl extends DynAnyCollectionImpl implements
044: DynArray {
045: //
046: // Constructors
047: //
048:
049: private DynArrayImpl() {
050: this (null, (Any) null, false);
051: }
052:
053: protected DynArrayImpl(ORB orb, Any any, boolean copyValue) {
054: super (orb, any, copyValue);
055: }
056:
057: protected DynArrayImpl(ORB orb, TypeCode typeCode) {
058: super (orb, typeCode);
059: }
060:
061: // Initializes components and anys representation
062: // from the Any representation
063: protected boolean initializeComponentsFromAny() {
064: // This typeCode is of kind tk_array.
065: TypeCode typeCode = any.type();
066: int length = getBound();
067: TypeCode contentType = getContentType();
068: InputStream input;
069:
070: try {
071: input = any.create_input_stream();
072: } catch (BAD_OPERATION e) {
073: return false;
074: }
075:
076: components = new DynAny[length];
077: anys = new Any[length];
078:
079: for (int i = 0; i < length; i++) {
080: // _REVISIT_ Could use read_xxx_array() methods on InputStream for efficiency
081: // but only for primitive types
082: anys[i] = DynAnyUtil.extractAnyFromStream(contentType,
083: input, orb);
084: try {
085: // Creates the appropriate subtype without copying the Any
086: components[i] = DynAnyUtil.createMostDerivedDynAny(
087: anys[i], orb, false);
088: } catch (InconsistentTypeCode itc) { // impossible
089: }
090: }
091: return true;
092: }
093:
094: // Initializes components and anys representation
095: // from the internal TypeCode information with default values.
096: // This is not done recursively, only one level.
097: // More levels are initialized lazily, on demand.
098: protected boolean initializeComponentsFromTypeCode() {
099: // This typeCode is of kind tk_array.
100: TypeCode typeCode = any.type();
101: int length = getBound();
102: TypeCode contentType = getContentType();
103:
104: components = new DynAny[length];
105: anys = new Any[length];
106:
107: for (int i = 0; i < length; i++) {
108: createDefaultComponentAt(i, contentType);
109: }
110: return true;
111: }
112:
113: //
114: // DynArray interface methods
115: //
116:
117: // Initializes the elements of the array.
118: // If value does not contain the same number of elements as the array dimension,
119: // the operation raises InvalidValue.
120: // If one or more elements have a type that is inconsistent with the DynArrays TypeCode,
121: // the operation raises TypeMismatch.
122: // This operation does not change the current position.
123: /*
124: public void set_elements (org.omg.CORBA.Any[] value)
125: throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
126: org.omg.DynamicAny.DynAnyPackage.InvalidValue;
127: */
128:
129: //
130: // Utility methods
131: //
132: protected void checkValue(Object[] value)
133: throws org.omg.DynamicAny.DynAnyPackage.InvalidValue {
134: if (value == null || value.length != getBound()) {
135: throw new InvalidValue();
136: }
137: }
138: }
|