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.util.List;
020
021 import org.apache.activemq.ActiveMQConnectionMetaData;
022 import org.apache.activemq.console.CommandContext;
023
024 public abstract class AbstractCommand implements Command {
025 public static final String COMMAND_OPTION_DELIMETER = ",";
026
027 private boolean isPrintHelp;
028 private boolean isPrintVersion;
029
030 protected CommandContext context;
031
032 public void setCommandContext(CommandContext context) {
033 this.context = context;
034 }
035
036 /**
037 * Execute a generic command, which includes parsing the options for the
038 * command and running the specific task.
039 *
040 * @param tokens - command arguments
041 * @throws Exception
042 */
043 public void execute(List<String> tokens) throws Exception {
044 // Parse the options specified by "-"
045 parseOptions(tokens);
046
047 // Print the help file of the task
048 if (isPrintHelp) {
049 printHelp();
050
051 // Print the AMQ version
052 } else if (isPrintVersion) {
053 context.printVersion(ActiveMQConnectionMetaData.PROVIDER_VERSION);
054
055 // Run the specified task
056 } else {
057 runTask(tokens);
058 }
059 }
060
061 /**
062 * Parse any option parameters in the command arguments specified by a '-'
063 * as the first character of the token.
064 *
065 * @param tokens - command arguments
066 * @throws Exception
067 */
068 protected void parseOptions(List<String> tokens) throws Exception {
069 while (!tokens.isEmpty()) {
070 String token = tokens.remove(0);
071 if (token.startsWith("-")) {
072 // Token is an option
073 handleOption(token, tokens);
074 } else {
075 // Push back to list of tokens
076 tokens.add(0, token);
077 return;
078 }
079 }
080 }
081
082 /**
083 * Handle the general options for each command, which includes -h, -?,
084 * --help, -D, --version.
085 *
086 * @param token - option token to handle
087 * @param tokens - succeeding command arguments
088 * @throws Exception
089 */
090 protected void handleOption(String token, List<String> tokens) throws Exception {
091 // If token is a help option
092 if (token.equals("-h") || token.equals("-?") || token.equals("--help")) {
093 isPrintHelp = true;
094 tokens.clear();
095
096 // If token is a version option
097 } else if (token.equals("--version")) {
098 isPrintVersion = true;
099 tokens.clear();
100 } else if (token.startsWith("-D")) {
101 // If token is a system property define option
102 String key = token.substring(2);
103 String value = "";
104 int pos = key.indexOf("=");
105 if (pos >= 0) {
106 value = key.substring(pos + 1);
107 key = key.substring(0, pos);
108 }
109 System.setProperty(key, value);
110 } else {
111 // Token is unrecognized
112 context.printInfo("Unrecognized option: " + token);
113 isPrintHelp = true;
114 }
115 }
116
117 /**
118 * Run the specific task.
119 *
120 * @param tokens - command arguments
121 * @throws Exception
122 */
123 protected abstract void runTask(List<String> tokens) throws Exception;
124
125 /**
126 * Print the help messages for the specific task
127 */
128 protected abstract void printHelp();
129 }