RecordAny.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /* RecordAny.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.TypeCodePackage.Bounds;
  40. import org.omg.CORBA.portable.Streamable;
  41. import org.omg.DynamicAny.DynAny;
  42. import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  43. import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  44. import org.omg.DynamicAny.DynStruct;
  45. import org.omg.DynamicAny.DynValueCommonOperations;
  46. import org.omg.DynamicAny.NameDynAnyPair;
  47. import org.omg.DynamicAny.NameValuePair;
  48. import java.io.Serializable;
  49. import java.lang.reflect.Field;
  50. /**
  51. * A shared base for both dynamic structure an dynamic value final_type.
  52. *
  53. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  54. */
  55. public abstract class RecordAny
  56. extends DivideableAny
  57. implements DynAny, Serializable
  58. {
  59. /**
  60. * Use serialVersionUID for interoperability.
  61. */
  62. private static final long serialVersionUID = 1;
  63. String[] fNames;
  64. /**
  65. * Creates the structure with the given typecode.
  66. *
  67. * @param fields The DynAny's, representing the fields of the structure.
  68. */
  69. public RecordAny(TypeCode oType, TypeCode aType,
  70. gnuDynAnyFactory aFactory, ORB anOrb
  71. )
  72. {
  73. super(oType, aType, aFactory, anOrb);
  74. }
  75. /** @inheritDoc */
  76. public TCKind current_member_kind()
  77. throws TypeMismatch, InvalidValue
  78. {
  79. if (array.length == 0)
  80. throw new TypeMismatch(EMPTY);
  81. try
  82. {
  83. return final_type.member_type(pos).kind();
  84. }
  85. catch (BadKind e)
  86. {
  87. TypeMismatch t = new TypeMismatch();
  88. t.initCause(e);
  89. throw t;
  90. }
  91. catch (Bounds e)
  92. {
  93. InvalidValue t = new InvalidValue();
  94. t.initCause(e);
  95. throw t;
  96. }
  97. }
  98. /** @inheritDoc */
  99. public String current_member_name()
  100. throws TypeMismatch, InvalidValue
  101. {
  102. if (array.length == 0)
  103. throw new TypeMismatch(EMPTY);
  104. try
  105. {
  106. return final_type.member_name(pos);
  107. }
  108. catch (BadKind e)
  109. {
  110. TypeMismatch t = new TypeMismatch();
  111. t.initCause(e);
  112. throw t;
  113. }
  114. catch (Bounds e)
  115. {
  116. InvalidValue t = new InvalidValue();
  117. t.initCause(e);
  118. throw t;
  119. }
  120. }
  121. /**
  122. * Get content of the structure. This method must be defined on a different
  123. * name because get_members_as_dyn_any() throws exception only in some of the
  124. * supported interfaces.
  125. */
  126. public NameDynAnyPair[] gnu_get_members_as_dyn_any()
  127. {
  128. NameDynAnyPair[] r = new NameDynAnyPair[ array.length ];
  129. for (int i = 0; i < r.length; i++)
  130. {
  131. try
  132. {
  133. r [ i ] = new NameDynAnyPair(fNames [ i ], array [ i ]);
  134. }
  135. catch (Exception ex)
  136. {
  137. throw new Unexpected(ex);
  138. }
  139. }
  140. return r;
  141. }
  142. /**
  143. * Get content of the structure. This method must be defined on a different
  144. * name because get_members_as_dyn_any() throws exception only in some of the
  145. * supported interfaces.
  146. */
  147. public NameValuePair[] gnu_get_members()
  148. {
  149. NameValuePair[] r = new NameValuePair[ array.length ];
  150. for (int i = 0; i < r.length; i++)
  151. {
  152. try
  153. {
  154. r [ i ] = new NameValuePair(fNames [ i ], array [ i ].to_any());
  155. }
  156. catch (Exception ex)
  157. {
  158. throw new Unexpected(ex);
  159. }
  160. }
  161. return r;
  162. }
  163. /**
  164. * Set members from the provided array.
  165. */
  166. public void set_members_as_dyn_any(NameDynAnyPair[] value)
  167. throws TypeMismatch, InvalidValue
  168. {
  169. if (value.length != array.length)
  170. throw new InvalidValue(sizeMismatch(array.length, value.length));
  171. for (int i = 0; i < value.length; i++)
  172. {
  173. DynAny dynAny = value [ i ].value;
  174. checkType(dynAny.type(), i);
  175. checkName(value [ i ].id, i);
  176. array [ i ] = dynAny;
  177. }
  178. pos = 0;
  179. }
  180. /**
  181. * Check the name at the given position ("" matches everything).
  182. */
  183. private void checkName(String xName, int i)
  184. throws TypeMismatch
  185. {
  186. if (xName.length() > 0 && fNames [ i ].length() > 0)
  187. if (!xName.equals(fNames [ i ]))
  188. throw new TypeMismatch("Field name mismatch " + xName + " expected " +
  189. fNames [ i ]
  190. );
  191. }
  192. /**
  193. * Check the type at the given position.
  194. */
  195. private void checkType(TypeCode t, int i)
  196. throws TypeMismatch
  197. {
  198. if (!array [ i ].type().equal(t))
  199. throw new TypeMismatch(typeMismatch(array [ i ].type(), t) + " field " +
  200. i
  201. );
  202. }
  203. /**
  204. * Set members from the provided array.
  205. */
  206. public void set_members(NameValuePair[] value)
  207. throws TypeMismatch, InvalidValue
  208. {
  209. if (value.length != array.length)
  210. throw new InvalidValue(sizeMismatch(array.length, value.length));
  211. for (int i = 0; i < value.length; i++)
  212. {
  213. Any any = value [ i ].value;
  214. checkType(any.type(), i);
  215. checkName(value [ i ].id, i);
  216. array [ i ].from_any(any);
  217. }
  218. pos = 0;
  219. }
  220. /** @inheritDoc */
  221. public void assign(DynAny from)
  222. throws TypeMismatch
  223. {
  224. checkType(official_type, from.type());
  225. if (from instanceof DynStruct)
  226. {
  227. try
  228. {
  229. set_members_as_dyn_any(((DynStruct) from).get_members_as_dyn_any());
  230. }
  231. catch (InvalidValue e)
  232. {
  233. TypeMismatch t = new TypeMismatch("Invalid value");
  234. t.initCause(e);
  235. throw t;
  236. }
  237. }
  238. else
  239. throw new TypeMismatch("Not a DynStruct");
  240. }
  241. /**
  242. * Create a copy.
  243. */
  244. public DynAny copy()
  245. {
  246. DynAny[] c = new DynAny[ array.length ];
  247. for (int i = 0; i < c.length; i++)
  248. {
  249. c [ i ] = array [ i ].copy();
  250. }
  251. RecordAny d = newInstance(official_type, final_type, factory, orb);
  252. d.array = c;
  253. return d;
  254. }
  255. /**
  256. * Create a new instance when copying.
  257. */
  258. protected abstract RecordAny newInstance(TypeCode oType, TypeCode aType,
  259. gnuDynAnyFactory aFactory,
  260. ORB anOrb
  261. );
  262. /**
  263. * Done via reflection.
  264. */
  265. public Any to_any()
  266. {
  267. try
  268. {
  269. Streamable sHolder = HolderLocator.createHolder(official_type);
  270. Class sHolderClass = sHolder.getClass();
  271. Field sHolderValue = sHolderClass.getField("value");
  272. Class sClass = sHolderValue.getType();
  273. Object structure = sClass.newInstance();
  274. Object member;
  275. Any am;
  276. Field vread;
  277. Field vwrite;
  278. Streamable memberHolder;
  279. for (int i = 0; i < array.length; i++)
  280. {
  281. am = array [ i ].to_any();
  282. memberHolder = am.extract_Streamable();
  283. vwrite = structure.getClass().getField(final_type.member_name(i));
  284. vread = memberHolder.getClass().getField("value");
  285. member = vread.get(memberHolder);
  286. vwrite.set(structure, member);
  287. }
  288. Any g = createAny();
  289. sHolderValue.set(sHolder, structure);
  290. g.insert_Streamable(sHolder);
  291. g.type(official_type);
  292. return g;
  293. }
  294. catch (Exception e)
  295. {
  296. throw new Unexpected(e);
  297. }
  298. }
  299. /**
  300. * Done via reflection.
  301. */
  302. public void from_any(Any an_any)
  303. throws TypeMismatch, InvalidValue
  304. {
  305. checkType(official_type, an_any.type());
  306. try
  307. {
  308. Streamable s = an_any.extract_Streamable();
  309. if (s == null)
  310. {
  311. if (this instanceof DynValueCommonOperations)
  312. {
  313. ((DynValueCommonOperations) this).set_to_null();
  314. return;
  315. }
  316. else
  317. throw new InvalidValue(ISNULL);
  318. }
  319. Object structure = s.getClass().getField("value").get(s);
  320. if (structure == null && (this instanceof DynValueCommonOperations))
  321. {
  322. ((DynValueCommonOperations) this).set_to_null();
  323. return;
  324. }
  325. Any member;
  326. Streamable holder;
  327. Object field;
  328. TypeCode fType;
  329. Field fField;
  330. for (int i = 0; i < array.length; i++)
  331. {
  332. fField = structure.getClass().getField(fNames [ i ]);
  333. field = fField.get(structure);
  334. fType = array [ i ].type();
  335. holder = HolderLocator.createHolder(fType);
  336. member = createAny();
  337. holder.getClass().getField("value").set(holder, field);
  338. member.insert_Streamable(holder);
  339. member.type(fType);
  340. array [ i ].from_any(member);
  341. }
  342. if (this instanceof DynValueCommonOperations)
  343. ((DynValueCommonOperations) this).set_to_value();
  344. }
  345. catch (InvalidValue v)
  346. {
  347. throw v;
  348. }
  349. catch (NoSuchFieldException ex)
  350. {
  351. TypeMismatch v =
  352. new TypeMismatch("holder value does not match typecode");
  353. v.initCause(ex);
  354. throw v;
  355. }
  356. catch (Exception ex)
  357. {
  358. TypeMismatch t = new TypeMismatch();
  359. t.initCause(ex);
  360. throw t;
  361. }
  362. }
  363. }