Fix for AMQ-1707

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@652598 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hiram R. Chirino 2008-05-01 18:13:29 +00:00
parent 4512f27fdd
commit 54eb93e124
17 changed files with 131 additions and 114 deletions

View File

@ -26,9 +26,21 @@
</parent>
<artifactId>activemq-console</artifactId>
<packaging>bundle</packaging>
<name>ActiveMQ :: Console</name>
<description>ActiveMQ Management Console</description>
<properties>
<activemq.osgi.import.pkg>
org.apache.activemq*;resolution:=optional,
org.springframework*;resolution:=optional,
*
</activemq.osgi.import.pkg>
<activemq.osgi.export>
org.apache.activemq.console*;version=${project.version},
</activemq.osgi.export>
</properties>
<dependencies>
<!-- activemq related dependencies -->
<dependency>

View File

@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.console.formatter;
package org.apache.activemq.console;
import java.io.OutputStream;
import java.util.Collection;
@ -25,30 +25,17 @@ import javax.management.AttributeList;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
public final class GlobalWriter {
private static OutputFormatter formatter;
import org.apache.activemq.console.formatter.OutputFormatter;
/**
* Creates a singleton global writer
*/
private GlobalWriter() {
}
/**
* Maintains a global output formatter
*
* @param formatter - the output formatter to use
*/
public static void instantiate(OutputFormatter formatter) {
GlobalWriter.formatter = formatter;
}
public final class CommandContext {
private OutputFormatter formatter;
/**
* Retrieve the output stream being used by the global formatter
*
* @return
*/
public static OutputStream getOutputStream() {
public OutputStream getOutputStream() {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -60,7 +47,7 @@ public final class GlobalWriter {
*
* @param mbean - mbean to print
*/
public static void printMBean(ObjectInstance mbean) {
public void printMBean(ObjectInstance mbean) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -72,7 +59,7 @@ public final class GlobalWriter {
*
* @param mbean - mbean to print
*/
public static void printMBean(ObjectName mbean) {
public void printMBean(ObjectName mbean) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -84,7 +71,7 @@ public final class GlobalWriter {
*
* @param mbean - mbean to print
*/
public static void printMBean(AttributeList mbean) {
public void printMBean(AttributeList mbean) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -96,7 +83,7 @@ public final class GlobalWriter {
*
* @param mbean
*/
public static void printMBean(Map mbean) {
public void printMBean(Map mbean) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -108,7 +95,7 @@ public final class GlobalWriter {
*
* @param mbean - collection of mbeans
*/
public static void printMBean(Collection mbean) {
public void printMBean(Collection mbean) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -120,7 +107,7 @@ public final class GlobalWriter {
*
* @param msg
*/
public static void printMessage(Map msg) {
public void printMessage(Map msg) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -132,7 +119,7 @@ public final class GlobalWriter {
*
* @param msg - JMS message to print
*/
public static void printMessage(Message msg) {
public void printMessage(Message msg) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -144,7 +131,7 @@ public final class GlobalWriter {
*
* @param msg - collection of JMS messages
*/
public static void printMessage(Collection msg) {
public void printMessage(Collection msg) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -156,7 +143,7 @@ public final class GlobalWriter {
*
* @param helpMsgs - help messages to print
*/
public static void printHelp(String[] helpMsgs) {
public void printHelp(String[] helpMsgs) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -168,7 +155,7 @@ public final class GlobalWriter {
*
* @param info - information message to print
*/
public static void printInfo(String info) {
public void printInfo(String info) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -180,7 +167,7 @@ public final class GlobalWriter {
*
* @param e - exception to print
*/
public static void printException(Exception e) {
public void printException(Exception e) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -192,7 +179,7 @@ public final class GlobalWriter {
*
* @param version - version info to print
*/
public static void printVersion(String version) {
public void printVersion(String version) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -204,7 +191,7 @@ public final class GlobalWriter {
*
* @param map to print
*/
public static void print(Map map) {
public void print(Map map) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -216,7 +203,7 @@ public final class GlobalWriter {
*
* @param strings - string array to print
*/
public static void print(String[] strings) {
public void print(String[] strings) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -228,7 +215,7 @@ public final class GlobalWriter {
*
* @param collection - collection to print
*/
public static void print(Collection collection) {
public void print(Collection collection) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
@ -240,10 +227,18 @@ public final class GlobalWriter {
*
* @param string - string to print
*/
public static void print(String string) {
public void print(String string) {
if (formatter == null) {
throw new IllegalStateException("No OutputFormatter specified. Use GlobalWriter.instantiate(OutputFormatter).");
}
formatter.print(string);
}
public OutputFormatter getFormatter() {
return formatter;
}
public void setFormatter(OutputFormatter formatter) {
this.formatter = formatter;
}
}

View File

@ -26,7 +26,6 @@ import javax.jms.TextMessage;
import org.apache.activemq.broker.util.CommandHandler;
import org.apache.activemq.console.command.ShellCommand;
import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
import org.apache.activemq.console.formatter.GlobalWriter;
/**
* A default implementation of the @{link CommandHandler} interface
@ -40,12 +39,14 @@ public class ConsoleCommandHandler implements CommandHandler {
public void processCommand(TextMessage request, TextMessage response) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GlobalWriter.instantiate(new CommandShellOutputFormatter(out));
CommandContext ctx = new CommandContext();
ctx.setFormatter(new CommandShellOutputFormatter(out));
// lets turn the text into a list of arguments
String requestText = request.getText();
List<String> tokens = tokenize(requestText);
command.setCommandContext(ctx);
command.execute(tokens);
out.flush();

View File

@ -27,7 +27,6 @@ import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.console.formatter.GlobalWriter;
public abstract class AbstractAmqCommand extends AbstractCommand {
private URI brokerUrl;
@ -43,7 +42,7 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
*/
protected Connection createConnection() throws JMSException {
if (getBrokerUrl() == null) {
GlobalWriter
context
.printException(new IllegalStateException("You must specify a broker "
+ "URL to connect to using the --amqurl option."));
return null;
@ -70,7 +69,7 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
*/
protected Connection createConnection(String username, String password) throws JMSException {
if (getBrokerUrl() == null) {
GlobalWriter
context
.printException(new IllegalStateException(
"You must specify a broker URL to connect to using the --amqurl option."));
return null;
@ -113,14 +112,14 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
if (token.equals("--amqurl")) {
// If no broker url specified, or next token is a new option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Broker URL not specified."));
context.printException(new IllegalArgumentException("Broker URL not specified."));
tokens.clear();
return;
}
// If broker url already specified
if (getBrokerUrl() != null) {
GlobalWriter
context
.printException(new IllegalArgumentException("Multiple broker URL cannot be specified."));
tokens.clear();
return;
@ -131,7 +130,7 @@ public abstract class AbstractAmqCommand extends AbstractCommand {
try {
setBrokerUrl(new URI(strBrokerUrl));
} catch (URISyntaxException e) {
GlobalWriter.printException(e);
context.printException(e);
tokens.clear();
return;
}

View File

@ -19,7 +19,7 @@ package org.apache.activemq.console.command;
import java.util.List;
import org.apache.activemq.ActiveMQConnectionMetaData;
import org.apache.activemq.console.formatter.GlobalWriter;
import org.apache.activemq.console.CommandContext;
public abstract class AbstractCommand implements Command {
public static final String COMMAND_OPTION_DELIMETER = ",";
@ -27,6 +27,12 @@ public abstract class AbstractCommand implements Command {
private boolean isPrintHelp;
private boolean isPrintVersion;
protected CommandContext context;
public void setCommandContext(CommandContext context) {
this.context = context;
}
/**
* Execute a generic command, which includes parsing the options for the
* command and running the specific task.
@ -44,7 +50,7 @@ public abstract class AbstractCommand implements Command {
// Print the AMQ version
} else if (isPrintVersion) {
GlobalWriter.printVersion(ActiveMQConnectionMetaData.PROVIDER_VERSION);
context.printVersion(ActiveMQConnectionMetaData.PROVIDER_VERSION);
// Run the specified task
} else {
@ -103,7 +109,7 @@ public abstract class AbstractCommand implements Command {
System.setProperty(key, value);
} else {
// Token is unrecognized
GlobalWriter.printInfo("Unrecognized option: " + token);
context.printInfo("Unrecognized option: " + token);
isPrintHelp = true;
}
}

View File

@ -24,8 +24,6 @@ import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.activemq.console.formatter.GlobalWriter;
public abstract class AbstractJmxCommand extends AbstractCommand {
public static final String DEFAULT_JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
@ -112,12 +110,12 @@ public abstract class AbstractJmxCommand extends AbstractCommand {
if (token.equals("--jmxurl")) {
// If no jmx url specified, or next token is a new option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("JMX URL not specified."));
context.printException(new IllegalArgumentException("JMX URL not specified."));
}
// If jmx url already specified
if (getJmxServiceUrl() != null) {
GlobalWriter.printException(new IllegalArgumentException("Multiple JMX URL cannot be specified."));
context.printException(new IllegalArgumentException("Multiple JMX URL cannot be specified."));
tokens.clear();
}
@ -125,7 +123,7 @@ public abstract class AbstractJmxCommand extends AbstractCommand {
try {
this.setJmxServiceUrl(new JMXServiceURL(strJmxUrl));
} catch (MalformedURLException e) {
GlobalWriter.printException(e);
context.printException(e);
tokens.clear();
}
} else {

View File

@ -27,7 +27,6 @@ import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.console.formatter.GlobalWriter;
import org.apache.activemq.console.util.AmqMessagesUtil;
public class AmqBrowseCommand extends AbstractAmqCommand {
@ -86,13 +85,13 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
try {
// If no destination specified
if (tokens.isEmpty()) {
GlobalWriter.printException(new IllegalArgumentException("No JMS destination specified."));
context.printException(new IllegalArgumentException("No JMS destination specified."));
return;
}
// If no broker url specified
if (getBrokerUrl() == null) {
GlobalWriter.printException(new IllegalStateException("No broker url specified. Use the --amqurl option to specify a broker url."));
context.printException(new IllegalStateException("No broker url specified. Use the --amqurl option to specify a broker url."));
return;
}
@ -124,11 +123,11 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
}
// Display the messages
GlobalWriter.printMessage(AmqMessagesUtil.filterMessagesView(addMsgs, groupViews, queryViews));
context.printMessage(AmqMessagesUtil.filterMessagesView(addMsgs, groupViews, queryViews));
}
} catch (Exception e) {
GlobalWriter.printException(new RuntimeException("Failed to execute browse task. Reason: " + e));
context.printException(new RuntimeException("Failed to execute browse task. Reason: " + e));
throw new Exception(e);
}
}
@ -148,7 +147,7 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
// If no message selector is specified, or next token is a new
// option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Message selector not specified"));
context.printException(new IllegalArgumentException("Message selector not specified"));
return;
}
@ -162,7 +161,7 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
// If no message selector is specified, or next token is a new
// option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Message selector not specified"));
context.printException(new IllegalArgumentException("Message selector not specified"));
return;
}
@ -176,7 +175,7 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
// If no view specified, or next token is a new option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Attributes to view not specified"));
context.printException(new IllegalArgumentException("Attributes to view not specified"));
return;
}
@ -223,7 +222,7 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
// Unknown group view
} else {
GlobalWriter.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option.");
context.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option.");
}
} else {
// Let super class handle unknown option
@ -235,7 +234,7 @@ public class AmqBrowseCommand extends AbstractAmqCommand {
* Print the help messages for the browse command
*/
protected void printHelp() {
GlobalWriter.printHelp(helpFile);
context.printHelp(helpFile);
}
}

View File

@ -25,7 +25,6 @@ import java.util.StringTokenizer;
import javax.management.ObjectInstance;
import org.apache.activemq.console.formatter.GlobalWriter;
import org.apache.activemq.console.util.AmqMessagesUtil;
import org.apache.activemq.console.util.JmxMBeansUtil;
@ -95,11 +94,11 @@ public class BrowseCommand extends AbstractJmxCommand {
// Iterate through the queue result
for (Iterator j = queueList.iterator(); j.hasNext();) {
List messages = JmxMBeansUtil.createMessageQueryFilter(useJmxServiceUrl(), ((ObjectInstance)j.next()).getObjectName()).query(queryAddObjects);
GlobalWriter.printMessage(JmxMBeansUtil.filterMessagesView(messages, groupViews, queryViews));
context.printMessage(JmxMBeansUtil.filterMessagesView(messages, groupViews, queryViews));
}
}
} catch (Exception e) {
GlobalWriter.printException(new RuntimeException("Failed to execute browse task. Reason: " + e));
context.printException(new RuntimeException("Failed to execute browse task. Reason: " + e));
throw new Exception(e);
}
}
@ -119,7 +118,7 @@ public class BrowseCommand extends AbstractJmxCommand {
// If no message selector is specified, or next token is a new
// option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Message selector not specified"));
context.printException(new IllegalArgumentException("Message selector not specified"));
return;
}
@ -133,7 +132,7 @@ public class BrowseCommand extends AbstractJmxCommand {
// If no message selector is specified, or next token is a new
// option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Message selector not specified"));
context.printException(new IllegalArgumentException("Message selector not specified"));
return;
}
@ -147,7 +146,7 @@ public class BrowseCommand extends AbstractJmxCommand {
// If no view specified, or next token is a new option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Attributes to view not specified"));
context.printException(new IllegalArgumentException("Attributes to view not specified"));
return;
}
@ -194,7 +193,7 @@ public class BrowseCommand extends AbstractJmxCommand {
// Unknown group view
} else {
GlobalWriter.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option.");
context.printInfo("Unknown group view: " + viewGroup + ". Ignoring group view option.");
}
} else {
// Let super class handle unknown option
@ -206,7 +205,7 @@ public class BrowseCommand extends AbstractJmxCommand {
* Print the help messages for the browse command
*/
protected void printHelp() {
GlobalWriter.printHelp(helpFile);
context.printHelp(helpFile);
}
}

View File

@ -19,7 +19,12 @@ package org.apache.activemq.console.command;
import java.util.List;
import org.apache.activemq.console.CommandContext;
public interface Command {
void setCommandContext( CommandContext context );
/**
* Execute the specified command
* @param tokens - arguments to the command

View File

@ -20,7 +20,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.activemq.console.formatter.GlobalWriter;
import org.apache.activemq.console.util.JmxMBeansUtil;
public class ListCommand extends AbstractJmxCommand {
@ -45,9 +44,9 @@ public class ListCommand extends AbstractJmxCommand {
try {
Set<String> propsView = new HashSet<String>();
propsView.add("BrokerName");
GlobalWriter.printMBean(JmxMBeansUtil.filterMBeansView(JmxMBeansUtil.getAllBrokers(useJmxServiceUrl()), propsView));
context.printMBean(JmxMBeansUtil.filterMBeansView(JmxMBeansUtil.getAllBrokers(useJmxServiceUrl()), propsView));
} catch (Exception e) {
GlobalWriter.printException(new RuntimeException("Failed to execute list task. Reason: " + e));
context.printException(new RuntimeException("Failed to execute list task. Reason: " + e));
throw new Exception(e);
}
}
@ -56,7 +55,7 @@ public class ListCommand extends AbstractJmxCommand {
* Print the help messages for the browse command
*/
protected void printHelp() {
GlobalWriter.printHelp(helpFile);
context.printHelp(helpFile);
}
}

View File

@ -27,7 +27,6 @@ import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.remote.JMXConnector;
import org.apache.activemq.console.formatter.GlobalWriter;
import org.apache.activemq.console.util.JmxMBeansUtil;
public class PurgeCommand extends AbstractJmxCommand {
@ -87,7 +86,7 @@ public class PurgeCommand extends AbstractJmxCommand {
}
}
} catch (Exception e) {
GlobalWriter.printException(new RuntimeException("Failed to execute purge task. Reason: " + e));
context.printException(new RuntimeException("Failed to execute purge task. Reason: " + e));
throw new Exception(e);
}
}
@ -101,7 +100,7 @@ public class PurgeCommand extends AbstractJmxCommand {
public void purgeQueue(ObjectName queue) throws Exception {
JMXConnector conn = createJmxConnector();
MBeanServerConnection server = conn.getMBeanServerConnection();
GlobalWriter.printInfo("Purging all messages in queue: " + queue.getKeyProperty("Destination"));
context.printInfo("Purging all messages in queue: " + queue.getKeyProperty("Destination"));
server.invoke(queue, "purge", new Object[] {}, new String[] {});
conn.close();
}
@ -121,7 +120,7 @@ public class PurgeCommand extends AbstractJmxCommand {
for (Iterator i = messages.iterator(); i.hasNext();) {
CompositeData msg = (CompositeData)i.next();
param[0] = "" + msg.get("JMSMessageID");
GlobalWriter.printInfo("Removing message: " + param[0] + " from queue: " + queue.getKeyProperty("Destination"));
context.printInfo("Removing message: " + param[0] + " from queue: " + queue.getKeyProperty("Destination"));
server.invoke(queue, "removeMessage", param, new String[] {
"java.lang.String"
});
@ -144,7 +143,7 @@ public class PurgeCommand extends AbstractJmxCommand {
// If no message selector is specified, or next token is a new
// option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Message selector not specified"));
context.printException(new IllegalArgumentException("Message selector not specified"));
return;
}
@ -158,7 +157,7 @@ public class PurgeCommand extends AbstractJmxCommand {
// If no message selector is specified, or next token is a new
// option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Message selector not specified"));
context.printException(new IllegalArgumentException("Message selector not specified"));
return;
}
@ -177,7 +176,7 @@ public class PurgeCommand extends AbstractJmxCommand {
* Print the help messages for the browse command
*/
protected void printHelp() {
GlobalWriter.printHelp(helpFile);
context.printHelp(helpFile);
}
}

View File

@ -24,7 +24,6 @@ import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import org.apache.activemq.console.formatter.GlobalWriter;
import org.apache.activemq.console.util.JmxMBeansUtil;
public class QueryCommand extends AbstractJmxCommand {
@ -112,10 +111,10 @@ public class QueryCommand extends AbstractJmxCommand {
addMBeans.removeAll(subMBeans);
}
GlobalWriter.printMBean(JmxMBeansUtil.filterMBeansView(addMBeans, queryViews));
context.printMBean(JmxMBeansUtil.filterMBeansView(addMBeans, queryViews));
} catch (Exception e) {
GlobalWriter.printException(new RuntimeException("Failed to execute query task. Reason: " + e));
context.printException(new RuntimeException("Failed to execute query task. Reason: " + e));
throw new Exception(e);
}
}
@ -141,7 +140,7 @@ public class QueryCommand extends AbstractJmxCommand {
// If additive query
String predefQuery = PREDEFINED_OBJNAME_QUERY.getProperty(key);
if (predefQuery == null) {
GlobalWriter.printException(new IllegalArgumentException("Unknown query object type: " + key));
context.printException(new IllegalArgumentException("Unknown query object type: " + key));
return;
}
String queryStr = JmxMBeansUtil.createQueryString(predefQuery, value);
@ -162,7 +161,7 @@ public class QueryCommand extends AbstractJmxCommand {
// If subtractive query
String predefQuery = PREDEFINED_OBJNAME_QUERY.getProperty(key);
if (predefQuery == null) {
GlobalWriter.printException(new IllegalArgumentException("Unknown query object type: " + key));
context.printException(new IllegalArgumentException("Unknown query object type: " + key));
return;
}
String queryStr = JmxMBeansUtil.createQueryString(predefQuery, value);
@ -176,7 +175,7 @@ public class QueryCommand extends AbstractJmxCommand {
// If no object name query is specified, or next token is a new
// option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Object name query not specified"));
context.printException(new IllegalArgumentException("Object name query not specified"));
return;
}
@ -190,7 +189,7 @@ public class QueryCommand extends AbstractJmxCommand {
// If no object name query is specified, or next token is a new
// option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Object name query not specified"));
context.printException(new IllegalArgumentException("Object name query not specified"));
return;
}
@ -203,7 +202,7 @@ public class QueryCommand extends AbstractJmxCommand {
// If no view specified, or next token is a new option
if (tokens.isEmpty() || ((String)tokens.get(0)).startsWith("-")) {
GlobalWriter.printException(new IllegalArgumentException("Attributes to view not specified"));
context.printException(new IllegalArgumentException("Attributes to view not specified"));
return;
}
@ -222,7 +221,7 @@ public class QueryCommand extends AbstractJmxCommand {
* Print the help messages for the browse command
*/
protected void printHelp() {
GlobalWriter.printHelp(helpFile);
context.printHelp(helpFile);
}
}

View File

@ -22,8 +22,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.activemq.console.CommandContext;
import org.apache.activemq.console.formatter.CommandShellOutputFormatter;
import org.apache.activemq.console.formatter.GlobalWriter;
public class ShellCommand extends AbstractCommand {
@ -66,17 +66,20 @@ public class ShellCommand extends AbstractCommand {
* @return 0 for a successful run, -1 if there are any exception
*/
public static int main(String[] args, InputStream in, PrintStream out) {
GlobalWriter.instantiate(new CommandShellOutputFormatter(out));
CommandContext context = new CommandContext();
context.setFormatter(new CommandShellOutputFormatter(out));
// Convert arguments to list for easier management
List<String> tokens = new ArrayList<String>(Arrays.asList(args));
ShellCommand main = new ShellCommand();
try {
main.setCommandContext(context);
main.execute(tokens);
return 0;
} catch (Exception e) {
GlobalWriter.printException(e);
context.printException(e);
return -1;
}
}
@ -99,26 +102,32 @@ public class ShellCommand extends AbstractCommand {
// Process task token
if (tokens.size() > 0) {
Command command=null;
String taskToken = (String)tokens.remove(0);
if (taskToken.equals("start")) {
new StartCommand().execute(tokens);
command = new StartCommand();
} else if (taskToken.equals("stop")) {
new ShutdownCommand().execute(tokens);
command = new ShutdownCommand();
} else if (taskToken.equals("list")) {
new ListCommand().execute(tokens);
command = new ListCommand();
} else if (taskToken.equals("query")) {
new QueryCommand().execute(tokens);
command = new QueryCommand();
} else if (taskToken.equals("bstat")) {
new BstatCommand().execute(tokens);
command = new BstatCommand();
} else if (taskToken.equals("browse")) {
new AmqBrowseCommand().execute(tokens);
command = new AmqBrowseCommand();
} else if (taskToken.equals("purge")) {
new PurgeCommand().execute(tokens);
command = new PurgeCommand();
} else if (taskToken.equals("help")) {
printHelp();
} else {
printHelp();
}
if( command!=null ) {
command.setCommandContext(context);
command.execute(tokens);
}
} else {
printHelp();
}
@ -129,6 +138,6 @@ public class ShellCommand extends AbstractCommand {
* Print the help messages for the browse command
*/
protected void printHelp() {
GlobalWriter.printHelp(helpFile);
context.printHelp(helpFile);
}
}

View File

@ -26,7 +26,6 @@ import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.remote.JMXServiceURL;
import org.apache.activemq.console.formatter.GlobalWriter;
import org.apache.activemq.console.util.JmxMBeansUtil;
public class ShutdownCommand extends AbstractJmxCommand {
@ -70,12 +69,12 @@ public class ShutdownCommand extends AbstractJmxCommand {
// If there is no broker to stop
if (mbeans.isEmpty()) {
GlobalWriter.printInfo("There are no brokers to stop.");
context.printInfo("There are no brokers to stop.");
return;
// There should only be one broker to stop
} else if (mbeans.size() > 1) {
GlobalWriter.printInfo("There are multiple brokers to stop. Please select the broker(s) to stop or use --all to stop all brokers.");
context.printInfo("There are multiple brokers to stop. Please select the broker(s) to stop or use --all to stop all brokers.");
return;
// Get the first broker only
@ -92,7 +91,7 @@ public class ShutdownCommand extends AbstractJmxCommand {
brokerName = (String)brokerNames.remove(0);
Collection matchedBrokers = JmxMBeansUtil.getBrokersByName(useJmxServiceUrl(), brokerName);
if (matchedBrokers.isEmpty()) {
GlobalWriter.printInfo(brokerName + " did not match any running brokers.");
context.printInfo(brokerName + " did not match any running brokers.");
} else {
mbeans.addAll(matchedBrokers);
}
@ -102,7 +101,7 @@ public class ShutdownCommand extends AbstractJmxCommand {
// Stop all brokers in set
stopBrokers(useJmxServiceUrl(), mbeans);
} catch (Exception e) {
GlobalWriter.printException(new RuntimeException("Failed to execute stop task. Reason: " + e));
context.printException(new RuntimeException("Failed to execute stop task. Reason: " + e));
throw new Exception(e);
}
}
@ -122,7 +121,7 @@ public class ShutdownCommand extends AbstractJmxCommand {
brokerObjName = ((ObjectInstance)i.next()).getObjectName();
String brokerName = brokerObjName.getKeyProperty("BrokerName");
GlobalWriter.print("Stopping broker: " + brokerName);
context.print("Stopping broker: " + brokerName);
try {
server.invoke(brokerObjName, "terminateJVM", new Object[] {
@ -130,7 +129,7 @@ public class ShutdownCommand extends AbstractJmxCommand {
}, new String[] {
"int"
});
GlobalWriter.print("Succesfully stopped broker: " + brokerName);
context.print("Succesfully stopped broker: " + brokerName);
} catch (Exception e) {
// TODO: Check exceptions throwned
// System.out.println("Failed to stop broker: [ " + brokerName +
@ -162,7 +161,7 @@ public class ShutdownCommand extends AbstractJmxCommand {
* Print the help messages for the browse command
*/
protected void printHelp() {
GlobalWriter.printHelp(helpFile);
context.printHelp(helpFile);
}
}

View File

@ -25,7 +25,6 @@ import java.util.List;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.console.formatter.GlobalWriter;
public class StartCommand extends AbstractCommand {
@ -84,7 +83,7 @@ public class StartCommand extends AbstractCommand {
try {
setConfigUri(new URI(strConfigURI));
} catch (URISyntaxException e) {
GlobalWriter.printException(e);
context.printException(e);
return;
}
@ -96,7 +95,7 @@ public class StartCommand extends AbstractCommand {
// elsewhere
waitForShutdown();
} catch (Exception e) {
GlobalWriter.printException(new RuntimeException("Failed to execute start task. Reason: " + e, e));
context.printException(new RuntimeException("Failed to execute start task. Reason: " + e, e));
throw new Exception(e);
}
}
@ -171,7 +170,7 @@ public class StartCommand extends AbstractCommand {
* Print the help messages for the browse command
*/
protected void printHelp() {
GlobalWriter.printHelp(helpFile);
context.printHelp(helpFile);
}
}

View File

@ -38,7 +38,6 @@ import org.apache.activemq.command.ActiveMQMessage;
import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.activemq.command.ActiveMQStreamMessage;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.activemq.console.formatter.GlobalWriter;
import org.apache.activemq.console.util.AmqMessagesUtil;
public class MapTransformFilter extends ResultTransformFilter {
@ -68,7 +67,7 @@ public class MapTransformFilter extends ResultTransformFilter {
object
});
} catch (NoSuchMethodException e) {
GlobalWriter.print("Unable to transform mbean of type: " + object.getClass().getName() + ". No corresponding transformToMap method found.");
// CommandContext.print("Unable to transform mbean of type: " + object.getClass().getName() + ". No corresponding transformToMap method found.");
return null;
}
}

View File

@ -25,7 +25,7 @@ import javax.management.AttributeList;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
interface OutputFormatter {
public interface OutputFormatter {
/**
* Retrieve the output stream being used by the formatter