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            isPrintHelp = false;
092            isPrintVersion = false;
093            // If token is a help option
094            if (token.equals("-h") || token.equals("-?") || token.equals("--help")) {
095                isPrintHelp = true;
096                tokens.clear();
097    
098                // If token is a version option
099            } else if (token.equals("--version")) {
100                isPrintVersion = true;
101                tokens.clear();
102            } else if (token.startsWith("-D")) {
103                // If token is a system property define option
104                String key = token.substring(2);
105                String value = "";
106                int pos = key.indexOf("=");
107                if (pos >= 0) {
108                    value = key.substring(pos + 1);
109                    key = key.substring(0, pos);
110                }
111                System.setProperty(key, value);
112            } else {
113                // Token is unrecognized
114                context.printInfo("Unrecognized option: " + token);
115                isPrintHelp = true;
116            }
117        }
118    
119        /**
120         * Run the specific task.
121         * 
122         * @param tokens - command arguments
123         * @throws Exception
124         */
125        protected abstract void runTask(List<String> tokens) throws Exception;
126    
127        /**
128         * Print the help messages for the specific task
129         */
130        protected abstract void printHelp();
131    }