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.NO_IMPLEMENT;
031: import org.omg.CORBA.TypeCodePackage.BadKind;
032: import org.omg.CORBA.TypeCodePackage.Bounds;
033: import org.omg.DynamicAny.*;
034: import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
035: import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
036: import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
037:
038: import com.sun.corba.se.spi.orb.ORB;
039: import com.sun.corba.se.spi.logging.CORBALogDomains;
040: import com.sun.corba.se.impl.logging.ORBUtilSystemException;
041:
042: abstract class DynAnyCollectionImpl extends DynAnyConstructedImpl {
043: //
044: // Instance variables
045: //
046:
047: // Keep in sync with DynAny[] components at all times.
048: Any[] anys = null;
049:
050: //
051: // Constructors
052: //
053:
054: private DynAnyCollectionImpl() {
055: this (null, (Any) null, false);
056: }
057:
058: protected DynAnyCollectionImpl(ORB orb, Any any, boolean copyValue) {
059: super (orb, any, copyValue);
060: }
061:
062: protected DynAnyCollectionImpl(ORB orb, TypeCode typeCode) {
063: super (orb, typeCode);
064: }
065:
066: //
067: // Utility methods
068: //
069:
070: protected void createDefaultComponentAt(int i, TypeCode contentType) {
071: try {
072: components[i] = DynAnyUtil.createMostDerivedDynAny(
073: contentType, orb);
074: } catch (InconsistentTypeCode itc) { // impossible
075: }
076: // get a hold of the default initialized Any without copying
077: anys[i] = getAny(components[i]);
078: }
079:
080: protected TypeCode getContentType() {
081: try {
082: return any.type().content_type();
083: } catch (BadKind badKind) { // impossible
084: return null;
085: }
086: }
087:
088: // This method has a different meaning for sequence and array:
089: // For sequence value of 0 indicates an unbounded sequence,
090: // values > 0 indicate a bounded sequence.
091: // For array any value indicates the boundary.
092: protected int getBound() {
093: try {
094: return any.type().length();
095: } catch (BadKind badKind) { // impossible
096: return 0;
097: }
098: }
099:
100: //
101: // DynAny interface methods
102: //
103:
104: // _REVISIT_ More efficient copy operation
105:
106: //
107: // Collection methods
108: //
109:
110: public org.omg.CORBA.Any[] get_elements() {
111: if (status == STATUS_DESTROYED) {
112: throw wrapper.dynAnyDestroyed();
113: }
114: return (checkInitComponents() ? anys : null);
115: }
116:
117: protected abstract void checkValue(Object[] value)
118: throws org.omg.DynamicAny.DynAnyPackage.InvalidValue;
119:
120: // Initializes the elements of the ordered collection.
121: // If value does not contain the same number of elements as the array dimension,
122: // the operation raises InvalidValue.
123: // If one or more elements have a type that is inconsistent with the collections TypeCode,
124: // the operation raises TypeMismatch.
125: // This operation does not change the current position.
126: public void set_elements(org.omg.CORBA.Any[] value)
127: throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
128: org.omg.DynamicAny.DynAnyPackage.InvalidValue {
129: if (status == STATUS_DESTROYED) {
130: throw wrapper.dynAnyDestroyed();
131: }
132: checkValue(value);
133:
134: components = new DynAny[value.length];
135: anys = value;
136:
137: // We know that this is of kind tk_sequence or tk_array
138: TypeCode expectedTypeCode = getContentType();
139: for (int i = 0; i < value.length; i++) {
140: if (value[i] != null) {
141: if (!value[i].type().equal(expectedTypeCode)) {
142: clearData();
143: // _REVISIT_ More info
144: throw new TypeMismatch();
145: }
146: try {
147: // Creates the appropriate subtype without copying the Any
148: components[i] = DynAnyUtil.createMostDerivedDynAny(
149: value[i], orb, false);
150: //System.out.println(this + " created component " + components[i]);
151: } catch (InconsistentTypeCode itc) {
152: throw new InvalidValue();
153: }
154: } else {
155: clearData();
156: // _REVISIT_ More info
157: throw new InvalidValue();
158: }
159: }
160: index = (value.length == 0 ? NO_INDEX : 0);
161: // Other representations are invalidated by this operation
162: representations = REPRESENTATION_COMPONENTS;
163: }
164:
165: public org.omg.DynamicAny.DynAny[] get_elements_as_dyn_any() {
166: if (status == STATUS_DESTROYED) {
167: throw wrapper.dynAnyDestroyed();
168: }
169: return (checkInitComponents() ? components : null);
170: }
171:
172: // Same semantics as set_elements(Any[])
173: public void set_elements_as_dyn_any(
174: org.omg.DynamicAny.DynAny[] value)
175: throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
176: org.omg.DynamicAny.DynAnyPackage.InvalidValue {
177: if (status == STATUS_DESTROYED) {
178: throw wrapper.dynAnyDestroyed();
179: }
180: checkValue(value);
181:
182: components = (value == null ? emptyComponents : value);
183: anys = new Any[value.length];
184:
185: // We know that this is of kind tk_sequence or tk_array
186: TypeCode expectedTypeCode = getContentType();
187: for (int i = 0; i < value.length; i++) {
188: if (value[i] != null) {
189: if (!value[i].type().equal(expectedTypeCode)) {
190: clearData();
191: // _REVISIT_ More info
192: throw new TypeMismatch();
193: }
194: anys[i] = getAny(value[i]);
195: } else {
196: clearData();
197: // _REVISIT_ More info
198: throw new InvalidValue();
199: }
200: }
201: index = (value.length == 0 ? NO_INDEX : 0);
202: // Other representations are invalidated by this operation
203: representations = REPRESENTATION_COMPONENTS;
204: }
205: }
|