/**
  * File: GroupMembershipNamingServiceFactory.java
  * Content: returns the GroupMembershipNamingService instance for a given configuration file
  * Author: LuisM Pena
  * Date: 5th December 2000
  * Version: 0.29.00
  * Last change:
  *
  **/

package sensei.gmns;

import sensei.middleware.gmns.GroupMembershipNamingService;
import sensei.middleware.gmns.GMNSnarrower;
import sensei.middleware.gms.GMSnarrower;
import sensei.middleware.util.ObjectsHandling;

import sensei.util.Configuration;

import java.net.URL;
import java.io.InputStream;

/**
  * returns the GroupMembershipNamingService instance for a given configuration file
  *	every gmns related there is contacted until one returns a valid gmns instance.
  *	They are contacted in the order specified (host.xxx), and the first one is the
  *	given by the referenceFile or referenceSocket
  **/
public class GroupMembershipNamingServiceFactory
{
  /**
    * Loads the GroupMembershipNamingService from the default configuration file
    * @returns null if no valid GroupMembershipNamingService found
    **/
  public static GroupMembershipNamingService load()
  {
    Trace.code("GroupMembershipNamingServiceFactory.java -> GroupMembershipNamingService load ( )");
    return load(Configuration.getSingleton());
  }

  /**
    * Loads the GroupMembershipNamingService specified  in the given configuration.
    * @returns null if no valid GroupMembershipNamingService found
    **/
  public static GroupMembershipNamingService load(Configuration conf)
  {
    Trace.code("GroupMembershipNamingServiceFactory.java -> GroupMembershipNamingService load ( Configuration conf )");
    GroupMembershipNamingService ret = load(conf.getGMNSHost());
    if (ret==null)
      ret=load(conf.getGMNSHosts());
    return ret;
  }

  /**
    * Loads the GroupMembershipNamingService specified from the given GMNShosts
    * @returns null if no valid GroupMembershipNamingService found
    **/
  public static GroupMembershipNamingService load(Configuration.GMNShost[] hosts)
  {
    Trace.code("GroupMembershipNamingServiceFactory.java -> GroupMembershipNamingService load ( Configuration.GMNShost [ ] hosts )");
    GroupMembershipNamingService ret=null;
    for (int i=0;i<hosts.length && ret==null;i++)
      ret=load(hosts[i]);
    return ret;
  }

  /**
    * Loads the GroupMembershipNamingService specified  in the given GMNShost.
    * @returns null if no valid GroupMembershipNamingService found
    **/
  public static GroupMembershipNamingService load(Configuration.GMNShost host)
  {
    Trace.code("GroupMembershipNamingServiceFactory.java -> GroupMembershipNamingService load ( Configuration.GMNShost host )");
    return host.socketSpecified()? load(host.host, host.port) : load(host.url);
  }

  /**
    * Loads the GroupMembershipNamingService specified from the specified url, which
    * should had been created by a valid GroupMembershipNamingServiceImpl
    * @returns null if no valid GroupMembershipNamingService found
    **/
  public static GroupMembershipNamingService load(URL url)
  {
    Trace.code("GroupMembershipNamingServiceFactory.java -> GroupMembershipNamingService load ( URL url )");
    InputStream is = null;
    GroupMembershipNamingService ret=null;
    try
    {
      is=url.openStream();
      if (GMSnarrower.toGroupHandler(ObjectsHandling.readObject(is)).isValidGroup())
        ret = GMNSnarrower.toGroupMembershipNamingService(ObjectsHandling.readObject(is));
    }
    catch (Exception ex)
    {
      ret=null;
      Trace.handledException(ex);
    }
    finally
    {
      if (is!=null)
        try{is.close();}catch(Exception ex){}
    }
    return ret;
  }

  /**
    * Loads the GroupMembershipNamingService specified from the specified port, which
    * should had been created by a valid GroupMembershipNamingServiceImpl
    * @returns null if no valid GroupMembershipNamingService found
    **/
  public static GroupMembershipNamingService load(String host, int port)
  {
    Trace.code("GroupMembershipNamingServiceFactory.java -> GroupMembershipNamingService load ( String host , int port )");
    GMNSreferenceListener socket = new GMNSreferenceListener(host, port);
    GroupMembershipNamingService ret=null;
    try
    {
      if (socket.getGroup()!=null && socket.getGroup().isValidGroup())
        ret = socket.getGMSN();
    }
    catch (Exception ex)
    {
      ret=null;
      Trace.handledException(ex);
    }
    return ret;
  }
}