KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > naming > namingutil > CorbalocURL


1 /*
2  * @(#)CorbalocURL.java 1.7 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
8 package com.sun.corba.se.impl.naming.namingutil;
9
10 import java.util.*;
11
12 import com.sun.corba.se.spi.logging.CORBALogDomains ;
13 import com.sun.corba.se.impl.logging.NamingSystemException ;
14
15 /**
16  * The corbaloc: URL definitions from the -ORBInitDef and -ORBDefaultInitDef's
17  * will be parsed and converted to this object. This object is capable of
18  * storing multiple Host profiles as defined in the CorbaLoc grammer.
19  *
20  * @author Hemanth
21  */

22 public class CorbalocURL extends INSURLBase
23 {
24     static NamingSystemException wrapper = NamingSystemException.get(
25     CORBALogDomains.NAMING_READ ) ;
26
27     /**
28      * This constructor parses the URL and initializes all the variables. Once
29      * the URL Object is constructed it is immutable. URL parameter is a
30      * corbaloc: URL string with 'corbaloc:' prefix stripped.
31      */

32     public CorbalocURL( String JavaDoc aURL ) {
33         String JavaDoc url = aURL;
34
35         if( url != null ) {
36             try {
37                 // First Clean the URL Escapes if there are any
38
url = Utility.cleanEscapes( url );
39             } catch( Exception JavaDoc e ) {
40                 // There is something wrong with the URL escapes used
41
// so throw an exception
42
badAddress( e );
43             }
44             int endIndex = url.indexOf( '/' );
45             if( endIndex == -1 ) {
46                 // If there is no '/' then the endIndex is at the end of the URL
47
endIndex = url.length();
48             }
49             // _REVISIT_: Add a testcase to check 'corbaloc:/'
50
if( endIndex == 0 ) {
51                 // The url starts with a '/', it's an error
52
badAddress( null );
53             }
54             // Anything between corbaloc: and / is the host,port information
55
// of the server where the Service Object is located
56
StringTokenizer endpoints = new StringTokenizer(
57                 url.substring( 0, endIndex ), "," );
58             // NOTE:
59
// There should be atleast one token, because there are checks
60
// to make sure that there is host information before the
61
// delimiter '/'. So no need to explicitly check for number of
62
// tokens != 0
63
while( endpoints.hasMoreTokens( ) ) {
64                 String JavaDoc endpointInfo = endpoints.nextToken();
65                 IIOPEndpointInfo iiopEndpointInfo = null;
66                 if( endpointInfo.startsWith( "iiop:" ) ) {
67                     iiopEndpointInfo = handleIIOPColon( endpointInfo );
68                 } else if( endpointInfo.startsWith( "rir:" ) ) {
69                     handleRIRColon( endpointInfo );
70                     rirFlag = true;
71                 } else if( endpointInfo.startsWith( ":" ) ) {
72                     iiopEndpointInfo = handleColon( endpointInfo );
73                 } else {
74                     // Right now we are not allowing any other protocol
75
// other than iiop:, rir: so raise exception indicating
76
// that the URL is malformed
77
badAddress( null );
78                 }
79                 if ( rirFlag == false ) {
80                     // Add the Host information if RIR flag is set,
81
// If RIR is set then it means use the internal Boot
82
// Strap protocol for Key String resolution
83
if( theEndpointInfo == null ) {
84                         theEndpointInfo = new java.util.ArrayList JavaDoc( );
85                     }
86                     theEndpointInfo.add( iiopEndpointInfo );
87                 }
88             }
89             // If there is something after corbaloc:endpointInfo/
90
// then that is the keyString
91
if( url.length() > (endIndex + 1) ) {
92                 theKeyString = url.substring( endIndex + 1 );
93             }
94         }
95     }
96
97
98     /**
99      * A Utility method to throw BAD_PARAM exception to signal malformed
100      * INS URL.
101      */

102     private void badAddress( java.lang.Throwable JavaDoc e )
103     {
104     throw wrapper.insBadAddress( e ) ;
105     }
106
107     /**
108      * If there is 'iiop:' token in the URL, this method will parses
109      * and validates that host and port information.
110      */

111     private IIOPEndpointInfo handleIIOPColon( String JavaDoc iiopInfo )
112     {
113          // Check the iiop syntax
114
iiopInfo = iiopInfo.substring( NamingConstants.IIOP_LENGTH );
115          return handleColon( iiopInfo );
116     }
117
118
119     /**
120      * This is to handle the case of host information with no 'iiop:' prefix.
121      * instead if ':' is specified then iiop is assumed.
122      */

123     private IIOPEndpointInfo handleColon( String JavaDoc iiopInfo ) {
124          // String after ":"
125
iiopInfo = iiopInfo.substring( 1 );
126          String JavaDoc hostandport = iiopInfo;
127          // The format can be 1.2@<host>:<port>
128
StringTokenizer tokenizer = new StringTokenizer( iiopInfo, "@" );
129          IIOPEndpointInfo iiopEndpointInfo = new IIOPEndpointInfo( );
130          int tokenCount = tokenizer.countTokens( );
131          // There can be 1 or 2 tokens with '@' as the delimiter
132
// - if there is only 1 token then there is no GIOP version
133
// information. A Default GIOP version of 1.2 is used.
134
// - if there are 2 tokens then there is GIOP version is specified
135
// - if there are no tokens or more than 2 tokens, then that's an
136
// error
137
if( ( tokenCount == 0 )
138            ||( tokenCount > 2 ))
139          {
140              badAddress( null );
141          }
142          if( tokenCount == 2 ) {
143             // There is VersionInformation after iiop:
144
String JavaDoc version = tokenizer.nextToken( );
145             int dot = version.indexOf('.');
146             // There is a version without ., which means
147
// Malformed list
148
if (dot == -1) {
149                 badAddress( null );
150             }
151             try {
152                 iiopEndpointInfo.setVersion(
153                     Integer.parseInt( version.substring( 0, dot )),
154                     Integer.parseInt( version.substring(dot+1)) );
155                 hostandport = tokenizer.nextToken( );
156             } catch( Throwable JavaDoc e ) {
157                 badAddress( e );
158             }
159          }
160          try {
161            // A Hack to differentiate IPV6 address
162
// from IPV4 address, Current Resolution
163
// is to use [ ] to differentiate ipv6 host
164
int squareBracketBeginIndex = hostandport.indexOf ( '[' );
165            if( squareBracketBeginIndex != -1 ) {
166                // ipv6Host should be enclosed in
167
// [ ], if not it will result in a
168
// BAD_PARAM exception
169
String JavaDoc ipv6Port = getIPV6Port( hostandport );
170                if( ipv6Port != null ) {
171                    iiopEndpointInfo.setPort( Integer.parseInt( ipv6Port ));
172                }
173                iiopEndpointInfo.setHost( getIPV6Host( hostandport ));
174                return iiopEndpointInfo;
175            }
176            tokenizer = new StringTokenizer( hostandport, ":" );
177            // There are three possible cases here
178
// 1. Host and Port is explicitly specified by using ":" as a
179
// a separator
180
// 2. Only Host is specified without the port
181
// 3. HostAndPort info is null
182
if( tokenizer.countTokens( ) == 2 ) {
183                // Case 1: There is Host and Port Info
184
iiopEndpointInfo.setHost( tokenizer.nextToken( ) );
185                iiopEndpointInfo.setPort( Integer.parseInt(
186                    tokenizer.nextToken( )));
187            } else {
188                if( ( hostandport != null )
189                  &&( hostandport.length() != 0 ) )
190                {
191                    // Case 2: Only Host is specified. iiopEndpointInfo is
192
// initialized to use the default INS port, if no port is
193
// specified
194
iiopEndpointInfo.setHost( hostandport );
195                }
196                // Case 3: If no Host and Port info is provided then we use the
197
// the default LocalHost and INSPort. iiopEndpointInfo is
198
// already initialized with this info.
199
}
200        } catch( Throwable JavaDoc e ) {
201            // Any kind of Exception is bad here.
202
// Possible causes: A Number Format exception because port info is
203
// malformed
204
badAddress( e );
205        }
206        Utility.validateGIOPVersion( iiopEndpointInfo );
207        return iiopEndpointInfo;
208     }
209
210     /**
211      * Validate 'rir:' case.
212      */

213     private void handleRIRColon( String JavaDoc rirInfo )
214     {
215         if( rirInfo.length() != NamingConstants.RIRCOLON_LENGTH ) {
216             badAddress( null );
217         }
218     }
219
220     /**
221       * Returns an IPV6 Port that is after [<ipv6>]:. There is no validation
222       * done here, if it is an incorrect port then the request through
223       * this URL results in a COMM_FAILURE, otherwise malformed list will
224       * result in BAD_PARAM exception thrown in checkcorbalocGrammer.
225       */

226      private String JavaDoc getIPV6Port( String JavaDoc endpointInfo )
227      {
228          int squareBracketEndIndex = endpointInfo.indexOf ( ']' );
229          // If there is port information, then it has to be after ] bracket
230
// indexOf returns the count from the index of zero as the base, so
231
// equality check requires squareBracketEndIndex + 1.
232
if( (squareBracketEndIndex + 1) != (endpointInfo.length( )) ) {
233              if( endpointInfo.charAt( squareBracketEndIndex + 1 ) != ':' ) {
234                   throw new RuntimeException JavaDoc(
235                       "Host and Port is not separated by ':'" );
236              }
237              // PortInformation should be after ']:' delimiter
238
// If there is an exception then it will be caught in
239
// checkcorbaGrammer method and rethrown as BAD_PARAM
240
return endpointInfo.substring( squareBracketEndIndex + 2 );
241          }
242          return null;
243      }
244
245
246      /**
247       * Returns an IPV6 Host that is inside [ ] tokens. There is no validation
248       * done here, if it is an incorrect IPV6 address then the request through
249       * this URL results in a COMM_FAILURE, otherwise malformed list will
250       * result in BAD_PARAM exception thrown in checkcorbalocGrammer.
251       */

252      private String JavaDoc getIPV6Host( String JavaDoc endpointInfo ) {
253           // ipv6Host should be enclosed in
254
// [ ], if not it will result in a
255
// BAD_PARAM exception
256
int squareBracketEndIndex = endpointInfo.indexOf ( ']' );
257           // get the host between [ ]
258
String JavaDoc ipv6Host = endpointInfo.substring( 1, squareBracketEndIndex );
259           return ipv6Host;
260     }
261
262     /**
263      * Will be true only in CorbanameURL class.
264      */

265     public boolean isCorbanameURL( ) {
266         return false;
267     }
268
269
270 }
271
Popular Tags