KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ssi > SSIExec


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation. Licensed under the
3  * Apache License, Version 2.0 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of the License
5  * at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
6  * law or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */

11 package org.apache.catalina.ssi;
12
13
14 import java.io.BufferedReader JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import org.apache.catalina.util.IOTools;
19 /**
20  * Implements the Server-side #exec command
21  *
22  * @author Bip Thelin
23  * @author Amy Roh
24  * @author Paul Speed
25  * @author Dan Sandberg
26  * @author David Becker
27  * @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
28  */

29 public class SSIExec implements SSICommand {
30     protected SSIInclude ssiInclude = new SSIInclude();
31     protected final static int BUFFER_SIZE = 1024;
32
33
34     /**
35      * @see SSICommand
36      */

37     public long process(SSIMediator ssiMediator, String JavaDoc commandName,
38             String JavaDoc[] paramNames, String JavaDoc[] paramValues, PrintWriter JavaDoc writer) {
39         long lastModified = 0;
40         String JavaDoc configErrMsg = ssiMediator.getConfigErrMsg();
41         String JavaDoc paramName = paramNames[0];
42         String JavaDoc paramValue = paramValues[0];
43         String JavaDoc substitutedValue = ssiMediator.substituteVariables(paramValue);
44         if (paramName.equalsIgnoreCase("cgi")) {
45             lastModified = ssiInclude.process(ssiMediator, "include",
46                                 new String JavaDoc[]{"virtual"}, new String JavaDoc[]{substitutedValue},
47                                 writer);
48         } else if (paramName.equalsIgnoreCase("cmd")) {
49             boolean foundProgram = false;
50             try {
51                 Runtime JavaDoc rt = Runtime.getRuntime();
52                 Process JavaDoc proc = rt.exec(substitutedValue);
53                 foundProgram = true;
54                 BufferedReader JavaDoc stdOutReader = new BufferedReader JavaDoc(
55                         new InputStreamReader JavaDoc(proc.getInputStream()));
56                 BufferedReader JavaDoc stdErrReader = new BufferedReader JavaDoc(
57                         new InputStreamReader JavaDoc(proc.getErrorStream()));
58                 char[] buf = new char[BUFFER_SIZE];
59                 IOTools.flow(stdErrReader, writer, buf);
60                 IOTools.flow(stdOutReader, writer, buf);
61                 proc.waitFor();
62                 lastModified = System.currentTimeMillis();
63             } catch (InterruptedException JavaDoc e) {
64                 ssiMediator.log("Couldn't exec file: " + substitutedValue, e);
65                 writer.write(configErrMsg);
66             } catch (IOException JavaDoc e) {
67                 if (!foundProgram) {
68                     //apache doesn't output an error message if it can't find
69
// a program
70
}
71                 ssiMediator.log("Couldn't exec file: " + substitutedValue, e);
72             }
73         }
74         return lastModified;
75     }
76 }
Popular Tags