package tutorial.sample17;

import java.io.IOException;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import weblogic.webservice.async.FutureResult;
import weblogic.webservice.async.AsyncInfo;
import weblogic.webservice.async.ResultListener;
import weblogic.webservice.async.InvokeCompletedEvent;

public class Main{

  private boolean gotCallback = false;

  public static void main( String[] args ){

    if( args.length == 1 ){
      new Main( args[0] );
    }else{
      throw new IllegalArgumentException( "URL of the service not specified" );
    }
  }

  public Main( String wsdlUrl ){
    try{
      SimpleTest service = new SimpleTest_Impl( wsdlUrl );
      SimpleTestSoap port = service.getSimpleTestSoap();

      invokeNormal( port );
      invokeCallback( port );
      invokePoll( port );
      invokeSync( port );

      waitForCallback();
    }catch( IOException e ){
      System.out.println( "Failed to create web service client:" + e );
    }catch( ServiceException e ){
      System.out.println( "Failed to create web service client:" + e );
    }
  }


  public void invokeCallback( SimpleTestSoap echoPort ) throws IOException{

    AsyncInfo wsContext = new AsyncInfo();

    wsContext.setResultListener( new ResultListener(){
      public void onCompletion( InvokeCompletedEvent event ){

        SimpleTestSoap source = (SimpleTestSoap)event.getSource();

        try{
          String result = source.endEchoString( event.getFutureResult() );
          weblogic.utils.Debug.say( "callback result: " + result ); 
          gotCallback = true;
        }catch( RemoteException e ){
          e.printStackTrace( System.out );
        }
      }
    });

    echoPort.startEchoString( "94501", wsContext );
  }

  
  public void invokeNormal( SimpleTestSoap echoPort ) throws IOException{
    String result = echoPort.echoString( "94501" );
    weblogic.utils.Debug.say( "Result from normal invoke: " + result );
  }

  public void invokePoll( SimpleTestSoap echoPort ) throws IOException{
    FutureResult futureResult = echoPort.startEchoString( "94501", null );

    while( !futureResult.isCompleted() ){
      weblogic.utils.Debug.say( "invokePoll: waiting for result ..." );
      try{
        Thread.sleep( 300 );
      }catch( InterruptedException e ){}
    }

    String result = echoPort.endEchoString( futureResult );
    weblogic.utils.Debug.say( "poll result: " + result ); 
  }

  public void invokeSync( SimpleTestSoap echoPort ) throws IOException{
    FutureResult futureResult = echoPort.startEchoString( "94501", null );
    //do something else
    String result = echoPort.endEchoString( futureResult );
    weblogic.utils.Debug.say( "sync result: " + result );
  }

  private void waitForCallback(){

    while( !gotCallback ){
      weblogic.utils.Debug.say( "callback waiting  for result  ..." );
      try{
        Thread.sleep( 300 );
      }catch( InterruptedException e ){}
    }
  }
}

