DynAnyImpl.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.dynamicany;
27
28import org.omg.CORBA.Any;
29import org.omg.CORBA.TypeCode;
30import org.omg.CORBA.TCKind;
31import org.omg.CORBA.LocalObject;
32import org.omg.CORBA.ORBPackage.InvalidName;
33import org.omg.CORBA.portable.OutputStream;
34
35import org.omg.DynamicAny.*;
36import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
37import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
38
39import com.sun.corba.se.impl.orbutil.ORBConstants ;
40
41import com.sun.corba.se.spi.orb.ORB ;
42import com.sun.corba.se.spi.logging.CORBALogDomains ;
43import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
44
45abstract class DynAnyImpl extends org.omg.CORBA.LocalObject implements DynAny
46{
47    protected static final int NO_INDEX = -1;
48    // A DynAny is destroyable if it is the root of a DynAny hierarchy.
49    protected static final byte STATUS_DESTROYABLE = 0;
50    // A DynAny is undestroyable if it is a node in a DynAny hierarchy other than the root.
51    protected static final byte STATUS_UNDESTROYABLE = 1;
52    // A DynAny is destroyed if its root has been destroyed.
53    protected static final byte STATUS_DESTROYED = 2;
54
55    //
56    // Instance variables
57    //
58
59    protected ORB orb = null;
60    protected ORBUtilSystemException wrapper ;
61
62    // An Any is used internally to implement the basic DynAny.
63    // It stores the DynAnys TypeCode.
64    // For primitive types it is the only representation.
65    // For complex types it is the streamed representation.
66    protected Any any = null;
67    // Destroyable is the default status for free standing DynAnys.
68    protected byte status = STATUS_DESTROYABLE;
69    protected int index = NO_INDEX;
70
71    //
72    // Constructors
73    //
74
75    protected DynAnyImpl() {
76        wrapper = ORBUtilSystemException.get(
77            CORBALogDomains.RPC_PRESENTATION ) ;
78    }
79
80    protected DynAnyImpl(ORB orb, Any any, boolean copyValue) {
81        this.orb = orb;
82        wrapper = ORBUtilSystemException.get( orb,
83            CORBALogDomains.RPC_PRESENTATION ) ;
84        if (copyValue)
85            this.any = DynAnyUtil.copy(any, orb);
86        else
87            this.any = any;
88        // set the current position to 0 if any has components, otherwise to -1.
89        index = NO_INDEX;
90    }
91
92    protected DynAnyImpl(ORB orb, TypeCode typeCode) {
93        this.orb = orb;
94        wrapper = ORBUtilSystemException.get( orb,
95            CORBALogDomains.RPC_PRESENTATION ) ;
96        this.any = DynAnyUtil.createDefaultAnyOfType(typeCode, orb);
97    }
98
99    protected DynAnyFactory factory() {
100        try {
101            return (DynAnyFactory)orb.resolve_initial_references(
102                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    {
154        if (status == STATUS_DESTROYED) {
155            throw wrapper.dynAnyDestroyed() ;
156        }
157        if ((any != null) && (! any.type().equal(dyn_any.type()))) {
158            throw new TypeMismatch();
159        }
160        any = dyn_any.to_any();
161    }
162
163    // Makes a copy of the Any parameter
164    public void from_any (org.omg.CORBA.Any value)
165        throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch,
166               org.omg.DynamicAny.DynAnyPackage.InvalidValue
167    {
168        if (status == STATUS_DESTROYED) {
169            throw wrapper.dynAnyDestroyed() ;
170        }
171        if ((any != null) && (! any.type().equal(value.type()))) {
172            throw new TypeMismatch();
173        }
174        // If the passed Any does not contain a legal value
175        // (such as a null string), the operation raises InvalidValue.
176        Any tempAny = null;
177        try {
178            tempAny = DynAnyUtil.copy(value, orb);
179        } catch (Exception e) {
180            throw new InvalidValue();
181        }
182        if ( ! DynAnyUtil.isInitialized(tempAny)) {
183            throw new InvalidValue();
184        }
185        any = tempAny;
186   }
187
188    public abstract org.omg.CORBA.Any to_any();
189    public abstract boolean equal (org.omg.DynamicAny.DynAny dyn_any);
190    public abstract void destroy();
191    public abstract org.omg.DynamicAny.DynAny copy();
192
193    // Needed for org.omg.CORBA.Object
194
195    private String[] __ids = { "IDL:omg.org/DynamicAny/DynAny:1.0" };
196
197    public String[] _ids() {
198        return (String[]) __ids.clone();
199    }
200}
201