1. /*
  2. * @(#)IORInfoImpl.java 1.23 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 java.util.Iterator ;
  9. import org.omg.IOP.TaggedComponent;
  10. import org.omg.CORBA.BAD_INV_ORDER;
  11. import org.omg.CORBA.BAD_PARAM;
  12. import org.omg.CORBA.INTERNAL;
  13. import org.omg.CORBA.CompletionStatus;
  14. import org.omg.CORBA.INV_POLICY;
  15. import org.omg.CORBA.NO_IMPLEMENT;
  16. import org.omg.CORBA.Policy;
  17. import org.omg.CORBA.LocalObject;
  18. import org.omg.PortableInterceptor.IORInfo;
  19. import org.omg.PortableInterceptor.ObjectReferenceTemplate;
  20. import org.omg.PortableInterceptor.ObjectReferenceFactory;
  21. import com.sun.corba.se.spi.orb.ORB ;
  22. import com.sun.corba.se.spi.oa.ObjectAdapter;
  23. import com.sun.corba.se.spi.legacy.interceptor.IORInfoExt;
  24. import com.sun.corba.se.spi.legacy.interceptor.UnknownType;
  25. import com.sun.corba.se.spi.ior.IORTemplate;
  26. import com.sun.corba.se.spi.ior.TaggedProfileTemplate;
  27. import com.sun.corba.se.spi.ior.TaggedComponentFactoryFinder ;
  28. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  29. import com.sun.corba.se.impl.logging.InterceptorsSystemException ;
  30. import com.sun.corba.se.impl.logging.OMGSystemException ;
  31. import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  32. /**
  33. * IORInfoImpl is the implementation of the IORInfo class, as described
  34. * in orbos/99-12-02, section 7.
  35. */
  36. public final class IORInfoImpl
  37. extends LocalObject
  38. implements IORInfo, IORInfoExt
  39. {
  40. // State values that determine which methods are allowed.
  41. // get_effective_policy, manager_id, and adapter_state are valid unless STATE_DONE
  42. // add_component, and add_component_to_profile are valid.
  43. private static final int STATE_INITIAL = 0 ;
  44. // adapter_template, and R/W to current_factory are valid
  45. private static final int STATE_ESTABLISHED = 1 ;
  46. // No methods are valid in this state
  47. private static final int STATE_DONE = 2 ;
  48. // The current state of this object
  49. private int state = STATE_INITIAL ;
  50. // The ObjectAdapter associated with this IORInfo object.
  51. private ObjectAdapter adapter;
  52. private ORB orb ;
  53. private ORBUtilSystemException orbutilWrapper ;
  54. private InterceptorsSystemException wrapper ;
  55. private OMGSystemException omgWrapper ;
  56. /**
  57. * Creates a new IORInfo implementation. This info object will establish
  58. * tagged components with the template for the provided IOR Template.
  59. */
  60. IORInfoImpl( ObjectAdapter adapter ) {
  61. this.orb = adapter.getORB() ;
  62. orbutilWrapper = ORBUtilSystemException.get( orb,
  63. CORBALogDomains.RPC_PROTOCOL ) ;
  64. wrapper = InterceptorsSystemException.get( orb,
  65. CORBALogDomains.RPC_PROTOCOL ) ;
  66. omgWrapper = OMGSystemException.get( orb,
  67. CORBALogDomains.RPC_PROTOCOL ) ;
  68. this.adapter = adapter;
  69. }
  70. /**
  71. * An ORB service implementation may determine what server side policy
  72. * of a particular type is in effect for an IOR being constructed by
  73. * calling the get_effective_policy operation. When the IOR being
  74. * constructed is for an object implemented using a POA, all Policy
  75. * objects passed to the PortableServer::POA::create_POA call that
  76. * created that POA are accessible via get_effective_policy.
  77. * <p>
  78. * If a policy for the given type is not known to the ORB, then this
  79. * operation will raise INV_POLICY with a standard minor code of 2.
  80. *
  81. * @param type The CORBA::PolicyType specifying the type of policy to
  82. * return.
  83. * @return The effective CORBA::Policy object of the requested type.
  84. * If the given policy type is known, but no policy of that tpye is
  85. * in effect, then this operation will return a nil object reference.
  86. */
  87. public Policy get_effective_policy (int type) {
  88. checkState( STATE_INITIAL, STATE_ESTABLISHED ) ;
  89. return adapter.getEffectivePolicy( type );
  90. }
  91. /**
  92. * A portable ORB service implementation calls this method from its
  93. * implementation of establish_components to add a tagged component to
  94. * the set which will be included when constructing IORs. The
  95. * components in this set will be included in all profiles.
  96. * <p>
  97. * Any number of components may exist with the same component ID.
  98. *
  99. * @param tagged_component The IOP::TaggedComponent to add
  100. */
  101. public void add_ior_component (TaggedComponent tagged_component) {
  102. checkState( STATE_INITIAL ) ;
  103. if( tagged_component == null ) nullParam();
  104. addIORComponentToProfileInternal( tagged_component,
  105. adapter.getIORTemplate().iterator());
  106. }
  107. /**
  108. * A portable ORB service implementation calls this method from its
  109. * implementation of establish_components to add a tagged component to
  110. * the set which will be included when constructing IORs. The
  111. * components in this set will be included in the specified profile.
  112. * <p>
  113. * Any number of components may exist with the same component ID.
  114. * <p>
  115. * If the given profile ID does not define a known profile or it is
  116. * impossible to add components to thgat profile, BAD_PARAM is raised
  117. * with a minor code of TBD_BP + 3.
  118. *
  119. * @param tagged_component The IOP::TaggedComponent to add.
  120. * @param profile_id The IOP::ProfileId tof the profile to which this
  121. * component will be added.
  122. */
  123. public void add_ior_component_to_profile (
  124. TaggedComponent tagged_component, int profile_id )
  125. {
  126. checkState( STATE_INITIAL ) ;
  127. if( tagged_component == null ) nullParam();
  128. addIORComponentToProfileInternal(
  129. tagged_component, adapter.getIORTemplate().iteratorById(
  130. profile_id ) );
  131. }
  132. /**
  133. * @param type The type of the server port
  134. * (see connection.ORBSocketFactory for discussion).
  135. * @return The listen port number for that type.
  136. * @throws UnknownType if no port of the given type is found.
  137. */
  138. public int getServerPort(String type)
  139. throws UnknownType
  140. {
  141. checkState( STATE_INITIAL, STATE_ESTABLISHED ) ;
  142. int port =
  143. orb.getLegacyServerSocketManager()
  144. .legacyGetTransientOrPersistentServerPort(type);
  145. if (port == -1) {
  146. throw new UnknownType();
  147. }
  148. return port;
  149. }
  150. public ObjectAdapter getObjectAdapter()
  151. {
  152. return adapter;
  153. }
  154. public int manager_id()
  155. {
  156. checkState( STATE_INITIAL, STATE_ESTABLISHED) ;
  157. return adapter.getManagerId() ;
  158. }
  159. public short state()
  160. {
  161. checkState( STATE_INITIAL, STATE_ESTABLISHED) ;
  162. return adapter.getState() ;
  163. }
  164. public ObjectReferenceTemplate adapter_template()
  165. {
  166. checkState( STATE_ESTABLISHED) ;
  167. // At this point, the iortemp must contain only a single
  168. // IIOPProfileTemplate. This is a restriction of our
  169. // implementation. Also, note the the ObjectReferenceTemplate
  170. // is called when a certain POA is created in a certain ORB
  171. // in a certain server, so the server_id, orb_id, and
  172. // poa_id operations must be well-defined no matter what
  173. // kind of implementation is used: e.g., if a POA creates
  174. // IORs with multiple profiles, they must still all agree
  175. // about this information. Thus, we are justified in
  176. // extracting the single IIOPProfileTemplate to create
  177. // an ObjectReferenceTemplate here.
  178. return adapter.getAdapterTemplate() ;
  179. }
  180. public ObjectReferenceFactory current_factory()
  181. {
  182. checkState( STATE_ESTABLISHED) ;
  183. return adapter.getCurrentFactory() ;
  184. }
  185. public void current_factory( ObjectReferenceFactory factory )
  186. {
  187. checkState( STATE_ESTABLISHED) ;
  188. adapter.setCurrentFactory( factory ) ;
  189. }
  190. /**
  191. * Internal utility method to add an IOR component to the set of profiles
  192. * present in the iterator.
  193. */
  194. private void addIORComponentToProfileInternal(
  195. TaggedComponent tagged_component, Iterator iterator )
  196. {
  197. // Convert the given IOP::TaggedComponent into the appropriate
  198. // type for the TaggedProfileTemplate
  199. TaggedComponentFactoryFinder finder =
  200. orb.getTaggedComponentFactoryFinder();
  201. Object newTaggedComponent = finder.create( orb, tagged_component );
  202. // Iterate through TaggedProfileTemplates and add the given tagged
  203. // component to the appropriate one(s).
  204. boolean found = false;
  205. while( iterator.hasNext() ) {
  206. found = true;
  207. TaggedProfileTemplate taggedProfileTemplate =
  208. (TaggedProfileTemplate)iterator.next();
  209. taggedProfileTemplate.add( newTaggedComponent );
  210. }
  211. // If no profile was found with the given id, throw a BAD_PARAM:
  212. // (See orbos/00-08-06, section 21.5.3.3.)
  213. if( !found ) {
  214. throw omgWrapper.invalidProfileId() ;
  215. }
  216. }
  217. /**
  218. * Called when an invalid null parameter was passed. Throws a
  219. * BAD_PARAM with a minor code of 1
  220. */
  221. private void nullParam()
  222. {
  223. throw orbutilWrapper.nullParam() ;
  224. }
  225. // REVISIT: add minor codes!
  226. private void checkState( int expectedState )
  227. {
  228. if (expectedState != state)
  229. throw wrapper.badState1( new Integer(expectedState), new Integer(state) ) ;
  230. }
  231. private void checkState( int expectedState1, int expectedState2 )
  232. {
  233. if ((expectedState1 != state) && (expectedState2 != state))
  234. throw wrapper.badState2( new Integer(expectedState1),
  235. new Integer(expectedState2), new Integer(state) ) ;
  236. }
  237. void makeStateEstablished()
  238. {
  239. checkState( STATE_INITIAL ) ;
  240. state = STATE_ESTABLISHED ;
  241. }
  242. void makeStateDone()
  243. {
  244. checkState( STATE_ESTABLISHED ) ;
  245. state = STATE_DONE ;
  246. }
  247. }