1 52 53 package freemarker.template.utility; 54 55 import freemarker.template.*; 56 import java.io.*; 57 import java.util.*; 58 59 92 public class Execute implements freemarker.template.TemplateMethodModel { 93 94 private final static int OUTPUT_BUFFER_SIZE = 1024; 95 96 103 public Object exec (List arguments) throws TemplateModelException { 104 String aExecute; 105 StringBuffer aOutputBuffer = new StringBuffer (); 106 107 if( arguments.size() < 1 ) { 108 throw new TemplateModelException( "Need an argument to execute" ); 109 } 110 111 aExecute = (String )(arguments.get(0)); 112 113 try { 114 Process exec = Runtime.getRuntime().exec( aExecute ); 115 116 InputStream execOut = exec.getInputStream(); 118 Reader execReader = new InputStreamReader( execOut ); 119 120 char[] buffer = new char[ OUTPUT_BUFFER_SIZE ]; 121 int bytes_read = execReader.read( buffer ); 122 123 while( bytes_read > 0 ) { 124 aOutputBuffer.append( buffer, 0, bytes_read ); 125 bytes_read = execReader.read( buffer ); 126 } 127 } 128 catch( IOException ioe ) { 129 throw new TemplateModelException( ioe.getMessage() ); 130 } 131 return aOutputBuffer.toString(); 132 } 133 } 134 | Popular Tags |