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 */ 017package org.apache.activemq.console.command; 018 019import org.apache.activemq.console.CommandContext; 020import org.apache.activemq.console.formatter.CommandShellOutputFormatter; 021 022import java.io.InputStream; 023import java.io.PrintStream; 024import java.util.*; 025 026public 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 static void main(String[] args) { 113 main(args, System.in, System.out); 114 } 115 116 public boolean isInteractive() { 117 return interactive; 118 } 119 120 public void setInteractive(boolean interactive) { 121 this.interactive = interactive; 122 } 123 124 /** 125 * Parses for specific command task. 126 * 127 * @param tokens - command arguments 128 * @throws Exception 129 */ 130 protected void runTask(List<String> tokens) throws Exception { 131 132 // Process task token 133 if (tokens.size() > 0) { 134 Command command=null; 135 String taskToken = (String)tokens.remove(0); 136 137 138 for( Command c: getCommands() ) { 139 if( taskToken.equals(c.getName()) ) { 140 command = c; 141 break; 142 } 143 } 144 if( command == null ) { 145 if (taskToken.equals("help")) { 146 printHelp(); 147 } else { 148 printHelp(); 149 } 150 } 151 152 if( command!=null ) { 153 command.setCommandContext(context); 154 command.execute(tokens); 155 } 156 } else { 157 printHelp(); 158 } 159 160 } 161 162 ArrayList<Command> getCommands() { 163 ServiceLoader<Command> loader = ServiceLoader.load(Command.class); 164 Iterator<Command> iterator = loader.iterator(); 165 ArrayList<Command> rc = new ArrayList<Command>(); 166 boolean done = false; 167 while (!done) { 168 try { 169 if( iterator.hasNext() ) { 170 rc.add(iterator.next()); 171 } else { 172 done = true; 173 } 174 } catch (ServiceConfigurationError e) { 175 // it's ok, some commands may not load if their dependencies 176 // are not available. 177 } 178 } 179 return rc; 180 } 181 182 /** 183 * Print the help messages for the browse command 184 */ 185 protected void printHelp() { 186 context.printHelp(helpFile); 187 } 188}