001    /*
002     * Created on Jul 7, 2005 @author paulk
003     */
004    package toolbus.atom.tool;
005    
006    import toolbus.TBTermFactory;
007    import toolbus.TBTermVar;
008    import toolbus.atom.Atom;
009    import toolbus.atom.Ref;
010    import toolbus.exceptions.ToolBusException;
011    import toolbus.logging.ILogger;
012    import toolbus.logging.IToolBusLoggerConstants;
013    import toolbus.logging.LoggerFactory;
014    import toolbus.parsercup.PositionInformation;
015    import toolbus.process.ProcessExpression;
016    import toolbus.tool.ToolInstance;
017    import aterm.ATerm;
018    
019    public class SndKill extends Atom{
020            private final Ref toolId;
021            private final Ref message;
022            private ToolInstance toolInstance;
023            
024            public SndKill(ATerm toolId, ATerm value, TBTermFactory tbfactory, PositionInformation posInfo){
025                    super(tbfactory, posInfo);
026                    
027                    this.toolId = new Ref(toolId);
028                    this.message = new Ref(value);
029                    setAtomArgs(new Ref[]{this.toolId});
030                    externalNameAsReceivedByTool = "snd-kill";
031            }
032            
033            /**
034             * @see toolbus.process.ProcessExpression#copy()
035             */
036            public ProcessExpression copy(){
037                    Atom a = new SndKill(toolId.value, message.value, tbfactory, getPosInfo());
038                    a.copyAtomAttributes(this);
039                    
040                    return a;
041            }
042            
043            public void activate(){
044                    toolInstance = null;
045                    super.activate();
046            }
047            
048            public boolean execute() throws ToolBusException{
049                    if(!isEnabled()) return false;
050                    
051                    if(toolInstance == null){
052                            ATerm tid = getEnv().getValue((TBTermVar) toolId.value);
053                            toolInstance = getToolBus().getToolInstanceManager().get(tid);
054                            if(toolInstance == null) return false;
055                    }
056                    
057                    if(toolInstance.isExecutedTool()){
058                            toolInstance.kill();
059                    }else{
060                            LoggerFactory.log("Unable to kill tool with id: "+getEnv().getValue((TBTermVar) toolId.value)+", since it it's not a remotely executed tool.", ILogger.ERROR, IToolBusLoggerConstants.TOOLCOM);
061                    }
062                    //LoggerFactory.log(getProcess().getProcessName(), "Send kill: "+message.value, IToolBusLoggerConstants.TOOLCOM);
063                    return true;
064            }
065    }