Renamed the *Task stuff to *Command, the Main method now just only calls the DefaultCommand, which delegates to the other commands.

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@368942 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hiram R. Chirino 2006-01-14 01:08:12 +00:00
parent 4d7ea5ef6e
commit 982a119ba6
10 changed files with 121 additions and 130 deletions

View File

@ -17,6 +17,8 @@
package org.apache.activemq.broker; package org.apache.activemq.broker;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.JarURLConnection; import java.net.JarURLConnection;
@ -25,10 +27,10 @@ import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/** /**
* Main class that can bootstrap an ActiveMQ broker console. Handles command line * Main class that can bootstrap an ActiveMQ broker console. Handles command line
@ -37,19 +39,9 @@ import java.util.Iterator;
* @version $Revision$ * @version $Revision$
*/ */
public class Main { public class Main {
public static final int TASK_DEFAULT = 0;
public static final int TASK_START = 1;
public static final int TASK_STOP = 2;
public static final int TASK_LIST = 3;
public static final int TASK_QUERY = 4;
public static final String TASK_DEFAULT_CLASS = "org.apache.activemq.broker.console.DefaultTask"; public static final String TASK_DEFAULT_CLASS = "org.apache.activemq.broker.console.DefaultCommand";
public static final String TASK_START_CLASS = "org.apache.activemq.broker.console.StartTask";
public static final String TASK_SHUTDOWN_CLASS = "org.apache.activemq.broker.console.ShutdownTask";
public static final String TASK_LIST_CLASS = "org.apache.activemq.broker.console.ListTask";
public static final String TASK_QUERY_CLASS = "org.apache.activemq.broker.console.QueryTask";
private int taskType;
private File activeMQHome; private File activeMQHome;
private ClassLoader classLoader; private ClassLoader classLoader;
private List extensions = new ArrayList(5); private List extensions = new ArrayList(5);
@ -62,9 +54,6 @@ public class Main {
// Convert arguments to collection for easier management // Convert arguments to collection for easier management
List tokens = new LinkedList(Arrays.asList(args)); List tokens = new LinkedList(Arrays.asList(args));
// First token should be task type (start|stop|list|query)
app.setTaskType(app.parseTask(tokens));
// Parse for extension directory option // Parse for extension directory option
app.parseExtensions(tokens); app.parseExtensions(tokens);
@ -75,45 +64,13 @@ public class Main {
app.addExtensionDirectory(new File(new File(app.getActiveMQHome(), "lib"), "optional")); app.addExtensionDirectory(new File(new File(app.getActiveMQHome(), "lib"), "optional"));
} }
// Succeeding tokens should be the task data
try { try {
switch (app.getTaskType()) { app.runTaskClass(tokens);
case TASK_START: app.runTaskClass(TASK_START_CLASS, tokens); break;
case TASK_STOP: app.runTaskClass(TASK_SHUTDOWN_CLASS, tokens); break;
case TASK_LIST: app.runTaskClass(TASK_LIST_CLASS, tokens); break;
case TASK_QUERY: app.runTaskClass(TASK_QUERY_CLASS, tokens); break;
case TASK_DEFAULT: app.runTaskClass(TASK_DEFAULT_CLASS, tokens); break;
default:
System.out.println("Encountered unknown task type: " + app.getTaskType());
}
} catch (Throwable e) { } catch (Throwable e) {
System.out.println("Failed to execute main task. Reason: " + e); System.out.println("Failed to execute main task. Reason: " + e);
} }
} }
public int parseTask(List tokens) {
if (tokens.isEmpty()) {
// If no task, run the default task
return TASK_DEFAULT;
}
// Process task token
String taskToken = (String)tokens.remove(0);
if (taskToken.equals("start")) {
return TASK_START;
} else if (taskToken.equals("stop")) {
return TASK_STOP;
} else if (taskToken.equals("list")) {
return TASK_LIST;
} else if (taskToken.equals("query")) {
return TASK_QUERY;
} else {
// If not valid task, push back to list
tokens.add(0, taskToken);
return TASK_DEFAULT;
}
}
public void parseExtensions(List tokens) { public void parseExtensions(List tokens) {
if (tokens.isEmpty()) { if (tokens.isEmpty()) {
return; return;
@ -155,16 +112,17 @@ public class Main {
} }
public void runTaskClass(String taskClass, List tokens) throws Throwable { public void runTaskClass(List tokens) throws Throwable {
System.out.println("ACTIVEMQ_HOME: "+ getActiveMQHome()); System.out.println("ACTIVEMQ_HOME: "+ getActiveMQHome());
ClassLoader cl = getClassLoader(); ClassLoader cl = getClassLoader();
// Use reflection to run the task. // Use reflection to run the task.
try { try {
Class task = cl.loadClass(taskClass); String[] args = (String[]) tokens.toArray(new String[tokens.size()]);
Method runTask = task.getMethod("runTask", new Class[] { List.class }); Class task = cl.loadClass(TASK_DEFAULT_CLASS);
runTask.invoke(task.newInstance(), new Object[] { tokens }); Method runTask = task.getMethod("main", new Class[] { String[].class, InputStream.class, PrintStream.class });
runTask.invoke(task.newInstance(), new Object[] { args, System.in, System.out });
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
throw e.getCause(); throw e.getCause();
} catch (Throwable e) { } catch (Throwable e) {
@ -220,14 +178,6 @@ public class Main {
return classLoader; return classLoader;
} }
public int getTaskType() {
return taskType;
}
public void setTaskType(int taskType) {
this.taskType = taskType;
}
public void setActiveMQHome(File activeMQHome) { public void setActiveMQHome(File activeMQHome) {
this.activeMQHome = activeMQHome; this.activeMQHome = activeMQHome;
} }

View File

@ -18,13 +18,21 @@ package org.apache.activemq.broker.console;
import org.apache.activemq.ActiveMQConnectionMetaData; import org.apache.activemq.ActiveMQConnectionMetaData;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public abstract class AbstractTask implements Task { public abstract class AbstractCommand implements Command {
private boolean isPrintHelp = false; private boolean isPrintHelp = false;
private boolean isPrintVersion = false; private boolean isPrintVersion = false;
protected PrintStream out;
public void runTask(List tokens) throws Exception { public int main(String[] args, InputStream in, PrintStream out) {
this.out = out;
try {
List tokens = new ArrayList(Arrays.asList(args));
parseOptions(tokens); parseOptions(tokens);
if (isPrintHelp) { if (isPrintHelp) {
@ -32,7 +40,13 @@ public abstract class AbstractTask implements Task {
} else if (isPrintVersion) { } else if (isPrintVersion) {
printVersion(); printVersion();
} else { } else {
startTask(tokens); execute(tokens);
}
return 0;
} catch (Exception e) {
out.println("Failed to execute main task. Reason: " + e);
e.printStackTrace(out);
return -1;
} }
} }
@ -77,23 +91,23 @@ public abstract class AbstractTask implements Task {
// Token is unrecognized // Token is unrecognized
else { else {
System.out.println("Ignoring unrecognized option: " + token); out.println("Ignoring unrecognized option: " + token);
} }
} }
protected void printVersion() { protected void printVersion() {
System.out.println(); out.println();
System.out.println("ActiveMQ " + ActiveMQConnectionMetaData.PROVIDER_VERSION); out.println("ActiveMQ " + ActiveMQConnectionMetaData.PROVIDER_VERSION);
System.out.println("For help or more information please see: http://www.logicblaze.com"); out.println("For help or more information please see: http://www.logicblaze.com");
System.out.println(); out.println();
} }
protected void printError(String message) { protected void printError(String message) {
isPrintHelp = true; isPrintHelp = true;
System.out.println(message); out.println(message);
System.out.println(); out.println();
} }
abstract protected void startTask(List tokens) throws Exception; abstract protected void execute(List tokens) throws Exception;
abstract protected void printHelp(); abstract protected void printHelp();
} }

View File

@ -23,7 +23,7 @@ import java.util.List;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.io.IOException; import java.io.IOException;
public abstract class AbstractJmxTask extends AbstractTask { public abstract class AbstractJmxCommand extends AbstractCommand {
public static final String DEFAULT_JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"; public static final String DEFAULT_JMX_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
private JMXServiceURL jmxServiceUrl; private JMXServiceURL jmxServiceUrl;

View File

@ -17,8 +17,9 @@
package org.apache.activemq.broker.console; package org.apache.activemq.broker.console;
import java.util.List; import java.io.InputStream;
import java.io.PrintStream;
public interface Task { public interface Command {
public void runTask(List tokens) throws Exception; public int main(String[] args, InputStream in, PrintStream out);
} }

View File

@ -0,0 +1,66 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker.console;
import java.util.List;
public class DefaultCommand extends AbstractCommand {
protected void execute(List tokens) {
// Process task token
if( tokens.size() > 0 ) {
String taskToken = (String)tokens.remove(0);
if (taskToken.equals("start")) {
new StartCommand().execute(tokens);
} else if (taskToken.equals("stop")) {
new ShutdownCommand().execute(tokens);
} else if (taskToken.equals("list")) {
new ListCommand().execute(tokens);
} else if (taskToken.equals("query")) {
new QueryCommand().execute(tokens);
} else {
// If not valid task, push back to list
tokens.add(0, taskToken);
new StartCommand().execute(tokens);
}
} else {
new StartCommand().execute(tokens);
}
}
protected void printHelp() {
out.println("Usage: Main [task] [--extdir <dir>] [task-options] [task data]");
out.println("");
out.println("Tasks (default task is start):");
out.println(" start - Creates and starts a broker using a configuration file, or a broker URI.");
out.println(" stop - Stops a running broker specified by the broker name.");
out.println(" list - Lists all available brokers in the specified JMX context.");
out.println(" query - Display selected broker component's attributes and statistics.");
out.println(" --extdir <dir> - Add the jar files in the directory to the classpath.");
out.println(" --version - Display the version information.");
out.println(" -h,-?,--help - Display this help information. To display task specific help, use Main [task] -h,-?,--help");
out.println("");
out.println("Task Options:");
out.println(" - Properties specific to each task.");
out.println("");
out.println("Task Data:");
out.println(" - Information needed by each specific task.");
out.println("");
}
}

View File

@ -1,40 +0,0 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.broker.console;
public class DefaultTask extends StartTask {
protected void printHelp() {
System.out.println("Usage: Main [task] [--extdir <dir>] [task-options] [task data]");
System.out.println("");
System.out.println("Tasks (default task is start):");
System.out.println(" start - Creates and starts a broker using a configuration file, or a broker URI.");
System.out.println(" stop - Stops a running broker specified by the broker name.");
System.out.println(" list - Lists all available brokers in the specified JMX context.");
System.out.println(" query - Display selected broker component's attributes and statistics.");
System.out.println(" --extdir <dir> - Add the jar files in the directory to the classpath.");
System.out.println(" --version - Display the version information.");
System.out.println(" -h,-?,--help - Display this help information. To display task specific help, use Main [task] -h,-?,--help");
System.out.println("");
System.out.println("Task Options:");
System.out.println(" - Properties specific to each task.");
System.out.println("");
System.out.println("Task Data:");
System.out.println(" - Information needed by each specific task.");
System.out.println("");
}
}

View File

@ -18,9 +18,9 @@ package org.apache.activemq.broker.console;
import java.util.List; import java.util.List;
public class ListTask extends AbstractJmxTask { public class ListCommand extends AbstractJmxCommand {
protected void startTask(List tokens) { protected void execute(List tokens) {
try { try {
AmqJmxSupport.printBrokerList(AmqJmxSupport.getAllBrokers(createJmxConnector().getMBeanServerConnection())); AmqJmxSupport.printBrokerList(AmqJmxSupport.getAllBrokers(createJmxConnector().getMBeanServerConnection()));
closeJmxConnector(); closeJmxConnector();

View File

@ -27,7 +27,7 @@ import java.util.StringTokenizer;
import java.util.Set; import java.util.Set;
import java.util.Iterator; import java.util.Iterator;
public class QueryTask extends AbstractJmxTask { public class QueryCommand extends AbstractJmxCommand {
// Predefined type=identifier query // Predefined type=identifier query
private static final Properties PREDEFINED_OBJNAME_QUERY = new Properties(); private static final Properties PREDEFINED_OBJNAME_QUERY = new Properties();
@ -44,7 +44,7 @@ public class QueryTask extends AbstractJmxTask {
private final List querySubObjects = new ArrayList(10); private final List querySubObjects = new ArrayList(10);
private final List queryViews = new ArrayList(10); private final List queryViews = new ArrayList(10);
protected void startTask(List tokens) { protected void execute(List tokens) {
try { try {
// Connect to jmx server // Connect to jmx server
JMXConnector jmxConnector = createJmxConnector(); JMXConnector jmxConnector = createJmxConnector();

View File

@ -24,10 +24,10 @@ import java.util.Set;
import java.util.Iterator; import java.util.Iterator;
import java.util.HashSet; import java.util.HashSet;
public class ShutdownTask extends AbstractJmxTask { public class ShutdownCommand extends AbstractJmxCommand {
private boolean isStopAllBrokers = false; private boolean isStopAllBrokers = false;
protected void startTask(List brokerNames) { protected void execute(List brokerNames) {
try { try {
Set mbeans = new HashSet(); Set mbeans = new HashSet();
MBeanServerConnection server = createJmxConnector().getMBeanServerConnection(); MBeanServerConnection server = createJmxConnector().getMBeanServerConnection();

View File

@ -26,7 +26,7 @@ import java.util.Iterator;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
public class StartTask extends AbstractTask { public class StartCommand extends AbstractCommand {
public static final String DEFAULT_CONFIG_URI = "xbean:activemq.xml"; public static final String DEFAULT_CONFIG_URI = "xbean:activemq.xml";
@ -37,7 +37,7 @@ public class StartTask extends AbstractTask {
* The default task to start a broker or a group of brokers * The default task to start a broker or a group of brokers
* @param brokerURIs * @param brokerURIs
*/ */
protected void startTask(List brokerURIs) { protected void execute(List brokerURIs) {
try { try {
// If no config uri, use default setting // If no config uri, use default setting
if (brokerURIs.isEmpty()) { if (brokerURIs.isEmpty()) {