KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > resolver > INSURLOperationImpl


1 /*
2  * @(#)INSURLOperationImpl.java 1.88 06/09/28
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.resolver;
9
10 import java.util.List JavaDoc ;
11 import java.util.Map JavaDoc ;
12 import java.util.Comparator JavaDoc ;
13 import java.util.Iterator JavaDoc ;
14 import java.util.HashMap JavaDoc ;
15 import java.util.ArrayList JavaDoc ;
16 import java.util.Collections JavaDoc ;
17
18 import org.omg.CosNaming.NamingContextExt JavaDoc ;
19 import org.omg.CosNaming.NamingContextExtHelper JavaDoc ;
20
21 import com.sun.corba.se.spi.ior.IOR;
22 import com.sun.corba.se.spi.ior.IORTemplate;
23 import com.sun.corba.se.spi.ior.ObjectKey;
24 import com.sun.corba.se.spi.ior.IORFactories;
25 import com.sun.corba.se.spi.ior.ObjectKeyFactory ;
26 import com.sun.corba.se.spi.ior.iiop.IIOPAddress;
27 import com.sun.corba.se.spi.ior.iiop.IIOPProfile ;
28 import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;
29 import com.sun.corba.se.spi.ior.iiop.IIOPFactories ;
30 import com.sun.corba.se.spi.ior.iiop.GIOPVersion;
31 import com.sun.corba.se.spi.ior.iiop.AlternateIIOPAddressComponent;
32 import com.sun.corba.se.spi.logging.CORBALogDomains ;
33 import com.sun.corba.se.spi.orb.Operation;
34 import com.sun.corba.se.spi.orb.ORB;
35 import com.sun.corba.se.spi.resolver.Resolver;
36
37 import com.sun.corba.se.impl.encoding.EncapsInputStream;
38 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
39 import com.sun.corba.se.impl.logging.OMGSystemException ;
40 import com.sun.corba.se.impl.naming.namingutil.INSURLHandler;
41 import com.sun.corba.se.impl.naming.namingutil.IIOPEndpointInfo;
42 import com.sun.corba.se.impl.naming.namingutil.INSURL;
43 import com.sun.corba.se.impl.naming.namingutil.CorbalocURL;
44 import com.sun.corba.se.impl.naming.namingutil.CorbanameURL;
45 import com.sun.corba.se.impl.orbutil.ORBConstants;
46 import com.sun.corba.se.impl.orbutil.ORBUtility;
47
48 /**
49  * This class provides an Operation that converts from CORBA INS URL strings into
50  * CORBA object references. It will eventually become extensible, but for now it
51  * simply encapsulates the existing implementation. Once the full extensibility
52  * is in place, we want this operation to convert string to INSURL, which has mainly
53  * a public resolver method that returns an object reference.
54  *
55  * @author Hemanth
56  * @author Ken
57  */

