KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > task > CommandLineTask


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

17
18 /* $Id: CommandLineTask.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.task;
21
22
23 public class CommandLineTask extends AbstractTask {
24     /**
25      * Creates a new CommandLineTask object.
26      */

27     public CommandLineTask() {
28     }
29
30     /**
31      * Execute the task. All parameters must have been set with parameterize().
32      *
33      * (non-Javadoc)
34      * @see org.apache.lenya.cms.task.Task#execute(java.lang.String)
35      */

36     public void execute(String JavaDoc path) throws ExecutionException {
37         String JavaDoc command = getParameters().getParameter("command",
38                 "echo \"Exception: No command parameter\"");
39
40         try {
41             Process JavaDoc process = Runtime.getRuntime().exec(command);
42
43             java.io.InputStream JavaDoc in = process.getInputStream();
44             byte[] buffer = new byte[1024];
45             int bytes_read = 0;
46             java.io.ByteArrayOutputStream JavaDoc baout = new java.io.ByteArrayOutputStream JavaDoc();
47
48             while ((bytes_read = in.read(buffer)) != -1) {
49                 baout.write(buffer, 0, bytes_read);
50             }
51
52             if (baout.toString().length() > 0) {
53                 throw new ExecutionException("%%%InputStream:S" + baout.toString() +
54                     "END:InputStream%%%");
55             }
56
57             java.io.InputStream JavaDoc in_e = process.getErrorStream();
58             java.io.ByteArrayOutputStream JavaDoc baout_e = new java.io.ByteArrayOutputStream JavaDoc();
59
60             while ((bytes_read = in_e.read(buffer)) != -1) {
61                 baout_e.write(buffer, 0, bytes_read);
62             }
63
64             if (baout_e.toString().length() > 0) {
65                 throw new ExecutionException("###ErrorStream:START" + baout_e.toString() +
66                     "END:ErrorStream###");
67             }
68         } catch (java.io.IOException JavaDoc e) {
69             throw new ExecutionException(e);
70         }
71     }
72 }
73
Popular Tags