1 18 package org.apache.activemq.console.command; 19 20 import org.apache.activemq.ActiveMQConnectionMetaData; 21 import org.apache.activemq.console.formatter.GlobalWriter; 22 23 import java.util.List ; 24 25 public abstract class AbstractCommand implements Command { 26 public static final String COMMAND_OPTION_DELIMETER = ","; 27 28 private boolean isPrintHelp = false; 29 private boolean isPrintVersion = false; 30 31 36 public void execute(List tokens) throws Exception { 37 parseOptions(tokens); 39 40 if (isPrintHelp) { 42 printHelp(); 43 44 } else if (isPrintVersion) { 46 GlobalWriter.printVersion(ActiveMQConnectionMetaData.PROVIDER_VERSION); 47 48 } else { 50 runTask(tokens); 51 } 52 } 53 54 59 protected void parseOptions(List tokens) throws Exception { 60 while (!tokens.isEmpty()) { 61 String token = (String )tokens.remove(0); 62 if (token.startsWith("-")) { 63 handleOption(token, tokens); 65 } else { 66 tokens.add(0, token); 68 return; 69 } 70 } 71 } 72 73 79 protected void handleOption(String token, List tokens) throws Exception { 80 if (token.equals("-h") || token.equals("-?") || token.equals("--help")) { 82 isPrintHelp = true; 83 tokens.clear(); 84 85 } else if (token.equals("--version")) { 87 isPrintVersion = true; 88 tokens.clear(); 89 } 90 91 else if (token.startsWith("-D")) { 93 String key = token.substring(2); 94 String value = ""; 95 int pos = key.indexOf("="); 96 if (pos >= 0) { 97 value = key.substring(pos + 1); 98 key = key.substring(0, pos); 99 } 100 System.setProperty(key, value); 101 102 } 103 104 else { 106 GlobalWriter.printInfo("Ignoring unrecognized option: " + token); 107 } 108 } 109 110 115 abstract protected void runTask(List tokens) throws Exception ; 116 117 120 abstract protected void printHelp(); 121 } 122 | Popular Tags |