1. /*
  2. * @(#)ObjectImpl.java 1.37 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.CORBA.portable;
  8. import org.omg.CORBA.Request;
  9. import org.omg.CORBA.NamedValue;
  10. import org.omg.CORBA.NVList;
  11. import org.omg.CORBA.ExceptionList;
  12. import org.omg.CORBA.ContextList;
  13. import org.omg.CORBA.Context;
  14. import org.omg.CORBA.TypeCode;
  15. import org.omg.CORBA.BAD_OPERATION;
  16. import org.omg.CORBA.SystemException;
  17. /**
  18. * The common base class for all stub classes; provides default implementations
  19. * of the <code>org.omg.CORBA.Object</code> methods. All method implementations are
  20. * forwarded to a <code>Delegate</code> object stored in the <code>ObjectImpl</code>
  21. * instance. <code>ObjectImpl</code> allows for portable stubs because the
  22. * <code>Delegate</code> can be implemented by a different vendor-specific ORB.
  23. */
  24. abstract public class ObjectImpl implements org.omg.CORBA.Object
  25. {
  26. /**
  27. * The field that stores the <code>Delegate</code> instance for
  28. * this <code>ObjectImpl</code> object. This <code>Delegate</code>
  29. * instance can be implemented by a vendor-specific ORB. Stub classes,
  30. * which are derived from this <code>ObjectImpl</code> class, can be
  31. * portable because they delegate all of the methods called on them to this
  32. * <code>Delegate</code> object.
  33. */
  34. private transient Delegate __delegate;
  35. /**
  36. * Retrieves the reference to the vendor-specific <code>Delegate</code>
  37. * object to which this <code>ObjectImpl</code> object delegates all
  38. * methods invoked on it.
  39. *
  40. * @return the Delegate contained in this ObjectImpl instance
  41. * @throws BAD_OPERATION if the delegate has not been set
  42. * @see #_set_delegate
  43. */
  44. public Delegate _get_delegate() {
  45. if (__delegate == null)
  46. throw new BAD_OPERATION("The delegate has not been set!");
  47. return __delegate;
  48. }
  49. /**
  50. * Sets the Delegate for this <code>ObjectImpl</code> instance to the given
  51. * <code>Delegate</code> object. All method invocations on this
  52. * <code>ObjectImpl</code> object will be forwarded to this delegate.
  53. *
  54. * @param delegate the <code>Delegate</code> instance to which
  55. * all method calls on this <code>ObjectImpl</code> object
  56. * will be delegated; may be implemented by a third-party ORB
  57. * @see #_get_delegate
  58. */
  59. public void _set_delegate(Delegate delegate) {
  60. __delegate = delegate;
  61. }
  62. /**
  63. * Retrieves a string array containing the repository identifiers
  64. * supported by this <code>ObjectImpl</code> object. For example,
  65. * for a stub, this method returns information about all the
  66. * interfaces supported by the stub.
  67. *
  68. * @return the array of all repository identifiers supported by this
  69. * <code>ObjectImpl</code> instance
  70. */
  71. public abstract String[] _ids();
  72. /**
  73. * Returns a duplicate of this <code>ObjectImpl</code> object.
  74. *
  75. * @return an <code>orb.omg.CORBA.Object</code> object that is
  76. * a duplicate of this object
  77. */
  78. public org.omg.CORBA.Object _duplicate() {
  79. return _get_delegate().duplicate(this);
  80. }
  81. /**
  82. * Releases the resources associated with this <code>ObjectImpl</code> object.
  83. */
  84. public void _release() {
  85. _get_delegate().release(this);
  86. }
  87. /**
  88. * Checks whether the object identified by the given repository
  89. * identifier is an <code>ObjectImpl</code> object.
  90. *
  91. * @param repository_id a <code>String</code> object with the repository
  92. * identifier to check
  93. * @return <code>true</code> if the object identified by the given
  94. * repository id is an instance of <code>ObjectImpl</code>
  95. * <code>false</code> otherwise
  96. */
  97. public boolean _is_a(String repository_id) {
  98. return _get_delegate().is_a(this, repository_id);
  99. }
  100. /**
  101. * Checks whether the the given <code>ObjectImpl</code> object is
  102. * equivalent to this <code>ObjectImpl</code> object.
  103. *
  104. * @param that an instance of <code>ObjectImpl</code> to compare with
  105. * this <code>ObjectImpl</code> object
  106. * @return <code>true</code> if the given object is equivalent
  107. * to this <code>ObjectImpl</code> object;
  108. * <code>false</code> otherwise
  109. */
  110. public boolean _is_equivalent(org.omg.CORBA.Object that) {
  111. return _get_delegate().is_equivalent(this, that);
  112. }
  113. /**
  114. * Checks whether the server object for this <code>ObjectImpl</code>
  115. * object has been destroyed.
  116. *
  117. * @return <code>true</code> if the ORB knows authoritatively that the
  118. * server object does not exist; <code>false</code> otherwise
  119. */
  120. public boolean _non_existent() {
  121. return _get_delegate().non_existent(this);
  122. }
  123. /**
  124. * Retrieves the hash code that serves as an ORB-internal identifier for
  125. * this <code>ObjectImpl</code> object.
  126. *
  127. * @param maximum an <code>int</code> indicating the upper bound on the hash
  128. * value returned by the ORB
  129. * @return an <code>int</code> representing the hash code for this
  130. * <code>ObjectImpl</code> object
  131. */
  132. public int _hash(int maximum) {
  133. return _get_delegate().hash(this, maximum);
  134. }
  135. /**
  136. * Creates a <code>Request</code> object containing the given method
  137. * that can be used with the Dynamic Invocation Interface.
  138. *
  139. * @param operation the method to be invoked by the new <code>Request</code>
  140. * object
  141. * @return a new <code>Request</code> object initialized with the
  142. * given method
  143. */
  144. public Request _request(String operation) {
  145. return _get_delegate().request(this, operation);
  146. }
  147. /**
  148. * Creates a <code>Request</code> object that contains the given context,
  149. * method, argument list, and container for the result.
  150. *
  151. * @param ctx the Context for the request
  152. * @param operation the method that the new <code>Request</code>
  153. * object will invoke
  154. * @param arg_list the arguments for the method; an <code>NVList</code>
  155. * in which each argument is a <code>NamedValue</code> object
  156. * @param result a <code>NamedValue</code> object to be used for
  157. * returning the result of executing the request's method
  158. * @return a new <code>Request</code> object initialized with the
  159. * given context, method, argument list, and container for the
  160. * return value
  161. */
  162. public Request _create_request(Context ctx,
  163. String operation,
  164. NVList arg_list,
  165. NamedValue result) {
  166. return _get_delegate().create_request(this,
  167. ctx,
  168. operation,
  169. arg_list,
  170. result);
  171. }
  172. /**
  173. * Creates a <code>Request</code> object that contains the given context,
  174. * method, argument list, container for the result, exceptions, and
  175. * list of property names to be used in resolving the context strings.
  176. * This <code>Request</code> object is for use in the Dynamic
  177. * Invocation Interface.
  178. *
  179. * @param ctx the <code>Context</code> object that contains the
  180. * context strings that must be resolved before they are
  181. * sent along with the request
  182. * @param operation the method that the new <code>Request</code>
  183. * object will invoke
  184. * @param arg_list the arguments for the method; an <code>NVList</code>
  185. * in which each argument is a <code>NamedValue</code> object
  186. * @param result a <code>NamedValue</code> object to be used for
  187. * returning the result of executing the request's method
  188. * @param exceptions a list of the exceptions that the given method
  189. * throws
  190. * @param contexts a list of the properties that are needed to
  191. * resolve the contexts in <i>ctx</i> the strings in
  192. * <i>contexts</i> are used as arguments to the method
  193. * <code>Context.get_values</code>,
  194. * which returns the value associated with the given property
  195. * @return a new <code>Request</code> object initialized with the
  196. * given context strings to resolve, method, argument list,
  197. * container for the result, exceptions, and list of property
  198. * names to be used in resolving the context strings
  199. */
  200. public Request _create_request(Context ctx,
  201. String operation,
  202. NVList arg_list,
  203. NamedValue result,
  204. ExceptionList exceptions,
  205. ContextList contexts) {
  206. return _get_delegate().create_request(this,
  207. ctx,
  208. operation,
  209. arg_list,
  210. result,
  211. exceptions,
  212. contexts);
  213. }
  214. /**
  215. * Retrieves the interface definition for this <code>ObjectImpl</code>
  216. * object.
  217. *
  218. * @return the <code>org.omg.CORBA.Object</code> instance that is the
  219. * interface definition for this <code>ObjectImpl</code> object
  220. */
  221. public org.omg.CORBA.Object _get_interface_def()
  222. {
  223. // First try to call the delegate implementation class's
  224. // "Object get_interface_def(..)" method (will work for JDK1.2 ORBs).
  225. // Else call the delegate implementation class's
  226. // "InterfaceDef get_interface(..)" method using reflection
  227. // (will work for pre-JDK1.2 ORBs).
  228. org.omg.CORBA.portable.Delegate delegate = _get_delegate();
  229. try {
  230. // If the ORB's delegate class does not implement
  231. // "Object get_interface_def(..)", this will call
  232. // get_interface_def(..) on portable.Delegate.
  233. return delegate.get_interface_def(this);
  234. }
  235. catch( org.omg.CORBA.NO_IMPLEMENT ex ) {
  236. // Call "InterfaceDef get_interface(..)" method using reflection.
  237. try {
  238. Class[] argc = { org.omg.CORBA.Object.class };
  239. java.lang.reflect.Method meth =
  240. delegate.getClass().getMethod("get_interface", argc);
  241. Object[] argx = { this };
  242. return (org.omg.CORBA.Object)meth.invoke(delegate, argx);
  243. }
  244. catch( java.lang.reflect.InvocationTargetException exs ) {
  245. Throwable t = exs.getTargetException();
  246. if (t instanceof Error) {
  247. throw (Error) t;
  248. }
  249. else if (t instanceof RuntimeException) {
  250. throw (RuntimeException) t;
  251. }
  252. else {
  253. throw new org.omg.CORBA.NO_IMPLEMENT();
  254. }
  255. } catch( RuntimeException rex ) {
  256. throw rex;
  257. } catch( Exception exr ) {
  258. throw new org.omg.CORBA.NO_IMPLEMENT();
  259. }
  260. }
  261. }
  262. /**
  263. * Returns a reference to the ORB associated with this object and
  264. * its delegate. This is the <code>ORB</code> object that created
  265. * the delegate.
  266. *
  267. * @return the <code>ORB</code> instance that created the
  268. * <code>Delegate</code> object contained in this
  269. * <code>ObjectImpl</code> object
  270. */
  271. public org.omg.CORBA.ORB _orb() {
  272. return _get_delegate().orb(this);
  273. }
  274. /**
  275. * Retrieves the <code>Policy</code> object for this
  276. * <code>ObjectImpl</code> object that has the given
  277. * policy type.
  278. *
  279. * @param policy_type an int indicating the policy type
  280. * @return the <code>Policy</code> object that is the specified policy type
  281. * and that applies to this <code>ObjectImpl</code> object
  282. * @see org.omg.CORBA.PolicyOperations#policy_type
  283. */
  284. public org.omg.CORBA.Policy _get_policy(int policy_type) {
  285. return _get_delegate().get_policy(this, policy_type);
  286. }
  287. /**
  288. * Retrieves a list of the domain managers for this
  289. * <code>ObjectImpl</code> object.
  290. *
  291. * @return an array containing the <code>DomainManager</code>
  292. * objects for this instance of <code>ObjectImpl</code>
  293. */
  294. public org.omg.CORBA.DomainManager[] _get_domain_managers() {
  295. return _get_delegate().get_domain_managers(this);
  296. }
  297. /**
  298. * Sets this <code>ObjectImpl</code> object's override type for
  299. * the given policies to the given instance of
  300. * <code>SetOverrideType</code>.
  301. *
  302. * @param policies an array of <code>Policy</code> objects with the
  303. * policies that will replace the current policies or be
  304. * added to the current policies
  305. * @param set_add either <code>SetOverrideType.SET_OVERRIDE</code>,
  306. * indicating that the given policies will replace any existing
  307. * ones, or <code>SetOverrideType.ADD_OVERRIDE</code>, indicating
  308. * that the given policies should be added to any existing ones
  309. * @return an <code>Object</code> with the given policies replacing or
  310. * added to its previous policies
  311. */
  312. public org.omg.CORBA.Object
  313. _set_policy_override(org.omg.CORBA.Policy[] policies,
  314. org.omg.CORBA.SetOverrideType set_add) {
  315. return _get_delegate().set_policy_override(this, policies,
  316. set_add);
  317. }
  318. /**
  319. * Checks whether this <code>ObjectImpl</code> object is implemented
  320. * by a local servant. If so, local invocation API's may be used.
  321. *
  322. * @return <code>true</code> if this object is implemented by a local
  323. * servant; <code>false</code> otherwise
  324. */
  325. public boolean _is_local() {
  326. return _get_delegate().is_local(this);
  327. }
  328. /**
  329. * Returns a Java reference to the local servant that should be used for sending
  330. * a request for the method specified. If this <code>ObjectImpl</code>
  331. * object is a local stub, it will invoke the <code>_servant_preinvoke</code>
  332. * method before sending a request in order to obtain the
  333. * <code>ServantObject</code> instance to use.
  334. * <P>
  335. * If a <code>ServantObject</code> object is returned, its <code>servant</code>
  336. * field has been set to an object of the expected type (Note: the object may
  337. * or may not be the actual servant instance). The local stub may cast
  338. * the servant field to the expected type, and then invoke the operation
  339. * directly. The <code>ServantRequest</code> object is valid for only one
  340. * invocation and cannot be used for more than one invocation.
  341. *
  342. * @param operation a <code>String</code> containing the name of the method
  343. * to be invoked. This name should correspond to the method name as
  344. * it would be encoded in a GIOP request.
  345. *
  346. * @param expectedType a <code>Class</code> object representing the
  347. * expected type of the servant that is returned. This expected
  348. * type is the <code>Class</code> object associated with the
  349. * operations class for the stub's interface. For example, a
  350. * stub for an interface <code>Foo</code> would pass the
  351. * <code>Class</code> object for the <code>FooOperations</code>
  352. * interface.
  353. *
  354. * @return (1) a <code>ServantObject</code> object, which may or may
  355. * not be the actual servant instance, or (2) <code>null</code> if
  356. * (a) the servant is not local or (b) the servant has ceased to
  357. * be local due to a ForwardRequest from a POA ServantManager
  358. * @throws org.omg.CORBA.BAD_PARAM if the servant is not the expected type
  359. */
  360. public ServantObject _servant_preinvoke(String operation,
  361. Class expectedType) {
  362. return _get_delegate().servant_preinvoke(this, operation,
  363. expectedType);
  364. }
  365. /**
  366. * Is called by the local stub after it has invoked an operation
  367. * on the local servant that was previously retrieved from a
  368. * call to the method <code>_servant_preinvoke</code>.
  369. * The <code>_servant_postinvoke</code> method must be called
  370. * if the <code>_servant_preinvoke</code>
  371. * method returned a non-null value, even if an exception was thrown
  372. * by the method invoked by the servant. For this reason, the call
  373. * to the method <code>_servant_postinvoke</code> should be placed
  374. * in a Java <code>finally</code> clause.
  375. *
  376. * @param servant the instance of the <code>ServantObject</code>
  377. * returned by the <code>_servant_preinvoke</code> method
  378. */
  379. public void _servant_postinvoke(ServantObject servant) {
  380. _get_delegate().servant_postinvoke(this, servant);
  381. }
  382. /*
  383. * The following methods were added by orbos/98-04-03: Java to IDL
  384. * Mapping. These are used by RMI over IIOP.
  385. */
  386. /**
  387. * Returns an <code>OutputStream</code> object to use for marshalling
  388. * the arguments of the given method. This method is called by a stub,
  389. * which must indicate if a response is expected, that is, whether or not
  390. * the call is oneway.
  391. *
  392. * @param operation a String giving the name of the method.
  393. * @param responseExpected a boolean -- <code>true</code> if the
  394. * request is not one way, that is, a response is expected
  395. * @return an <code>OutputStream</code> object for dispatching the request
  396. */
  397. public OutputStream _request(String operation,
  398. boolean responseExpected) {
  399. return _get_delegate().request(this, operation, responseExpected);
  400. }
  401. /**
  402. * Invokes an operation and returns an <code>InputStream</code>
  403. * object for reading the response. The stub provides the
  404. * <code>OutputStream</code> object that was previously returned by a
  405. * call to the <code>_request</code> method. The method specified
  406. * as an argument to <code>_request</code> when it was
  407. * called previously is the method that this method invokes.
  408. * <P>
  409. * If an exception occurs, the <code>_invoke</code> method may throw an
  410. * <code>ApplicationException</code> object that contains an InputStream from
  411. * which the user exception state may be unmarshalled.
  412. *
  413. * @param output an OutputStream object for dispatching the request
  414. * @return an <code>InputStream</code> object containing the marshalled
  415. * response to the method invoked
  416. * @throws ApplicationException if the invocation
  417. * meets application-defined exception
  418. * @throws RemarshalException if the invocation leads
  419. * to a remarshalling error
  420. * @see #_request
  421. */
  422. public InputStream _invoke(OutputStream output)
  423. throws ApplicationException, RemarshalException {
  424. return _get_delegate().invoke(this, output);
  425. }
  426. /**
  427. * Releases the given
  428. * reply stream back to the ORB when unmarshalling has
  429. * completed after a call to the method <code>_invoke</code>.
  430. * Calling this method is optional for the stub.
  431. *
  432. * @param input the <code>InputStream</code> object that was returned
  433. * by the <code>_invoke</code> method or the
  434. * <code>ApplicationException.getInputStream</code> method;
  435. * may be <code>null</code>, in which case this method does
  436. * nothing
  437. * @see #_invoke
  438. */
  439. public void _releaseReply(InputStream input) {
  440. _get_delegate().releaseReply(this, input);
  441. }
  442. /**
  443. * Returns a <code>String</code> object that represents this
  444. * <code>ObjectImpl</code> object.
  445. *
  446. * @return the <code>String</code> representation of this object
  447. */
  448. public String toString() {
  449. if ( __delegate != null )
  450. return __delegate.toString(this);
  451. else
  452. return getClass().getName() + ": no delegate set";
  453. }
  454. /**
  455. * Returns the hash code for this <code>ObjectImpl</code> object.
  456. *
  457. * @return the hash code for this object
  458. */
  459. public int hashCode() {
  460. if ( __delegate != null )
  461. return __delegate.hashCode(this);
  462. else
  463. return System.identityHashCode(this);
  464. }
  465. /**
  466. * Compares this <code>ObjectImpl</code> object with the given one
  467. * for equality.
  468. *
  469. *@param obj the object with which to compare this object
  470. *@return <code>true</code> if the two objects are equal;
  471. * <code>false</code> otherwise
  472. */
  473. public boolean equals(java.lang.Object obj) {
  474. if ( __delegate != null )
  475. return __delegate.equals(this, obj);
  476. else
  477. return (this==obj);
  478. }
  479. }