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.Any;
029: import org.omg.CORBA.TypeCode;
030: import org.omg.CORBA.TCKind;
031: import org.omg.CORBA.LocalObject;
032: import org.omg.CORBA.ORBPackage.InvalidName;
033: import org.omg.CORBA.portable.OutputStream;
034:
035: import org.omg.DynamicAny.*;
036: import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
037: import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
038:
039: import com.sun.corba.se.impl.orbutil.ORBConstants;
040:
041: import com.sun.corba.se.spi.orb.ORB;
042: import com.sun.corba.se.spi.logging.CORBALogDomains;
043: import com.sun.corba.se.impl.logging.ORBUtilSystemException;
044:
045: abstract class DynAnyImpl extends org.omg.CORBA.LocalObject implements
046: DynAny {
047: protected static final int NO_INDEX = -1;
048: // A DynAny is destroyable if it is the root of a DynAny hierarchy.
049: protected static final byte STATUS_DESTROYABLE = 0;
050: // A DynAny is undestroyable if it is a node in a DynAny hierarchy other than the root.
051: protected static final byte STATUS_UNDESTROYABLE = 1;
052: // A DynAny is destroyed if its root has been destroyed.
053: protected static final byte STATUS_DESTROYED = 2;
054:
055: //
056: // Instance variables
057: //
058:
059: protected ORB orb = null;
060: protected ORBUtilSystemException wrapper;
061:
062: // An Any is used internally to implement the basic DynAny.
063: // It stores the DynAnys TypeCode.
064: // For primitive types it is the only representation.
065: // For complex types it is the streamed representation.
066: protected Any any = null;
067: // Destroyable is the default status for free standing DynAnys.
068: protected byte status = STATUS_DESTROYABLE;
069: protected int index = NO_INDEX;
070:
071: //
072: // Constructors
073: //
074:
075: protected DynAnyImpl() {
076: wrapper = ORBUtilSystemException
077: .get(CORBALogDomains.RPC_PRESENTATION);
078: }
079:
080: protected DynAnyImpl(ORB orb, Any any, boolean copyValue) {
081: this .orb = orb;
082: wrapper = ORBUtilSystemException.get(orb,
083: CORBALogDomains.RPC_PRESENTATION);
084: if (copyValue)
085: this .any = DynAnyUtil.copy(any, orb);
086: else
087: this .any = any;
088: // set the current position to 0 if any has components, otherwise to -1.
089: index = NO_INDEX;
090: }
091:
092: protected DynAnyImpl(ORB orb, TypeCode typeCode) {
093: this .orb = orb;
094: wrapper = ORBUtilSystemException.get(orb,
095: CORBALogDomains.RPC_PRESENTATION);
096: this .any = DynAnyUtil.createDefaultAnyOfType(typeCode, orb);
097: }
098:
099: protected DynAnyFactory factory() {
100: try {
101: return (DynAnyFactory) orb
102: .resolve_initial_references(ORBConstants.DYN_ANY_FACTORY_NAME);
103: } catch (InvalidName in) {
104: throw new RuntimeException("Unable to find DynAnyFactory");
105: }
106: }
107:
108: protected Any getAny() {
109: return any;
110: }
111:
112: // Uses getAny() if this is our implementation, otherwise uses to_any()
113: // which copies the Any.
114: protected Any getAny(DynAny dynAny) {
115: if (dynAny instanceof DynAnyImpl)
116: return ((DynAnyImpl) dynAny).getAny();
117: else
118: // _REVISIT_ Nothing we can do about copying at this point
119: // if this is not our implementation of DynAny.
120: // To prevent this we would need another representation,
121: // one where component DynAnys are initialized but not the component Anys.
122: return dynAny.to_any();
123: }
124:
125: protected void writeAny(OutputStream out) {
126: //System.out.println(this + " writeAny of type " + type().kind().value());
127: any.write_value(out);
128: }
129:
130: protected void setStatus(byte newStatus) {
131: status = newStatus;
132: }
133:
134: protected void clearData() {
135: // This clears the data part of the Any while keeping the TypeCode info.
136: any.type(any.type());
137: }
138:
139: //
140: // DynAny interface methods
141: //
142:
143: public org.omg.CORBA.TypeCode type() {
144: if (status == STATUS_DESTROYED) {
145: throw wrapper.dynAnyDestroyed();
146: }
147: return any.type();
148: }
149:
150: // Makes a copy of the Any value inside the parameter
151: public void assign(org.omg.DynamicAny.DynAny dyn_any)
152: throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch {
153: if (status == STATUS_DESTROYED) {
154: throw wrapper.dynAnyDestroyed();
155: }
156: if ((any != null) && (!any.type().equal(dyn_any.type()))) {
157: throw new TypeMismatch();
158: }
159: any = dyn_any.to_any();
160: }
161:
162: // Makes a copy of the Any parameter
163: public void from_any(org.omg.CORBA.Any value)
164: throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
165: org.omg.DynamicAny.DynAnyPackage.InvalidValue {
166: if (status == STATUS_DESTROYED) {
167: throw wrapper.dynAnyDestroyed();
168: }
169: if ((any != null) && (!any.type().equal(value.type()))) {
170: throw new TypeMismatch();
171: }
172: // If the passed Any does not contain a legal value
173: // (such as a null string), the operation raises InvalidValue.
174: Any tempAny = null;
175: try {
176: tempAny = DynAnyUtil.copy(value, orb);
177: } catch (Exception e) {
178: throw new InvalidValue();
179: }
180: if (!DynAnyUtil.isInitialized(tempAny)) {
181: throw new InvalidValue();
182: }
183: any = tempAny;
184: }
185:
186: public abstract org.omg.CORBA.Any to_any();
187:
188: public abstract boolean equal(org.omg.DynamicAny.DynAny dyn_any);
189:
190: public abstract void destroy();
191:
192: public abstract org.omg.DynamicAny.DynAny copy();
193:
194: // Needed for org.omg.CORBA.Object
195:
196: private String[] __ids = { "IDL:omg.org/DynamicAny/DynAny:1.0" };
197:
198: public String[] _ids() {
199: return __ids;
200: }
201: }
|