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        /**
046         * @see java.lang.Thread#run()
047         */
048        public void run() {
049            try {
050                _run();
051            } catch (Exception e) {}
052        }
053         void _run() {
054            //synchronized(monitor){
055                            try {
056                                    String rst=null;
057                                    
058                                    process = Command.createProcess(command,true);
059                                    rst=Command.execute(process);
060                                    finished = true;
061                                    if(!aborted) {
062                                            if(outputfile==null && variable==null) pc.write(rst);
063                                            else {
064                                                    if(outputfile!=null)    IOUtil.write(outputfile, rst, SystemUtil.getCharset(), false);
065                                                    if(variable!=null)      pc.setVariable(variable,rst);
066                                            }
067                                    }
068                            }
069                            catch(Exception ioe){
070                                    exception=ioe;
071                            }
072                            finally {
073                                    synchronized(monitor){
074                                            monitor.notify();
075                                    }
076                            }
077                    //}
078        }
079    
080        /**
081         * define that execution is aborted
082         */
083        public void abort(boolean terminateProcess) {
084            aborted=true;
085            if(terminateProcess)process.destroy();
086        }
087    
088            public boolean hasException() {
089                    return exception!=null;
090            }
091            public boolean hasFinished() {
092                    return finished;
093            }
094    
095            /**
096             * @return the exception
097             */
098            public Exception getException() {
099                    return exception;
100            }
101    
102    }