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