/*
* $Id: FunctionTask.java,v 1.4 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.runtime;
import anvil.core.Any;
import anvil.Log;
import anvil.script.Context;
import anvil.script.Function;
import anvil.script.ExitException;
import anvil.script.ScriptException;
import anvil.java.lang.Task;
/**
* class FunctionTask
*
* @author: Jani Lehtim�ki
*/
public class FunctionTask implements Task
{
private Context _context = null;
private Any _callable = null;
private Any[] _parameters = null;
public FunctionTask()
{
}
public FunctionTask(Context context, Any callable, Any[] parameters)
{
init(context);
prepare(callable, parameters);
}
public void init(Context context)
{
_context = context.copy(null);
}
public void prepare(Any callable, Any[] parameters)
{
_callable = callable;
_parameters = parameters;
}
public void shutdown()
{
Context ctx = _context;
if (ctx != null) {
ctx.log().info("Thread shutting down");
}
clear();
}
public void overload()
{
Context ctx = _context;
if (ctx != null) {
ctx.log().error("Overload, cannot start thread");
}
clear();
}
public void run()
{
Context context = _context;
Any callable = _callable;
Any[] parameters = _parameters;
if (context != null && callable != null && parameters != null) {
context.join(Thread.currentThread());
try {
context.log().info("Executing function: "+_callable);
Any rv = _callable.execute(context, parameters);
context.log().info("Function "+_callable+" exited, return value: "+rv);
} catch (ExitException e) {
context.log().info("Explicit thread termination, exit value: "+e.getExitValue());
} catch (ScriptException e) {
context.log().info("Uncaught exception\n" + e.getData());
} catch (Throwable t) {
context.log().info("Uncaught exception", t);
}
}
clear();
}
public void clear()
{
Context ctx = _context;
if (ctx != null) {
ctx.destroy();
}
_context = null;
_callable = null;
_parameters = null;
}
}