1 7 8 package com.sun.corba.se.impl.resolver; 9 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.Comparator ; 13 import java.util.Iterator ; 14 import java.util.HashMap ; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 18 import org.omg.CosNaming.NamingContextExt ; 19 import org.omg.CosNaming.NamingContextExtHelper ; 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 58 public class INSURLOperationImpl implements Operation 59 { 60 ORB orb; 61 ORBUtilSystemException wrapper ; 62 OMGSystemException omgWrapper ; 63 Resolver bootstrapResolver ; 64 65 private NamingContextExt rootNamingContextExt; 67 private Object rootContextCacheLock = new Object () ; 68 69 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 ; 85 88 private org.omg.CORBA.Object getIORFromString( String str ) 89 { 90 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 operate( Object arg ) 106 { 107 if (arg instanceof String ) { 108 String str = (String )arg ; 109 110 if (str.startsWith( ORBConstants.STRINGIFY_PREFIX )) 111 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 resolveINSURL( INSURL theURLObject ) { 125 if( theURLObject.isCorbanameURL() ) { 127 return resolveCorbaname( (CorbanameURL)theURLObject ); 128 } else { 129 return resolveCorbaloc( (CorbalocURL)theURLObject ); 130 } 131 } 132 133 138 private org.omg.CORBA.Object resolveCorbaloc( 139 CorbalocURL theCorbaLocObject ) 140 { 141 org.omg.CORBA.Object result = null; 142 if( theCorbaLocObject.getRIRFlag( ) ) { 144 result = bootstrapResolver.resolve(theCorbaLocObject.getKeyString()); 145 } else { 146 result = getIORUsingCorbaloc( theCorbaLocObject ); 147 } 148 149 return result; 150 } 151 152 157 private org.omg.CORBA.Object resolveCorbaname( CorbanameURL theCorbaName ) { 158 org.omg.CORBA.Object result = null; 159 160 try { 161 NamingContextExt theNamingContext = null; 162 163 if( theCorbaName.getRIRFlag( ) ) { 164 theNamingContext = getDefaultRootNamingContext( ); 166 } else { 167 org.omg.CORBA.Object corbalocResult = 169 getIORUsingCorbaloc( theCorbaName ); 170 if( corbalocResult == null ) { 171 return null; 172 } 173 174 theNamingContext = 175 NamingContextExtHelper.narrow( corbalocResult ); 176 } 177 178 String StringifiedName = theCorbaName.getStringifiedName( ); 179 180 if( StringifiedName == null ) { 181 return theNamingContext; 183 } else { 184 return theNamingContext.resolve_str( StringifiedName ); 185 } 186 } catch( Exception e ) { 187 clearRootNamingContextCache( ); 188 return null; 189 } 190 } 191 192 197 private org.omg.CORBA.Object getIORUsingCorbaloc( INSURL corbalocObject ) 198 { 199 Map profileMap = new HashMap (); 200 List profileList1_0 = new ArrayList (); 201 202 java.util.List theEndpointInfo = corbalocObject.getEndpointInfo(); 205 String theKeyString = corbalocObject.getKeyString(); 206 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 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); profileMap.remove(giopVersion); } 247 248 Comparator comp = new Comparator () { 250 public int compare(Object o1, Object 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 List list = new ArrayList (profileMap.keySet()); 259 Collections.sort(list, comp); 260 261 Iterator iter = list.iterator(); 263 while (iter.hasNext()) { 264 IIOPProfileTemplate pt = (IIOPProfileTemplate)profileMap.get(iter.next()); 265 iortemp.add(pt); 266 } 267 268 iortemp.addAll(profileList1_0); 270 271 IOR ior = iortemp.makeIOR( orb, "", key.getId() ) ; 272 return ORBUtility.makeObjectReference( ior ) ; 273 } 274 275 285 private NamingContextExt getDefaultRootNamingContext( ) { 286 synchronized( rootContextCacheLock ) { 287 if( rootNamingContextExt == null ) { 288 try { 289 rootNamingContextExt = 290 NamingContextExtHelper.narrow( 291 orb.getLocalResolver().resolve( "NameService" ) ); 292 } catch( Exception e ) { 293 rootNamingContextExt = null; 294 } 295 } 296 } 297 return rootNamingContextExt; 298 } 299 300 304 private void clearRootNamingContextCache( ) { 305 synchronized( rootContextCacheLock ) { 306 rootNamingContextExt = null; 307 } 308 } 309 } 310 | Popular Tags |