gnuDynValueBox.java 9.6 KB

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