1. /*
  2. * @(#)Servant.java 1.18 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package org.omg.PortableServer;
  8. import org.omg.CORBA.ORB;
  9. import org.omg.PortableServer.portable.Delegate;
  10. /**
  11. * Defines the native <code>Servant</code> type. In Java, the
  12. * <code>Servant</code> type is mapped to the Java
  13. * <code>org.omg.PortableServer.Servant</code> class.
  14. * It serves as the base class for all POA servant
  15. * implementations and provides a number of methods that may
  16. * be invoked by the application programmer, as well as methods
  17. * which are invoked by the POA itself and may be overridden by
  18. * the user to control aspects of servant behavior.
  19. * Based on IDL to Java spec. (CORBA V2.3.1) ptc/00-01-08.pdf.
  20. */
  21. abstract public class Servant {
  22. private transient Delegate _delegate = null;
  23. /**
  24. * Gets the ORB vendor-specific implementation of
  25. * <code>PortableServer::Servant</code>.
  26. * @return <code>_delegate</code> the ORB vendor-specific
  27. * implementation of <code>PortableServer::Servant</code>.
  28. */
  29. final public Delegate _get_delegate() {
  30. if (_delegate == null) {
  31. throw
  32. new
  33. org.omg.CORBA.BAD_INV_ORDER
  34. ("The Servant has not been associated with an ORB instance");
  35. }
  36. return _delegate;
  37. }
  38. /**
  39. * Supports the Java ORB portability
  40. * interfaces by providing a method for classes that support
  41. * ORB portability through delegation to set their delegate.
  42. * @param delegate ORB vendor-specific implementation of
  43. * the <code>PortableServer::Servant</code>.
  44. */
  45. final public void _set_delegate(Delegate delegate) {
  46. _delegate = delegate;
  47. }
  48. /**
  49. * Allows the servant to obtain the object reference for
  50. * the target CORBA object it is incarnating for that request.
  51. * @return <code>this_object</code> <code>Object</code> reference
  52. * associated with the request.
  53. */
  54. final public org.omg.CORBA.Object _this_object() {
  55. return _get_delegate().this_object(this);
  56. }
  57. /**
  58. * Allows the servant to obtain the object reference for
  59. * the target CORBA Object it is incarnating for that request.
  60. * @param orb ORB with which the servant is associated.
  61. * @return <code>_this_object</code> reference associated with the request.
  62. */
  63. final public org.omg.CORBA.Object _this_object(ORB orb) {
  64. try {
  65. ((org.omg.CORBA_2_3.ORB)orb).set_delegate(this);
  66. }
  67. catch(ClassCastException e) {
  68. throw
  69. new
  70. org.omg.CORBA.BAD_PARAM
  71. ("POA Servant requires an instance of org.omg.CORBA_2_3.ORB");
  72. }
  73. return _this_object();
  74. }
  75. /**
  76. * Returns the instance of the ORB
  77. * currently associated with the <code>Servant</code> (convenience method).
  78. * @return <code>orb</code> the instance of the ORB currently
  79. * associated with the <code>Servant</code>.
  80. */
  81. final public ORB _orb() {
  82. return _get_delegate().orb(this);
  83. }
  84. /**
  85. * Allows easy execution of common methods, equivalent to
  86. * <code>PortableServer::Current:get_POA</code>.
  87. * @return <code>poa</code> POA associated with the servant.
  88. */
  89. final public POA _poa() {
  90. return _get_delegate().poa(this);
  91. }
  92. /**
  93. * Allows easy execution of
  94. * common methods, equivalent
  95. * to calling <code>PortableServer::Current::get_object_id</code>.
  96. * @return <code>object_id</code> the <code>Object</code> ID associated
  97. * with this servant.
  98. */
  99. final public byte[] _object_id() {
  100. return _get_delegate().object_id(this);
  101. }
  102. /**
  103. * Returns the
  104. * root POA from the ORB instance associated with the servant.
  105. * Subclasses may override this method to return a different POA.
  106. * @return <code>default_POA</code> the POA associated with the
  107. * <code>Servant</code>.
  108. */
  109. public POA _default_POA() {
  110. return _get_delegate().default_POA(this);
  111. }
  112. /**
  113. * Checks to see if the specified <code>repository_id</code> is present
  114. * on the list returned by <code>_all_interfaces()</code> or is the
  115. * <code>repository_id</code> for the generic CORBA Object.
  116. * @param repository_id the <code>repository_id</code>
  117. * to be checked in the repository list or against the id
  118. * of generic CORBA objects.
  119. * @return <code>is_a</code> boolean indicating whether the specified
  120. * <code>repository_id</code> is
  121. * in the repository list or is same as a generic CORBA
  122. * object.
  123. */
  124. public boolean _is_a(String repository_id) {
  125. return _get_delegate().is_a(this, repository_id);
  126. }
  127. /**
  128. * Checks for the existence of an
  129. * <code>Object</code>.
  130. * The <code>Servant</code> provides a default implementation of
  131. * <code>_non_existent()</code> that can be overridden by derived servants.
  132. * @return <code>non_existent</code> <code>true</code> if that object does
  133. * not exist, <code>false</code> otherwise.
  134. */
  135. public boolean _non_existent() {
  136. return _get_delegate().non_existent(this);
  137. }
  138. // Ken and Simon will ask about editorial changes
  139. // needed in IDL to Java mapping to the following
  140. // signature.
  141. /**
  142. * Returns an object in the Interface Repository
  143. * which provides type information that may be useful to a program.
  144. * <code>Servant</code> provides a default implementation of
  145. * <code>_get_interface()</code>
  146. * that can be overridden by derived servants if the default
  147. * behavior is not adequate.
  148. * @return <code>get_interface</code> type information that corresponds to this servant.
  149. */
  150. /*
  151. public org.omg.CORBA.Object _get_interface() {
  152. return _get_delegate().get_interface(this);
  153. }
  154. */
  155. // _get_interface_def() replaces the _get_interface() method
  156. /**
  157. * Returns an <code>InterfaceDef</code> object as a
  158. * <code>CORBA::Object</code> that defines the runtime type of the
  159. * <code>CORBA::Object</code> implemented by the <code>Servant</code>.
  160. * The invoker of <code>_get_interface_def</code>
  161. * must narrow the result to an <code>InterfaceDef</code> in order
  162. * to use it.
  163. * <P>This default implementation of <code>_get_interface_def()</code>
  164. * can be overridden
  165. * by derived servants if the default behavior is not adequate.
  166. * As defined in the CORBA 2.3.1 specification, section 11.3.1, the
  167. * default behavior of <code>_get_interface_def()</code> is to use
  168. * the most derived
  169. * interface of a static servant or the most derived interface retrieved
  170. * from a dynamic servant to obtain the <code>InterfaceDef</code>.
  171. * This behavior must
  172. * be supported by the <code>Delegate</code> that implements the
  173. * <code>Servant</code>.
  174. * @return <code>get_interface_def</code> an <code>InterfaceDef</code>
  175. * object as a
  176. * <code>CORBA::Object</code> that defines the runtime type of the
  177. * <code>CORBA::Object</code> implemented by the <code>Servant</code>.
  178. */
  179. public org.omg.CORBA.Object _get_interface_def()
  180. {
  181. // First try to call the delegate implementation class's
  182. // "Object get_interface_def(..)" method (will work for ORBs
  183. // whose delegates implement this method).
  184. // Else call the delegate implementation class's
  185. // "InterfaceDef get_interface(..)" method using reflection
  186. // (will work for ORBs that were built using an older version
  187. // of the Delegate interface with a get_interface method
  188. // but not a get_interface_def method).
  189. org.omg.PortableServer.portable.Delegate delegate = _get_delegate();
  190. try {
  191. // If the ORB's delegate class does not implement
  192. // "Object get_interface_def(..)", this will throw
  193. // an AbstractMethodError.
  194. return delegate.get_interface_def(this);
  195. } catch( AbstractMethodError aex ) {
  196. // Call "InterfaceDef get_interface(..)" method using reflection.
  197. try {
  198. Class[] argc = { org.omg.PortableServer.Servant.class };
  199. java.lang.reflect.Method meth =
  200. delegate.getClass().getMethod("get_interface", argc);
  201. Object[] argx = { this };
  202. return (org.omg.CORBA.Object)meth.invoke(delegate, argx);
  203. } catch( java.lang.reflect.InvocationTargetException exs ) {
  204. Throwable t = exs.getTargetException();
  205. if (t instanceof Error) {
  206. throw (Error) t;
  207. } else if (t instanceof RuntimeException) {
  208. throw (RuntimeException) t;
  209. } else {
  210. throw new org.omg.CORBA.NO_IMPLEMENT();
  211. }
  212. } catch( RuntimeException rex ) {
  213. throw rex;
  214. } catch( Exception exr ) {
  215. throw new org.omg.CORBA.NO_IMPLEMENT();
  216. }
  217. }
  218. }
  219. // methods for which the user must provide an
  220. // implementation
  221. /**
  222. * Used by the ORB to obtain complete type
  223. * information from the servant.
  224. * @param poa POA with which the servant is associated.
  225. * @param objectId is the id corresponding to the object
  226. * associated with this servant.
  227. * @return list of type information for the object.
  228. */
  229. abstract public String[] _all_interfaces( POA poa, byte[] objectId);
  230. }