1. /*
  2. * @(#)ORBInitInfoImpl.java 1.23 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.Interceptors;
  8. import org.omg.PortableInterceptor.ORBInitInfo;
  9. import org.omg.IOP.CodecFactory;
  10. import org.omg.PortableInterceptor.ClientRequestInterceptor;
  11. import org.omg.PortableInterceptor.IORInterceptor;
  12. import org.omg.PortableInterceptor.PolicyFactory;
  13. import org.omg.PortableInterceptor.ServerRequestInterceptor;
  14. import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;
  15. import org.omg.PortableInterceptor.ORBInitInfoPackage.InvalidName;
  16. import org.omg.CORBA.BAD_PARAM;
  17. import org.omg.CORBA.BAD_INV_ORDER;
  18. import org.omg.CORBA.CompletionStatus;
  19. import org.omg.CORBA.NO_IMPLEMENT;
  20. import org.omg.CORBA.OBJECT_NOT_EXIST;
  21. import org.omg.CORBA.ORB;
  22. import org.omg.CORBA.LocalObject;
  23. import org.omg.CORBA.portable.Delegate;
  24. import org.omg.CORBA.portable.ObjectImpl;
  25. import com.sun.corba.se.internal.core.ClientSubcontract;
  26. import com.sun.corba.se.internal.core.IOR;
  27. /**
  28. * ORBInitInfoImpl is the implementation of the ORBInitInfo class to be
  29. * passed to ORBInitializers, as described in orbos/99-12-02.
  30. */
  31. public final class ORBInitInfoImpl
  32. extends org.omg.CORBA.LocalObject
  33. implements ORBInitInfo
  34. {
  35. // The ORB we are initializing
  36. private PIORB orb;
  37. // The arguments passed to ORB_init
  38. private String[] args;
  39. // The ID of the ORB being initialized
  40. private String orbId;
  41. // The CodecFactory
  42. private CodecFactory codecFactory;
  43. // The current stage of initialization
  44. private int stage = STAGE_PRE_INIT;
  45. // The pre-initialization stage (pre_init() being called)
  46. public static final int STAGE_PRE_INIT = 0;
  47. // The post-initialization stage (post_init() being called)
  48. public static final int STAGE_POST_INIT = 1;
  49. // Reject all calls - this object should no longer be around.
  50. public static final int STAGE_CLOSED = 2;
  51. // The description for the OBJECT_NOT_EXIST exception in STAGE_CLOSED
  52. private static final String MESSAGE_ORBINITINFO_INVALID =
  53. "ORBInitInfo object is only valid during ORB_init";
  54. /**
  55. * Creates a new ORBInitInfoImpl object (scoped to package)
  56. *
  57. * @param args The arguments passed to ORB_init.
  58. */
  59. ORBInitInfoImpl( PIORB orb, String[] args, String orbId,
  60. CodecFactory codecFactory )
  61. {
  62. this.orb = orb;
  63. this.args = args;
  64. this.orbId = orbId;
  65. this.codecFactory = codecFactory;
  66. }
  67. /**
  68. * Sets the current stage we are in. This limits access to certain
  69. * functionality.
  70. */
  71. void setStage( int stage ) {
  72. this.stage = stage;
  73. }
  74. /**
  75. * Throws an exception if the current stage is STAGE_CLOSED.
  76. * This is called before any method is invoked to ensure that
  77. * no method invocations are attempted after all calls to post_init()
  78. * are completed.
  79. */
  80. private void checkStage() {
  81. if( stage == STAGE_CLOSED ) {
  82. throw new OBJECT_NOT_EXIST( MESSAGE_ORBINITINFO_INVALID );
  83. }
  84. }
  85. /*
  86. *******************************************************************
  87. * The following are implementations of the ORBInitInfo operations.
  88. *******************************************************************/
  89. /**
  90. * This attribute contains the arguments passed to ORB_init. They may
  91. * or may not contain the ORB's arguments
  92. */
  93. public String[] arguments () {
  94. checkStage();
  95. return args;
  96. }
  97. /**
  98. * This attribute is the ID of the ORB being initialized
  99. */
  100. public String orb_id () {
  101. checkStage();
  102. return orbId;
  103. }
  104. /**
  105. * This attribute is the IOP::CodecFactory. The CodecFactory is normally
  106. * obtained via a call to ORB::resolve_initial_references( "CodecFactory" )
  107. * but since the ORB is not yet available and Interceptors, particularly
  108. * when processing service contexts, will require a Codec, a means of
  109. * obtaining a Codec is necessary during ORB intialization.
  110. */
  111. public CodecFactory codec_factory () {
  112. checkStage();
  113. return codecFactory;
  114. }
  115. /**
  116. * See orbos/99-12-02, Chapter 11, Dynamic Initial References on page
  117. * 11-81. This operation is identical to ORB::register_initial_reference
  118. * described there. This same functionality exists here because the ORB,
  119. * not yet fully initialized, is not yet available but initial references
  120. * may need to be registered as part of Interceptor registration.
  121. * <p>
  122. * This method may not be called during post_init.
  123. */
  124. public void register_initial_reference( String id,
  125. org.omg.CORBA.Object obj )
  126. throws InvalidName
  127. {
  128. checkStage();
  129. if( id == null ) nullParam();
  130. // As per ptc/00-08-06, if null is passed as the obj parameter,
  131. // throw BAD_PARAM with minor code MinorCodes.RIR_WITH_NULL_OBJECT.
  132. // Though the spec is talking about IDL null, we will address both
  133. // Java null and IDL null:
  134. // Note: Local Objects can never be nil!
  135. boolean isNil = false;
  136. if( obj == null ) {
  137. isNil = true;
  138. }
  139. else if( obj instanceof ObjectImpl ) {
  140. Delegate delegate = ((ObjectImpl)obj)._get_delegate();
  141. ClientSubcontract csub = (ClientSubcontract)delegate;
  142. IOR ior = csub.marshal();
  143. isNil = ior.is_nil();
  144. }
  145. if( isNil ) {
  146. throw new BAD_PARAM(
  147. "register_initial_reference called with nil Object.",
  148. MinorCodes.RIR_WITH_NULL_OBJECT,
  149. CompletionStatus.COMPLETED_NO );
  150. }
  151. // Delegate to ORB. If ORB version throws InvalidName, convert to
  152. // equivalent Portable Interceptors InvalidName.
  153. try {
  154. orb.register_initial_reference( id, obj );
  155. }
  156. catch( org.omg.CORBA.ORBPackage.InvalidName e ) {
  157. throw new InvalidName( e.getMessage() );
  158. }
  159. }
  160. /**
  161. * This operation is only valid during post_init. It is identical to
  162. * ORB::resolve_initial_references. This same functionality exists here
  163. * because the ORB, not yet fully initialized, is not yet available,
  164. * but initial references may be required from the ORB as part
  165. * of Interceptor registration.
  166. * <p>
  167. * (incorporates changes from errata in orbos/00-01-01)
  168. * <p>
  169. * This method may not be called during pre_init.
  170. */
  171. public org.omg.CORBA.Object resolve_initial_references (String id)
  172. throws InvalidName
  173. {
  174. checkStage();
  175. if( id == null ) nullParam();
  176. if( stage == STAGE_PRE_INIT ) {
  177. // Initializer is not allowed to invoke this method during
  178. // this stage.
  179. // _REVISIT_ Spec issue: What exception should really be
  180. // thrown here?
  181. throw new BAD_INV_ORDER(
  182. "Resolve Initial References cannot be called in pre_init",
  183. MinorCodes.RIR_INVALID_PRE_INIT,
  184. CompletionStatus.COMPLETED_NO );
  185. }
  186. org.omg.CORBA.Object objRef = null;
  187. try {
  188. objRef = orb.resolve_initial_references( id );
  189. }
  190. catch( org.omg.CORBA.ORBPackage.InvalidName e ) {
  191. // Convert PIDL to IDL exception:
  192. throw new InvalidName();
  193. }
  194. return objRef;
  195. }
  196. /**
  197. * This operation is used to add a client-side request Interceptor to
  198. * the list of client-side request Interceptors.
  199. * <p>
  200. * If a client-side request Interceptor has already been registered
  201. * with this Interceptor's name, DuplicateName is raised.
  202. */
  203. public void add_client_request_interceptor (
  204. ClientRequestInterceptor interceptor)
  205. throws DuplicateName
  206. {
  207. checkStage();
  208. if( interceptor == null ) nullParam();
  209. orb.register_interceptor( interceptor,
  210. InterceptorList.INTERCEPTOR_TYPE_CLIENT );
  211. }
  212. /**
  213. * This operation is used to add a server-side request Interceptor to
  214. * the list of server-side request Interceptors.
  215. * <p>
  216. * If a server-side request Interceptor has already been registered
  217. * with this Interceptor's name, DuplicateName is raised.
  218. */
  219. public void add_server_request_interceptor (
  220. ServerRequestInterceptor interceptor)
  221. throws DuplicateName
  222. {
  223. checkStage();
  224. if( interceptor == null ) nullParam();
  225. orb.register_interceptor( interceptor,
  226. InterceptorList.INTERCEPTOR_TYPE_SERVER );
  227. }
  228. /**
  229. * This operation is used to add an IOR Interceptor to
  230. * the list of IOR Interceptors.
  231. * <p>
  232. * If an IOR Interceptor has already been registered
  233. * with this Interceptor's name, DuplicateName is raised.
  234. */
  235. public void add_ior_interceptor (
  236. IORInterceptor interceptor )
  237. throws DuplicateName
  238. {
  239. checkStage();
  240. if( interceptor == null ) nullParam();
  241. orb.register_interceptor( interceptor,
  242. InterceptorList.INTERCEPTOR_TYPE_IOR );
  243. }
  244. /**
  245. * A service calls allocate_slot_id to allocate a slot on
  246. * PortableInterceptor::Current.
  247. *
  248. * @return The index to the slot which has been allocated.
  249. */
  250. public int allocate_slot_id () {
  251. checkStage();
  252. return orb.getPICurrent().allocateSlotId( );
  253. }
  254. /**
  255. * Register a PolicyFactory for the given PolicyType.
  256. * <p>
  257. * If a PolicyFactory already exists for the given PolicyType,
  258. * BAD_INV_ORDER is raised with a minor code of TBD_BIO+2.
  259. */
  260. public void register_policy_factory( int type,
  261. PolicyFactory policy_factory )
  262. {
  263. checkStage();
  264. if( policy_factory == null ) nullParam();
  265. orb.registerPolicyFactory( type, policy_factory );
  266. }
  267. /**
  268. * Called when an invalid null parameter was passed. Throws a
  269. * BAD_PARAM with a minor code of 1
  270. */
  271. private void nullParam()
  272. throws BAD_PARAM
  273. {
  274. throw new BAD_PARAM(
  275. com.sun.corba.se.internal.orbutil.MinorCodes.NULL_PARAM,
  276. CompletionStatus.COMPLETED_NO );
  277. }
  278. }