FileDocCategorySizeDatePackage
S1ASCtxFactory.javaAPI DocGlassfish v2 API12278Tue Jul 24 13:49:16 BST 2007com.sun.appserv.naming

S1ASCtxFactory

public class S1ASCtxFactory extends com.sun.jndi.cosnaming.CNCtxFactory
Implements the JNDI SPI InitialContextFactory interface used to create the InitialContext objects. The static block creates the list of endpoints from the system property com.sun.appserv.iiop.endpoints. The list is randomised the very first time S1ASCtxFactory is initialized. When a call for a new InitialContext comes, the pointer in the list is moved one element ahead. Thus for loadbalancing purposes, there is always a different list created. failover is taken care of by the ORB infrastructure.
author
Dhiru Pandey & Vijay Raghavan
author
Sheetal Vartak

Fields Summary
protected static final Logger
_logger
private static final RoundRobinPolicy
rrPolicy
private static final String
IIOP_URL
private static final String
CORBALOC
public static final String
LOAD_BALANCING_PROPERTY
public static final String
IIOP_ENDPOINTS_PROPERTY
private final Hashtable
defaultEnv
private static com.sun.corba.ee.spi.folb.GroupInfoService
gis
public static final String
IC_BASED_WEIGHTED
public static final String
IC_BASED
private static boolean
firstTime
private static GroupInfoServiceObserverImpl
giso
Constructors Summary
public S1ASCtxFactory()


         
        String [] list = null;
        String [] commaDelimitedValues = null;
        String policy = null;
          
        // Get the load balancing policy
        String propertyValue = System.getProperty(
                              LOAD_BALANCING_PROPERTY);

        if (propertyValue != null) {
            commaDelimitedValues = propertyValue.split(",");
            
            
            if (commaDelimitedValues != null) {
            if (commaDelimitedValues[0].trim().equals(IC_BASED) ||
                commaDelimitedValues[0].trim().equals(IC_BASED_WEIGHTED)) {
                policy = commaDelimitedValues[0];
            }
            }
            if(policy != null) {
            System.setProperty(LOAD_BALANCING_PROPERTY, policy);
            }
        }
        
        propertyValue = System.getProperty(
                           IIOP_ENDPOINTS_PROPERTY);
        
        if(propertyValue==null||propertyValue.length()==0){
          //do not use the defaults here as then we are not giving a 
          //chance to the <policy>,host:port(,host:port)* type of policy
          //specification
            list = null;
        } else {
            list = propertyValue.split(",");	    
        }
                
        //if the endpoints property was not specified, give a
        //chance to the <policy>,host:port(,host:port)* type of policy
        //specification

        String host;
        String port;

        if(list == null ) {
            if (commaDelimitedValues != null && 
            commaDelimitedValues.length > 1) {
                
                list = new String[commaDelimitedValues.length-1];
            for (int i=0; i<list.length; i++) {
                list[i] = commaDelimitedValues[i+1];
            }		
            } else if((host = System.getProperty(ORBManager.OMG_ORB_INIT_HOST_PROPERTY)) != null &&
                  (port = System.getProperty(ORBManager.OMG_ORB_INIT_PORT_PROPERTY)) != null) {
                list = new String[1];
            list[0] = host + ":" + port;
              
            }
        }
        
        rrPolicy = new RoundRobinPolicy(list);

	if (list != null && list.length > 0) {
	    getGIS();
	}

    
        defaultEnv = new Hashtable();
    
public S1ASCtxFactory(Hashtable env)

        defaultEnv = env;
    
Methods Summary
public static java.lang.StringgetCorbalocURL(java.lang.Object[] list)


	String corbalocURL = "";
	//convert list into corbaloc url
	for (int i = 0; i < list.length;i++) {
	    _logger.info("list[i] ==> " + list[i]);
	    if (corbalocURL.equals("")) {
	        corbalocURL = IIOP_URL + ((String)list[i]).trim();
	    } else {
	        corbalocURL = corbalocURL + "," +
		    IIOP_URL + ((String)list[i]).trim();
	    }
	}	
	_logger.info("corbaloc url ==> " + corbalocURL);
	return corbalocURL;
    
private static voidgetGIS()

        try {
	    //fix for bug 6527987
	    // passing the first endpoint for ORBManager.getORB() to connect to
	  
	    //need to make NameService HA...what if first endpoint is down.
	    //need to address this issue.
	  
	  /*  Properties props = new Properties();
	      String hostPort = endpoint;
	      // for IPv6 support, using lastIndex of ":"
	      int lastIndex = hostPort.lastIndexOf(':');
	      
	      _logger.fine("hostPort = " + hostPort + " lastIndex = " + lastIndex);
	      _logger.fine("hostPort.substring(0, lastIndex) = " + hostPort.substring(0, lastIndex));
	      _logger.fine("hostPort.substring(lastIndex + 1) = " + hostPort.substring(lastIndex + 1));
	      
	      props.put("org.omg.CORBA.ORBInitialHost", hostPort.substring(0, lastIndex));
	      props.put("org.omg.CORBA.ORBInitialPort", hostPort.substring(lastIndex + 1));
	      */	

	    gis = (GroupInfoService)
	      ((ORBManager.getORB()).resolve_initial_references(
				     ORBConstants.FOLB_CLIENT_GROUP_INFO_SERVICE));
	    giso = new GroupInfoServiceObserverImpl(gis);
	    gis.addObserver(giso);
	    
	    rrPolicy.print();
	} catch (org.omg.CORBA.ORBPackage.InvalidName in) {
	    _logger.fine("GroupInfoService not available. This is PE");
	} 
    
