001    
002    package railo.runtime.tag;
003    
004    import railo.commons.cli.Command;
005    import railo.commons.io.IOUtil;
006    import railo.commons.io.SystemUtil;
007    import railo.commons.io.res.Resource;
008    import railo.runtime.PageContext;
009    
010    /**
011     * 
012     */
013    public final class _Execute extends Thread {
014    
015        private PageContext pc;
016        private Resource outputfile;
017        private String variable;
018        private boolean aborted;
019        private String command;
020        //private static final int BLOCK_SIZE=4096;
021        private Object monitor;
022            private Exception exception;
023            //private String body;
024            private boolean finished;
025            private Process process;
026    
027        /**
028         * @param pageContext
029         * @param monitor
030         * @param process
031         * @param outputfile
032         * @param variable
033         * @param body 
034         * @param terminateOnTimeout 
035         */
036        public _Execute(PageContext pageContext, Object monitor, String command, Resource outputfile, String variable, String body) {
037             this.pc=pageContext; 
038             this.monitor=monitor;
039             this.command=command;
040             this.outputfile=outputfile;
041             this.variable=variable;
042             //this.body=body;
043        }
044        
045        @Override
046        public void run() {
047            try {
048                _run();
049            } catch (Exception e) {}
050        }
051         void _run() {
052            //synchronized(monitor){
053                            try {
054                                    String rst=null;
055                                    
056                                    process = Command.createProcess(command,true);
057                                    rst=Command.execute(process);
058                                    finished = true;
059                                    if(!aborted) {
060                                            if(outputfile==null && variable==null) pc.write(rst);
061                                            else {
062                                                    if(outputfile!=null)    IOUtil.write(outputfile, rst, SystemUtil.getCharset(), false);
063                                                    if(variable!=null)      pc.setVariable(variable,rst);
064                                            }
065                                    }
066                            }
067                            catch(Exception ioe){
068                                    exception=ioe;
069                            }
070                            finally {
071                                    synchronized(monitor){
072                                            monitor.notify();
073                                    }
074                            }
075                    //}
076        }
077    
078        /**
079         * define that execution is aborted
080         */
081        public void abort(boolean terminateProcess) {
082            aborted=true;
083            if(terminateProcess)process.destroy();
084        }
085    
086            public boolean hasException() {
087                    return exception!=null;
088            }
089            public boolean hasFinished() {
090                    return finished;
091            }
092    
093            /**
094             * @return the exception
095             */
096            public Exception getException() {
097                    return exception;
098            }
099    
100    }