// MobyServiceLocator.java
//
//    Created: August 2005
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

package org.biomoby.client;

import org.biomoby.shared.MobyService;

/**
 * A container for information how to reach a Biomoby service. <p>
 *
 * Normally, a Biomoby service is sufficiently defined by its URL
 * (endpoint) and by its name (which is used as a method name on that
 * URL). See {@link #getService}. <p>
 *
 * Sometimes, however, the service URL is not (yet) available, so a
 * client has to go to a BioMoby registry and fetch it. The locator
 * can provide, therefore, information about a registry. See {@link
 * #getRegistryEndpoint} and {@link #getRegistryNamespace}. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: MobyServiceLocator.java,v 1.6 2008/12/17 14:09:00 groscurt Exp $
 */

public class MobyServiceLocator {

    protected MobyService service;
    protected String registryEndpoint;
    protected String registryNamespace;
    protected boolean asBytes = false;
    protected int timeout = 0;
    protected String user;
    protected String password;

    /**************************************************************************
     * Default constructor.
     *************************************************************************/
    public MobyServiceLocator() {
    }

    /**************************************************************************
     * Another constructor, seeting also a service.
     *************************************************************************/
    public MobyServiceLocator (MobyService service) {
	setService (service);
    }

    /**************************************************************************
     *
     *************************************************************************/
    public MobyService getService() {
	return service;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public void setService (MobyService service) {
	this.service = service;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public String getRegistryEndpoint() {
	return registryEndpoint;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public void setRegistryEndpoint (String value) {
	registryEndpoint = value;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public String getRegistryNamespace() {
	return registryNamespace;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public void setRegistryNamespace (String value) {
	registryNamespace = value;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public int getSuggestedTimeout() {
	return timeout;
    }

    /**************************************************************************
     * Set a timeout that will be used when calling the service. By
     * default, the timeout is disabled (which is equivalent to
     * setting timeout to zero. <p>
     *
     * Note, however, that disabling timeout in the client does not
     * mean that the call cannot time-out: there are other players on
     * the way to the services and back that can cause time-outing
     * (proxies, web server, servlet container, etc.). <p>
     *
     * @param timeout suggested timeout in milliseconds; zero or a
     * negative value means to disable timeout
     *************************************************************************/
    public void setSuggestedTimeout (int timeout) {
	this.timeout = timeout;
    }

    /**************************************************************************
     * Return the current setting - see {@link #setAsBytes}.
     *************************************************************************/
    public boolean isAsBytes() {
	return asBytes;
    }

    /**************************************************************************
     * Biomoby API requested that a service must accept input data
     * either as a String, or as an byte array. By default, the string
     * is used, but one can set it differently in this locator. <p>
     *
     * @param enabled if true the data will be sent to the service as
     * byte array
     *************************************************************************/
    public void setAsBytes (boolean enabled) {
	asBytes = enabled;
    }
    
    /*****************************************
     * Sets the user for authentication
     * @param user
     ***************************************/
    public void setUser(String user) {
	this.user = user;
    }

    /***************************************
     * Sets the password for authentication
     * @param password
     ***************************************/
    public void setPassword(String password) {
	this.password = password;
    }

    /******************************************
     * Returns the user for the authentication
     * @return user
     ******************************************/
    public String getUser() {
	return user;
    }

    /**********************************************
     * Returns the password for the authentication
     * @return password
     **********************************************/
    public String getPassword() {
	return password;
    }

    /***************************************************
     * Returns true if the user AND the password is set
     * @return if authentication shall be used
     ***************************************************/
    public boolean hasAuthentication() {
	return user != null && password != null;
    }
}
