001package com.ganteater.ae.processor;
002
003import java.io.File;
004import java.util.HashMap;
005import java.util.Map;
006
007import com.ganteater.ae.CommandException;
008import com.ganteater.ae.ILogger;
009import com.ganteater.ae.util.xml.easyparser.Node;
010
011public class TaskProcessorThread implements Runnable {
012
013        public static final String THREAD_ID = "THREAD_ID";
014
015        private Node task;
016        private File baseDir;
017        private Processor parent;
018        private Processor processor;
019        private ILogger log;
020        private Map<String, Object> variables = new HashMap<String, Object>();
021
022        public TaskProcessorThread(Processor parent, Node task, File aBaseDir, ILogger aLog) throws CommandException {
023                this.parent = parent;
024                parent.regChildProcess(this);
025
026                this.task = task;
027                baseDir = aBaseDir;
028                log = aLog;
029
030                processor = new BaseProcessor(parent.getConfigNode(), parent.getStartVariables(), parent.getListener(), null,
031                                log, parent);
032
033                processor.setTestListener(parent.getListener());
034        }
035
036        public void run() {
037
038                Node theRunNode = new Node("Recipe");
039                theRunNode.setAttributes(task.getAttributes());
040                theRunNode.add(task);
041                String name = theRunNode.getAttribute("description");
042                if (name == null) {
043                        name = theRunNode.getAttribute("name");
044                }
045                if (name == null) {
046                        name = Integer.toString(hashCode());
047                }
048                log.debug("Thread '" + name + "' is started.");
049                try {
050                        variables.putAll(parent.getVariables());
051                        processor.processTesting(theRunNode, variables, baseDir);
052
053                } catch (Throwable e) {
054                        log.error("Exception in thread. Error: " + e.getMessage());
055                        log.debug("Stack trace", e);
056
057                } finally {
058
059                        parent.unregChildProcess(this);
060                        log.debug("Thread '" + name + "' is stopped.");
061                }
062        }
063
064        public void stopTest() {
065                processor.stop();
066        }
067
068        public void getVaribleValue(String aName, String aValue) {
069                variables.put(aName, aValue);
070        }
071
072        public Processor getProcessor() {
073                return processor;
074        }
075
076        public TaskProcessorThread clone(int cloneId) throws CommandException {
077                TaskProcessorThread clone;
078                if (cloneId == 0) {
079                        clone = this;
080                } else {
081                        String aLogName = this.log.getName() + " #" + cloneId;
082                        ILogger log = parent.getListener().createLog(aLogName, false);
083                        clone = new TaskProcessorThread(parent, task, baseDir, log);
084                }
085                clone.processor.setVariableValue(THREAD_ID, String.valueOf(cloneId));
086                return clone;
087        }
088}