gnuDynValue.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* gnuDynValue.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA.DynAn;
  32. import gnu.CORBA.Minor;
  33. import gnu.CORBA.Unexpected;
  34. import org.omg.CORBA.Any;
  35. import org.omg.CORBA.BAD_PARAM;
  36. import org.omg.CORBA.MARSHAL;
  37. import org.omg.CORBA.ORB;
  38. import org.omg.CORBA.TCKind;
  39. import org.omg.CORBA.TypeCode;
  40. import org.omg.CORBA.VM_TRUNCATABLE;
  41. import org.omg.CORBA.portable.OutputStream;
  42. import org.omg.CORBA.portable.ValueFactory;
  43. import org.omg.DynamicAny.DynAny;
  44. import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  45. import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  46. import org.omg.DynamicAny.DynStruct;
  47. import org.omg.DynamicAny.DynValue;
  48. import org.omg.DynamicAny.DynValueCommon;
  49. import org.omg.DynamicAny.DynValueOperations;
  50. import org.omg.DynamicAny.NameDynAnyPair;
  51. import org.omg.DynamicAny.NameValuePair;
  52. import java.io.Serializable;
  53. /**
  54. * Implementation of DynValue.
  55. *
  56. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  57. */
  58. public class gnuDynValue extends RecordAny implements DynValue,
  59. Serializable
  60. {
  61. /**
  62. * Use serialVersionUID for interoperability.
  63. */
  64. private static final long serialVersionUID = 1;
  65. /**
  66. * If true, the value of this ValueType is set to null.
  67. */
  68. boolean isNull;
  69. /**
  70. * Create an instance.
  71. */
  72. public gnuDynValue(TypeCode oType, TypeCode aType,
  73. gnuDynAnyFactory aFactory, ORB anOrb
  74. )
  75. {
  76. super(oType, aType, aFactory, anOrb);
  77. // Initialise fields. The array of fields also includes all inherited
  78. // fields.
  79. try
  80. {
  81. array = new DynAny[ final_type.member_count() ];
  82. fNames = new String[ array.length ];
  83. for (int i = 0; i < array.length; i++)
  84. {
  85. array [ i ] =
  86. factory.create_dyn_any_from_type_code(final_type.member_type(i));
  87. fNames [ i ] = final_type.member_name(i);
  88. }
  89. // Search of inherited members.
  90. if (final_type.type_modifier() == VM_TRUNCATABLE.value)
  91. {
  92. TypeCode parent = final_type.concrete_base_type();
  93. DynAny ancestor = factory.create_dyn_any_from_type_code(parent);
  94. if (ancestor instanceof DynValue)
  95. {
  96. // Add members of ancestor in front of the curren members.
  97. DynValue anc = (DynValue) ancestor;
  98. anc.set_to_value();
  99. NameDynAnyPair[] aar = anc.get_members_as_dyn_any();
  100. inheritFields(aar);
  101. }
  102. else if (ancestor instanceof DynStruct)
  103. {
  104. // Add members of ancestor in front of the curren members.
  105. DynStruct anc = (DynStruct) ancestor;
  106. NameDynAnyPair[] aar = anc.get_members_as_dyn_any();
  107. inheritFields(aar);
  108. }
  109. else
  110. throw new BAD_PARAM("The parent of " + final_type.id() + ", " +
  111. parent.id() + ", is not structure nor value."
  112. );
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. throw new Unexpected(e);
  118. }
  119. set_to_null();
  120. }
  121. /**
  122. * Inherit the provided fields.
  123. */
  124. private void inheritFields(NameDynAnyPair[] aar)
  125. {
  126. DynAny[] nArray = new DynAny[ array.length + aar.length ];
  127. String[] nNames = new String[ array.length + aar.length ];
  128. int p = 0;
  129. for (int i = 0; i < aar.length; i++)
  130. {
  131. nArray [ p ] = aar [ i ].value;
  132. nNames [ p ] = aar [ i ].id;
  133. p++;
  134. }
  135. for (int i = 0; i < array.length; i++)
  136. {
  137. nArray [ p ] = array [ i ];
  138. nNames [ p ] = fNames [ i ];
  139. p++;
  140. }
  141. array = nArray;
  142. fNames = nNames;
  143. }
  144. /** @inheritDoc */
  145. public TCKind current_member_kind() throws TypeMismatch, InvalidValue
  146. {
  147. if (isNull)
  148. throw new TypeMismatch(ISNULL);
  149. else
  150. return super.current_member_kind();
  151. }
  152. /** @inheritDoc */
  153. public String current_member_name() throws TypeMismatch, InvalidValue
  154. {
  155. if (isNull)
  156. throw new TypeMismatch(ISNULL);
  157. else
  158. return super.current_member_name();
  159. }
  160. /** @inheritDoc */
  161. public NameDynAnyPair[] get_members_as_dyn_any() throws InvalidValue
  162. {
  163. if (isNull)
  164. throw new InvalidValue(ISNULL);
  165. return super.gnu_get_members_as_dyn_any();
  166. }
  167. /** @inheritDoc */
  168. public NameValuePair[] get_members() throws InvalidValue
  169. {
  170. if (isNull)
  171. throw new InvalidValue(ISNULL);
  172. else
  173. return super.gnu_get_members();
  174. }
  175. /** @inheritDoc */
  176. public void set_members_as_dyn_any(NameDynAnyPair[] value)
  177. throws TypeMismatch, InvalidValue
  178. {
  179. super.set_members_as_dyn_any(value);
  180. isNull = false;
  181. }
  182. /** @inheritDoc */
  183. public void set_members(NameValuePair[] value)
  184. throws TypeMismatch, InvalidValue
  185. {
  186. super.set_members(value);
  187. isNull = false;
  188. }
  189. /** @inheritDoc */
  190. public boolean is_null()
  191. {
  192. return isNull;
  193. }
  194. /** @inheritDoc */
  195. public void set_to_null()
  196. {
  197. isNull = true;
  198. valueChanged();
  199. }
  200. /** @inheritDoc */
  201. public void set_to_value()
  202. {
  203. isNull = false;
  204. valueChanged();
  205. }
  206. /**
  207. * Create a new instance.
  208. */
  209. protected RecordAny newInstance(TypeCode oType, TypeCode aType,
  210. gnuDynAnyFactory aFactory, ORB anOrb
  211. )
  212. {
  213. gnuDynValue v = new gnuDynValue(oType, aType, aFactory, anOrb);
  214. if (isNull)
  215. v.set_to_null();
  216. else
  217. v.set_to_value();
  218. return v;
  219. }
  220. /**
  221. * Compare for equality, minding null values.
  222. */
  223. public boolean equal(DynAny other)
  224. {
  225. if (other instanceof DynValueOperations)
  226. {
  227. DynValueCommon o = (DynValueCommon) other;
  228. if (isNull)
  229. return o.is_null() && o.type().equal(official_type);
  230. else
  231. return !o.is_null() && super.equal(other);
  232. }
  233. else
  234. return false;
  235. }
  236. /**
  237. * Get the focused component, throwing exception if the current value is null.
  238. */
  239. protected DynAny focused() throws InvalidValue, TypeMismatch
  240. {
  241. if (isNull)
  242. throw new TypeMismatch(ISNULL);
  243. else
  244. return super.focused();
  245. }
  246. /**
  247. * Convert into Any.
  248. */
  249. public Any to_any()
  250. {
  251. if (isNull)
  252. {
  253. Any a0 = createAny();
  254. a0.type(orb.get_primitive_tc(TCKind.tk_null));
  255. return a0;
  256. }
  257. else
  258. {
  259. try
  260. {
  261. ValueFactory factory =
  262. ((org.omg.CORBA_2_3.ORB) orb).lookup_value_factory(official_type.id());
  263. if (factory == null)
  264. {
  265. MARSHAL m = new MARSHAL("Factory for " + official_type.id() +
  266. " not registered.");
  267. m.minor = Minor.Factory;
  268. throw m;
  269. }
  270. OutputStream out = orb.create_output_stream();
  271. for (int i = 0; i < array.length; i++)
  272. array [ i ].to_any().write_value(out);
  273. org.omg.CORBA_2_3.portable.InputStream in =
  274. (org.omg.CORBA_2_3.portable.InputStream) out.create_input_stream();
  275. Serializable v = factory.read_value(in);
  276. Any g = createAny();
  277. g.type(official_type);
  278. g.insert_Value(v, official_type);
  279. return g;
  280. }
  281. catch (Exception e)
  282. {
  283. throw new Unexpected(e);
  284. }
  285. }
  286. }
  287. /** @inheritDoc */
  288. public void assign(DynAny from) throws TypeMismatch
  289. {
  290. checkType(official_type, from.type());
  291. if (from instanceof DynValue)
  292. {
  293. DynValue other = (DynValue) from;
  294. if (other.is_null())
  295. set_to_null();
  296. else
  297. {
  298. set_to_value();
  299. try
  300. {
  301. DynValueOperations src = (DynValueOperations) from;
  302. set_members_as_dyn_any(src.get_members_as_dyn_any());
  303. }
  304. catch (InvalidValue e)
  305. {
  306. TypeMismatch t = new TypeMismatch("Invalid value");
  307. t.initCause(e);
  308. throw t;
  309. }
  310. }
  311. }
  312. else
  313. throw new TypeMismatch("Not a DynValue");
  314. }
  315. /**
  316. * Get the number of components.
  317. */
  318. public int component_count()
  319. {
  320. return isNull ? 0 : super.component_count();
  321. }
  322. /** {@inheritDoc} */
  323. public Serializable get_val() throws TypeMismatch, InvalidValue
  324. {
  325. return to_any().extract_Value();
  326. }
  327. /** {@inheritDoc} */
  328. public void insert_val(Serializable a_x) throws InvalidValue, TypeMismatch
  329. {
  330. Any a = to_any();
  331. a.insert_Value(a_x);
  332. from_any(a);
  333. valueChanged();
  334. }
  335. }