FileDocCategorySizeDatePackage
FailoverIORInterceptor.javaAPI DocGlassfish v2 API9414Fri May 04 22:34:54 BST 2007com.sun.enterprise.iiop

FailoverIORInterceptor

public class FailoverIORInterceptor extends LocalObject implements IORInterceptor, ORBInitializer
author
Harold Carr

Fields Summary
private static Logger
_logger
private com.sun.corba.ee.spi.orb.ORB
orb
private static final String
baseMsg
private static final String
ORB_LISTENER
private List
currentAddressList
private List
previousAddressList
private List
randomizedAddressList
private List
randomizedAndMarshaledAddressList
Constructors Summary
public FailoverIORInterceptor()

    
public FailoverIORInterceptor(com.sun.corba.ee.spi.orb.ORB orb)

	this.orb = orb ;
    
Methods Summary
public voidadapter_manager_state_changed(int managerId, short state)

    
public voidadapter_state_changed(org.omg.PortableInterceptor.ObjectReferenceTemplate[] templates, short state)

    
public voidcomponents_established(org.omg.PortableInterceptor.IORInfo iorInfo)

    
public voiddestroy()

 
public voidestablish_components(org.omg.PortableInterceptor.IORInfo iorInfo)

	try {
	    _logger.log(Level.FINE,
			baseMsg + ".establish_components->:");	 
 
	    ServerRef[] servers = IIOPEndpointsInfo.getServersInCluster();
	    IiopListener[][] listeners = IIOPEndpointsInfo.getListenersInCluster();
              
	    currentAddressList = new ArrayList();
	    if (servers != null) {
	        for (int i = 0; i < servers.length; i++) {
		    String serverName = servers[i].getRef();
		    String hostName =
		      IIOPEndpointsInfo.getHostNameForServerInstance(serverName);
		    if (hostName==null) {
		        hostName = listeners[i][0].getAddress();
		    }		 
		    for (int j = 0; j < listeners[i].length; j++) { 
		        String id = listeners[i][j].getId();
			String port = 
			  IIOPEndpointsInfo.getResolvedPort(listeners[i][j], serverName);
			// REVISIT: Make question an official API.
			if (! id.startsWith(ORB_LISTENER)) {
			    if (_logger.isLoggable(Level.INFO)) {
			        _logger.log(Level.INFO, 
					    baseMsg + ".establish_components:"
					    + " ignoring SSL ports: " +
					    id + " " + hostName + " " + port);
			    }
			    continue;
			}
			
			if (_logger.isLoggable(Level.FINE)) {
			    _logger.log(Level.FINE, 
					baseMsg + ".establish_components:"
					+ " adding AlternateIIOPAddressComponent"
					+ " for listener id: " + id
					+ " address/port: " + hostName
					+ "/" + port);
			}
			
			AlternateIIOPAddressComponent address =
			  new AlternateIIOPAddressComponent(
							    hostName,
							    Integer.decode(port).shortValue());
			
			currentAddressList.add(address);
		    }
		}
	    }

	    if (previousAddressList == null) {

		//
		// Save the previous list for subsequent comparisons.
		//

		previousAddressList = new ArrayList();
		// List does not define .clone and Object.clone is protected.
		Iterator i = currentAddressList.iterator();
		while (i.hasNext()) { previousAddressList.add(i.next()); }

		//
		// Randomize the list for this ORB.
		// Save it in unmarshaled form for logging.
		//

		java.util.Collections.shuffle(currentAddressList);
		randomizedAddressList = currentAddressList;

		if (_logger.isLoggable(Level.FINE)) {
		    _logger.log(Level.FINE, baseMsg 
				+ " first call - saving randomized alternate address list: "
				+ randomizedAddressList);
		}

		//
		// Save a marshaled version for subsequent calls.
		//

		randomizedAndMarshaledAddressList = new ArrayList();
		i = randomizedAddressList.iterator();
		while (i.hasNext()) {
		    AlternateIIOPAddressComponent address =
			(AlternateIIOPAddressComponent) i.next();
		    Any any = orb.create_any();
		    AlternateIIOPAddressComponentHelper.insert(any, address);
		    byte[] data = getCodec(orb).encode_value(any);
		    TaggedComponent tc =
			new TaggedComponent(org.omg.IOP.TAG_ALTERNATE_IIOP_ADDRESS.value,
					    //AlternateIIOPAddressComponent.TAG_ALTERNATE_IIOP_ADDRESS_ID,
					    data);
		    randomizedAndMarshaledAddressList.add(tc);
		}
	    } else {
		if (! currentAddressList.equals(previousAddressList)) {
		    throw new RuntimeException(
		        "Current address list: "
			+ currentAddressList
			+ " different from previous list: "
			+ previousAddressList);
		}
	    }

	    Iterator i = randomizedAndMarshaledAddressList.iterator();
	    while (i.hasNext()) {
		iorInfo.add_ior_component((TaggedComponent) i.next());
	    }

	    if (_logger.isLoggable(Level.FINE)) {
		_logger.log(Level.FINE, baseMsg 
			    + " adding randomized alternate addresses: "
			    + randomizedAddressList);
	    }
	} catch (Throwable e) {
            _logger.log(Level.WARNING, 
			"Problem in " + baseMsg + ".establish_components", 
			e);
	    /*
	    RuntimeException rte = 
		new RuntimeException("Problem in " + baseMsg + ".establish_components");
	    rte.initCause(e);
	    throw rte;
	    */
	} finally {
	    _logger.log(Level.FINE, baseMsg + ".establish_components<-:");
	}
    
static org.omg.IOP.CodecgetCodec(com.sun.corba.ee.spi.orb.ORB orb)

	Codec result = null;
	try {
	    CodecFactory codecFactory = 
		CodecFactoryHelper.narrow(
                    orb.resolve_initial_references("CodecFactory"));
	    result = codecFactory.create_codec(
                new Encoding((short)ENCODING_CDR_ENCAPS.value, 
			     (byte)1, (byte)2));
	} catch (Throwable e) {
            _logger.log(Level.WARNING, 
			"Problem in " + baseMsg + ".getCodec",
			e);
	}
	return result;
    
public java.lang.Stringname()


    //
    // Interceptor
    //

           return baseMsg; 
public voidpost_init(org.omg.PortableInterceptor.ORBInitInfo info)

	ORB orb = ((ORBInitInfoExt)info).getORB() ;
	try {
	    info.add_ior_interceptor(new FailoverIORInterceptor(orb));
	    _logger.log(Level.FINE, baseMsg + " registered.");
	} catch (DuplicateName ex) {
            _logger.log(Level.WARNING, "DuplicateName from " + baseMsg , ex);
	    /*
	    RuntimeException rte = 
		new RuntimeException("DuplicateName from " + baseMsg , ex);
	    rte.initCause(ex);
	    throw rte;
	    */
	}
    
public voidpre_init(org.omg.PortableInterceptor.ORBInitInfo info)