package oracle.forms.fd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.VBean;
import oracle.forms.properties.ID;
import oracle.forms.ui.CustomEvent;

/**
   * A Bean to execute program on the local machine
   *
   * @author Francois Degrelle (bean)
   * @author Fabio MARAZZATO (process and output stuff)
   * @author Yann D'ISANTO   (process and output stuff)
   * @version 1.0
   */
   
public class JavaHost extends VBean
{
  private static final ID SetProg     = ID.registerProperty("SET_PROG");      
  private static final ID MSGSTND     = ID.registerProperty("MSGSTND");         
  private static final ID MSGERROR    = ID.registerProperty("MSGERROR");  
  private static final ID MSGTEXT     = ID.registerProperty("MSGTEXT");       
  private IHandler     m_handler; 
  Process p = null ;
  
  public JavaHost()
  {
  }
  
  public void init(IHandler handler)
  {
    m_handler = handler;
    super.init(handler);
  }    

  /*************************
   *  Set some properties  *
   ************************/
  public boolean setProperty(ID property, Object value)
  {
    /*
     *  Start the process  *
     */
    if (property == SetProg)   
    {
      String s= value.toString(), s2="" ;
      String[] sTabParams = null ;
      int iNbParams = 0, iPos=-1 ;
      iPos = s.indexOf(",") ;
      if(iPos > -1)
      {
        StringTokenizer st = new StringTokenizer(s,",");
        // get the total number of parameters
        while (st.hasMoreTokens()) 
        {
          s2 = st.nextToken() ;
          iNbParams++;
        }
        sTabParams = new String[iNbParams];
        st = new StringTokenizer(s,",");
        for( int i=0; i<iNbParams; i++ ) sTabParams[i] = st.nextToken() ;
      }
      // launch the programme
      try {  
        if(iPos > -1) p = Runtime.getRuntime().exec(sTabParams); 
        else          p = Runtime.getRuntime().exec(s); 

        new Thread() 
        {  
          public void run() 
          {  
            try 
            {  
              BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); 
              String line = null; 
              try {  
                while((line = br.readLine()) != null) 
                {  
                  // Send standard output to Forms
                  SendProperties(MSGSTND,line);
                }  
            } 
            finally 
            {  
              br.close(); 
            }  
          } 
          catch(IOException ioe)
          {  
            ioe.printStackTrace(); 
            SendProperties(MSGERROR,ioe.getMessage());
          }  
         }  
       }.start(); 
       // Error output
       new Thread() 
       {  
         public void run() 
         {  
           try 
           {  
              BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
              String line = null; 
              try 
              {  
                 while((line = br.readLine()) != null) 
                 {  
                    // Send error output to Forms
                     SendProperties(MSGERROR,line);
                 }  
              } 
              finally 
              {  
                br.close();
              }  
            } 
            catch(IOException ioe)
            {  
              ioe.printStackTrace();
              SendProperties(MSGERROR,ioe.getMessage());
            }  
          }  
        }.start(); 
      } 
      catch(IOException ioe) {  ioe.printStackTrace(); SendProperties(MSGERROR,ioe.getMessage());}

      return true ;
    }
    else
    {
     return super.setProperty(property, value);
    }
  }  

  // send standard/error output back to Forms
  private void SendProperties(ID id, String sMessage) 
  {
    try{
      CustomEvent ce = new CustomEvent(m_handler, id);
      m_handler.setProperty( MSGTEXT, sMessage );
      dispatchCustomEvent(ce);  
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }    
  }
    
}
