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 "", 052 "Task Options (Options specific to each task):", 053 " --extdir <dir> - Add the jar files in the directory to the classpath.", 054 " --version - Display the version information.", 055 " -h,-?,--help - Display this help information. To display task specific help, use " + (interactive ? "" : "Main ") + "[task] -h,-?,--help", 056 "", 057 "Task Data:", 058 " - Information needed by each specific task.", 059 "" 060 }; 061 } 062 063 /** 064 * Main method to run a command shell client. 065 * 066 * @param args - command line arguments 067 * @param in - input stream to use 068 * @param out - output stream to use 069 * @return 0 for a successful run, -1 if there are any exception 070 */ 071 public static int main(String[] args, InputStream in, PrintStream out) { 072 073 CommandContext context = new CommandContext(); 074 context.setFormatter(new CommandShellOutputFormatter(out)); 075 076 // Convert arguments to list for easier management 077 List<String> tokens = new ArrayList<String>(Arrays.asList(args)); 078 079 ShellCommand main = new ShellCommand(); 080 try { 081 main.setCommandContext(context); 082 main.execute(tokens); 083 return 0; 084 } catch (Exception e) { 085 context.printException(e); 086 return -1; 087 } 088 } 089 090 public boolean isInteractive() { 091 return interactive; 092 } 093 094 public void setInteractive(boolean interactive) { 095 this.interactive = interactive; 096 } 097 098 /** 099 * Parses for specific command task. 100 * 101 * @param tokens - command arguments 102 * @throws Exception 103 */ 104 protected void runTask(List<String> tokens) throws Exception { 105 106 // Process task token 107 if (tokens.size() > 0) { 108 Command command=null; 109 String taskToken = (String)tokens.remove(0); 110 if (taskToken.equals("start")) { 111 command = new StartCommand(); 112 } else if (taskToken.equals("create")) { 113 command = new CreateCommand(); 114 } else if (taskToken.equals("stop")) { 115 command = new ShutdownCommand(); 116 } else if (taskToken.equals("list")) { 117 command = new ListCommand(); 118 } else if (taskToken.equals("query")) { 119 command = new QueryCommand(); 120 } else if (taskToken.equals("bstat")) { 121 command = new BstatCommand(); 122 } else if (taskToken.equals("browse")) { 123 command = new AmqBrowseCommand(); 124 } else if (taskToken.equals("purge")) { 125 command = new PurgeCommand(); 126 } else if (taskToken.equals("journal-audit")) { 127 command = new AMQJournalToolCommand(); 128 } else if (taskToken.equals("help")) { 129 printHelp(); 130 } else { 131 printHelp(); 132 } 133 134 if( command!=null ) { 135 command.setCommandContext(context); 136 command.execute(tokens); 137 } 138 } else { 139 printHelp(); 140 } 141 142 } 143 144 /** 145 * Print the help messages for the browse command 146 */ 147 protected void printHelp() { 148 context.printHelp(helpFile); 149 } 150 }