Source for gnu.CORBA.DynAn.gnuDynAnyFactory

   1: /* gnuDynAnyFactory.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.CORBA.DynAn;
  40: 
  41: import gnu.CORBA.Poa.ORB_1_4;
  42: import gnu.CORBA.Unexpected;
  43: import gnu.CORBA.HolderLocator;
  44: import gnu.CORBA.TypeKindNamer;
  45: 
  46: import org.omg.CORBA.Any;
  47: import org.omg.CORBA.LocalObject;
  48: import org.omg.CORBA.TCKind;
  49: import org.omg.CORBA.TypeCode;
  50: import org.omg.CORBA.TypeCodePackage.BadKind;
  51: import org.omg.CORBA.UserException;
  52: import org.omg.CORBA.portable.Streamable;
  53: import org.omg.DynamicAny.DynAny;
  54: import org.omg.DynamicAny.DynAnyFactory;
  55: import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
  56: import org.omg.DynamicAny.DynArray;
  57: import org.omg.DynamicAny.DynEnum;
  58: import org.omg.DynamicAny.DynFixed;
  59: import org.omg.DynamicAny.DynSequence;
  60: import org.omg.DynamicAny.DynStruct;
  61: import org.omg.DynamicAny.DynUnion;
  62: import org.omg.DynamicAny.DynValue;
  63: import org.omg.DynamicAny.DynValueBox;
  64: 
  65: /**
  66:  * This class is returned by ORB when resolving
  67:  * initial reference "DynAnyFactory".
  68:  *
  69:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  70:  */
  71: public class gnuDynAnyFactory
  72:   extends LocalObject
  73:   implements DynAnyFactory
  74: {
  75:   /**
  76:    * Use serialVersionUID for interoperability.
  77:    */
  78:   private static final long serialVersionUID = 1;
  79: 
  80:   /**
  81:    * The ORB, to that the factory belongs.
  82:    */
  83:   final ORB_1_4 orb;
  84: 
  85:   /**
  86:    * Create a new factory, specifying the ORB to that the factory belongs.
  87:    *
  88:    * @param anOrb
  89:    */
  90:   public gnuDynAnyFactory(ORB_1_4 anOrb)
  91:   {
  92:     orb = anOrb;
  93:   }
  94: 
  95:   /**
  96:    * Get the orb.
  97:    */
  98:   public ORB_1_4 getOrb()
  99:   {
 100:     return orb;
 101:   }
 102: 
 103:   /**
 104:    * Create an initialised array.
 105:    */
 106:   public DynArray create_array(TypeCode official, TypeCode type)
 107:   {
 108:     return new gnuDynArray(official, type, this, orb, true);
 109:   }
 110: 
 111:   /**
 112:    * Create an empty sequence.
 113:    */
 114:   public DynSequence create_sequence(TypeCode official, TypeCode type)
 115:   {
 116:     return new gnuDynSequence(official, type, this, orb);
 117:   }
 118: 
 119:   /**
 120:    * Create structure.
 121:    *
 122:    * @param official the type that was originally passed as a parameter by user.
 123:    * May be alias of some other type.
 124:    * @param type the type into that the "official type" evaluates during alias
 125:    * resolving. Initially equal to "official type".
 126:    */
 127:   public DynStruct create_structure(TypeCode official, TypeCode type)
 128:   {
 129:     return new gnuDynStruct(official, type, this, orb);
 130:   }
 131: 
 132:   /**
 133:    * Create union.
 134:    *
 135:    * @param official the type that was originally passed as a parameter by user.
 136:    * May be alias of some other type.
 137:    * @param type the type into that the "official type" evaluates during alias
 138:    * resolving. Initially equal to "official type".
 139:    */
 140:   public DynUnion create_union(TypeCode official, TypeCode type)
 141:   {
 142:     try
 143:       {
 144:         return new gnuDynUnion(official, type, this, orb);
 145:       }
 146:     catch (Exception ex)
 147:       {
 148:         throw new Unexpected(ex);
 149:       }
 150:   }
 151: 
 152:   /**
 153:    * Create value.
 154:    *
 155:    * @param official the type that was originally passed as a parameter by user.
 156:    * May be alias of some other type.
 157:    * @param type the type into that the "official type" evaluates during alias
 158:    * resolving. Initially equal to "official type".
 159:    */
 160:   public DynValue create_value(TypeCode official, TypeCode type)
 161:   {
 162:     return new gnuDynValue(official, type, this, orb);
 163:   }
 164: 
 165:   /**
 166:    * Create value box.
 167:    *
 168:    * @param official the type that was originally passed as a parameter by user.
 169:    * May be alias of some other type.
 170:    * @param type the type into that the "official type" evaluates during alias
 171:    * resolving. Initially equal to "official type".
 172:    */
 173:   public DynValueBox create_value_box(TypeCode official, TypeCode type)
 174:   {
 175:     return new gnuDynValueBox(official, type, this, orb);
 176:   }
 177: 
 178:   /**
 179:    * Create enumeration.
 180:    *
 181:    * @param official the type that was originally passed as a parameter by user.
 182:    * May be alias of some other type.
 183:    * @param type the type into that the "official type" evaluates during alias
 184:    * resolving. Initially equal to "official type".
 185:    */
 186:   public DynEnum create_enumeration(TypeCode official, TypeCode type)
 187:   {
 188:     return new gnuDynEnum(official, type, this, orb);
 189:   }
 190: 
 191:   /**
 192:    * Create fixed.
 193:    *
 194:    * @param official the type that was originally passed as a parameter by user.
 195:    * May be alias of some other type.
 196:    * @param type the type into that the "official type" evaluates during alias
 197:    * resolving. Initially equal to "official type".
 198:    */
 199:   public DynFixed create_fixed(TypeCode official, TypeCode type)
 200:   {
 201:     return new gnuDynFixed(official, type, this, orb);
 202:   }
 203: 
 204:   /**
 205:    * Create alias.
 206:    *
 207:    * @param official the type that was originally passed as a parameter by user.
 208:    * May be alias of some other type.
 209:    * @param type the type into that the "official type" evaluates during alias
 210:    * resolving. Initially equal to "official type".
 211:    */
 212:   public DynAny create_alias(TypeCode official, TypeCode type)
 213:                       throws InconsistentTypeCode
 214:   {
 215:     try
 216:       {
 217:         return create_dyn_any_from_type_code(official, type.content_type());
 218:       }
 219:     catch (BadKind e)
 220:       {
 221:         throw new Unexpected(e);
 222:       }
 223:   }
 224: 
 225:   /**
 226:    * Create the undivideable DynAny.
 227:    */
 228:   public DynAny create_simple(TypeCode official, TypeCode type)
 229:   {
 230:     Streamable holder = HolderLocator.createHolder(type);
 231:     return new gnuDynAny(holder, official, type, this, orb);
 232:   }
 233: 
 234:   /**
 235:    * Create the DynAny from typecode.
 236:    */
 237:   public DynAny create_dyn_any_from_type_code(TypeCode type)
 238:                                        throws InconsistentTypeCode
 239:   {
 240:     return create_dyn_any_from_type_code(type, type);
 241:   }
 242: 
 243:   /**
 244:    * Create the DynAny from typecode.
 245:    *
 246:    * @param official the type that was originally passed as a parameter by user.
 247:    * May be alias of some other type.
 248:    * @param type the type into that the "official type" evaluates during alias
 249:    * resolving. Initially equal to "official type".
 250:    */
 251:   public DynAny create_dyn_any_from_type_code(TypeCode official, TypeCode type)
 252:                                        throws InconsistentTypeCode
 253:   {
 254:     DynAny d;
 255:     try
 256:       {
 257:         switch (type.kind().value())
 258:           {
 259:             case TCKind._tk_array :
 260:               return create_array(official, type);
 261: 
 262:             case TCKind._tk_sequence :
 263:               return create_sequence(official, type);
 264: 
 265:             case TCKind._tk_struct :
 266:             case TCKind._tk_except :
 267:               return create_structure(official, type);
 268: 
 269:             case TCKind._tk_union :
 270:               return create_union(official, type);
 271: 
 272:             case TCKind._tk_value :
 273:               return create_value(official, type);
 274: 
 275:             case TCKind._tk_value_box :
 276:               return create_value_box(official, type);
 277: 
 278:             case TCKind._tk_enum :
 279:               return create_enumeration(official, type);
 280: 
 281:             case TCKind._tk_fixed :
 282:               return create_fixed(official, type);
 283: 
 284:             case TCKind._tk_alias :
 285:               return create_alias(official, type);
 286: 
 287:             case TCKind._tk_null :
 288:               return new gnuDynAny(null, official, type, this, orb);
 289: 
 290:             case TCKind._tk_TypeCode :
 291:               d = create_simple(official, type);
 292:               d.insert_typecode(orb.get_primitive_tc(TCKind.tk_null));
 293:               return d;
 294: 
 295:             case TCKind._tk_any :
 296:               d = create_simple(official, type);
 297: 
 298:               Any empty_any = orb.create_any();
 299:               empty_any.type(orb.get_primitive_tc(TCKind.tk_null));
 300:               d.insert_any(empty_any);
 301:               return d;
 302: 
 303:             case TCKind._tk_wstring :
 304:               d = create_simple(official, type);
 305:               d.insert_wstring("");
 306:               return d;
 307: 
 308:             case TCKind._tk_string :
 309:               d = create_simple(official, type);
 310:               d.insert_string("");
 311:               return d;
 312: 
 313:             case TCKind._tk_native :
 314:             case TCKind._tk_Principal :
 315:             case TCKind._tk_abstract_interface :
 316:               throw new InconsistentTypeCode("Following API, the " +
 317:                                              TypeKindNamer.nameIt(type) +
 318:                                              " must not be supported."
 319:                                             );
 320: 
 321:             default :
 322:               return create_simple(official, type);
 323:           }
 324:       }
 325:     catch (UserException uex)
 326:       {
 327:         InconsistentTypeCode it = new InconsistentTypeCode();
 328:         it.initCause(uex);
 329:         throw it;
 330:       }
 331:   }
 332: 
 333:   /**
 334:    * Create the DynAny using the passed value as template and assign this value.
 335:    */
 336:   public DynAny create_dyn_any(Any value)
 337:                         throws InconsistentTypeCode
 338:   {
 339:     DynAny created = create_dyn_any_from_type_code(value.type());
 340:     try
 341:       {
 342:         created.from_any(value);
 343:       }
 344:     catch (UserException uex)
 345:       {
 346:         InconsistentTypeCode t = new InconsistentTypeCode("Inconsistent Any");
 347:         t.initCause(uex);
 348:         throw t;
 349:       }
 350:     catch (Exception e)
 351:       {
 352:         throw new Unexpected(e);
 353:       }
 354:     return created;
 355:   }
 356: }