KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > command > ShellCommand


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

18 package org.apache.activemq.console.command;
19
20 import org.apache.activemq.console.formatter.GlobalWriter;
21 import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
22
23 import java.util.List JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.PrintStream JavaDoc;
28
29 public class ShellCommand extends AbstractCommand {
30
31     private boolean interactive;
32     private String JavaDoc[] helpFile;
33
34
35     public ShellCommand() {
36         this(false);
37     }
38
39     public ShellCommand(boolean interactive) {
40         this.interactive = interactive;
41         this.helpFile = new String JavaDoc[]{
42                 (interactive ? "Usage: [task] [task-options] [task data]" : "Usage: Main [--extdir <dir>] [task] [task-options] [task data]"),
43                 "",
44                 "Tasks (default task is start):",
45                 " start - Creates and starts a broker using a configuration file, or a broker URI.",
46                 " stop - Stops a running broker specified by the broker name.",
47                 " list - Lists all available brokers in the specified JMX context.",
48                 " query - Display selected broker component's attributes and statistics.",
49                 " browse - Display selected messages in a specified destination.",
50                 "",
51                 "Task Options (Options specific to each task):",
52                 " --extdir <dir> - Add the jar files in the directory to the classpath.",
53                 " --version - Display the version information.",
54                 " -h,-?,--help - Display this help information. To display task specific help, use " + (interactive ? "" : "Main ") + "[task] -h,-?,--help",
55                 "",
56                 "Task Data:",
57                 " - Information needed by each specific task.",
58                 ""
59         };
60     }
61
62     /**
63      * Main method to run a command shell client.
64      * @param args - command line arguments
65      * @param in - input stream to use
66      * @param out - output stream to use
67      * @return 0 for a successful run, -1 if there are any exception
68      */

69     public static int main(String JavaDoc[] args, InputStream JavaDoc in, PrintStream JavaDoc out) {
70         GlobalWriter.instantiate(new CommandShellOutputFormatter(out));
71
72         // Convert arguments to list for easier management
73
List JavaDoc tokens = new ArrayList JavaDoc(Arrays.asList(args));
74
75         ShellCommand main = new ShellCommand();
76         try {
77             main.execute(tokens);
78             return 0;
79         } catch (Exception JavaDoc e) {
80             GlobalWriter.printException(e);
81             return -1;
82         }
83     }
84
85
86     public boolean isInteractive() {
87         return interactive;
88     }
89
90     public void setInteractive(boolean interactive) {
91         this.interactive = interactive;
92     }
93
94     /**
95      * Parses for specific command task.
96      * @param tokens - command arguments
97      * @throws Exception
98      */

99     protected void runTask(List JavaDoc tokens) throws Exception JavaDoc {
100         
101         // Process task token
102
if( tokens.size() > 0 ) {
103             String JavaDoc taskToken = (String JavaDoc)tokens.remove(0);
104             if (taskToken.equals("start")) {
105                 new StartCommand().execute(tokens);
106             } else if (taskToken.equals("stop")) {
107                 new ShutdownCommand().execute(tokens);
108             } else if (taskToken.equals("list")) {
109                 new ListCommand().execute(tokens);
110             } else if (taskToken.equals("query")) {
111                 new QueryCommand().execute(tokens);
112             } else if (taskToken.equals("bstat")) {
113                 new BstatCommand().execute(tokens);
114             } else if (taskToken.equals("browse")) {
115                 new AmqBrowseCommand().execute(tokens);
116             } else if (taskToken.equals("purge")) {
117                 new PurgeCommand().execute(tokens);
118             } else if (taskToken.equals("help")) {
119                 printHelp();
120             } else {
121                 printHelp();
122             }
123         } else {
124             printHelp();
125         }
126         
127     }
128
129     /**
130      * Print the help messages for the browse command
131      */

132     protected void printHelp() {
133         GlobalWriter.printHelp(helpFile);
134     }
135 }
136
Popular Tags