gnuDynAnyFactory.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* gnuDynAnyFactory.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.Poa.ORB_1_4;
  33. import gnu.CORBA.Unexpected;
  34. import gnu.CORBA.HolderLocator;
  35. import gnu.CORBA.TypeKindNamer;
  36. import org.omg.CORBA.Any;
  37. import org.omg.CORBA.LocalObject;
  38. import org.omg.CORBA.TCKind;
  39. import org.omg.CORBA.TypeCode;
  40. import org.omg.CORBA.TypeCodePackage.BadKind;
  41. import org.omg.CORBA.UserException;
  42. import org.omg.CORBA.portable.Streamable;
  43. import org.omg.DynamicAny.DynAny;
  44. import org.omg.DynamicAny.DynAnyFactory;
  45. import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;
  46. import org.omg.DynamicAny.DynArray;
  47. import org.omg.DynamicAny.DynEnum;
  48. import org.omg.DynamicAny.DynFixed;
  49. import org.omg.DynamicAny.DynSequence;
  50. import org.omg.DynamicAny.DynStruct;
  51. import org.omg.DynamicAny.DynUnion;
  52. import org.omg.DynamicAny.DynValue;
  53. import org.omg.DynamicAny.DynValueBox;
  54. /**
  55. * This class is returned by ORB when resolving
  56. * initial reference "DynAnyFactory".
  57. *
  58. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  59. */
  60. public class gnuDynAnyFactory
  61. extends LocalObject
  62. implements DynAnyFactory
  63. {
  64. /**
  65. * Use serialVersionUID for interoperability.
  66. */
  67. private static final long serialVersionUID = 1;
  68. /**
  69. * The ORB, to that the factory belongs.
  70. */
  71. final ORB_1_4 orb;
  72. /**
  73. * Create a new factory, specifying the ORB to that the factory belongs.
  74. *
  75. * @param anOrb
  76. */
  77. public gnuDynAnyFactory(ORB_1_4 anOrb)
  78. {
  79. orb = anOrb;
  80. }
  81. /**
  82. * Get the orb.
  83. */
  84. public ORB_1_4 getOrb()
  85. {
  86. return orb;
  87. }
  88. /**
  89. * Create an initialised array.
  90. */
  91. public DynArray create_array(TypeCode official, TypeCode type)
  92. {
  93. return new gnuDynArray(official, type, this, orb, true);
  94. }
  95. /**
  96. * Create an empty sequence.
  97. */
  98. public DynSequence create_sequence(TypeCode official, TypeCode type)
  99. {
  100. return new gnuDynSequence(official, type, this, orb);
  101. }
  102. /**
  103. * Create structure.
  104. *
  105. * @param official the type that was originally passed as a parameter by user.
  106. * May be alias of some other type.
  107. * @param type the type into that the "official type" evaluates during alias
  108. * resolving. Initially equal to "official type".
  109. */
  110. public DynStruct create_structure(TypeCode official, TypeCode type)
  111. {
  112. return new gnuDynStruct(official, type, this, orb);
  113. }
  114. /**
  115. * Create union.
  116. *
  117. * @param official the type that was originally passed as a parameter by user.
  118. * May be alias of some other type.
  119. * @param type the type into that the "official type" evaluates during alias
  120. * resolving. Initially equal to "official type".
  121. */
  122. public DynUnion create_union(TypeCode official, TypeCode type)
  123. {
  124. try
  125. {
  126. return new gnuDynUnion(official, type, this, orb);
  127. }
  128. catch (Exception ex)
  129. {
  130. throw new Unexpected(ex);
  131. }
  132. }
  133. /**
  134. * Create value.
  135. *
  136. * @param official the type that was originally passed as a parameter by user.
  137. * May be alias of some other type.
  138. * @param type the type into that the "official type" evaluates during alias
  139. * resolving. Initially equal to "official type".
  140. */
  141. public DynValue create_value(TypeCode official, TypeCode type)
  142. {
  143. return new gnuDynValue(official, type, this, orb);
  144. }
  145. /**
  146. * Create value box.
  147. *
  148. * @param official the type that was originally passed as a parameter by user.
  149. * May be alias of some other type.
  150. * @param type the type into that the "official type" evaluates during alias
  151. * resolving. Initially equal to "official type".
  152. */
  153. public DynValueBox create_value_box(TypeCode official, TypeCode type)
  154. {
  155. return new gnuDynValueBox(official, type, this, orb);
  156. }
  157. /**
  158. * Create enumeration.
  159. *
  160. * @param official the type that was originally passed as a parameter by user.
  161. * May be alias of some other type.
  162. * @param type the type into that the "official type" evaluates during alias
  163. * resolving. Initially equal to "official type".
  164. */
  165. public DynEnum create_enumeration(TypeCode official, TypeCode type)
  166. {
  167. return new gnuDynEnum(official, type, this, orb);
  168. }
  169. /**
  170. * Create fixed.
  171. *
  172. * @param official the type that was originally passed as a parameter by user.
  173. * May be alias of some other type.
  174. * @param type the type into that the "official type" evaluates during alias
  175. * resolving. Initially equal to "official type".
  176. */
  177. public DynFixed create_fixed(TypeCode official, TypeCode type)
  178. {
  179. return new gnuDynFixed(official, type, this, orb);
  180. }
  181. /**
  182. * Create alias.
  183. *
  184. * @param official the type that was originally passed as a parameter by user.
  185. * May be alias of some other type.
  186. * @param type the type into that the "official type" evaluates during alias
  187. * resolving. Initially equal to "official type".
  188. */
  189. public DynAny create_alias(TypeCode official, TypeCode type)
  190. throws InconsistentTypeCode
  191. {
  192. try
  193. {
  194. return create_dyn_any_from_type_code(official, type.content_type());
  195. }
  196. catch (BadKind e)
  197. {
  198. throw new Unexpected(e);
  199. }
  200. }
  201. /**
  202. * Create the undivideable DynAny.
  203. */
  204. public DynAny create_simple(TypeCode official, TypeCode type)
  205. {
  206. Streamable holder = HolderLocator.createHolder(type);
  207. return new gnuDynAny(holder, official, type, this, orb);
  208. }
  209. /**
  210. * Create the DynAny from typecode.
  211. */
  212. public DynAny create_dyn_any_from_type_code(TypeCode type)
  213. throws InconsistentTypeCode
  214. {
  215. return create_dyn_any_from_type_code(type, type);
  216. }
  217. /**
  218. * Create the DynAny from typecode.
  219. *
  220. * @param official the type that was originally passed as a parameter by user.
  221. * May be alias of some other type.
  222. * @param type the type into that the "official type" evaluates during alias
  223. * resolving. Initially equal to "official type".
  224. */
  225. public DynAny create_dyn_any_from_type_code(TypeCode official, TypeCode type)
  226. throws InconsistentTypeCode
  227. {
  228. DynAny d;
  229. try
  230. {
  231. switch (type.kind().value())
  232. {
  233. case TCKind._tk_array :
  234. return create_array(official, type);
  235. case TCKind._tk_sequence :
  236. return create_sequence(official, type);
  237. case TCKind._tk_struct :
  238. case TCKind._tk_except :
  239. return create_structure(official, type);
  240. case TCKind._tk_union :
  241. return create_union(official, type);
  242. case TCKind._tk_value :
  243. return create_value(official, type);
  244. case TCKind._tk_value_box :
  245. return create_value_box(official, type);
  246. case TCKind._tk_enum :
  247. return create_enumeration(official, type);
  248. case TCKind._tk_fixed :
  249. return create_fixed(official, type);
  250. case TCKind._tk_alias :
  251. return create_alias(official, type);
  252. case TCKind._tk_null :
  253. return new gnuDynAny(null, official, type, this, orb);
  254. case TCKind._tk_TypeCode :
  255. d = create_simple(official, type);
  256. d.insert_typecode(orb.get_primitive_tc(TCKind.tk_null));
  257. return d;
  258. case TCKind._tk_any :
  259. d = create_simple(official, type);
  260. Any empty_any = orb.create_any();
  261. empty_any.type(orb.get_primitive_tc(TCKind.tk_null));
  262. d.insert_any(empty_any);
  263. return d;
  264. case TCKind._tk_wstring :
  265. d = create_simple(official, type);
  266. d.insert_wstring("");
  267. return d;
  268. case TCKind._tk_string :
  269. d = create_simple(official, type);
  270. d.insert_string("");
  271. return d;
  272. case TCKind._tk_native :
  273. case TCKind._tk_Principal :
  274. case TCKind._tk_abstract_interface :
  275. throw new InconsistentTypeCode("Following API, the " +
  276. TypeKindNamer.nameIt(type) +
  277. " must not be supported."
  278. );
  279. default :
  280. return create_simple(official, type);
  281. }
  282. }
  283. catch (UserException uex)
  284. {
  285. InconsistentTypeCode it = new InconsistentTypeCode();
  286. it.initCause(uex);
  287. throw it;
  288. }
  289. }
  290. /**
  291. * Create the DynAny using the passed value as template and assign this value.
  292. */
  293. public DynAny create_dyn_any(Any value)
  294. throws InconsistentTypeCode
  295. {
  296. DynAny created = create_dyn_any_from_type_code(value.type());
  297. try
  298. {
  299. created.from_any(value);
  300. }
  301. catch (UserException uex)
  302. {
  303. InconsistentTypeCode t = new InconsistentTypeCode("Inconsistent Any");
  304. t.initCause(uex);
  305. throw t;
  306. }
  307. catch (Exception e)
  308. {
  309. throw new Unexpected(e);
  310. }
  311. return created;
  312. }
  313. }