public synchronized javax.naming.ContextgetInitialContext(java.util.Hashtable env)


        Object [] list;

	if (SerialContext.getSticky() != null) {
	    Context ctx = SerialContext.getStickyContext();	
	    return ctx;
	}
	
	// firstTime is a boolean introduced to check if this is the first call to 
	// new InitialContext(). This is important especially on the server side
	if (firstTime == true) {

	    String policy = null;
	    
	    if (env == null) {
	      env = defaultEnv;
	    }
	    
	    //user can specify the load balancing policy and endpoints
	    // via env. Hence the logic below. 
	    
	    String propertyValue = (String) env.get(LOAD_BALANCING_PROPERTY);
	    String[] commaDelimitedValues = null;
	    String host = null;
	    String port = null;
	    
	    if (propertyValue != null) {
	      commaDelimitedValues = propertyValue.split(",");	
	      
	      if (commaDelimitedValues != null) {
		if (commaDelimitedValues[0].trim().equals(IC_BASED) ||
		    commaDelimitedValues[0].trim().equals(IC_BASED_WEIGHTED)) {
		  policy = commaDelimitedValues[0];
		}
	      }
	      if (policy != null) {
		System.setProperty(LOAD_BALANCING_PROPERTY, policy);
	      }
	    }
	    
	    propertyValue = (String) env.get(IIOP_ENDPOINTS_PROPERTY);
	    
	    String [] temp_list = (propertyValue == null || 
				   propertyValue.length() == 0)
	      ? null 
	      : propertyValue.split(",");
	    if(temp_list == null || temp_list.length == 0) {
	      if (commaDelimitedValues != null) {
	        temp_list = new String[commaDelimitedValues.length - 1];
		
		for (int i=0; i<temp_list.length; i++) {
		  temp_list[i] = commaDelimitedValues[i+1];
		}
	      }	    	
	    }
	    //if endpoints property is not set by commandline or via env
	    // check for JNDI provider url
	    // else use ORB host:port value
	    if ((System.getProperty(IIOP_ENDPOINTS_PROPERTY) == null) &&
		(temp_list == null || 
		 temp_list.length == 0)) {
	      if (env.get(ORBManager.JNDI_PROVIDER_URL_PROPERTY) != null) {
		temp_list = rrPolicy.getEndpointForProviderURL(
							       (String)env.get(ORBManager.JNDI_PROVIDER_URL_PROPERTY));
	      }
	      if (temp_list == null || temp_list.length == 0) {
		if (env.get(ORBManager.OMG_ORB_INIT_HOST_PROPERTY) != null &&
		    env.get(ORBManager.OMG_ORB_INIT_PORT_PROPERTY) != null) {
		  host = (String)env.get(
					 ORBManager.OMG_ORB_INIT_HOST_PROPERTY);
		  port = (String)env.get(
					 ORBManager.OMG_ORB_INIT_PORT_PROPERTY);
		} else {
		  host = System.getProperty(
					    ORBManager.OMG_ORB_INIT_HOST_PROPERTY);
		  port = System.getProperty(
					    ORBManager.OMG_ORB_INIT_PORT_PROPERTY);
		}
		if (host != null &&
		    port != null) {
		  temp_list = rrPolicy.getAddressPortList(host, port);	 
		  _logger.log(Level.WARNING, "no.endpoints.selected", 
			      new Object[] {host, port});
		} else {	  
		  _logger.log(Level.SEVERE, "no.endpoints");
		  throw new RuntimeException("Cannot Proceed. No Endpoints specified.");
		}
	      }	    
	    }

	    if (giso == null) {
  	        if (temp_list != null && temp_list.length > 0) {		   
		    getGIS();
		} else {
		   _logger.warning("Cannot obtain GroupInfoServiceObserverImpl");
		}
	    }
	    
	    //add the list after randomising it to the circular list in rrPolicy
	    if (temp_list != null && temp_list.length > 0) {
	      rrPolicy.setClusterInstanceInfo(temp_list);
	    } 
	    
	    firstTime = false;
	    if (giso != null) {
	        //need to get the list of cluster instances the very first time a context is created
	        giso.membershipChange();
	    } else {
	        _logger.warning("Cannot obtain GroupInfoServiceObserverImpl");
	    }

	} 
	

	//get next version of the randomized list using round robin algo
	list = rrPolicy.getNextRotation();
	
	if (_logger.isLoggable(Level.FINE)) {
	    rrPolicy.print();
	}
	
	String corbalocURL = getCorbalocURL(list);

	env.put("com.sun.appserv.ee.iiop.endpointslist", 
		CORBALOC + corbalocURL);	
	env.put(ORBManager.JNDI_CORBA_ORB_PROPERTY, ORBManager.getORB());
	
	return new SerialContext(env);
    
public static RoundRobinPolicygetRRPolicy()

	return rrPolicy;