001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.console.command;
018    
019    import java.io.InputStream;
020    import java.io.PrintStream;
021    import java.util.ArrayList;
022    import java.util.Arrays;
023    import java.util.List;
024    
025    import org.apache.activemq.console.CommandContext;
026    import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
027    
028    public class ShellCommand extends AbstractCommand {
029    
030        private boolean interactive;
031        private String[] helpFile;
032    
033        public ShellCommand() {
034            this(false);
035        }
036    
037        public ShellCommand(boolean interactive) {
038            this.interactive = interactive;
039            this.helpFile = new String[] {
040                interactive ? "Usage: [task] [task-options] [task data]" : "Usage: Main [--extdir <dir>] [task] [task-options] [task data]", 
041                "",
042                "Tasks (default task is start):",
043                "    start           - Creates and starts a broker using a configuration file, or a broker URI.",
044                "    stop            - Stops a running broker specified by the broker name.",
045                "    list            - Lists all available brokers in the specified JMX context.",
046                "    query           - Display selected broker component's attributes and statistics.",
047                "    browse          - Display selected messages in a specified destination.",
048                "",
049                "Task Options (Options specific to each task):",
050                "    --extdir <dir>  - Add the jar files in the directory to the classpath.",
051                "    --version       - Display the version information.",
052                "    -h,-?,--help    - Display this help information. To display task specific help, use " + (interactive ? "" : "Main ") + "[task] -h,-?,--help", 
053                "",
054                "Task Data:",
055                "    - Information needed by each specific task.",
056                ""
057            };
058        }
059    
060        /**
061         * Main method to run a command shell client.
062         * 
063         * @param args - command line arguments
064         * @param in - input stream to use
065         * @param out - output stream to use
066         * @return 0 for a successful run, -1 if there are any exception
067         */
068        public static int main(String[] args, InputStream in, PrintStream out) {
069            
070            CommandContext context = new CommandContext();
071            context.setFormatter(new CommandShellOutputFormatter(out));
072    
073            // Convert arguments to list for easier management
074            List<String> tokens = new ArrayList<String>(Arrays.asList(args));
075    
076            ShellCommand main = new ShellCommand();
077            try {
078                main.setCommandContext(context);
079                main.execute(tokens);
080                return 0;
081            } catch (Exception e) {
082                context.printException(e);
083                return -1;
084            }
085        }
086    
087        public boolean isInteractive() {
088            return interactive;
089        }
090    
091        public void setInteractive(boolean interactive) {
092            this.interactive = interactive;
093        }
094    
095        /**
096         * Parses for specific command task.
097         * 
098         * @param tokens - command arguments
099         * @throws Exception
100         */
101        protected void runTask(List<String> tokens) throws Exception {
102    
103            // Process task token
104            if (tokens.size() > 0) {
105                Command command=null;
106                String taskToken = (String)tokens.remove(0);
107                if (taskToken.equals("start")) {
108                    command = new StartCommand();
109                } else if (taskToken.equals("stop")) {
110                    command = new ShutdownCommand();
111                } else if (taskToken.equals("list")) {
112                    command = new ListCommand();
113                } else if (taskToken.equals("query")) {
114                    command = new QueryCommand();
115                } else if (taskToken.equals("bstat")) {
116                    command = new BstatCommand();
117                } else if (taskToken.equals("browse")) {
118                    command = new AmqBrowseCommand();
119                } else if (taskToken.equals("purge")) {
120                    command = new PurgeCommand();
121                } else if (taskToken.equals("help")) {
122                    printHelp();
123                } else {
124                    printHelp();
125                }
126                
127                if( command!=null ) {
128                    command.setCommandContext(context);
129                    command.execute(tokens);
130                }
131            } else {
132                printHelp();
133            }
134    
135        }
136    
137        /**
138         * Print the help messages for the browse command
139         */
140        protected void printHelp() {
141            context.printHelp(helpFile);
142        }
143    }