1 23 package com.sun.enterprise.iiop; 24 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 33 35 import org.omg.CORBA.Any ; 36 37 import com.sun.corba.ee.spi.ior.IOR; 38 import com.sun.corba.ee.spi.ior.iiop.AlternateIIOPAddressComponent; 39 import com.sun.corba.ee.spi.ior.TaggedComponent; 41 import com.sun.corba.ee.spi.ior.iiop.IIOPProfileTemplate ; 42 import com.sun.corba.ee.spi.ior.iiop.IIOPAddress ; 43 import com.sun.corba.ee.spi.transport.IORToSocketInfo; 44 import com.sun.corba.ee.spi.transport.SocketInfo; 45 46 import com.sun.enterprise.iiop.security.SecurityMechanismSelector; 47 import com.sun.enterprise.iiop.security.ConnectionContext; 48 49 import com.sun.logging.LogDomains; 50 51 56 public class IORToSocketInfoImpl implements IORToSocketInfo { 57 private static Logger _logger = null; 58 static { 59 _logger = LogDomains.getLogger(LogDomains.CORBA_LOGGER); 60 } 61 62 private final String baseMsg = IORToSocketInfoImpl.class.getName(); 63 64 private Map primaryToAddresses = new HashMap (); 68 69 private Map primaryToRandomizedAddresses = new HashMap (); 71 72 74 public List getSocketInfo(IOR ior, List previous) 75 { 76 try { 77 if (_logger.isLoggable(Level.FINE)) { 78 _logger.log(Level.FINE, baseMsg + ".getSocketInfo->:"); 79 } 80 List result = new ArrayList (); 81 SecurityMechanismSelector selector = new SecurityMechanismSelector(); 82 IIOPProfileTemplate iiopProfileTemplate = (IIOPProfileTemplate)ior. 83 getProfile().getTaggedProfileTemplate(); 84 IIOPAddress primary = iiopProfileTemplate.getPrimaryAddress() ; 85 String host = primary.getHost().toLowerCase(); 86 87 String type = null; 88 int port = 0; 89 ConnectionContext ctx = new ConnectionContext(); 90 SocketInfo socketInfo = selector.getSSLPort(ior, ctx); 91 selector.setClientConnectionContext(ctx); 92 if (socketInfo == null) { 93 type = SocketInfo.IIOP_CLEAR_TEXT; 94 port = primary.getPort(); 95 if (_logger.isLoggable(Level.FINE)) { 96 _logger.log(Level.FINE, baseMsg 97 + ".getSocketInfo: did not find SSL SocketInfo"); 98 } 99 } else { 100 type = socketInfo.getType(); 101 port = socketInfo.getPort(); 102 if (_logger.isLoggable(Level.FINE)) { 103 _logger.log(Level.FINE, baseMsg 104 + ".getSocketInfo: found SSL socketInfo"); 105 } 106 } 107 108 if (_logger.isLoggable(Level.FINE)) { 109 _logger.log(Level.FINE, baseMsg 110 + ".getSocketInfo: Connection Context:" + ctx); 111 _logger.log(Level.FINE, baseMsg 112 + ".getSocketInfo: ENDPOINT INFO:type=" + type + ",host=" +host + ", port=" + port); 113 } 114 115 if (socketInfo != null ) { 117 result.add(socketInfo); 118 if (_logger.isLoggable(Level.FINE)) { 119 _logger.log(Level.FINE, baseMsg 120 + ".getSocketInfo: returning SSL socketInfo:" 121 + " " + socketInfo.getType() 122 + " " + socketInfo.getHost() 123 + " " + socketInfo.getPort()); 124 } 125 return result; 129 } 130 131 138 if (_logger.isLoggable(Level.FINE)) { 140 _logger.log(Level.FINE, baseMsg 141 + ".getSocketInfo: returning non SSL socketInfo"); 142 } 143 144 if (! previous.isEmpty()) { 145 if (_logger.isLoggable(Level.FINE)) { 146 _logger.log(Level.FINE, baseMsg 147 + ".getSocketInfo: returning previous socketInfo: " 148 + previous); 149 } 150 return previous; 151 } 152 153 157 SocketInfo primarySocketInfo = 158 createSocketInfo("primary", type, host, port); 159 result.add(primarySocketInfo); 160 161 165 Iterator iterator = iiopProfileTemplate.iteratorById( 166 org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS.value 167 ); 168 169 while (iterator.hasNext()) { 170 AlternateIIOPAddressComponent alternate = 171 (AlternateIIOPAddressComponent) iterator.next(); 172 173 host = alternate.getAddress().getHost().toLowerCase(); 174 port = alternate.getAddress().getPort(); 175 176 result.add(createSocketInfo( 177 "AlternateIIOPAddressComponent", 178 SocketInfo.IIOP_CLEAR_TEXT, host, port)); 179 } 180 181 synchronized (this) { 182 List existing = (List ) 183 primaryToAddresses.get(primarySocketInfo); 184 if ( existing == null ) { 185 primaryToAddresses.put(primarySocketInfo, result); 188 result.remove(0); java.util.Collections.shuffle(result); 191 result.add(0, primarySocketInfo); primaryToRandomizedAddresses.put(primarySocketInfo,result); 194 if (_logger.isLoggable(Level.FINE)) { 195 _logger.log(Level.FINE, baseMsg 196 + ".getSocketInfo: initial randomized result: " 197 + result); 198 } 199 return result; 200 } else { 201 if ( result.equals(existing) ) { 202 result = (List ) 204 primaryToRandomizedAddresses.get(primarySocketInfo); 205 if (_logger.isLoggable(Level.FINE)) { 206 _logger.log(Level.FINE, baseMsg 207 + ".getSocketInfo: existing randomized result: " 208 + result); 209 } 210 return result; 211 } else { 212 _logger.log(Level.FINE, 216 baseMsg + ".getSocketInfo:" 217 + " Address lists do not match: primary: " 218 + primarySocketInfo 219 + "; returning current: " + result 220 + "; existing is: " + existing); 221 return result; 222 } 223 } 224 } 225 } catch ( Exception ex ) { 226 _logger.log(Level.WARNING, "Exception getting SocketInfo",ex); 227 RuntimeException rte = new RuntimeException (ex.getMessage()); 228 rte.initCause(ex); 229 throw rte; 230 } finally { 231 if (_logger.isLoggable(Level.FINE)) { 232 _logger.log(Level.FINE, baseMsg + ".getSocketInfo<-:"); 233 } 234 } 235 } 236 237 239 public static SocketInfo createSocketInfo(String msg, 240 final String type, 241 final String host, 242 final int port) 243 { 244 if (_logger.isLoggable(Level.FINE)) { 245 _logger.log(Level.FINE, 246 "Address from: " 247 + msg 248 + "; type/address/port: " 249 + type + "/" + host + "/" + port); 250 } 251 252 return new SocketInfo() { 253 public String getType() { 254 return type; 255 } 256 257 public String getHost() { 258 return host; 259 } 260 261 public int getPort() { 262 return port; 263 } 264 265 public boolean equals(Object o) { 266 if (o == null) { 267 return false; 268 } 269 if (! (o instanceof SocketInfo)) { 270 return false; 271 } 272 SocketInfo other = (SocketInfo)o; 273 if (other.getPort() != port) { 274 return false; 275 } 276 if (! other.getHost().equals(host)) { 277 return false; 278 } 279 if (! other.getType().equals(type)) { 280 return false; 281 } 282 return true; 283 } 284 285 public int hashCode() { 286 return type.hashCode() ^ host.hashCode() ^ port; 287 } 288 289 public String toString() { 290 return "SocketInfo[" + type + " " + host + " " + port +"]"; 291 } 292 }; 293 } 294 } 295 296 298 | Popular Tags |