001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019 020package lucee.runtime.tag; 021 022import lucee.commons.cli.Command; 023import lucee.commons.cli.CommandResult; 024import lucee.commons.io.IOUtil; 025import lucee.commons.io.SystemUtil; 026import lucee.commons.io.res.Resource; 027import lucee.runtime.PageContext; 028 029/** 030 * 031 */ 032public final class _Execute extends Thread { 033 034 private PageContext pc; 035 private Resource outputfile; 036 private Resource errorFile; 037 private String variable; 038 private String errorVariable; 039 private boolean aborted; 040 private String command; 041 //private static final int BLOCK_SIZE=4096; 042 private Object monitor; 043 private Exception exception; 044 //private String body; 045 private boolean finished; 046 private Process process; 047 048 /** 049 * @param pageContext 050 * @param monitor 051 * @param process 052 * @param outputfile 053 * @param variable 054 * @param body 055 * @param terminateOnTimeout 056 */ 057 public _Execute(PageContext pageContext, Object monitor, String command, Resource outputfile, String variable, Resource errorFile, String errorVariable) { 058 this.pc=pageContext; 059 this.monitor=monitor; 060 this.command=command; 061 this.outputfile=outputfile; 062 this.variable=variable; 063 064 this.errorFile = errorFile; 065 this.errorVariable = errorVariable; 066 //this.body=body; 067 } 068 069 @Override 070 public void run() { 071 try { 072 _run(); 073 } catch (Exception e) {} 074 } 075 void _run() { 076 //synchronized(monitor){ 077 try { 078 079 process = Command.createProcess(command,true); 080 081 CommandResult result = Command.execute(process); 082 String rst = result.getOutput(); 083 084 finished = true; 085 if(!aborted) { 086 if(outputfile==null && variable==null) pc.write(rst); 087 else { 088 if(outputfile!=null) IOUtil.write(outputfile, rst, SystemUtil.getCharset(), false); 089 if(variable!=null) pc.setVariable(variable,rst); 090 } 091 092 if(errorFile != null) IOUtil.write(errorFile, result.getError(), SystemUtil.getCharset(), false); 093 if(errorVariable != null) pc.setVariable(errorVariable, result.getError()); 094 } 095 } 096 catch(Exception ioe){ 097 exception=ioe; 098 } 099 finally { 100 synchronized(monitor){ 101 monitor.notify(); 102 } 103 } 104 //} 105 } 106 107 /** 108 * define that execution is aborted 109 */ 110 public void abort(boolean terminateProcess) { 111 aborted=true; 112 if(terminateProcess)process.destroy(); 113 } 114 115 public boolean hasException() { 116 return exception!=null; 117 } 118 public boolean hasFinished() { 119 return finished; 120 } 121 122 /** 123 * @return the exception 124 */ 125 public Exception getException() { 126 return exception; 127 } 128 129}