001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.template.utility;
054:
055: import freemarker.template.*;
056: import java.io.*;
057: import java.util.*;
058:
059: /**
060: * <p>Gives FreeMarker the the ability to execute external commands. Will fork
061: * a process, and inline anything that process sends to stdout in the template.
062: * Based on a patch submitted by Peter Molettiere.</p>
063: *
064: * <p>BE CAREFUL! this tag, depending on use, may allow you
065: * to set something up so that users of your web
066: * application could run arbitrary code on your server.
067: * This can only happen if you allow unchecked GET/POST
068: * submissions to be used as the command string in the
069: * exec tag.</p>
070: *
071: * <p>Usage:<br />
072: * From java:</p>
073: * <pre>
074: * SimpleHash root = new SimpleHash();
075: *
076: * root.put( "exec", new freemarker.template.utility.Execute() );
077: *
078: * ...
079: * </pre>
080: *
081: * <p>From your FreeMarker template:</p>
082: * <pre>
083: *
084: * The following is executed:
085: * ${exec( "/usr/bin/ls" )}
086: *
087: * ...
088: * </pre>
089: *
090: * @version $Id: Execute.java,v 1.14 2003/10/13 11:57:18 szegedia Exp $
091: */
092: public class Execute implements freemarker.template.TemplateMethodModel {
093:
094: private final static int OUTPUT_BUFFER_SIZE = 1024;
095:
096: /**
097: * Executes a method call.
098: *
099: * @param arguments a <tt>List</tt> of <tt>String</tt> objects containing the values
100: * of the arguments passed to the method.
101: * @return the <tt>TemplateModel</tt> produced by the method, or null.
102: */
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(
109: "Need an argument to execute");
110: }
111:
112: aExecute = (String) (arguments.get(0));
113:
114: try {
115: Process exec = Runtime.getRuntime().exec(aExecute);
116:
117: // stdout from the process comes in here
118: InputStream execOut = exec.getInputStream();
119: Reader execReader = new InputStreamReader(execOut);
120:
121: char[] buffer = new char[OUTPUT_BUFFER_SIZE];
122: int bytes_read = execReader.read(buffer);
123:
124: while (bytes_read > 0) {
125: aOutputBuffer.append(buffer, 0, bytes_read);
126: bytes_read = execReader.read(buffer);
127: }
128: } catch (IOException ioe) {
129: throw new TemplateModelException(ioe.getMessage());
130: }
131: return aOutputBuffer.toString();
132: }
133: }
|