PageRenderTime 23ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/MRI-J/corba/src/share/classes/com/sun/corba/se/impl/naming/namingutil/CorbalocURL.java

https://github.com/GregBowyer/ManagedRuntimeInitiative
Java | 288 lines | 140 code | 18 blank | 130 comment | 39 complexity | 54fb60ac9fb6a5fb0a341cbd0bfbab01 MD5 | raw file
  1. /*
  2. * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Sun designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Sun in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22. * CA 95054 USA or visit www.sun.com if you need additional information or
  23. * have any questions.
  24. */
  25. package com.sun.corba.se.impl.naming.namingutil;
  26. import java.util.*;
  27. import com.sun.corba.se.spi.logging.CORBALogDomains ;
  28. import com.sun.corba.se.impl.logging.NamingSystemException ;
  29. /**
  30. * The corbaloc: URL definitions from the -ORBInitDef and -ORBDefaultInitDef's
  31. * will be parsed and converted to this object. This object is capable of
  32. * storing multiple Host profiles as defined in the CorbaLoc grammer.
  33. *
  34. * @author Hemanth
  35. */
  36. public class CorbalocURL extends INSURLBase
  37. {
  38. static NamingSystemException wrapper = NamingSystemException.get(
  39. CORBALogDomains.NAMING_READ ) ;
  40. /**
  41. * This constructor parses the URL and initializes all the variables. Once
  42. * the URL Object is constructed it is immutable. URL parameter is a
  43. * corbaloc: URL string with 'corbaloc:' prefix stripped.
  44. */
  45. public CorbalocURL( String aURL ) {
  46. String url = aURL;
  47. if( url != null ) {
  48. try {
  49. // First Clean the URL Escapes if there are any
  50. url = Utility.cleanEscapes( url );
  51. } catch( Exception e ) {
  52. // There is something wrong with the URL escapes used
  53. // so throw an exception
  54. badAddress( e );
  55. }
  56. int endIndex = url.indexOf( '/' );
  57. if( endIndex == -1 ) {
  58. // If there is no '/' then the endIndex is at the end of the URL
  59. endIndex = url.length();
  60. }
  61. // _REVISIT_: Add a testcase to check 'corbaloc:/'
  62. if( endIndex == 0 ) {
  63. // The url starts with a '/', it's an error
  64. badAddress( null );
  65. }
  66. // Anything between corbaloc: and / is the host,port information
  67. // of the server where the Service Object is located
  68. StringTokenizer endpoints = new StringTokenizer(
  69. url.substring( 0, endIndex ), "," );
  70. // NOTE:
  71. // There should be atleast one token, because there are checks
  72. // to make sure that there is host information before the
  73. // delimiter '/'. So no need to explicitly check for number of
  74. // tokens != 0
  75. while( endpoints.hasMoreTokens( ) ) {
  76. String endpointInfo = endpoints.nextToken();
  77. IIOPEndpointInfo iiopEndpointInfo = null;
  78. if( endpointInfo.startsWith( "iiop:" ) ) {
  79. iiopEndpointInfo = handleIIOPColon( endpointInfo );
  80. } else if( endpointInfo.startsWith( "rir:" ) ) {
  81. handleRIRColon( endpointInfo );
  82. rirFlag = true;
  83. } else if( endpointInfo.startsWith( ":" ) ) {
  84. iiopEndpointInfo = handleColon( endpointInfo );
  85. } else {
  86. // Right now we are not allowing any other protocol
  87. // other than iiop:, rir: so raise exception indicating
  88. // that the URL is malformed
  89. badAddress( null );
  90. }
  91. if ( rirFlag == false ) {
  92. // Add the Host information if RIR flag is set,
  93. // If RIR is set then it means use the internal Boot
  94. // Strap protocol for Key String resolution
  95. if( theEndpointInfo == null ) {
  96. theEndpointInfo = new java.util.ArrayList( );
  97. }
  98. theEndpointInfo.add( iiopEndpointInfo );
  99. }
  100. }
  101. // If there is something after corbaloc:endpointInfo/
  102. // then that is the keyString
  103. if( url.length() > (endIndex + 1) ) {
  104. theKeyString = url.substring( endIndex + 1 );
  105. }
  106. }
  107. }
  108. /**
  109. * A Utility method to throw BAD_PARAM exception to signal malformed
  110. * INS URL.
  111. */
  112. private void badAddress( java.lang.Throwable e )
  113. {
  114. throw wrapper.insBadAddress( e ) ;
  115. }
  116. /**
  117. * If there is 'iiop:' token in the URL, this method will parses
  118. * and validates that host and port information.
  119. */
  120. private IIOPEndpointInfo handleIIOPColon( String iiopInfo )
  121. {
  122. // Check the iiop syntax
  123. iiopInfo = iiopInfo.substring( NamingConstants.IIOP_LENGTH );
  124. return handleColon( iiopInfo );
  125. }
  126. /**
  127. * This is to handle the case of host information with no 'iiop:' prefix.
  128. * instead if ':' is specified then iiop is assumed.
  129. */
  130. private IIOPEndpointInfo handleColon( String iiopInfo ) {
  131. // String after ":"
  132. iiopInfo = iiopInfo.substring( 1 );
  133. String hostandport = iiopInfo;
  134. // The format can be 1.2@<host>:<port>
  135. StringTokenizer tokenizer = new StringTokenizer( iiopInfo, "@" );
  136. IIOPEndpointInfo iiopEndpointInfo = new IIOPEndpointInfo( );
  137. int tokenCount = tokenizer.countTokens( );
  138. // There can be 1 or 2 tokens with '@' as the delimiter
  139. // - if there is only 1 token then there is no GIOP version
  140. // information. A Default GIOP version of 1.2 is used.
  141. // - if there are 2 tokens then there is GIOP version is specified
  142. // - if there are no tokens or more than 2 tokens, then that's an
  143. // error
  144. if( ( tokenCount == 0 )
  145. ||( tokenCount > 2 ))
  146. {
  147. badAddress( null );
  148. }
  149. if( tokenCount == 2 ) {
  150. // There is VersionInformation after iiop:
  151. String version = tokenizer.nextToken( );
  152. int dot = version.indexOf('.');
  153. // There is a version without ., which means
  154. // Malformed list
  155. if (dot == -1) {
  156. badAddress( null );
  157. }
  158. try {
  159. iiopEndpointInfo.setVersion(
  160. Integer.parseInt( version.substring( 0, dot )),
  161. Integer.parseInt( version.substring(dot+1)) );
  162. hostandport = tokenizer.nextToken( );
  163. } catch( Throwable e ) {
  164. badAddress( e );
  165. }
  166. }
  167. try {
  168. // A Hack to differentiate IPV6 address
  169. // from IPV4 address, Current Resolution
  170. // is to use [ ] to differentiate ipv6 host
  171. int squareBracketBeginIndex = hostandport.indexOf ( '[' );
  172. if( squareBracketBeginIndex != -1 ) {
  173. // ipv6Host should be enclosed in
  174. // [ ], if not it will result in a
  175. // BAD_PARAM exception
  176. String ipv6Port = getIPV6Port( hostandport );
  177. if( ipv6Port != null ) {
  178. iiopEndpointInfo.setPort( Integer.parseInt( ipv6Port ));
  179. }
  180. iiopEndpointInfo.setHost( getIPV6Host( hostandport ));
  181. return iiopEndpointInfo;
  182. }
  183. tokenizer = new StringTokenizer( hostandport, ":" );
  184. // There are three possible cases here
  185. // 1. Host and Port is explicitly specified by using ":" as a
  186. // a separator
  187. // 2. Only Host is specified without the port
  188. // 3. HostAndPort info is null
  189. if( tokenizer.countTokens( ) == 2 ) {
  190. // Case 1: There is Host and Port Info
  191. iiopEndpointInfo.setHost( tokenizer.nextToken( ) );
  192. iiopEndpointInfo.setPort( Integer.parseInt(
  193. tokenizer.nextToken( )));
  194. } else {
  195. if( ( hostandport != null )
  196. &&( hostandport.length() != 0 ) )
  197. {
  198. // Case 2: Only Host is specified. iiopEndpointInfo is
  199. // initialized to use the default INS port, if no port is
  200. // specified
  201. iiopEndpointInfo.setHost( hostandport );
  202. }
  203. // Case 3: If no Host and Port info is provided then we use the
  204. // the default LocalHost and INSPort. iiopEndpointInfo is
  205. // already initialized with this info.
  206. }
  207. } catch( Throwable e ) {
  208. // Any kind of Exception is bad here.
  209. // Possible causes: A Number Format exception because port info is
  210. // malformed
  211. badAddress( e );
  212. }
  213. Utility.validateGIOPVersion( iiopEndpointInfo );
  214. return iiopEndpointInfo;
  215. }
  216. /**
  217. * Validate 'rir:' case.
  218. */
  219. private void handleRIRColon( String rirInfo )
  220. {
  221. if( rirInfo.length() != NamingConstants.RIRCOLON_LENGTH ) {
  222. badAddress( null );
  223. }
  224. }
  225. /**
  226. * Returns an IPV6 Port that is after [<ipv6>]:. There is no validation
  227. * done here, if it is an incorrect port then the request through
  228. * this URL results in a COMM_FAILURE, otherwise malformed list will
  229. * result in BAD_PARAM exception thrown in checkcorbalocGrammer.
  230. */
  231. private String getIPV6Port( String endpointInfo )
  232. {
  233. int squareBracketEndIndex = endpointInfo.indexOf ( ']' );
  234. // If there is port information, then it has to be after ] bracket
  235. // indexOf returns the count from the index of zero as the base, so
  236. // equality check requires squareBracketEndIndex + 1.
  237. if( (squareBracketEndIndex + 1) != (endpointInfo.length( )) ) {
  238. if( endpointInfo.charAt( squareBracketEndIndex + 1 ) != ':' ) {
  239. throw new RuntimeException(
  240. "Host and Port is not separated by ':'" );
  241. }
  242. // PortInformation should be after ']:' delimiter
  243. // If there is an exception then it will be caught in
  244. // checkcorbaGrammer method and rethrown as BAD_PARAM
  245. return endpointInfo.substring( squareBracketEndIndex + 2 );
  246. }
  247. return null;
  248. }
  249. /**
  250. * Returns an IPV6 Host that is inside [ ] tokens. There is no validation
  251. * done here, if it is an incorrect IPV6 address then the request through
  252. * this URL results in a COMM_FAILURE, otherwise malformed list will
  253. * result in BAD_PARAM exception thrown in checkcorbalocGrammer.
  254. */
  255. private String getIPV6Host( String endpointInfo ) {
  256. // ipv6Host should be enclosed in
  257. // [ ], if not it will result in a
  258. // BAD_PARAM exception
  259. int squareBracketEndIndex = endpointInfo.indexOf ( ']' );
  260. // get the host between [ ]
  261. String ipv6Host = endpointInfo.substring( 1, squareBracketEndIndex );
  262. return ipv6Host;
  263. }
  264. /**
  265. * Will be true only in CorbanameURL class.
  266. */
  267. public boolean isCorbanameURL( ) {
  268. return false;
  269. }
  270. }