Hi Peter,
Hi Slava,

Here is what I have done, to run programs on the server and to reflect
their stdout to
a Web-Browser (I have stripped some code to make things a little bit
clearer  (temp-file handling,
environment settings, ...) ):

The key elements are a tool running in session scope and a velocity screen
for displaying
the output of the program.

Hope that helps

/Uwe

java-part:
=======

// MODULE: TaskManager header
/**
 *
 */
package de.gea.wiegand.teben.tools.task;

// java classes
import java.lang.*;
import java.util.*;
import java.io.*;

// turbine classes
import org.apache.turbine.services.resources.*;
import org.apache.turbine.services.servlet.*;
import org.apache.turbine.util.*;
import org.apache.turbine.util.template.TemplateLink;
import org.apache.turbine.util.Log;
import org.apache.turbine.om.security.TurbineUser;
import org.apache.turbine.om.security.User;

// javax classes
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionBindingEvent;

class Task extends Thread
{
  private TaskManager taskMgr;

  public Task(TaskManager tmgr) {
    taskMgr = tmgr;
  }

  public void run () throws ThreadDeath
  {
    try {

      // get remaining parameters for exec
      // and start the process
      String complete_cmd = taskMgr.createCompleteCmd();
      String[] envp = taskMgr.getEnvironment();
      File    dir  = new File(TurbineServlet.getRealPath("/"));
      Process proc = Runtime.getRuntime().exec(complete_cmd,envp,dir);

      // cleanup stdout etc
      taskMgr.stdout.clear();
      taskMgr.stderr.clear();

      taskMgr.state = 1;
      taskMgr.is = proc.getInputStream();
      taskMgr.es = proc.getErrorStream();

      // read from process until the process terminates
      if (taskMgr.is != null) {

        // process stdout of the program
        BufferedReader bf = new BufferedReader(new
InputStreamReader(taskMgr.is));
        String s;
        while ((s = bf.readLine()) != null)
        {
          synchronized(taskMgr.stdout) {
            taskMgr.stdout.addElement(s);
          }
        }

        // now process the error stream
        bf = new BufferedReader(new InputStreamReader(taskMgr.es));
        while ((s = bf.readLine()) != null)
        {
          synchronized(taskMgr.stdout) {
            taskMgr.stdout.addElement(s);
          }
        }
      }
      else
        System.out.println("No stdout");

      // wait until process has completely finished
      proc.waitFor();

      // set state to finished
      // so the watchdog knows he can finish
      // and at the next refresh the screen
      // knows that he should switch to show
      // the result file
      //////////////////////////////////////////////////
      taskMgr.state = 2;
      taskMgr.retCode = proc.exitValue();
      taskMgr.addTaskResult();

    }
    catch (ThreadDeath td) { taskMgr.state = 2; throw td; }
    catch (Exception e) {
      taskMgr.stdout.add("<p class=\"error\">Got an exception running
program :"
                         + e.getMessage());
      taskMgr.state = 2;
    }

    finally { taskMgr.setDefaults(); }
  }
}

