001: /*
002: * $Id: FunctionTask.java,v 1.4 2002/09/16 08:05:03 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.runtime;
011:
012: import anvil.core.Any;
013: import anvil.Log;
014: import anvil.script.Context;
015: import anvil.script.Function;
016: import anvil.script.ExitException;
017: import anvil.script.ScriptException;
018: import anvil.java.lang.Task;
019:
020: /**
021: * class FunctionTask
022: *
023: * @author: Jani Lehtimäki
024: */
025: public class FunctionTask implements Task {
026:
027: private Context _context = null;
028: private Any _callable = null;
029: private Any[] _parameters = null;
030:
031: public FunctionTask() {
032: }
033:
034: public FunctionTask(Context context, Any callable, Any[] parameters) {
035: init(context);
036: prepare(callable, parameters);
037: }
038:
039: public void init(Context context) {
040: _context = context.copy(null);
041: }
042:
043: public void prepare(Any callable, Any[] parameters) {
044: _callable = callable;
045: _parameters = parameters;
046: }
047:
048: public void shutdown() {
049: Context ctx = _context;
050: if (ctx != null) {
051: ctx.log().info("Thread shutting down");
052: }
053: clear();
054: }
055:
056: public void overload() {
057: Context ctx = _context;
058: if (ctx != null) {
059: ctx.log().error("Overload, cannot start thread");
060: }
061: clear();
062: }
063:
064: public void run() {
065: Context context = _context;
066: Any callable = _callable;
067: Any[] parameters = _parameters;
068: if (context != null && callable != null && parameters != null) {
069: context.join(Thread.currentThread());
070: try {
071: context.log().info("Executing function: " + _callable);
072: Any rv = _callable.execute(context, parameters);
073: context.log().info(
074: "Function " + _callable
075: + " exited, return value: " + rv);
076:
077: } catch (ExitException e) {
078: context.log().info(
079: "Explicit thread termination, exit value: "
080: + e.getExitValue());
081:
082: } catch (ScriptException e) {
083: context.log()
084: .info("Uncaught exception\n" + e.getData());
085:
086: } catch (Throwable t) {
087: context.log().info("Uncaught exception", t);
088: }
089: }
090: clear();
091: }
092:
093: public void clear() {
094: Context ctx = _context;
095: if (ctx != null) {
096: ctx.destroy();
097: }
098: _context = null;
099: _callable = null;
100: _parameters = null;
101: }
102: }
|