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.command.store.amq.AMQJournalToolCommand;
027    import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
028    
029    public class ShellCommand extends AbstractCommand {
030    
031        private boolean interactive;
032        private String[] helpFile;
033    
034        public ShellCommand() {
035            this(false);
036        }
037    
038        public ShellCommand(boolean interactive) {
039            this.interactive = interactive;
040            this.helpFile = new String[] {
041                interactive ? "Usage: [task] [task-options] [task data]" : "Usage: Main [--extdir <dir>] [task] [task-options] [task data]", 
042                "",
043                "Tasks (default task is start):",
044                "    start           - Creates and starts a broker using a configuration file, or a broker URI.",
045                "    create          - Creates a runnable broker instance in the specified path",
046                "    stop            - Stops a running broker specified by the broker name.",
047                "    list            - Lists all available brokers in the specified JMX context.",
048                "    query           - Display selected broker component's attributes and statistics.",
049                "    browse          - Display selected messages in a specified destination.",
050                "    journal-audit   - Allows you to view records stored in the persistent journal.",
051                "    purge           - Delete selected destination's messages that matches the message selector",
052                "    encrypt         - Encrypts given text",
053                "    decrypt         - Decrypts given text",
054                "",
055                "Task Options (Options specific to each task):",
056                "    --extdir <dir>  - Add the jar files in the directory to the classpath.",
057                "    --version       - Display the version information.",
058                "    -h,-?,--help    - Display this help information. To display task specific help, use " + (interactive ? "" : "Main ") + "[task] -h,-?,--help", 
059                "",
060                "Task Data:",
061                "    - Information needed by each specific task.",
062                ""
063            };
064        }
065    
066        /**
067         * Main method to run a command shell client.
068         * 
069         * @param args - command line arguments
070         * @param in - input stream to use
071         * @param out - output stream to use
072         * @return 0 for a successful run, -1 if there are any exception
073         */
074        public static int main(String[] args, InputStream in, PrintStream out) {
075            
076            CommandContext context = new CommandContext();
077            context.setFormatter(new CommandShellOutputFormatter(out));
078    
079            // Convert arguments to list for easier management
080            List<String> tokens = new ArrayList<String>(Arrays.asList(args));
081    
082            ShellCommand main = new ShellCommand();
083            try {
084                main.setCommandContext(context);
085                main.execute(tokens);
086                return 0;
087            } catch (Exception e) {
088                context.printException(e);
089                return -1;
090            }
091        }
092    
093        public boolean isInteractive() {
094            return interactive;
095        }
096    
097        public void setInteractive(boolean interactive) {
098            this.interactive = interactive;
099        }
100    
101        /**
102         * Parses for specific command task.
103         * 
104         * @param tokens - command arguments
105         * @throws Exception
106         */
107        protected void runTask(List<String> tokens) throws Exception {
108    
109            // Process task token
110            if (tokens.size() > 0) {
111                Command command=null;
112                String taskToken = (String)tokens.remove(0);
113                if (taskToken.equals("start")) {
114                    command = new StartCommand();
115                } else if (taskToken.equals("create")) {
116                    command = new CreateCommand();
117                } else if (taskToken.equals("stop")) {
118                    command = new ShutdownCommand();
119                } else if (taskToken.equals("list")) {
120                    command = new ListCommand();
121                } else if (taskToken.equals("query")) {
122                    command = new QueryCommand();
123                } else if (taskToken.equals("bstat")) {
124                    command = new BstatCommand();
125                } else if (taskToken.equals("browse")) {
126                    command = new AmqBrowseCommand();
127                } else if (taskToken.equals("purge")) {
128                    command = new PurgeCommand();
129                } else if (taskToken.equals("journal-audit")) {
130                    command = new AMQJournalToolCommand();
131                } else if (taskToken.equals("encrypt")) {
132                    command = new EncryptCommand();
133                } else if (taskToken.equals("decrypt")) {
134                    command = new DecryptCommand();
135                } else if (taskToken.equals("help")) {
136                    printHelp();
137                } else {
138                    printHelp();
139                }
140                
141                if( command!=null ) {
142                    command.setCommandContext(context);
143                    command.execute(tokens);
144                }
145            } else {
146                printHelp();
147            }
148    
149        }
150    
151            /**
152         * Print the help messages for the browse command
153         */
154        protected void printHelp() {
155            context.printHelp(helpFile);
156        }
157    }