KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > command > AbstractCommand


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
24
25 public abstract class AbstractCommand implements Command {
26     public static final String JavaDoc COMMAND_OPTION_DELIMETER = ",";
27
28     private boolean isPrintHelp = false;
29     private boolean isPrintVersion = false;
30
31     /**
32      * Exceute a generic command, which includes parsing the options for the command and running the specific task.
33      * @param tokens - command arguments
34      * @throws Exception
35      */

36     public void execute(List JavaDoc tokens) throws Exception JavaDoc {
37         // Parse the options specified by "-"
38
parseOptions(tokens);
39
40         // Print the help file of the task
41
if (isPrintHelp) {
42             printHelp();
43
44         // Print the AMQ version
45
} else if (isPrintVersion) {
46             GlobalWriter.printVersion(ActiveMQConnectionMetaData.PROVIDER_VERSION);
47
48         // Run the specified task
49
} else {
50             runTask(tokens);
51         }
52     }
53
54     /**
55      * Parse any option parameters in the command arguments specified by a '-' as the first character of the token.
56      * @param tokens - command arguments
57      * @throws Exception
58      */

59     protected void parseOptions(List JavaDoc tokens) throws Exception JavaDoc {
60         while (!tokens.isEmpty()) {
61             String JavaDoc token = (String JavaDoc)tokens.remove(0);
62             if (token.startsWith("-")) {
63                 // Token is an option
64
handleOption(token, tokens);
65             } else {
66                 // Push back to list of tokens
67
tokens.add(0, token);
68                 return;
69             }
70         }
71     }
72
73     /**
74      * Handle the general options for each command, which includes -h, -?, --help, -D, --version.
75      * @param token - option token to handle
76      * @param tokens - succeeding command arguments
77      * @throws Exception
78      */

79     protected void handleOption(String JavaDoc token, List JavaDoc tokens) throws Exception JavaDoc {
80         // If token is a help option
81
if (token.equals("-h") || token.equals("-?") || token.equals("--help")) {
82             isPrintHelp = true;
83             tokens.clear();
84
85         // If token is a version option
86
} else if (token.equals("--version")) {
87             isPrintVersion = true;
88             tokens.clear();
89         }
90
91         // If token is a system property define option
92
else if (token.startsWith("-D")) {
93             String JavaDoc key = token.substring(2);
94             String JavaDoc 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         // Token is unrecognized
105
else {
106             GlobalWriter.printInfo("Ignoring unrecognized option: " + token);
107         }
108     }
109
110     /**
111      * Run the specific task.
112      * @param tokens - command arguments
113      * @throws Exception
114      */

115     abstract protected void runTask(List JavaDoc tokens) throws Exception JavaDoc;
116
117     /**
118      * Print the help messages for the specific task
119      */

120     abstract protected void printHelp();
121 }
122
Popular Tags