1. /*
  2. * @(#)ORBInitInfoImpl.java 1.26 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 com.sun.corba.se.impl.interceptors;
  8. import org.omg.CORBA.BAD_PARAM;
  9. import org.omg.CORBA.BAD_INV_ORDER;
  10. import org.omg.CORBA.CompletionStatus;
  11. import org.omg.CORBA.NO_IMPLEMENT;
  12. import org.omg.CORBA.OBJECT_NOT_EXIST;
  13. import org.omg.CORBA.LocalObject;
  14. import org.omg.CORBA.Policy;
  15. import org.omg.CORBA.PolicyError;
  16. import org.omg.IOP.CodecFactory;
  17. import org.omg.PortableInterceptor.ORBInitInfo;
  18. import org.omg.PortableInterceptor.ClientRequestInterceptor;
  19. import org.omg.PortableInterceptor.IORInterceptor;
  20. import org.omg.PortableInterceptor.PolicyFactory;
  21. import org.omg.PortableInterceptor.ServerRequestInterceptor;
  22. import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName;
  23. import org.omg.PortableInterceptor.ORBInitInfoPackage.InvalidName;
  24. import com.sun.corba.se.spi.orb.ORB;
  25. import com.sun.corba.se.spi.legacy.interceptor.ORBInitInfoExt ;
  26. import com.sun.corba.se.spi.logging.CORBALogDomains;
  27. import com.sun.corba.se.impl.orbutil.ORBUtility;
  28. import com.sun.corba.se.impl.logging.InterceptorsSystemException;
  29. import com.sun.corba.se.impl.logging.ORBUtilSystemException;
  30. import com.sun.corba.se.impl.logging.OMGSystemException;
  31. /**
  32. * ORBInitInfoImpl is the implementation of the ORBInitInfo class to be
  33. * passed to ORBInitializers, as described in orbos/99-12-02.
  34. */
  35. public final class ORBInitInfoImpl
  36. extends org.omg.CORBA.LocalObject
  37. implements ORBInitInfo, ORBInitInfoExt
  38. {
  39. // The ORB we are initializing
  40. private ORB orb;
  41. private InterceptorsSystemException wrapper ;
  42. private ORBUtilSystemException orbutilWrapper ;
  43. private OMGSystemException omgWrapper ;
  44. // The arguments passed to ORB_init
  45. private String[] args;
  46. // The ID of the ORB being initialized
  47. private String orbId;
  48. // The CodecFactory
  49. private CodecFactory codecFactory;
  50. // The current stage of initialization
  51. private int stage = STAGE_PRE_INIT;
  52. // The pre-initialization stage (pre_init() being called)
  53. public static final int STAGE_PRE_INIT = 0;
  54. // The post-initialization stage (post_init() being called)
  55. public static final int STAGE_POST_INIT = 1;
  56. // Reject all calls - this object should no longer be around.
  57. public static final int STAGE_CLOSED = 2;
  58. // The description for the OBJECT_NOT_EXIST exception in STAGE_CLOSED
  59. private static final String MESSAGE_ORBINITINFO_INVALID =
  60. "ORBInitInfo object is only valid during ORB_init";
  61. /**
  62. * Creates a new ORBInitInfoImpl object (scoped to package)
  63. *
  64. * @param args The arguments passed to ORB_init.
  65. */
  66. ORBInitInfoImpl( ORB orb, String[] args,
  67. String orbId, CodecFactory codecFactory )
  68. {
  69. this.orb = orb;
  70. wrapper = InterceptorsSystemException.get( orb,
  71. CORBALogDomains.RPC_PROTOCOL ) ;
  72. orbutilWrapper = ORBUtilSystemException.get( orb,
  73. CORBALogDomains.RPC_PROTOCOL ) ;
  74. omgWrapper = OMGSystemException.get( orb,
  75. CORBALogDomains.RPC_PROTOCOL ) ;
  76. this.args = args;
  77. this.orbId = orbId;
  78. this.codecFactory = codecFactory;
  79. }
  80. /** Return the ORB behind this ORBInitInfo. This is defined in the
  81. * ORBInitInfoExt interface.
  82. */
  83. public ORB getORB()
  84. {
  85. return orb ;
  86. }
  87. /**
  88. * Sets the current stage we are in. This limits access to certain
  89. * functionality.
  90. */
  91. void setStage( int stage ) {
  92. this.stage = stage;
  93. }
  94. /**
  95. * Throws an exception if the current stage is STAGE_CLOSED.
  96. * This is called before any method is invoked to ensure that
  97. * no method invocations are attempted after all calls to post_init()
  98. * are completed.
  99. */
  100. private void checkStage() {
  101. if( stage == STAGE_CLOSED ) {
  102. throw wrapper.orbinitinfoInvalid() ;
  103. }
  104. }
  105. /*
  106. *******************************************************************
  107. * The following are implementations of the ORBInitInfo operations.
  108. *******************************************************************/
  109. /**
  110. * This attribute contains the arguments passed to ORB_init. They may
  111. * or may not contain the ORB's arguments
  112. */
  113. public String[] arguments () {
  114. checkStage();
  115. return args;
  116. }
  117. /**
  118. * This attribute is the ID of the ORB being initialized
  119. */
  120. public String orb_id () {
  121. checkStage();
  122. return orbId;
  123. }
  124. /**
  125. * This attribute is the IOP::CodecFactory. The CodecFactory is normally
  126. * obtained via a call to ORB::resolve_initial_references( "CodecFactory" )
  127. * but since the ORB is not yet available and Interceptors, particularly
  128. * when processing service contexts, will require a Codec, a means of
  129. * obtaining a Codec is necessary during ORB intialization.
  130. */
  131. public CodecFactory codec_factory () {
  132. checkStage();
  133. return codecFactory;
  134. }
  135. /**
  136. * See orbos/99-12-02, Chapter 11, Dynamic Initial References on page
  137. * 11-81. This operation is identical to ORB::register_initial_reference
  138. * described there. This same functionality exists here because the ORB,
  139. * not yet fully initialized, is not yet available but initial references
  140. * may need to be registered as part of Interceptor registration.
  141. * <p>
  142. * This method may not be called during post_init.
  143. */
  144. public void register_initial_reference( String id,
  145. org.omg.CORBA.Object obj )
  146. throws InvalidName
  147. {
  148. checkStage();
  149. if( id == null ) nullParam();
  150. // As per CORBA 3.0 section 21.8.1,
  151. // if null is passed as the obj parameter,
  152. // throw BAD_PARAM with minor code OMGSystemException.RIR_WITH_NULL_OBJECT.
  153. // Though the spec is talking about IDL null, we will address both
  154. // Java null and IDL null:
  155. // Note: Local Objects can never be nil!
  156. if( obj == null ) {
  157. throw omgWrapper.rirWithNullObject() ;
  158. }
  159. // This check was made to determine that the objref is a
  160. // non-local objref that is fully
  161. // initialized: this was called only for its side-effects of
  162. // possibly throwing exceptions. However, registering
  163. // local objects should be permitted!
  164. // XXX/Revisit?
  165. // IOR ior = ORBUtility.getIOR( obj ) ;
  166. // Delegate to ORB. If ORB version throws InvalidName, convert to
  167. // equivalent Portable Interceptors InvalidName.
  168. try {
  169. orb.register_initial_reference( id, obj );
  170. } catch( org.omg.CORBA.ORBPackage.InvalidName e ) {
  171. InvalidName exc = new InvalidName( e.getMessage() );
  172. exc.initCause( e ) ;
  173. throw exc ;
  174. }
  175. }
  176. /**
  177. * This operation is only valid during post_init. It is identical to
  178. * ORB::resolve_initial_references. This same functionality exists here
  179. * because the ORB, not yet fully initialized, is not yet available,
  180. * but initial references may be required from the ORB as part
  181. * of Interceptor registration.
  182. * <p>
  183. * (incorporates changes from errata in orbos/00-01-01)
  184. * <p>
  185. * This method may not be called during pre_init.
  186. */
  187. public org.omg.CORBA.Object resolve_initial_references (String id)
  188. throws InvalidName
  189. {
  190. checkStage();
  191. if( id == null ) nullParam();
  192. if( stage == STAGE_PRE_INIT ) {
  193. // Initializer is not allowed to invoke this method during
  194. // this stage.
  195. // _REVISIT_ Spec issue: What exception should really be
  196. // thrown here?
  197. throw wrapper.rirInvalidPreInit() ;
  198. }
  199. org.omg.CORBA.Object objRef = null;
  200. try {
  201. objRef = orb.resolve_initial_references( id );
  202. }
  203. catch( org.omg.CORBA.ORBPackage.InvalidName e ) {
  204. // Convert PIDL to IDL exception:
  205. throw new InvalidName();
  206. }
  207. return objRef;
  208. }
  209. // New method from CORBA 3.1
  210. public void add_client_request_interceptor_with_policy (
  211. ClientRequestInterceptor interceptor, Policy[] policies )
  212. throws DuplicateName
  213. {
  214. // XXX ignore policies for now
  215. add_client_request_interceptor( interceptor ) ;
  216. }
  217. /**
  218. * This operation is used to add a client-side request Interceptor to
  219. * the list of client-side request Interceptors.
  220. * <p>
  221. * If a client-side request Interceptor has already been registered
  222. * with this Interceptor's name, DuplicateName is raised.
  223. */
  224. public void add_client_request_interceptor (
  225. ClientRequestInterceptor interceptor)
  226. throws DuplicateName
  227. {
  228. checkStage();
  229. if( interceptor == null ) nullParam();
  230. orb.getPIHandler().register_interceptor( interceptor,
  231. InterceptorList.INTERCEPTOR_TYPE_CLIENT );
  232. }
  233. // New method from CORBA 3.1
  234. public void add_server_request_interceptor_with_policy (
  235. ServerRequestInterceptor interceptor, Policy[] policies )
  236. throws DuplicateName, PolicyError
  237. {
  238. // XXX ignore policies for now
  239. add_server_request_interceptor( interceptor ) ;
  240. }
  241. /**
  242. * This operation is used to add a server-side request Interceptor to
  243. * the list of server-side request Interceptors.
  244. * <p>
  245. * If a server-side request Interceptor has already been registered
  246. * with this Interceptor's name, DuplicateName is raised.
  247. */
  248. public void add_server_request_interceptor (
  249. ServerRequestInterceptor interceptor)
  250. throws DuplicateName
  251. {
  252. checkStage();
  253. if( interceptor == null ) nullParam();
  254. orb.getPIHandler().register_interceptor( interceptor,
  255. InterceptorList.INTERCEPTOR_TYPE_SERVER );
  256. }
  257. // New method from CORBA 3.1
  258. public void add_ior_interceptor_with_policy (
  259. IORInterceptor interceptor, Policy[] policies )
  260. throws DuplicateName, PolicyError
  261. {
  262. // XXX ignore policies for now
  263. add_ior_interceptor( interceptor ) ;
  264. }
  265. /**
  266. * This operation is used to add an IOR Interceptor to
  267. * the list of IOR Interceptors.
  268. * <p>
  269. * If an IOR Interceptor has already been registered
  270. * with this Interceptor's name, DuplicateName is raised.
  271. */
  272. public void add_ior_interceptor (
  273. IORInterceptor interceptor )
  274. throws DuplicateName
  275. {
  276. checkStage();
  277. if( interceptor == null ) nullParam();
  278. orb.getPIHandler().register_interceptor( interceptor,
  279. InterceptorList.INTERCEPTOR_TYPE_IOR );
  280. }
  281. /**
  282. * A service calls allocate_slot_id to allocate a slot on
  283. * PortableInterceptor::Current.
  284. *
  285. * @return The index to the slot which has been allocated.
  286. */
  287. public int allocate_slot_id () {
  288. checkStage();
  289. return ((PICurrent)orb.getPIHandler().getPICurrent()).allocateSlotId( );
  290. }
  291. /**
  292. * Register a PolicyFactory for the given PolicyType.
  293. * <p>
  294. * If a PolicyFactory already exists for the given PolicyType,
  295. * BAD_INV_ORDER is raised with a minor code of TBD_BIO+2.
  296. */
  297. public void register_policy_factory( int type,
  298. PolicyFactory policy_factory )
  299. {
  300. checkStage();
  301. if( policy_factory == null ) nullParam();
  302. orb.getPIHandler().registerPolicyFactory( type, policy_factory );
  303. }
  304. /**
  305. * Called when an invalid null parameter was passed. Throws a
  306. * BAD_PARAM with a minor code of 1
  307. */
  308. private void nullParam()
  309. throws BAD_PARAM
  310. {
  311. throw orbutilWrapper.nullParam() ;
  312. }
  313. }