Thu, 31 Aug 2017 18:10:36 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.corba.se.impl.dynamicany; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import org.omg.CORBA.Any; |
aoqi@0 | 29 | import org.omg.CORBA.TypeCode; |
aoqi@0 | 30 | import org.omg.CORBA.TCKind; |
aoqi@0 | 31 | import org.omg.CORBA.LocalObject; |
aoqi@0 | 32 | import org.omg.CORBA.ORBPackage.InvalidName; |
aoqi@0 | 33 | import org.omg.CORBA.portable.OutputStream; |
aoqi@0 | 34 | |
aoqi@0 | 35 | import org.omg.DynamicAny.*; |
aoqi@0 | 36 | import org.omg.DynamicAny.DynAnyPackage.TypeMismatch; |
aoqi@0 | 37 | import org.omg.DynamicAny.DynAnyPackage.InvalidValue; |
aoqi@0 | 38 | |
aoqi@0 | 39 | import com.sun.corba.se.impl.orbutil.ORBConstants ; |
aoqi@0 | 40 | |
aoqi@0 | 41 | import com.sun.corba.se.spi.orb.ORB ; |
aoqi@0 | 42 | import com.sun.corba.se.spi.logging.CORBALogDomains ; |
aoqi@0 | 43 | import com.sun.corba.se.impl.logging.ORBUtilSystemException ; |
aoqi@0 | 44 | |
aoqi@0 | 45 | abstract class DynAnyImpl extends org.omg.CORBA.LocalObject implements DynAny |
aoqi@0 | 46 | { |
aoqi@0 | 47 | protected static final int NO_INDEX = -1; |
aoqi@0 | 48 | // A DynAny is destroyable if it is the root of a DynAny hierarchy. |
aoqi@0 | 49 | protected static final byte STATUS_DESTROYABLE = 0; |
aoqi@0 | 50 | // A DynAny is undestroyable if it is a node in a DynAny hierarchy other than the root. |
aoqi@0 | 51 | protected static final byte STATUS_UNDESTROYABLE = 1; |
aoqi@0 | 52 | // A DynAny is destroyed if its root has been destroyed. |
aoqi@0 | 53 | protected static final byte STATUS_DESTROYED = 2; |
aoqi@0 | 54 | |
aoqi@0 | 55 | // |
aoqi@0 | 56 | // Instance variables |
aoqi@0 | 57 | // |
aoqi@0 | 58 | |
aoqi@0 | 59 | protected ORB orb = null; |
aoqi@0 | 60 | protected ORBUtilSystemException wrapper ; |
aoqi@0 | 61 | |
aoqi@0 | 62 | // An Any is used internally to implement the basic DynAny. |
aoqi@0 | 63 | // It stores the DynAnys TypeCode. |
aoqi@0 | 64 | // For primitive types it is the only representation. |
aoqi@0 | 65 | // For complex types it is the streamed representation. |
aoqi@0 | 66 | protected Any any = null; |
aoqi@0 | 67 | // Destroyable is the default status for free standing DynAnys. |
aoqi@0 | 68 | protected byte status = STATUS_DESTROYABLE; |
aoqi@0 | 69 | protected int index = NO_INDEX; |
aoqi@0 | 70 | |
aoqi@0 | 71 | // |
aoqi@0 | 72 | // Constructors |
aoqi@0 | 73 | // |
aoqi@0 | 74 | |
aoqi@0 | 75 | protected DynAnyImpl() { |
aoqi@0 | 76 | wrapper = ORBUtilSystemException.get( |
aoqi@0 | 77 | CORBALogDomains.RPC_PRESENTATION ) ; |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | protected DynAnyImpl(ORB orb, Any any, boolean copyValue) { |
aoqi@0 | 81 | this.orb = orb; |
aoqi@0 | 82 | wrapper = ORBUtilSystemException.get( orb, |
aoqi@0 | 83 | CORBALogDomains.RPC_PRESENTATION ) ; |
aoqi@0 | 84 | if (copyValue) |
aoqi@0 | 85 | this.any = DynAnyUtil.copy(any, orb); |
aoqi@0 | 86 | else |
aoqi@0 | 87 | this.any = any; |
aoqi@0 | 88 | // set the current position to 0 if any has components, otherwise to -1. |
aoqi@0 | 89 | index = NO_INDEX; |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | protected DynAnyImpl(ORB orb, TypeCode typeCode) { |
aoqi@0 | 93 | this.orb = orb; |
aoqi@0 | 94 | wrapper = ORBUtilSystemException.get( orb, |
aoqi@0 | 95 | CORBALogDomains.RPC_PRESENTATION ) ; |
aoqi@0 | 96 | this.any = DynAnyUtil.createDefaultAnyOfType(typeCode, orb); |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | protected DynAnyFactory factory() { |
aoqi@0 | 100 | try { |
aoqi@0 | 101 | return (DynAnyFactory)orb.resolve_initial_references( |
aoqi@0 | 102 | ORBConstants.DYN_ANY_FACTORY_NAME ); |
aoqi@0 | 103 | } catch (InvalidName in) { |
aoqi@0 | 104 | throw new RuntimeException("Unable to find DynAnyFactory"); |
aoqi@0 | 105 | } |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | protected Any getAny() { |
aoqi@0 | 109 | return any; |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | // Uses getAny() if this is our implementation, otherwise uses to_any() |
aoqi@0 | 113 | // which copies the Any. |
aoqi@0 | 114 | protected Any getAny(DynAny dynAny) { |
aoqi@0 | 115 | if (dynAny instanceof DynAnyImpl) |
aoqi@0 | 116 | return ((DynAnyImpl)dynAny).getAny(); |
aoqi@0 | 117 | else |
aoqi@0 | 118 | // _REVISIT_ Nothing we can do about copying at this point |
aoqi@0 | 119 | // if this is not our implementation of DynAny. |
aoqi@0 | 120 | // To prevent this we would need another representation, |
aoqi@0 | 121 | // one where component DynAnys are initialized but not the component Anys. |
aoqi@0 | 122 | return dynAny.to_any(); |
aoqi@0 | 123 | } |
aoqi@0 | 124 | |
aoqi@0 | 125 | protected void writeAny(OutputStream out) { |
aoqi@0 | 126 | //System.out.println(this + " writeAny of type " + type().kind().value()); |
aoqi@0 | 127 | any.write_value(out); |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | protected void setStatus(byte newStatus) { |
aoqi@0 | 131 | status = newStatus; |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | protected void clearData() { |
aoqi@0 | 135 | // This clears the data part of the Any while keeping the TypeCode info. |
aoqi@0 | 136 | any.type(any.type()); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | // |
aoqi@0 | 140 | // DynAny interface methods |
aoqi@0 | 141 | // |
aoqi@0 | 142 | |
aoqi@0 | 143 | public org.omg.CORBA.TypeCode type() { |
aoqi@0 | 144 | if (status == STATUS_DESTROYED) { |
aoqi@0 | 145 | throw wrapper.dynAnyDestroyed() ; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | return any.type(); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | // Makes a copy of the Any value inside the parameter |
aoqi@0 | 151 | public void assign (org.omg.DynamicAny.DynAny dyn_any) |
aoqi@0 | 152 | throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch |
aoqi@0 | 153 | { |
aoqi@0 | 154 | if (status == STATUS_DESTROYED) { |
aoqi@0 | 155 | throw wrapper.dynAnyDestroyed() ; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | if ((any != null) && (! any.type().equal(dyn_any.type()))) { |
aoqi@0 | 158 | throw new TypeMismatch(); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | any = dyn_any.to_any(); |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | // Makes a copy of the Any parameter |
aoqi@0 | 164 | public void from_any (org.omg.CORBA.Any value) |
aoqi@0 | 165 | throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch, |
aoqi@0 | 166 | org.omg.DynamicAny.DynAnyPackage.InvalidValue |
aoqi@0 | 167 | { |
aoqi@0 | 168 | if (status == STATUS_DESTROYED) { |
aoqi@0 | 169 | throw wrapper.dynAnyDestroyed() ; |
aoqi@0 | 170 | } |
aoqi@0 | 171 | if ((any != null) && (! any.type().equal(value.type()))) { |
aoqi@0 | 172 | throw new TypeMismatch(); |
aoqi@0 | 173 | } |
aoqi@0 | 174 | // If the passed Any does not contain a legal value |
aoqi@0 | 175 | // (such as a null string), the operation raises InvalidValue. |
aoqi@0 | 176 | Any tempAny = null; |
aoqi@0 | 177 | try { |
aoqi@0 | 178 | tempAny = DynAnyUtil.copy(value, orb); |
aoqi@0 | 179 | } catch (Exception e) { |
aoqi@0 | 180 | throw new InvalidValue(); |
aoqi@0 | 181 | } |
aoqi@0 | 182 | if ( ! DynAnyUtil.isInitialized(tempAny)) { |
aoqi@0 | 183 | throw new InvalidValue(); |
aoqi@0 | 184 | } |
aoqi@0 | 185 | any = tempAny; |
aoqi@0 | 186 | } |
aoqi@0 | 187 | |
aoqi@0 | 188 | public abstract org.omg.CORBA.Any to_any(); |
aoqi@0 | 189 | public abstract boolean equal (org.omg.DynamicAny.DynAny dyn_any); |
aoqi@0 | 190 | public abstract void destroy(); |
aoqi@0 | 191 | public abstract org.omg.DynamicAny.DynAny copy(); |
aoqi@0 | 192 | |
aoqi@0 | 193 | // Needed for org.omg.CORBA.Object |
aoqi@0 | 194 | |
aoqi@0 | 195 | private String[] __ids = { "IDL:omg.org/DynamicAny/DynAny:1.0" }; |
aoqi@0 | 196 | |
aoqi@0 | 197 | public String[] _ids() { |
aoqi@0 | 198 | return (String[]) __ids.clone(); |
aoqi@0 | 199 | } |
aoqi@0 | 200 | } |