KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > cmdui > CommandHandlerFactory


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

16 package org.jmanage.cmdui;
17
18 import org.jmanage.cmdui.commands.*;
19
20 import java.util.*;
21
22 /**
23  *
24  * date: Feb 4, 2005
25  * @author Rakesh Kalra
26  */

27 public class CommandHandlerFactory implements CommandConstants {
28
29     private static Map commandNameToInstanceMap = new HashMap();
30     private static Set commandNames = new TreeSet();
31
32     static{
33         commandNameToInstanceMap.put(APPS, new ListApplicationsHandler());
34         commandNameToInstanceMap.put(MBEANS, new MBeansHandler());
35         commandNameToInstanceMap.put(QUERY_MBEANS, new QueryMBeansHandler());
36         commandNameToInstanceMap.put(INFO, new InfoHandler());
37         commandNameToInstanceMap.put(GET, new GetHandler());
38         commandNameToInstanceMap.put(SET, new SetHandler());
39         commandNameToInstanceMap.put(SET_ATTRS, new SetAttributesHandler());
40         commandNameToInstanceMap.put(PRINT, new PrintHandler());
41         commandNameToInstanceMap.put(EXECUTE, new ExecuteHandler());
42         commandNameToInstanceMap.put(HELP, new HelpHandler());
43         commandNameToInstanceMap.put(EXIT, new ExitHandler());
44
45         /* add to the command names set */
46         commandNames.addAll(commandNameToInstanceMap.keySet());
47     }
48
49     public static CommandHandler getHandler(String JavaDoc commandName)
50         throws InvalidCommandException {
51
52         CommandHandler handler =
53                 (CommandHandler)commandNameToInstanceMap.get(commandName);
54         if(handler == null){
55             throw new InvalidCommandException(commandName);
56         }
57         return handler;
58     }
59
60     public static Collection getCommandNames(){
61         return commandNames;
62     }
63
64     static void validateCommand(String JavaDoc commandName)
65         throws InvalidCommandException {
66
67         if(!commandNameToInstanceMap.containsKey(commandName)){
68             throw new InvalidCommandException(commandName);
69         }
70     }
71 }
72
Popular Tags