package oracle.j2ee.ws.client.impl;

import java.util.*;
import java.io.*;
import java.net.*;
import oracle.j2ee.ws.client.*;
import oracle.j2ee.ws.client.wsdl.*;

public class WSClientBean implements java.io.Serializable
{
 
  public WSClientBean()
  {
     _proxyF = new WebServiceProxyFactory();
     _htProperties = new Hashtable();
     _alMaps = new ArrayList();
  }

  /**
   * invoke operation/method
   */
  public Object invokeOperation(String opName, 
                                String[] alPartNames,
                                Object[] alPartValues)
                              throws WebServiceProxyException,Exception
  {
    if(_proxy == null)
      setWebServiceProxy();

    WebServiceMethod wsm = _proxy.getMethod(opName);
    Object objRet = wsm.invoke(alPartNames,
                               alPartValues);
                               
    return objRet;
  }
  
  	
  /**
   * Sets a property for the web service factory
   */
  public void setProperty(String name , String value) 
  {
    _htProperties.put(name, value); 
  }
  
  /**
   * Sets the member _proxy to a new WebServiceProxy 
   */ 
   public void setWebServiceProxy() throws Exception 
  {
      // sets _proxy
      URL url = null;
      try { url = new URL(getWsdlUrl()); }
      catch(Exception e) 
      {
       throw new Exception("Invalid WSDL URL: " + e.getMessage());
      }
	  
	  if(_httpProxyHost == null)
		  _httpProxyHost = System.getProperty("http.proxyHost");
	  if(_httpProxyPort == null)
		  _httpProxyPort = System.getProperty("http.proxyPort");

      if(_httpProxyHost != null)
	  {
		setProperty("http.proxyHost",_httpProxyHost);
	  }
	  if(_httpProxyPort != null)
	  {
		setProperty("http.proxyPort",_httpProxyPort);
	  }

	  _proxyF.setProperties(_htProperties);
	  _proxy = _proxyF.createWebServiceProxy(url);
  }
	
  /**
   * Format String to have proper line breaks.
   */
  public String formatText(String str)
  {
    String returnVal = null;
    StringBuffer strBuf = new StringBuffer(str);
    int idx = 0;
    for(;;)
    {
      idx = strBuf.toString().indexOf("\n",idx);
      if(idx == -1)
        break;
      strBuf = strBuf.insert(idx,"<BR>");
      idx += 5;
    } 
    returnVal = strBuf.toString();
    return returnVal;
  } 
  /**
   * Release resources
   */
  public void release() 
  {
  	_proxy = null;
    _proxyF = new WebServiceProxyFactory();
    _htProperties.clear();
    _alMaps.clear();
  }
  
  public void   setWsdlUrl(String wUrl) { _wsdlUrl = wUrl; }
  public String getWsdlUrl() { return _wsdlUrl; }
  public WebServiceProxy getWebServiceProxy() throws Exception
  { 
	  if(_proxy == null)
		setWebServiceProxy();
	  return _proxy; 
  }
   
  public void   setHttpProxyHost(String h) { _httpProxyHost = h; }
  public String getHttpProxyHost() { return _httpProxyHost; }
  public void   setHttpProxyPort(String p) { _httpProxyPort = p; }
  public String getHttpProxyPort() { return _httpProxyPort; }
  
  
  private WebServiceProxyFactory _proxyF;
  private Hashtable _htProperties;
  private ArrayList _alMaps;

  private WebServiceProxy _proxy;
  private String _wsdlUrl;
  private String _scope;
  private String _binding;
  private String _soapLocation;
  private String _service;
  private String _port;
  private String _httpProxyHost;
  private String _httpProxyPort;
}