public class TaskManager
      extends Thread
      implements org.apache.turbine.services.pull.ApplicationTool,
        java.io.Serializable,
        HttpSessionBindingListener
{
  InputStream is;
  InputStream es;
  HashMap envStrings;
  String cmd;
  Vector stdout;
  Vector stderr;
  int state;
  int retCode;

  private Vector taskResults;

  /**
   * refresh time for the screen
   */
  private long refresh;

  /**
   * the screen to display stdout and the result file
   * should be set in turbineResource.properties
   * could be given as: tool.task.screen=run.vm
   */
  private String taskScreen;

  private static final String TASK_SCREEN = "tool.task.screen";

  /**
   * default refresh time in msec
   */
  private static final String TASK_REFRESH = "tool.task.refresh";

  /**
   * default task parameters passed in the commandline
   * as the parameters following the tempfile name
   */
  private static final String TASK_DEFAULT_PARMS = "tool.task.parms";

  /**
   * where to find scripts and binaries
   */
  private static final String TASK_BIN = "tool.task.bin";

  public int getState ()
  {
    return state;
  }

  public int getRetCode()
  {
    return retCode;
  }

  /**
   * returns an enumeration of the current output of the program
   * it also sets the lastRefresh time, so the watchdogs knows
   * that the user is still interested in the results ...
   * @return Enumeration of Strings
   */
  public Enumeration getOutput ()
  {
    return stdout.elements();
  }

  public long getRefresh()
  {
    return refresh;
  }

  /**
   * calculates the refresh in seconds and returns
   * them as int because velocity can�t deal with
   * longs ...
   *
   * @return timeout in seconds
   */
  public int getRefreshSeconds()
  {
    return (int) (refresh/1000);
  }

  public void setRefresh(long r)
  {
    refresh = r;
  }
  ....

  public void execute (RunData data, String cmd, boolean doWait)
  {
    Task task = new Task(this);

    this.cmd = cmd;
    state = 0;

    if (data.getSession().getAttribute("task") == null)
      data.getSession().setAttribute("task",this);

    if (doWait) {
      task.run();
    }
    else {
      data.setScreenTemplate(taskScreen);
      task.start();
    }
  }

  public TaskManager ()
  {
    cmd = null;
    state = 0;
    is = null;
    es = null;
    stdout = new Vector();
    stderr = new Vector();
    parmStrings = new Vector();
    envStrings = new HashMap();
    taskResults = new Vector();

    retCode = 0;
    tmpfile = null;
    tmpFiles = new Vector();
    returnLink = null;
    taskScreen = TurbineResources.getString(TASK_SCREEN,"");

    setDefaults();
  }

  /**
   * set the default values as given in TurbineResource.properties
   * (or hardcoded)
   * is called at the end of each task, and at initialization
   */
  void setDefaults()
  {
    try {
      refresh = TurbineResources.getLong(TASK_REFRESH,20000);
      envStrings.clear();
      parmStrings.clear();
      defaultParms = TurbineResources.getString(TASK_DEFAULT_PARMS,"");
    }
    catch(Exception e) {
      Log.error("task","TaskManager: Error setting defaults:" , e);
      refresh = 20000;
    }
  }

  /**
   * Does additional setup
   * @param u
   */
  public void init (Object u)
  {
  }

  public void refresh()
  {
  }

}


Velocity-part:
==========

$page.setHttpEquiv("expires", "0")

  <h2>STDOUT of the program:</h2>

  #foreach($line in $task.Output)
     $line <br>
  #end

  #if( $task.State > 1 )
     <form action="$link.setPage("Result.vm")">
     <input type="hidden" name="ausgabe_anfordern" value="1">
     <input type="submit" value="Weiter ...">
     </form>
  #else
    #set( $content=$link.setPage("RunTool.vm") )
    #set( $refresh=$task.Refresh/1000 )
    $page.setHttpEquiv("refresh", "$task.RefreshSeconds; url=$content")
  #end



                                                                                       
                           
                      Peter Courcoux                                                   
                           
                      <[EMAIL PROTECTED]        An:       Turbine Users List 
<[EMAIL PROTECTED]>     
                      iz>                      Kopie:                                  
                           
                                               Thema:    Re: Antwort: upload progress  
                           
                      07.02.2003 18:02                                                 
                           
                      Bitte antworten                                                  
                           
                      an "Turbine Users                                                
                           
                      List"                                                            
                           
                                                                                       
                           
                                                                                       
                           




Hi Uwe,

Yes please, could you send me the screen source as well. I have not
thought through all the options for this yet and I would be very
interested to see what you have done.

Many thanks,

Peter

On Fri, 2003-02-07 at 15:44, [EMAIL PROTECTED] wrote:
> Hi Peter
>
> I've implemented something similar. I am executing some long time running
> programs
> on the server, returning their stdout to a velocity screen. The screen
> reloads it self every 10 seconds
> and pulls out the stdout from the tool that controls the program. This
> happens until the tool signals that
> the program has finished.
>
> Looks like you wan�t to implement something much the same.
> If you are interested I can send you the source of that tool ...
>
> /uwe
>
>
>
>

>                       Peter Courcoux

>                       <[EMAIL PROTECTED]        An:       Turbine Users
List <[EMAIL PROTECTED]>
>                       iz>                      Kopie:

>                                                Thema:    upload progress

>                       07.02.2003 13:12

>                       Bitte antworten

>                       an "Turbine Users

>                       List"

>

>

>
>
>
>
> Hi all,
>
> I'm toying with the idea of implementing an upload progress monitor for
> the upload service. It looks like I may have to patch the upload
> service, write a pull tool and write a HOWTO to do this.
>
> Has anyone done this already or have any thoughts or comments? Is anyone
> interested?
> --
> Peter Courcoux <[EMAIL PROTECTED]>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
--
Peter Courcoux <[EMAIL PROTECTED]>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to