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:",
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                "JMX system property options:",
064                "    -Dactivemq.jmx.url=<jmx service uri> (default is: 'service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi')",
065                "    -Dactivemq.jmx.user=<user name>",
066                "    -Dactivemq.jmx.password=<password>",
067                ""
068            };
069        }
070    
071        /**
072         * Main method to run a command shell client.
073         * 
074         * @param args - command line arguments
075         * @param in - input stream to use
076         * @param out - output stream to use
077         * @return 0 for a successful run, -1 if there are any exception
078         */
079        public static int main(String[] args, InputStream in, PrintStream out) {
080            
081            CommandContext context = new CommandContext();
082            context.setFormatter(new CommandShellOutputFormatter(out));
083    
084            // Convert arguments to list for easier management
085            List<String> tokens = new ArrayList<String>(Arrays.asList(args));
086    
087            ShellCommand main = new ShellCommand();
088            try {
089                main.setCommandContext(context);
090                main.execute(tokens);
091                return 0;
092            } catch (Exception e) {
093                context.printException(e);
094                return -1;
095            }
096        }
097    
098        public boolean isInteractive() {
099            return interactive;
100        }
101    
102        public void setInteractive(boolean interactive) {
103            this.interactive = interactive;
104        }
105    
106        /**
107         * Parses for specific command task.
108         * 
109         * @param tokens - command arguments
110         * @throws Exception
111         */
112        protected void runTask(List<String> tokens) throws Exception {
113    
114            // Process task token
115            if (tokens.size() > 0) {
116                Command command=null;
117                String taskToken = (String)tokens.remove(0);
118                if (taskToken.equals("start")) {
119                    command = new StartCommand();
120                } else if (taskToken.equals("create")) {
121                    command = new CreateCommand();
122                } else if (taskToken.equals("stop")) {
123                    command = new ShutdownCommand();
124                } else if (taskToken.equals("list")) {
125                    command = new ListCommand();
126                } else if (taskToken.equals("query")) {
127                    command = new QueryCommand();
128                } else if (taskToken.equals("bstat")) {
129                    command = new BstatCommand();
130                } else if (taskToken.equals("browse")) {
131                    command = new AmqBrowseCommand();
132                } else if (taskToken.equals("purge")) {
133                    command = new PurgeCommand();
134                } else if (taskToken.equals("journal-audit")) {
135                    command = new AMQJournalToolCommand();
136                } else if (taskToken.equals("encrypt")) {
137                    command = new EncryptCommand();
138                } else if (taskToken.equals("decrypt")) {
139                    command = new DecryptCommand();
140                } else if (taskToken.equals("help")) {
141                    printHelp();
142                } else {
143                    printHelp();
144                }
145                
146                if( command!=null ) {
147                    command.setCommandContext(context);
148                    command.execute(tokens);
149                }
150            } else {
151                printHelp();
152            }
153    
154        }
155    
156            /**
157         * Print the help messages for the browse command
158         */
159        protected void printHelp() {
160            context.printHelp(helpFile);
161        }
162    }