58 public class INSURLOperationImpl implements Operation
59 {
60     ORB orb;
61     ORBUtilSystemException wrapper ;
62     OMGSystemException omgWrapper ;
63     Resolver bootstrapResolver ;
64
65     // Root Naming Context for default resolution of names.
66
private NamingContextExt JavaDoc rootNamingContextExt;
67     private Object JavaDoc rootContextCacheLock = new Object JavaDoc() ;
68
69     // The URLHandler to parse INS URL's
70
private INSURLHandler insURLHandler = INSURLHandler.getINSURLHandler() ;
71
72     public INSURLOperationImpl( ORB orb, Resolver bootstrapResolver )
73     {
74     this.orb = orb ;
75     wrapper = ORBUtilSystemException.get( orb,
76         CORBALogDomains.ORB_RESOLVER ) ;
77     omgWrapper = OMGSystemException.get( orb,
78         CORBALogDomains.ORB_RESOLVER ) ;
79     this.bootstrapResolver = bootstrapResolver ;
80     }
81
82     private static final int NIBBLES_PER_BYTE = 2 ;
83     private static final int UN_SHIFT = 4 ; // "UPPER NIBBLE" shift factor for <<
84

85     /** This static method takes a Stringified IOR and converts it into IOR object.
86       * It is the caller's responsibility to only pass strings that start with "IOR:".
87       */

88     private org.omg.CORBA.Object JavaDoc getIORFromString( String JavaDoc str )
89     {
90     // Length must be even for str to be valid
91
if ( (str.length() & 1) == 1 )
92         throw wrapper.badStringifiedIorLen() ;
93
94     byte[] buf = new byte[(str.length() - ORBConstants.STRINGIFY_PREFIX.length()) / NIBBLES_PER_BYTE];
95     for (int i=ORBConstants.STRINGIFY_PREFIX.length(), j=0; i < str.length(); i +=NIBBLES_PER_BYTE, j++) {
96          buf[j] = (byte)((ORBUtility.hexOf(str.charAt(i)) << UN_SHIFT) & 0xF0);
97          buf[j] |= (byte)(ORBUtility.hexOf(str.charAt(i+1)) & 0x0F);
98     }
99     EncapsInputStream s = new EncapsInputStream(orb, buf, buf.length,
100         orb.getORBData().getGIOPVersion());
101     s.consumeEndian();
102     return s.read_Object() ;
103     }
104
105     public Object JavaDoc operate( Object JavaDoc arg )
106     {
107     if (arg instanceof String JavaDoc) {
108         String JavaDoc str = (String JavaDoc)arg ;
109
110         if (str.startsWith( ORBConstants.STRINGIFY_PREFIX ))
111         // XXX handle this as just another URL scheme
112
return getIORFromString( str ) ;
113         else {
114         INSURL insURL = insURLHandler.parseURL( str ) ;
115         if (insURL == null)
116             throw omgWrapper.soBadSchemeName() ;
117         return resolveINSURL( insURL ) ;
118         }
119     }
120
121     throw wrapper.stringExpected() ;
122     }
123
124     private org.omg.CORBA.Object JavaDoc resolveINSURL( INSURL theURLObject ) {
125     // XXX resolve should be a method on INSURL
126
if( theURLObject.isCorbanameURL() ) {
127             return resolveCorbaname( (CorbanameURL)theURLObject );
128         } else {
129             return resolveCorbaloc( (CorbalocURL)theURLObject );
130         }
131     }
132       
133     /**
134      * resolves a corbaloc: url that is encapsulated in a CorbalocURL object.
135      *
136      * @return the CORBA.Object if resolution is successful
137      */

138     private org.omg.CORBA.Object JavaDoc resolveCorbaloc(
139         CorbalocURL theCorbaLocObject )
140     {
141         org.omg.CORBA.Object JavaDoc result = null;
142         // If RIR flag is true use the Bootstrap protocol
143
if( theCorbaLocObject.getRIRFlag( ) ) {
144             result = bootstrapResolver.resolve(theCorbaLocObject.getKeyString());
145     } else {
146         result = getIORUsingCorbaloc( theCorbaLocObject );
147     }
148
149         return result;
150     }
151
152     /**
153      * resolves a corbaname: url that is encapsulated in a CorbanameURL object.
154      *
155      * @return the CORBA.Object if resolution is successful
156      */

157     private org.omg.CORBA.Object JavaDoc resolveCorbaname( CorbanameURL theCorbaName ) {
158         org.omg.CORBA.Object JavaDoc result = null;
159
160         try {
161             NamingContextExt JavaDoc theNamingContext = null;
162
163             if( theCorbaName.getRIRFlag( ) ) {
164                 // Case 1 of corbaname: rir#
165
theNamingContext = getDefaultRootNamingContext( );
166             } else {
167                 // Case 2 of corbaname: ::hostname#
168
org.omg.CORBA.Object JavaDoc corbalocResult =
169                     getIORUsingCorbaloc( theCorbaName );
170                 if( corbalocResult == null ) {
171                     return null;
172                 }
173
174                 theNamingContext =
175                     NamingContextExtHelper.narrow( corbalocResult );
176             }
177
178             String JavaDoc StringifiedName = theCorbaName.getStringifiedName( );
179
180             if( StringifiedName == null ) {
181                 // This means return the Root Naming context
182
return theNamingContext;
183             } else {
184             return theNamingContext.resolve_str( StringifiedName );
185             }
186         } catch( Exception JavaDoc e ) {
187             clearRootNamingContextCache( );
188             return null;
189         }
190      }
191
192     /**
193      * This is an internal method to get the IOR from the CorbalocURL object.
194      *
195      * @return the CORBA.Object if resolution is successful
196      */

197      private org.omg.CORBA.Object JavaDoc getIORUsingCorbaloc( INSURL corbalocObject )
198      {
199         Map JavaDoc profileMap = new HashMap JavaDoc();
200         List JavaDoc profileList1_0 = new ArrayList JavaDoc();
201                                                                                 
202         // corbalocObject cannot be null, because it's validated during
203
// parsing. So no null check is required.
204
java.util.List JavaDoc theEndpointInfo = corbalocObject.getEndpointInfo();
205         String JavaDoc theKeyString = corbalocObject.getKeyString();
206         // If there is no KeyString then it's invalid
207
if( theKeyString == null ) {
208             return null;
209         }
210                                                                                 
211         ObjectKey key = orb.getObjectKeyFactory().create(
212             theKeyString.getBytes() );
213         IORTemplate iortemp = IORFactories.makeIORTemplate( key.getTemplate() );
214         java.util.Iterator JavaDoc iterator = theEndpointInfo.iterator( );
215         while( iterator.hasNext( ) ) {
216             IIOPEndpointInfo element =
217                 (IIOPEndpointInfo) iterator.next( );
218             IIOPAddress addr = IIOPFactories.makeIIOPAddress( orb, element.getHost(),
219                 element.getPort() );
220         GIOPVersion giopVersion = GIOPVersion.getInstance( (byte)element.getMajor(),
221                          (byte)element.getMinor());
222         IIOPProfileTemplate profileTemplate = null;
223         if (giopVersion.equals(GIOPVersion.V1_0)) {
224         profileTemplate = IIOPFactories.makeIIOPProfileTemplate(
225             orb, giopVersion, addr);
226         profileList1_0.add(profileTemplate);
227         } else {
228         if (profileMap.get(giopVersion) == null) {
229             profileTemplate = IIOPFactories.makeIIOPProfileTemplate(
230                 orb, giopVersion, addr);
231             profileMap.put(giopVersion, profileTemplate);
232         } else {
233             profileTemplate = (IIOPProfileTemplate)profileMap.get(giopVersion);
234             AlternateIIOPAddressComponent iiopAddressComponent =
235                 IIOPFactories.makeAlternateIIOPAddressComponent(addr);
236             profileTemplate.add(iiopAddressComponent);
237         }
238         }
239     }
240
241     GIOPVersion giopVersion = orb.getORBData().getGIOPVersion();
242     IIOPProfileTemplate pTemplate = (IIOPProfileTemplate)profileMap.get(giopVersion);
243     if (pTemplate != null) {
244         iortemp.add(pTemplate); // Add profile for GIOP version used by this ORB
245
profileMap.remove(giopVersion); // Now remove this value from the map
246
}
247
248     // Create a comparator that can sort in decending order (1.2, 1.1, ...)
249
Comparator JavaDoc comp = new Comparator JavaDoc() {
250         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
251         GIOPVersion gv1 = (GIOPVersion)o1;
252         GIOPVersion gv2 = (GIOPVersion)o2;
253         return (gv1.lessThan(gv2) ? 1 : (gv1.equals(gv2) ? 0 : -1));
254         };
255     };
256
257     // Now sort using the above comparator
258
List JavaDoc list = new ArrayList JavaDoc(profileMap.keySet());
259     Collections.sort(list, comp);
260
261     // Add the profiles in the sorted order
262
Iterator JavaDoc iter = list.iterator();
263     while (iter.hasNext()) {
264         IIOPProfileTemplate pt = (IIOPProfileTemplate)profileMap.get(iter.next());
265         iortemp.add(pt);
266     }
267
268     // Finally add the 1.0 profiles
269
iortemp.addAll(profileList1_0);
270
271     IOR ior = iortemp.makeIOR( orb, "", key.getId() ) ;
272     return ORBUtility.makeObjectReference( ior ) ;
273     }
274
275     /**
276      * This is required for corbaname: resolution. Currently we
277      * are not caching RootNamingContext as the reference to rootNamingContext
278      * may not be Persistent in all the implementations.
279      * _REVISIT_ to clear the rootNamingContext in case of COMM_FAILURE.
280      *
281      * @return the org.omg.COSNaming.NamingContextExt if resolution is
282      * successful
283      *
284      */

285     private NamingContextExt JavaDoc getDefaultRootNamingContext( ) {
286         synchronized( rootContextCacheLock ) {
287         if( rootNamingContextExt == null ) {
288             try {
289                 rootNamingContextExt =
290                 NamingContextExtHelper.narrow(
291                 orb.getLocalResolver().resolve( "NameService" ) );
292             } catch( Exception JavaDoc e ) {
293                 rootNamingContextExt = null;
294             }
295             }
296         }
297     return rootNamingContextExt;
298     }
299
300     /**
301      * A utility method to clear the RootNamingContext, if there is an
302      * exception in resolving CosNaming:Name from the RootNamingContext,
303      */

304     private void clearRootNamingContextCache( ) {
305         synchronized( rootContextCacheLock ) {
306             rootNamingContextExt = null;
307         }
308     }
309 }
310
Popular Tags