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