KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > ConsoleCommandHandler


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

17 package org.apache.activemq.console;
18
19 import org.apache.activemq.broker.util.CommandHandler;
20 import org.apache.activemq.console.command.ShellCommand;
21 import org.apache.activemq.console.formatter.GlobalWriter;
22 import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
23
24 import javax.jms.TextMessage JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29
30 /**
31  * A default implementation of the @{link CommandHandler} interface
32  *
33  * @version $Revision: $
34  */

35 public class ConsoleCommandHandler implements CommandHandler {
36
37     private ShellCommand command = new ShellCommand(true);
38
39     public void processCommand(TextMessage JavaDoc request, TextMessage JavaDoc response) throws Exception JavaDoc {
40
41         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
42         GlobalWriter.instantiate(new CommandShellOutputFormatter(out));
43
44         // lets turn the text into a list of arguments
45
String JavaDoc requestText = request.getText();
46
47         List JavaDoc tokens = tokenize(requestText);
48         command.execute(tokens);
49
50         out.flush();
51         byte[] bytes = out.toByteArray();
52
53         String JavaDoc answer = new String JavaDoc(bytes);
54
55         response.setText(answer);
56     }
57
58     protected List JavaDoc tokenize(String JavaDoc text) {
59         List JavaDoc answer = new ArrayList JavaDoc();
60         StringTokenizer JavaDoc iter = new StringTokenizer JavaDoc(text);
61         while (iter.hasMoreTokens()) {
62             answer.add(iter.nextToken());
63         }
64         return answer;
65     }
66 }
67
Popular Tags