Source for gnu.CORBA.DynAn.gnuDynValueBox

   1: /* gnuDynValueBox.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.Unexpected;
  42: import gnu.CORBA.HolderLocator;
  43: 
  44: import org.omg.CORBA.Any;
  45: import org.omg.CORBA.ORB;
  46: import org.omg.CORBA.TCKind;
  47: import org.omg.CORBA.TypeCode;
  48: import org.omg.CORBA.TypeCodePackage.BadKind;
  49: import org.omg.CORBA.portable.Streamable;
  50: import org.omg.DynamicAny.DynAny;
  51: import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
  52: import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  53: import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  54: import org.omg.DynamicAny.DynValueBox;
  55: import org.omg.DynamicAny.DynValueBoxOperations;
  56: import org.omg.DynamicAny.DynValueCommon;
  57: 
  58: import java.io.Serializable;
  59: 
  60: import java.lang.reflect.Field;
  61: 
  62: /**
  63:  * Implementation of the DynValueBox.
  64:  *
  65:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  66:  */
  67: public class gnuDynValueBox
  68:   extends DivideableAny
  69:   implements DynValueBox, Serializable
  70: {
  71:   /**
  72:    * Use serialVersionUID for interoperability.
  73:    */
  74:   private static final long serialVersionUID = 1;
  75: 
  76:   /**
  77:    * The final_type of contents of this value box.
  78:    */
  79:   final TypeCode content;
  80: 
  81:   /**
  82:    * The string for some TypeMismatch exceptions.
  83:    */
  84:   String CONTENT = "Box content final_type mismatch";
  85: 
  86:   /**
  87:    * Create a new instance of gnuDynValueBox.
  88:    */
  89:   public gnuDynValueBox(TypeCode oType, TypeCode aType,
  90:                         gnuDynAnyFactory aFactory, ORB anOrb
  91:                        )
  92:   {
  93:     super(oType, aType, aFactory, anOrb);
  94:     try
  95:       {
  96:         content = final_type.content_type();
  97:         array = new DynAny[] { factory.create_dyn_any_from_type_code(content) };
  98:         set_to_null();
  99:       }
 100:     catch (Exception e)
 101:       {
 102:         throw new Unexpected(e);
 103:       }
 104:   }
 105: 
 106:   /** @inheritDoc */
 107:   public void assign(DynAny from)
 108:               throws TypeMismatch
 109:   {
 110:     checkType(official_type, from.type());
 111:     if (from instanceof DynValueBoxOperations)
 112:       {
 113:         DynValueBoxOperations other = (DynValueBoxOperations) from;
 114:         if (other.is_null())
 115:           set_to_null();
 116:         else
 117:           {
 118:             DynAny inBox;
 119:             try
 120:               {
 121:                 inBox = other.get_boxed_value_as_dyn_any();
 122:               }
 123:             catch (InvalidValue e)
 124:               {
 125:                 TypeMismatch t = new TypeMismatch("Invalid value");
 126:                 t.initCause(e);
 127:                 throw t;
 128:               }
 129:             if (!content.equal(inBox.type()))
 130:               throw new TypeMismatch(CONTENT);
 131:             array = new DynAny[] { inBox.copy() };
 132:           }
 133:       }
 134:     valueChanged();
 135:   }
 136: 
 137:   /** @inheritDoc */
 138:   public DynAny copy()
 139:   {
 140:     gnuDynValueBox other =
 141:       new gnuDynValueBox(official_type, final_type, factory, orb);
 142:     if (is_null())
 143:       other.set_to_null();
 144:     else
 145:       {
 146:         try
 147:           {
 148:             other.array = new DynAny[] { array [ 0 ].copy() };
 149:           }
 150:         catch (Exception e)
 151:           {
 152:             throw new Unexpected(e);
 153:           }
 154:       }
 155:     return other;
 156:   }
 157: 
 158:   /**
 159:    * Returns null for null value, delegates to super. otherwise.
 160:    */
 161:   public DynAny current_component()
 162:                            throws TypeMismatch
 163:   {
 164:     if (is_null())
 165:       return null;
 166:     else
 167:       return super.current_component();
 168:   }
 169: 
 170:   /**
 171:    * Compare for equality, minding null values.
 172:    */
 173:   public boolean equal(DynAny other)
 174:   {
 175:     if (other instanceof DynValueCommon)
 176:       {
 177:         DynValueCommon o = (DynValueCommon) other;
 178:         if (is_null())
 179:           return o.is_null() && o.type().equal(official_type);
 180:         else
 181:           return !o.is_null() && super.equal(other);
 182:       }
 183:     else
 184:       return false;
 185:   }
 186: 
 187:   /** @inheritDoc */
 188:   public void from_any(Any an_any)
 189:                 throws TypeMismatch, InvalidValue
 190:   {
 191:     checkType(official_type, an_any.type());
 192:     try
 193:       {
 194:         if (!an_any.type().content_type().equal(content))
 195:           throw new InvalidValue(CONTENT);
 196:       }
 197:     catch (BadKind e)
 198:       {
 199:         TypeMismatch t = new TypeMismatch("Not a box");
 200:         t.initCause(e);
 201:         throw t;
 202:       }
 203: 
 204:     Serializable s = an_any.extract_Value();
 205:     if (s == null)
 206:       set_to_null();
 207:     else
 208:       {
 209:         try
 210:           {
 211:             Streamable holder = HolderLocator.createHolder(content);
 212:             Field v = holder.getClass().getField("value");
 213:             v.set(holder, s);
 214: 
 215:             Any cont = createAny();
 216:             cont.insert_Streamable(holder);
 217: 
 218:             array = new DynAny[] { factory.create_dyn_any(cont) };
 219:           }
 220:         catch (Exception ex)
 221:           {
 222:             throw new Unexpected(ex);
 223:           }
 224:       }
 225:     valueChanged();
 226:   }
 227: 
 228:   /** @inheritDoc */
 229:   public Any get_boxed_value()
 230:                       throws InvalidValue
 231:   {
 232:     try
 233:       {
 234:         if (is_null())
 235:           throw new InvalidValue(ISNULL);
 236:         else
 237:           return array [ 0 ].to_any();
 238:       }
 239:     catch (Exception e)
 240:       {
 241:         InvalidValue t = new InvalidValue();
 242:         t.initCause(e);
 243:         throw t;
 244:       }
 245:   }
 246: 
 247:   /** @inheritDoc */
 248:   public DynAny get_boxed_value_as_dyn_any()
 249:                                     throws InvalidValue
 250:   {
 251:     if (is_null())
 252:       throw new InvalidValue(ISNULL);
 253:     else
 254:       return array [ 0 ].copy();
 255:   }
 256: 
 257:   /** {@inheritDoc} */
 258:   public Serializable get_val()
 259:                        throws TypeMismatch, InvalidValue
 260:   {
 261:     return to_any().extract_Value();
 262:   }
 263: 
 264:   /** {@inheritDoc} */
 265:   public void insert_val(Serializable a_x)
 266:                   throws InvalidValue, TypeMismatch
 267:   {
 268:     Any a = to_any();
 269:     a.insert_Value(a_x);
 270:     from_any(a);
 271:     valueChanged();
 272:   }
 273: 
 274:   /** @inheritDoc */
 275:   public boolean is_null()
 276:   {
 277:     return array.length == 0;
 278:   }
 279: 
 280:   /** @inheritDoc */
 281:   public void set_boxed_value(Any boxIt)
 282:                        throws TypeMismatch
 283:   {
 284:     if (!content.equal(boxIt.type()))
 285:       throw new TypeMismatch(CONTENT);
 286:     try
 287:       {
 288:         if (is_null())
 289:           {
 290:             array = new DynAny[] { factory.create_dyn_any(boxIt) };
 291:           }
 292:         else
 293:           {
 294:             array [ 0 ].from_any(boxIt);
 295:           }
 296:       }
 297:     catch (Exception e)
 298:       {
 299:         TypeMismatch t = new TypeMismatch();
 300:         t.initCause(e);
 301:         throw t;
 302:       }
 303:     valueChanged();
 304:   }
 305: 
 306:   /** @inheritDoc */
 307:   public void set_boxed_value_as_dyn_any(DynAny boxIt)
 308:                                   throws TypeMismatch
 309:   {
 310:     if (!content.equal(boxIt.type()))
 311:       throw new TypeMismatch(CONTENT);
 312:     try
 313:       {
 314:         if (is_null())
 315:           {
 316:             array = new DynAny[] { boxIt.copy() };
 317:           }
 318:         else
 319:           {
 320:             array [ 0 ].assign(boxIt);
 321:           }
 322:       }
 323:     catch (Exception e)
 324:       {
 325:         TypeMismatch t = new TypeMismatch();
 326:         t.initCause(e);
 327:         throw t;
 328:       }
 329:     valueChanged();
 330:   }
 331: 
 332:   /** @inheritDoc */
 333:   public void set_to_null()
 334:   {
 335:     array = new DynAny[ 0 ];
 336:     valueChanged();
 337:   }
 338: 
 339:   /** @inheritDoc */
 340:   public void set_to_value()
 341:   {
 342:     try
 343:       {
 344:         if (array.length == 0)
 345:           {
 346:             array =
 347:               new DynAny[] { factory.create_dyn_any_from_type_code(content) };
 348:           }
 349:       }
 350:     catch (InconsistentTypeCode e)
 351:       {
 352:         throw new Unexpected(e);
 353:       }
 354:     valueChanged();
 355:   }
 356: 
 357:   /** @inheritDoc */
 358:   public Any to_any()
 359:   {
 360:     Any a = createAny();
 361: 
 362:     if (!is_null())
 363:       {
 364:         try
 365:           {
 366:             Streamable holder;
 367:             if (array [ 0 ] instanceof gnuDynAny)
 368:               holder = ((gnuDynAny) array [ 0 ]).holder;
 369:             else
 370:               {
 371:                 Any uan = array [ 0 ].to_any();
 372:                 holder = uan.extract_Streamable();
 373:               }
 374: 
 375:             Field v = holder.getClass().getField("value");
 376:             Serializable value = (Serializable) v.get(holder);
 377:             a.type(official_type);
 378:             a.insert_Value(value, content);
 379:           }
 380:         catch (Exception ex)
 381:           {
 382:             throw new Unexpected(ex);
 383:           }
 384:       }
 385:     else
 386:       a.type(orb.get_primitive_tc(TCKind.tk_null));
 387:     return a;
 388:   }
 389: }