From 982a119ba6be7cd8a42feab61115d678eb612e9a Mon Sep 17 00:00:00 2001 From: "Hiram R. Chirino" Date: Sat, 14 Jan 2006 01:08:12 +0000 Subject: [PATCH] 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 --- .../java/org/apache/activemq/broker/Main.java | 72 +++---------------- ...AbstractTask.java => AbstractCommand.java} | 48 ++++++++----- ...ctJmxTask.java => AbstractJmxCommand.java} | 2 +- .../console/{Task.java => Command.java} | 7 +- .../broker/console/DefaultCommand.java | 66 +++++++++++++++++ .../activemq/broker/console/DefaultTask.java | 40 ----------- .../{ListTask.java => ListCommand.java} | 4 +- .../{QueryTask.java => QueryCommand.java} | 4 +- ...ShutdownTask.java => ShutdownCommand.java} | 4 +- .../{StartTask.java => StartCommand.java} | 4 +- 10 files changed, 121 insertions(+), 130 deletions(-) rename activemq-core/src/main/java/org/apache/activemq/broker/console/{AbstractTask.java => AbstractCommand.java} (64%) rename activemq-core/src/main/java/org/apache/activemq/broker/console/{AbstractJmxTask.java => AbstractJmxCommand.java} (95%) rename activemq-core/src/main/java/org/apache/activemq/broker/console/{Task.java => Command.java} (79%) create mode 100644 activemq-core/src/main/java/org/apache/activemq/broker/console/DefaultCommand.java delete mode 100644 activemq-core/src/main/java/org/apache/activemq/broker/console/DefaultTask.java rename activemq-core/src/main/java/org/apache/activemq/broker/console/{ListTask.java => ListCommand.java} (91%) rename activemq-core/src/main/java/org/apache/activemq/broker/console/{QueryTask.java => QueryCommand.java} (96%) rename activemq-core/src/main/java/org/apache/activemq/broker/console/{ShutdownTask.java => ShutdownCommand.java} (95%) rename activemq-core/src/main/java/org/apache/activemq/broker/console/{StartTask.java => StartCommand.java} (95%) diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/Main.java b/activemq-core/src/main/java/org/apache/activemq/broker/Main.java index 8c12f3ad2a..841141e363 100755 --- a/activemq-core/src/main/java/org/apache/activemq/broker/Main.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/Main.java @@ -17,6 +17,8 @@ package org.apache.activemq.broker; import java.io.File; +import java.io.InputStream; +import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.JarURLConnection; @@ -25,10 +27,10 @@ import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; -import java.util.List; -import java.util.LinkedList; import java.util.Arrays; import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; /** * Main class that can bootstrap an ActiveMQ broker console. Handles command line @@ -37,19 +39,9 @@ import java.util.Iterator; * @version $Revision$ */ 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_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"; + public static final String TASK_DEFAULT_CLASS = "org.apache.activemq.broker.console.DefaultCommand"; - private int taskType; private File activeMQHome; private ClassLoader classLoader; private List extensions = new ArrayList(5); @@ -62,9 +54,6 @@ public class Main { // Convert arguments to collection for easier management 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 app.parseExtensions(tokens); @@ -75,45 +64,13 @@ public class Main { app.addExtensionDirectory(new File(new File(app.getActiveMQHome(), "lib"), "optional")); } - // Succeeding tokens should be the task data try { - switch (app.getTaskType()) { - 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()); - } + app.runTaskClass(tokens); } catch (Throwable 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) { if (tokens.isEmpty()) { 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()); ClassLoader cl = getClassLoader(); // Use reflection to run the task. try { - Class task = cl.loadClass(taskClass); - Method runTask = task.getMethod("runTask", new Class[] { List.class }); - runTask.invoke(task.newInstance(), new Object[] { tokens }); + String[] args = (String[]) tokens.toArray(new String[tokens.size()]); + Class task = cl.loadClass(TASK_DEFAULT_CLASS); + 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) { throw e.getCause(); } catch (Throwable e) { @@ -220,14 +178,6 @@ public class Main { return classLoader; } - public int getTaskType() { - return taskType; - } - - public void setTaskType(int taskType) { - this.taskType = taskType; - } - public void setActiveMQHome(File activeMQHome) { this.activeMQHome = activeMQHome; } diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractTask.java b/activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractCommand.java similarity index 64% rename from activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractTask.java rename to activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractCommand.java index aeb3cc29bb..f03bc2bf72 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractTask.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractCommand.java @@ -18,21 +18,35 @@ package org.apache.activemq.broker.console; 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; -public abstract class AbstractTask implements Task { +public abstract class AbstractCommand implements Command { private boolean isPrintHelp = false; private boolean isPrintVersion = false; + protected PrintStream out; - public void runTask(List tokens) throws Exception { - parseOptions(tokens); + public int main(String[] args, InputStream in, PrintStream out) { + this.out = out; + try { + List tokens = new ArrayList(Arrays.asList(args)); + parseOptions(tokens); - if (isPrintHelp) { - printHelp(); - } else if (isPrintVersion) { - printVersion(); - } else { - startTask(tokens); + if (isPrintHelp) { + printHelp(); + } else if (isPrintVersion) { + printVersion(); + } else { + 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 else { - System.out.println("Ignoring unrecognized option: " + token); + out.println("Ignoring unrecognized option: " + token); } } protected void printVersion() { - System.out.println(); - System.out.println("ActiveMQ " + ActiveMQConnectionMetaData.PROVIDER_VERSION); - System.out.println("For help or more information please see: http://www.logicblaze.com"); - System.out.println(); + out.println(); + out.println("ActiveMQ " + ActiveMQConnectionMetaData.PROVIDER_VERSION); + out.println("For help or more information please see: http://www.logicblaze.com"); + out.println(); } protected void printError(String message) { isPrintHelp = true; - System.out.println(message); - System.out.println(); + out.println(message); + out.println(); } - abstract protected void startTask(List tokens) throws Exception; + abstract protected void execute(List tokens) throws Exception; abstract protected void printHelp(); } diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractJmxTask.java b/activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractJmxCommand.java similarity index 95% rename from activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractJmxTask.java rename to activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractJmxCommand.java index af5c1dc190..91a95a4231 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractJmxTask.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/console/AbstractJmxCommand.java @@ -23,7 +23,7 @@ import java.util.List; import java.net.MalformedURLException; 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"; private JMXServiceURL jmxServiceUrl; diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/console/Task.java b/activemq-core/src/main/java/org/apache/activemq/broker/console/Command.java similarity index 79% rename from activemq-core/src/main/java/org/apache/activemq/broker/console/Task.java rename to activemq-core/src/main/java/org/apache/activemq/broker/console/Command.java index 3b279ba5df..ee02dcfc2c 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/console/Task.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/console/Command.java @@ -17,8 +17,9 @@ package org.apache.activemq.broker.console; -import java.util.List; +import java.io.InputStream; +import java.io.PrintStream; -public interface Task { - public void runTask(List tokens) throws Exception; +public interface Command { + public int main(String[] args, InputStream in, PrintStream out); } diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/console/DefaultCommand.java b/activemq-core/src/main/java/org/apache/activemq/broker/console/DefaultCommand.java new file mode 100644 index 0000000000..de80ed4749 --- /dev/null +++ b/activemq-core/src/main/java/org/apache/activemq/broker/console/DefaultCommand.java @@ -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 ] [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 - 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(""); + } +} diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/console/DefaultTask.java b/activemq-core/src/main/java/org/apache/activemq/broker/console/DefaultTask.java deleted file mode 100644 index 6656afc31c..0000000000 --- a/activemq-core/src/main/java/org/apache/activemq/broker/console/DefaultTask.java +++ /dev/null @@ -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 ] [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 - 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(""); - } -} diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/console/ListTask.java b/activemq-core/src/main/java/org/apache/activemq/broker/console/ListCommand.java similarity index 91% rename from activemq-core/src/main/java/org/apache/activemq/broker/console/ListTask.java rename to activemq-core/src/main/java/org/apache/activemq/broker/console/ListCommand.java index dde1e426ed..d1d9503c27 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/console/ListTask.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/console/ListCommand.java @@ -18,9 +18,9 @@ package org.apache.activemq.broker.console; 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 { AmqJmxSupport.printBrokerList(AmqJmxSupport.getAllBrokers(createJmxConnector().getMBeanServerConnection())); closeJmxConnector(); diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/console/QueryTask.java b/activemq-core/src/main/java/org/apache/activemq/broker/console/QueryCommand.java similarity index 96% rename from activemq-core/src/main/java/org/apache/activemq/broker/console/QueryTask.java rename to activemq-core/src/main/java/org/apache/activemq/broker/console/QueryCommand.java index fd3f564045..8ddbce602d 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/console/QueryTask.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/console/QueryCommand.java @@ -27,7 +27,7 @@ import java.util.StringTokenizer; import java.util.Set; import java.util.Iterator; -public class QueryTask extends AbstractJmxTask { +public class QueryCommand extends AbstractJmxCommand { // Predefined type=identifier query 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 queryViews = new ArrayList(10); - protected void startTask(List tokens) { + protected void execute(List tokens) { try { // Connect to jmx server JMXConnector jmxConnector = createJmxConnector(); diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownTask.java b/activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownCommand.java similarity index 95% rename from activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownTask.java rename to activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownCommand.java index fa203bed9c..756dd95a8e 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownTask.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/console/ShutdownCommand.java @@ -24,10 +24,10 @@ import java.util.Set; import java.util.Iterator; import java.util.HashSet; -public class ShutdownTask extends AbstractJmxTask { +public class ShutdownCommand extends AbstractJmxCommand { private boolean isStopAllBrokers = false; - protected void startTask(List brokerNames) { + protected void execute(List brokerNames) { try { Set mbeans = new HashSet(); MBeanServerConnection server = createJmxConnector().getMBeanServerConnection(); diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/console/StartTask.java b/activemq-core/src/main/java/org/apache/activemq/broker/console/StartCommand.java similarity index 95% rename from activemq-core/src/main/java/org/apache/activemq/broker/console/StartTask.java rename to activemq-core/src/main/java/org/apache/activemq/broker/console/StartCommand.java index c241af4013..25ea67d59e 100644 --- a/activemq-core/src/main/java/org/apache/activemq/broker/console/StartTask.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/console/StartCommand.java @@ -26,7 +26,7 @@ import java.util.Iterator; import java.net.URI; import java.net.URISyntaxException; -public class StartTask extends AbstractTask { +public class StartCommand extends AbstractCommand { 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 * @param brokerURIs */ - protected void startTask(List brokerURIs) { + protected void execute(List brokerURIs) { try { // If no config uri, use default setting if (brokerURIs.isEmpty()) {