KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > dynamicany > DynAnyImpl


1 /*
2  * @(#)DynAnyImpl.java 1.15 03/06/25
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.dynamicany;
9
10 import org.omg.CORBA.Any JavaDoc;
11 import org.omg.CORBA.TypeCode JavaDoc;
12 import org.omg.CORBA.TCKind JavaDoc;
13 import org.omg.CORBA.LocalObject JavaDoc;
14 import org.omg.CORBA.ORBPackage.InvalidName JavaDoc;
15 import org.omg.CORBA.portable.OutputStream JavaDoc;
16
17 import org.omg.DynamicAny.*;
18 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc;
19 import org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc;
20
21 import com.sun.corba.se.impl.orbutil.ORBConstants ;
22
23 import com.sun.corba.se.spi.orb.ORB ;
24 import com.sun.corba.se.spi.logging.CORBALogDomains ;
25 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
26
27 abstract class DynAnyImpl extends org.omg.CORBA.LocalObject JavaDoc implements DynAny
28 {
29     protected static final int NO_INDEX = -1;
30     // A DynAny is destroyable if it is the root of a DynAny hierarchy.
31
protected static final byte STATUS_DESTROYABLE = 0;
32     // A DynAny is undestroyable if it is a node in a DynAny hierarchy other than the root.
33
protected static final byte STATUS_UNDESTROYABLE = 1;
34     // A DynAny is destroyed if its root has been destroyed.
35
protected static final byte STATUS_DESTROYED = 2;
36
37     //
38
// Instance variables
39
//
40

41     protected ORB orb = null;
42     protected ORBUtilSystemException wrapper ;
43
44     // An Any is used internally to implement the basic DynAny.
45
// It stores the DynAnys TypeCode.
46
// For primitive types it is the only representation.
47
// For complex types it is the streamed representation.
48
protected Any JavaDoc any = null;
49     // Destroyable is the default status for free standing DynAnys.
50
protected byte status = STATUS_DESTROYABLE;
51     protected int index = NO_INDEX;
52
53     //
54
// Constructors
55
//
56

57     protected DynAnyImpl() {
58     wrapper = ORBUtilSystemException.get(
59             CORBALogDomains.RPC_PRESENTATION ) ;
60     }
61
62     protected DynAnyImpl(ORB orb, Any JavaDoc any, boolean copyValue) {
63         this.orb = orb;
64     wrapper = ORBUtilSystemException.get( orb,
65         CORBALogDomains.RPC_PRESENTATION ) ;
66         if (copyValue)
67             this.any = DynAnyUtil.copy(any, orb);
68         else
69             this.any = any;
70         // set the current position to 0 if any has components, otherwise to -1.
71
index = NO_INDEX;
72     }
73
74     protected DynAnyImpl(ORB orb, TypeCode JavaDoc typeCode) {
75         this.orb = orb;
76     wrapper = ORBUtilSystemException.get( orb,
77         CORBALogDomains.RPC_PRESENTATION ) ;
78         this.any = DynAnyUtil.createDefaultAnyOfType(typeCode, orb);
79     }
80
81     protected DynAnyFactory factory() {
82         try {
83             return (DynAnyFactory)orb.resolve_initial_references(
84         ORBConstants.DYN_ANY_FACTORY_NAME );
85         } catch (InvalidName JavaDoc in) {
86             throw new RuntimeException JavaDoc("Unable to find DynAnyFactory");
87         }
88     }
89
90     protected Any JavaDoc getAny() {
91         return any;
92     }
93
94     // Uses getAny() if this is our implementation, otherwise uses to_any()
95
// which copies the Any.
96
protected Any JavaDoc getAny(DynAny dynAny) {
97         if (dynAny instanceof DynAnyImpl)
98             return ((DynAnyImpl)dynAny).getAny();
99         else
100             // _REVISIT_ Nothing we can do about copying at this point
101
// if this is not our implementation of DynAny.
102
// To prevent this we would need another representation,
103
// one where component DynAnys are initialized but not the component Anys.
104
return dynAny.to_any();
105     }
106
107     protected void writeAny(OutputStream JavaDoc out) {
108         //System.out.println(this + " writeAny of type " + type().kind().value());
109
any.write_value(out);
110     }
111
112     protected void setStatus(byte newStatus) {
113         status = newStatus;
114     }
115
116     protected void clearData() {
117         // This clears the data part of the Any while keeping the TypeCode info.
118
any.type(any.type());
119     }
120
121     //
122
// DynAny interface methods
123
//
124

125     public org.omg.CORBA.TypeCode JavaDoc type() {
126         if (status == STATUS_DESTROYED) {
127         throw wrapper.dynAnyDestroyed() ;
128         }
129         return any.type();
130     }
131
132     // Makes a copy of the Any value inside the parameter
133
public void assign (org.omg.DynamicAny.DynAny JavaDoc dyn_any)
134         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc
135     {
136         if (status == STATUS_DESTROYED) {
137         throw wrapper.dynAnyDestroyed() ;
138         }
139         if ((any != null) && (! any.type().equal(dyn_any.type()))) {
140             throw new TypeMismatch JavaDoc();
141         }
142         any = dyn_any.to_any();
143     }
144
145     // Makes a copy of the Any parameter
146
public void from_any (org.omg.CORBA.Any JavaDoc value)
147         throws org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc,
148                org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc
149     {
150         if (status == STATUS_DESTROYED) {
151         throw wrapper.dynAnyDestroyed() ;
152         }
153         if ((any != null) && (! any.type().equal(value.type()))) {
154             throw new TypeMismatch JavaDoc();
155         }
156         // If the passed Any does not contain a legal value
157
// (such as a null string), the operation raises InvalidValue.
158
Any JavaDoc tempAny = null;
159         try {
160             tempAny = DynAnyUtil.copy(value, orb);
161         } catch (Exception JavaDoc e) {
162             throw new InvalidValue JavaDoc();
163         }
164         if ( ! DynAnyUtil.isInitialized(tempAny)) {
165             throw new InvalidValue JavaDoc();
166         }
167         any = tempAny;
168    }
169
170     public abstract org.omg.CORBA.Any JavaDoc to_any();
171     public abstract boolean equal (org.omg.DynamicAny.DynAny JavaDoc dyn_any);
172     public abstract void destroy();
173     public abstract org.omg.DynamicAny.DynAny JavaDoc copy();
174
175     // Needed for org.omg.CORBA.Object
176

177     private String JavaDoc[] __ids = { "IDL:omg.org/DynamicAny/DynAny:1.0" };
178
179     public String JavaDoc[] _ids() {
180         return __ids;
181     }
182 }
183
Popular Tags