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 org.apache.activemq.console.CommandContext;
020    import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
021    
022    import java.io.InputStream;
023    import java.io.PrintStream;
024    import java.util.*;
025    
026    public class ShellCommand extends AbstractCommand {
027    
028        private boolean interactive;
029        private String[] helpFile;
030    
031        public ShellCommand() {
032            this(false);
033        }
034    
035        public ShellCommand(boolean interactive) {
036            this.interactive = interactive;
037            ArrayList<String> help = new ArrayList<String>();
038            help.addAll(Arrays.asList(new String[] {
039                        interactive ? "Usage: [task] [task-options] [task data]" : "Usage: Main [--extdir <dir>] [task] [task-options] [task data]",
040                        "",
041                        "Tasks:"}));
042    
043            ArrayList<Command> commands = getCommands();
044            Collections.sort(commands, new Comparator<Command>() {
045                @Override
046                public int compare(Command command, Command command1) {
047                    return command.getName().compareTo(command1.getName());
048                }
049            });
050    
051            for( Command command: commands) {
052                help.add(String.format("    %-24s - %s", command.getName(), command.getOneLineDescription()));
053            }
054    
055            help.addAll(Arrays.asList(new String[] {
056                        "",
057                        "Task Options (Options specific to each task):",
058                        "    --extdir <dir>  - Add the jar files in the directory to the classpath.",
059                        "    --version       - Display the version information.",
060                        "    -h,-?,--help    - Display this help information. To display task specific help, use " + (interactive ? "" : "Main ") + "[task] -h,-?,--help",
061                        "",
062                        "Task Data:",
063                        "    - Information needed by each specific task.",
064                        "",
065                        "JMX system property options:",
066                        "    -Dactivemq.jmx.url=<jmx service uri> (default is: 'service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi')",
067                        "    -Dactivemq.jmx.user=<user name>",
068                        "    -Dactivemq.jmx.password=<password>",
069                        ""
070                    }));
071    
072            this.helpFile = help.toArray(new String[help.size()]);
073        }
074    
075        @Override
076        public String getName() {
077            return "shell";
078        }
079    
080        @Override
081        public String getOneLineDescription() {
082            return "Runs the activemq sub shell";
083        }
084    
085        /**
086         * Main method to run a command shell client.
087         * 
088         * @param args - command line arguments
089         * @param in - input stream to use
090         * @param out - output stream to use
091         * @return 0 for a successful run, -1 if there are any exception
092         */
093        public static int main(String[] args, InputStream in, PrintStream out) {
094            
095            CommandContext context = new CommandContext();
096            context.setFormatter(new CommandShellOutputFormatter(out));
097    
098            // Convert arguments to list for easier management
099            List<String> tokens = new ArrayList<String>(Arrays.asList(args));
100    
101            ShellCommand main = new ShellCommand();
102            try {
103                main.setCommandContext(context);
104                main.execute(tokens);
105                return 0;
106            } catch (Exception e) {
107                context.printException(e);
108                return -1;
109            }
110        }
111    
112        public boolean isInteractive() {
113            return interactive;
114        }
115    
116        public void setInteractive(boolean interactive) {
117            this.interactive = interactive;
118        }
119    
120        /**
121         * Parses for specific command task.
122         * 
123         * @param tokens - command arguments
124         * @throws Exception
125         */
126        protected void runTask(List<String> tokens) throws Exception {
127    
128            // Process task token
129            if (tokens.size() > 0) {
130                Command command=null;
131                String taskToken = (String)tokens.remove(0);
132    
133    
134                for( Command c: getCommands() ) {
135                    if( taskToken.equals(c.getName()) ) {
136                        command = c;
137                        break;
138                    }
139                }
140                if( command == null ) {
141                    if (taskToken.equals("help")) {
142                        printHelp();
143                    } else {
144                        printHelp();
145                    }
146                }
147    
148                if( command!=null ) {
149                    command.setCommandContext(context);
150                    command.execute(tokens);
151                }
152            } else {
153                printHelp();
154            }
155    
156        }
157    
158        ArrayList<Command> getCommands() {
159            ServiceLoader<Command> loader = ServiceLoader.load(Command.class);
160            ArrayList<Command> rc = new ArrayList<Command>();
161            for( Command command: loader ) {
162                rc.add(command);
163            }
164            return rc;
165        }
166    
167            /**
168         * Print the help messages for the browse command
169         */
170        protected void printHelp() {
171            context.printHelp(helpFile);
172        }
173    }