KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > dynany > DynStruct


1 package org.jacorb.orb.dynany;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.omg.DynamicAny.DynAnyPackage.*;
24 import org.omg.DynamicAny.*;
25 import org.jacorb.orb.*;
26
27 /**
28  * CORBA DynStruct
29  *
30  * @author (c) Gerald Brose, FU Berlin 1999
31  * $Id: DynStruct.java,v 1.18 2005/03/25 13:15:55 andre.spiegel Exp $
32  */

33
34 public final class DynStruct
35     extends DynAny
36     implements org.omg.DynamicAny.DynStruct JavaDoc
37 {
38     private org.omg.DynamicAny.NameValuePair JavaDoc[] members;
39
40     /** only set if this represents an exception */
41     private String JavaDoc exception_msg;
42
43     DynStruct( org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory,
44                org.omg.CORBA.TypeCode JavaDoc tc)
45         throws InvalidValue, TypeMismatch
46     {
47         org.omg.CORBA.TypeCode JavaDoc _type = TypeCode.originalType( tc );
48
49         if( _type.kind().value() != org.omg.CORBA.TCKind._tk_except &&
50             _type.kind().value() != org.omg.CORBA.TCKind._tk_struct )
51             throw new TypeMismatch();
52
53         this.orb = org.omg.CORBA.ORB.init();
54         this.dynFactory = dynFactory;
55
56         type = _type;
57
58         try
59         {
60             /* initialize position for all except empty exceptions */
61             if( !isEmptyEx () )
62             {
63                 pos = 0;
64             }
65             if( _type.kind().value() == org.omg.CORBA.TCKind._tk_except )
66                 exception_msg = type.id();
67
68             limit = type.member_count();
69             members = new NameValuePair[limit];
70             for( int i = 0 ; i < limit; i++ )
71             {
72                 org.omg.CORBA.TypeCode JavaDoc _tc =
73                     TypeCode.originalType( type.member_type(i) );
74                 members[i] =
75                     new NameValuePair(
76                                       type.member_name(i),
77                                       dynFactory.create_dyn_any_from_type_code( _tc ).to_any());
78
79             }
80         }
81         catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
82         {
83             itc.printStackTrace();
84         }
85         catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
86         {
87             bk.printStackTrace();
88         }
89         catch( org.omg.CORBA.TypeCodePackage.Bounds JavaDoc b )
90         {
91             b.printStackTrace();
92         }
93     }
94     DynStruct( org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory,
95             org.omg.CORBA.TypeCode JavaDoc tc,org.omg.CORBA.ORB JavaDoc orb)
96      throws InvalidValue, TypeMismatch
97     {
98      org.omg.CORBA.TypeCode JavaDoc _type = TypeCode.originalType( tc );
99
100      if( _type.kind().value() != org.omg.CORBA.TCKind._tk_except &&
101          _type.kind().value() != org.omg.CORBA.TCKind._tk_struct )
102          throw new TypeMismatch();
103
104      this.orb = orb;
105      this.dynFactory = dynFactory;
106
107      type = _type;
108
109      try
110      {
111          /* initialize position for all except empty exceptions */
112          if( !isEmptyEx () )
113          {
114              pos = 0;
115          }
116          if( _type.kind().value() == org.omg.CORBA.TCKind._tk_except )
117              exception_msg = type.id();
118
119          limit = type.member_count();
120          members = new NameValuePair[limit];
121          for( int i = 0 ; i < limit; i++ )
122          {
123              org.omg.CORBA.TypeCode JavaDoc _tc =
124                  TypeCode.originalType( type.member_type(i) );
125              members[i] =
126                  new NameValuePair(
127                                    type.member_name(i),
128                                    dynFactory.create_dyn_any_from_type_code( _tc ).to_any());
129
130          }
131      }
132      catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
133      {
134          itc.printStackTrace();
135      }
136      catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
137      {
138          bk.printStackTrace();
139      }
140      catch( org.omg.CORBA.TypeCodePackage.Bounds JavaDoc b )
141      {
142          b.printStackTrace();
143      }
144     }
145
146     public void from_any(org.omg.CORBA.Any JavaDoc value)
147         throws InvalidValue, TypeMismatch
148     {
149         checkDestroyed ();
150
151         if( !value.type().equivalent( type() ))
152             throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc();
153
154         type = TypeCode.originalType( value.type() );
155
156         try
157         {
158             limit = type().member_count();
159             members = new NameValuePair[limit];
160             org.omg.CORBA.portable.InputStream JavaDoc is =
161                 value.create_input_stream();
162
163             if( type.kind().value() == org.omg.CORBA.TCKind._tk_except )
164             {
165                 exception_msg = is.read_string();
166             }
167
168             for( int i = 0 ; i < limit; i++ )
169             {
170                 try
171                 {
172                     Any a = (org.jacorb.orb.Any)orb.create_any();
173                     a.read_value(is,
174                                  TypeCode.originalType(type.member_type(i)));
175
176                     members[i] = new NameValuePair( type().member_name(i), a);
177                 }
178                 catch( org.omg.CORBA.TypeCodePackage.Bounds JavaDoc b )
179                 {
180                     b.printStackTrace();
181                 }
182             }
183         }
184         catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
185         {
186             // should not happen anymore
187
bk.printStackTrace();
188         }
189          
190         super.from_any( value );
191     }
192
193
194     public org.omg.CORBA.Any JavaDoc to_any()
195     {
196         checkDestroyed ();
197         org.jacorb.orb.Any out_any =
198             (org.jacorb.orb.Any)orb.create_any();
199         out_any.type( type());
200
201         CDROutputStream os = new CDROutputStream();
202
203         if( type().kind().value() == org.omg.CORBA.TCKind._tk_except )
204         {
205             os.write_string( exception_msg );
206         }
207
208         for( int i = 0; i < members.length; i++)
209         {
210             os.write_value( members[i].value.type(),
211                             (CDRInputStream)members[i].value.create_input_stream());
212         }
213
214         CDRInputStream is = new CDRInputStream(orb, os.getBufferCopy());
215         out_any.read_value( is, type());
216         return out_any;
217     }
218
219     /**
220      * Overrides equal() in DynAny
221      */

222
223     public boolean equal( org.omg.DynamicAny.DynAny JavaDoc dyn_any )
224     {
225         checkDestroyed ();
226         if( !type().equal( dyn_any.type()) )
227             return false;
228
229         org.omg.DynamicAny.DynStruct JavaDoc other = DynStructHelper.narrow( dyn_any );
230
231         NameValuePair[] elements = get_members();
232         NameValuePair[] other_elements = other.get_members();
233
234         for( int i = 0; i < elements.length; i++ )
235         {
236             if( !(elements[i].value.equal( other_elements[i].value )))
237                 return false;
238         }
239
240         return true;
241     }
242
243
244     /* DynStruct specials */
245
246     public java.lang.String JavaDoc current_member_name()
247         throws TypeMismatch, InvalidValue
248     {
249         checkDestroyed ();
250
251         if (isEmptyEx ())
252         {
253             throw new TypeMismatch ();
254         }
255         if (pos == -1)
256         {
257             throw new InvalidValue ();
258         }
259         return members[pos].id;
260     }
261
262
263     public org.omg.CORBA.TCKind JavaDoc current_member_kind()
264         throws TypeMismatch, InvalidValue
265     {
266         checkDestroyed ();
267
268         if (isEmptyEx ())
269         {
270             throw new TypeMismatch ();
271         }
272         if (pos == -1)
273         {
274             throw new InvalidValue ();
275         }
276         return members[pos].value.type().kind();
277     }
278
279
280     public NameValuePair[] get_members()
281     {
282         checkDestroyed ();
283         return members;
284     }
285
286
287     public void set_members( NameValuePair[] nvp )
288         throws InvalidValue, TypeMismatch
289     {
290         checkDestroyed ();
291         if( nvp.length != limit )
292             throw new org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc();
293  
294         for( int i = 0; i < limit; i++ )
295         {
296             if( ! nvp[i].value.type().equivalent( members[i].value.type() ))
297             {
298                 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc();
299             }
300
301             if(! (nvp[i].id.equals("") || nvp[i].id.equals( members[i].id )))
302                 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc();
303         }
304         members = nvp;
305     }
306
307
308     public org.omg.DynamicAny.NameDynAnyPair JavaDoc[] get_members_as_dyn_any()
309     {
310         checkDestroyed ();
311         NameDynAnyPair[] result = new NameDynAnyPair[limit];
312         try
313         {
314             for( int i = 0; i < limit; i++ )
315             {
316                 result[i] = new NameDynAnyPair( members[i].id,
317                                                 dynFactory.create_dyn_any( members[i].value ));
318             }
319         }
320         catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
321         {
322             itc.printStackTrace();
323         }
324         return result ;
325     }
326
327
328     public void set_members_as_dyn_any(org.omg.DynamicAny.NameDynAnyPair JavaDoc[] nvp)
329         throws TypeMismatch, InvalidValue
330     {
331         checkDestroyed ();
332         if( nvp.length != limit )
333             throw new org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc();
334
335         for( int i = 0; i < limit; i++ )
336         {
337             if(! nvp[i].value.type().equivalent( members[i].value.type() ))
338                 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc();
339         
340             if(! (nvp[i].id.equals("") || nvp[i].id.equals( members[i].id )))
341                 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc();
342         
343         }
344         members = new NameValuePair[nvp.length];
345         for( int i = 0; i < limit; i++ )
346         {
347             members[i] = new NameValuePair( nvp[i].id, nvp[i].value.to_any() );
348         }
349     }
350
351     public void destroy()
352     {
353         super.destroy();
354         members = null;
355     }
356
357     /**
358      * returns the DynAny's internal any representation.
359      * <p>
360      * Overrides getRepresentation() in DynAny.
361      */

362
363     protected org.omg.CORBA.Any JavaDoc getRepresentation()
364     {
365         return members[pos].value;
366     }
367
368     /* iteration interface */
369
370     public org.omg.DynamicAny.DynAny JavaDoc current_component()
371         throws TypeMismatch
372     {
373         checkDestroyed ();
374         try
375         {
376             /* special case for empty exceptions */
377             if( isEmptyEx () )
378             {
379                 throw new TypeMismatch ();
380             }
381
382             if( pos == -1 )
383             {
384                 return null;
385             }
386             return dynFactory.create_dyn_any( members[pos].value );
387         }
388         catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc itc )
389         {
390             itc.printStackTrace();
391         }
392         return null;
393     }
394
395     private boolean isEmptyEx ()
396     {
397         try
398         {
399             return (type.kind().value() == org.omg.CORBA.TCKind._tk_except &&
400                     type.member_count() == 0);
401         }
402         catch( org.omg.CORBA.TypeCodePackage.BadKind JavaDoc bk )
403         {
404             bk.printStackTrace();
405         }
406         return false;
407     }
408    
409 }
410
Popular Tags