1 23 24 package com.sun.enterprise.iiop; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.LinkedList ; 30 import java.util.logging.Logger ; 31 import java.util.logging.Level ; 32 33 import com.sun.logging.LogDomains; 34 35 import org.omg.CORBA.Any ; 36 import org.omg.IOP.Codec ; 37 import org.omg.IOP.CodecFactory ; 38 import org.omg.IOP.CodecFactoryHelper ; 39 import org.omg.IOP.Encoding ; 40 import org.omg.IOP.ENCODING_CDR_ENCAPS ; 41 import org.omg.IOP.TaggedComponent ; 42 import org.omg.PortableInterceptor.IORInfo ; 43 import org.omg.PortableInterceptor.IORInterceptor ; 44 import org.omg.PortableInterceptor.ObjectReferenceTemplate ; 45 import org.omg.PortableInterceptor.ORBInitializer ; 46 import org.omg.PortableInterceptor.ORBInitInfo ; 47 import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName ; 48 49 import com.sun.enterprise.admin.util.JMXConnectorConfig; 51 import com.sun.enterprise.config.serverbeans.IiopListener; 52 import com.sun.enterprise.config.ConfigException; 53 import com.sun.enterprise.config.ConfigContext; 54 import com.sun.enterprise.server.ApplicationServer; 55 import com.sun.enterprise.config.serverbeans.Cluster; 56 import com.sun.enterprise.config.serverbeans.ClusterHelper; 57 import com.sun.enterprise.config.serverbeans.ServerRef; 58 import com.sun.enterprise.config.serverbeans.Server; 59 import com.sun.enterprise.config.serverbeans.Config; 60 import com.sun.enterprise.config.serverbeans.ConfigAPIHelper; 61 import com.sun.enterprise.config.serverbeans.ServerHelper; 62 import com.sun.enterprise.config.serverbeans.IiopService; 63 import com.sun.enterprise.config.serverbeans.PropertyResolver; 64 66 import com.sun.enterprise.iiop.AlternateIIOPAddressComponent; 67 import com.sun.enterprise.iiop.AlternateIIOPAddressComponentHelper; 68 69 import com.sun.corba.ee.spi.legacy.interceptor.ORBInitInfoExt ; 70 import com.sun.corba.ee.spi.orb.ORB; 71 72 import com.sun.corba.ee.spi.folb.ClusterInstanceInfo; 73 import com.sun.corba.ee.spi.folb.SocketInfo; 74 75 78 public class FailoverIORInterceptor 79 extends org.omg.CORBA.LocalObject 80 implements ORBInitializer , IORInterceptor 81 { 82 private static Logger _logger = null; 83 static { 84 _logger = LogDomains.getLogger(LogDomains.CORBA_LOGGER); 85 } 86 87 private ORB orb ; 88 89 private static final String baseMsg 90 = FailoverIORInterceptor.class.getName(); 91 private static final String ORB_LISTENTER = "orb-listener"; 92 93 private List currentAddressList; 94 private List previousAddressList; 95 private List randomizedAddressList; 96 private List randomizedAndMarshaledAddressList; 97 98 102 public String name() { return baseMsg; } 103 public void destroy() { } 104 105 109 public FailoverIORInterceptor() 110 { 111 } 112 113 public void pre_init(ORBInitInfo info) { } 114 115 public void post_init(ORBInitInfo info) 116 { 117 ORB orb = ((ORBInitInfoExt)info).getORB() ; 118 try { 119 info.add_ior_interceptor(new FailoverIORInterceptor(orb)); 120 _logger.log(Level.FINE, baseMsg + " registered."); 121 } catch (DuplicateName ex) { 122 _logger.log(Level.WARNING, "DuplicateName from " + baseMsg , ex); 123 129 } 130 } 131 132 136 public FailoverIORInterceptor( ORB orb ) 137 { 138 this.orb = orb ; 139 } 140 141 public void establish_components(IORInfo iorInfo) 142 { 143 try { 144 _logger.log(Level.FINE, 145 baseMsg + ".establish_components->:"); 146 147 if (ApplicationServer.getServerContext() == null) { 148 _logger.log(Level.FINE, 149 baseMsg + ".establish_components:" 150 + " not adding AlternateIIOPAddressComponent" 151 + " because: no server context"); 152 return; 153 } 154 155 ConfigContext configCtx = 156 ApplicationServer.getServerContext().getConfigContext(); 157 String instanceName = 158 ApplicationServer.getServerContext().getInstanceName(); 159 Cluster cluster = null; 160 try { 161 cluster = 162 ClusterHelper 163 .getClusterForInstance(configCtx, instanceName); 164 } catch (ConfigException e) { 165 _logger.log(Level.FINE, 166 baseMsg + ".establish_components:" 167 + " not adding AlternateIIOPAddressComponent" 168 + " because: " 169 + e); 170 return; 171 } 172 ServerRef[] servers = cluster.getServerRef(); 173 IiopListener[][] listeners = getIIOPEndPointsForCurrentCluster( 174 configCtx, instanceName, servers); 175 currentAddressList = new ArrayList (); 176 for (int i = 0; i < servers.length; i++) { 177 String serverName = servers[i].getRef(); 178 String hostName = 179 getHostNameForServerInstance(configCtx, serverName); 180 if (hostName==null) { 181 hostName = listeners[i][0].getAddress(); 182 } 183 for (int j = 0; j < listeners[i].length; j++) { 184 String id = listeners[i][j].getId(); 185 String port = 186 getResolvedPort(configCtx,listeners[i][j], serverName); 187 if (! id.startsWith(ORB_LISTENTER)) { 189 if (_logger.isLoggable(Level.FINE)) { 190 _logger.log(Level.FINE, 191 baseMsg + ".establish_components:" 192 + " ignoring SSL ports: " 193 + id + " " + hostName + " " + port); 194 } 195 continue; 196 } 197 198 if (_logger.isLoggable(Level.FINE)) { 199 _logger.log(Level.FINE, 200 baseMsg + ".establish_components:" 201 + " adding AlternateIIOPAddressComponent" 202 + " for listener id: " 203 + id 204 + " address/port: " 205 + hostName 206 + "/" 207 + port); 208 } 209 210 AlternateIIOPAddressComponent address = 211 new AlternateIIOPAddressComponent( 212 hostName, 213 Integer.decode(port).shortValue()); 214 215 currentAddressList.add(address); 216 } 217 } 218 219 if (previousAddressList == null) { 220 221 225 previousAddressList = new ArrayList (); 226 Iterator i = currentAddressList.iterator(); 228 while (i.hasNext()) { previousAddressList.add(i.next()); } 229 230 235 java.util.Collections.shuffle(currentAddressList); 236 randomizedAddressList = currentAddressList; 237 238 if (_logger.isLoggable(Level.FINE)) { 239 _logger.log(Level.FINE, baseMsg 240 + " first call - saving randomized alternate address list: " 241 + randomizedAddressList); 242 } 243 244 248 randomizedAndMarshaledAddressList = new ArrayList (); 249 i = randomizedAddressList.iterator(); 250 while (i.hasNext()) { 251 AlternateIIOPAddressComponent address = 252 (AlternateIIOPAddressComponent) i.next(); 253 Any any = orb.create_any(); 254 AlternateIIOPAddressComponentHelper.insert(any, address); 255 byte[] data = getCodec(orb).encode_value(any); 256 TaggedComponent tc = 257 new TaggedComponent (org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS.value, 258 data); 260 randomizedAndMarshaledAddressList.add(tc); 261 } 262 } else { 263 if (! currentAddressList.equals(previousAddressList)) { 264 throw new RuntimeException ( 265 "Current address list: " 266 + currentAddressList 267 + " different from previous list: " 268 + previousAddressList); 269 } 270 } 271 272 Iterator i = randomizedAndMarshaledAddressList.iterator(); 273 while (i.hasNext()) { 274 iorInfo.add_ior_component((TaggedComponent ) i.next()); 275 } 276 277 if (_logger.isLoggable(Level.FINE)) { 278 _logger.log(Level.FINE, baseMsg 279 + " adding randomized alternate addresses: " 280 + randomizedAddressList); 281 } 282 } catch (Throwable e) { 283 _logger.log(Level.WARNING, 284 "Problem in " + baseMsg + ".establish_components", 285 e); 286 292 } finally { 293 _logger.log(Level.FINE, baseMsg + ".establish_components<-:"); 294 } 295 } 296 297 public void components_established( IORInfo iorInfo ) 298 { 299 } 300 301 public void adapter_manager_state_changed( int managerId, short state ) 302 { 303 } 304 305 public void adapter_state_changed( ObjectReferenceTemplate [] templates, 306 short state ) 307 { 308 } 309 310 315 public static List <ClusterInstanceInfo> getClusterInstanceInfo() 316 { 317 List <ClusterInstanceInfo> result = 318 new LinkedList <ClusterInstanceInfo>(); 319 320 try { 321 _logger.log(Level.FINE, 322 baseMsg + ".getClusterForInstanceInfo->:"); 323 324 if (ApplicationServer.getServerContext() == null) { 325 _logger.log(Level.FINE, 326 baseMsg + ".getClusterForInstanceInfo:" 327 + " not adding addresses " 328 + " because: no server context"); 329 return null; 330 } 331 332 ConfigContext configCtx = 333 ApplicationServer.getServerContext().getConfigContext(); 334 String instanceName = 335 ApplicationServer.getServerContext().getInstanceName(); 336 Cluster cluster = null; 337 try { 338 cluster = 339 ClusterHelper 340 .getClusterForInstance(configCtx, instanceName); 341 } catch (ConfigException e) { 342 _logger.log(Level.FINE, 343 baseMsg + ".getClusterForInstance: " 344 + e); 345 return null; 346 } 347 ServerRef[] servers = cluster.getServerRef(); 348 IiopListener[][] listeners = getIIOPEndPointsForCurrentCluster( 349 configCtx, instanceName, servers); 350 for (int i = 0; i < servers.length; i++) { 351 String serverName = servers[i].getRef(); 352 String hostName = 353 getHostNameForServerInstance(configCtx, serverName); 354 if (hostName==null) { 355 hostName = listeners[i][0].getAddress(); 356 } 357 List <SocketInfo> listOfSocketInfo = 358 new LinkedList <SocketInfo>(); 359 for (int j = 0; j < listeners[i].length; j++) { 360 String id = listeners[i][j].getId(); 361 String port = 362 getResolvedPort(configCtx,listeners[i][j], serverName); 363 if (_logger.isLoggable(Level.FINE)) { 364 _logger.log(Level.FINE, 365 baseMsg + ".getClusterForInstance:" 366 + " adding address for " 367 + serverName + "/" + id 368 + "/" + hostName + "/" + port); 369 } 370 listOfSocketInfo.add( 371 new SocketInfo(id, hostName, Integer.valueOf(port))); 372 } 373 SocketInfo[] arrayOfSocketInfo = 376 new SocketInfo[listOfSocketInfo.size()]; 377 int x = 0; 378 for (SocketInfo si : listOfSocketInfo) { 379 arrayOfSocketInfo[x++] = si; 380 } 381 ClusterInstanceInfo clusterInstanceInfo = 384 new ClusterInstanceInfo(serverName, 100, arrayOfSocketInfo); 385 result.add(clusterInstanceInfo); 386 if (_logger.isLoggable(Level.FINE)) { 387 _logger.log(Level.FINE, 388 baseMsg + ".getClusterForInstance: " + 389 ASORBUtilities.toString(clusterInstanceInfo)); 390 } 391 } 392 return result; 393 } catch (ConfigException e) { 394 return null; 395 } finally { 396 _logger.log(Level.FINE, 397 baseMsg + ".getClusterForInstanceInfo<-: " + result); 398 } 399 } 400 401 416 private static IiopListener[][] getIIOPEndPointsForCurrentCluster( 417 ConfigContext configCtx, String instanceName, ServerRef[] servers) 418 throws ConfigException 419 { 420 423 IiopListener[][] listeners = new IiopListener[servers.length][3]; 424 for (int i = 0; i < servers.length; i++) { 425 Server server = 426 ServerHelper.getServerByName(configCtx, servers[i].getRef()); 427 String configRef = server.getConfigRef(); 428 Config config = 429 ConfigAPIHelper.getConfigByName(configCtx, configRef); 430 IiopService iiopService = config.getIiopService(); 431 listeners[i] = iiopService.getIiopListener(); 432 } 433 return listeners; 434 } 435 436 442 private static String getHostNameForServerInstance(ConfigContext ctx, 443 String serverName) 444 { 445 try { 446 JMXConnectorConfig info = 447 ServerHelper.getJMXConnectorInfo(ctx, serverName); 448 _logger.log(Level.FINE, 449 baseMsg + ".getHostNameForServerInstance: " + 450 "found info: " + info.toString()); 451 String host = info.getHost(); 452 _logger.log(Level.FINE, 453 baseMsg + ".getHostNameForServerInstance: " + 454 "found host: " + host); 455 return host; 456 } catch (Throwable e){ 457 _logger.log(Level.FINE, 458 baseMsg + ".getHostNameForServerInstance: " + 459 "gotException: " + e + " " + e.getMessage() + 460 "; returning null"); 461 return null; 462 } 463 } 464 465 472 private static String getResolvedPort(ConfigContext ctx, 473 IiopListener l, 474 String server) 475 throws ConfigException 476 { 477 String rawPort = l.getRawAttributeValue("port"); 478 PropertyResolver pr = new PropertyResolver(ctx, server); 479 return pr.resolve(rawPort); 480 } 481 482 static Codec getCodec(ORB orb) 483 { 484 Codec result = null; 485 try { 486 CodecFactory codecFactory = 487 CodecFactoryHelper.narrow( 488 orb.resolve_initial_references("CodecFactory")); 489 result = codecFactory.create_codec( 490 new Encoding ((short)ENCODING_CDR_ENCAPS.value, 491 (byte)1, (byte)2)); 492 } catch (Throwable e) { 493 _logger.log(Level.WARNING, 494 "Problem in " + baseMsg + ".getCodec", 495 e); 496 } 497 return result; 498 } 499 } 500 501 | Popular Tags |