SimpleDelegate.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Local_delegate.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;
  32. import org.omg.CORBA.Context;
  33. import org.omg.CORBA.ContextList;
  34. import org.omg.CORBA.ExceptionList;
  35. import org.omg.CORBA.NO_IMPLEMENT;
  36. import org.omg.CORBA.NVList;
  37. import org.omg.CORBA.NamedValue;
  38. import org.omg.CORBA.ORB;
  39. import org.omg.CORBA.Request;
  40. import org.omg.CORBA.portable.Delegate;
  41. import org.omg.CORBA.portable.ObjectImpl;
  42. /**
  43. * The delegate, implementing the basic functionality only. This delegate
  44. * is set in {@link ORG.connect(org.omg.CORBA.Object)} if ORB
  45. * determines that the object is an instance of the
  46. * {@link org.omg.CORBA.portable.ObjectImpl} and no other delegate is set.
  47. *
  48. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  49. */
  50. public class SimpleDelegate
  51. extends Delegate
  52. implements IorProvider
  53. {
  54. /**
  55. * The orb.
  56. */
  57. protected final ORB orb;
  58. /**
  59. * The ior.
  60. */
  61. protected IOR ior;
  62. public SimpleDelegate(ORB an_orb, IOR an_ior)
  63. {
  64. orb = an_orb;
  65. ior = an_ior;
  66. }
  67. /**
  68. * Set the IOR of this object. The IOR must be newly set if
  69. * the server reports that the object has permanently moved to a new
  70. * location.
  71. *
  72. * @param an_ior the new IOR.
  73. */
  74. public void setIor(IOR an_ior)
  75. {
  76. this.ior = an_ior;
  77. }
  78. /**
  79. * Get the IOR of this object.
  80. */
  81. public IOR getIor()
  82. {
  83. return ior;
  84. }
  85. /**
  86. * Create the request for the local call
  87. */
  88. public Request create_request(org.omg.CORBA.Object target, Context context,
  89. String operation, NVList parameters,
  90. NamedValue returns
  91. )
  92. {
  93. if (orb instanceof OrbFunctional)
  94. {
  95. ((OrbFunctional) orb).ensureRunning();
  96. }
  97. gnuRequest g = new gnuRequest();
  98. g.setORB(orb);
  99. g.setOperation(operation);
  100. g.setIor(ior);
  101. g.m_target = target;
  102. g.ctx(context);
  103. g.set_args(parameters);
  104. if (returns != null)
  105. g.set_result(returns);
  106. return g;
  107. }
  108. /**
  109. * Create the request for the local call.
  110. */
  111. public Request create_request(org.omg.CORBA.Object target, Context context,
  112. String operation, NVList parameters,
  113. NamedValue returns, ExceptionList exceptions,
  114. ContextList ctx_list
  115. )
  116. {
  117. if (orb instanceof OrbFunctional)
  118. {
  119. ((OrbFunctional) orb).ensureRunning();
  120. }
  121. gnuRequest g = new gnuRequest();
  122. g.setORB(orb);
  123. g.setOperation(operation);
  124. g.setIor(ior);
  125. g.m_target = target;
  126. g.ctx(context);
  127. g.set_args(parameters);
  128. g.set_exceptions(exceptions);
  129. g.set_context_list(ctx_list);
  130. if (returns != null)
  131. g.set_result(returns);
  132. return g;
  133. }
  134. /**
  135. * Not implemented.
  136. *
  137. * @throws NO_IMPLEMENT, always.
  138. */
  139. public org.omg.CORBA.Object duplicate(org.omg.CORBA.Object target)
  140. {
  141. throw new NO_IMPLEMENT();
  142. }
  143. /**
  144. * Performs direct comparison ('==').
  145. */
  146. public boolean equals(org.omg.CORBA.Object self, org.omg.CORBA.Object other)
  147. {
  148. return self == other;
  149. }
  150. /**
  151. * Not implemented.
  152. *
  153. * @throws NO_IMPLEMENT, always.
  154. */
  155. public org.omg.CORBA.Object get_interface_def(org.omg.CORBA.Object target)
  156. {
  157. throw new NO_IMPLEMENT();
  158. }
  159. /**
  160. * Return the hashcode (0 <= hashcode < maximum).
  161. */
  162. public int hash(org.omg.CORBA.Object target, int maximum)
  163. {
  164. return target == null ? 0 : target.hashCode() % maximum;
  165. }
  166. /**
  167. * Delegates functionality to java.lang.Object.hashCode();
  168. */
  169. public int hashCode(org.omg.CORBA.Object target)
  170. {
  171. return target == null ? 0 : target.hashCode();
  172. }
  173. /**
  174. * Check if this object can be referenced by the given repository id.
  175. *
  176. * @param target the CORBA object, must be an instance of
  177. * {@link org.omg.CORBA.portable.ObjectImpl}.
  178. *
  179. * @param repositoryIdentifer the repository id.
  180. *
  181. * @return true if the passed parameter is a repository id of this
  182. * CORBA object.
  183. */
  184. public boolean is_a(org.omg.CORBA.Object target, String repositoryIdentifer)
  185. {
  186. if (!(target instanceof ObjectImpl))
  187. throw new NO_IMPLEMENT("Supported only for org.omg.CORBA.portable.ObjectImpl");
  188. ObjectImpl imp = (ObjectImpl) target;
  189. String[] ids = imp._ids();
  190. for (int i = 0; i < ids.length; i++)
  191. {
  192. if (ids [ i ].equals(repositoryIdentifer))
  193. return true;
  194. }
  195. return false;
  196. }
  197. /**
  198. * Returns true if the objects are the same or have the same delegate set. All
  199. * objects in this implementation have a separate delegate.
  200. */
  201. public boolean is_equivalent(org.omg.CORBA.Object target,
  202. org.omg.CORBA.Object other)
  203. {
  204. if (target == other)
  205. return true;
  206. if ((target instanceof ObjectImpl) && other instanceof ObjectImpl)
  207. {
  208. try
  209. {
  210. org.omg.CORBA.portable.Delegate a = ((ObjectImpl) target)._get_delegate();
  211. org.omg.CORBA.portable.Delegate b = ((ObjectImpl) other)._get_delegate();
  212. if (a == b)
  213. {
  214. return true;
  215. }
  216. else
  217. {
  218. // We compere the IOR's in this case.
  219. if (a instanceof IorProvider && b instanceof IorProvider)
  220. {
  221. IOR ia = ((IorProvider) a).getIor();
  222. IOR ib = ((IorProvider) b).getIor();
  223. if (ia != null && ib != null)
  224. return (ia.equals(ib));
  225. else
  226. return ia == ib;
  227. }
  228. }
  229. if (a != null && b != null)
  230. {
  231. return a.equals(b);
  232. }
  233. }
  234. catch (Exception ex)
  235. {
  236. // Unable to get one of the delegates.
  237. return false;
  238. }
  239. }
  240. return false;
  241. }
  242. /**
  243. * Returns true by default.
  244. */
  245. public boolean is_local(org.omg.CORBA.Object self)
  246. {
  247. return true;
  248. }
  249. /**
  250. * Returns true if the target is null.
  251. */
  252. public boolean non_existent(org.omg.CORBA.Object target)
  253. {
  254. return target == null;
  255. }
  256. /**
  257. * Returns the ORB, passed in constructor,
  258. * regardless of the argument. This class requires a single instance
  259. * per each object.
  260. */
  261. public ORB orb(org.omg.CORBA.Object target)
  262. {
  263. return orb;
  264. }
  265. /**
  266. * Returns without action.
  267. */
  268. public void release(org.omg.CORBA.Object target)
  269. {
  270. }
  271. /**
  272. * This method assumes that the target is local and connected to the ORB.
  273. */
  274. public Request request(org.omg.CORBA.Object target, String operation)
  275. {
  276. if (orb instanceof OrbFunctional)
  277. {
  278. ((OrbFunctional) orb).ensureRunning();
  279. }
  280. gnuRequest g = new gnuRequest();
  281. g.setORB(orb);
  282. g.setOperation(operation);
  283. g.setIor(ior);
  284. g.m_target = target;
  285. return g;
  286. }
  287. }