KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.In;
19 import org.jmanage.cmdui.util.Out;
20 import org.jmanage.core.util.Loggers;
21 import org.jmanage.core.services.AuthService;
22 import org.jmanage.core.services.ServiceFactory;
23 import org.jmanage.core.services.ServiceContextImpl;
24 import org.jmanage.core.services.ServiceException;
25
26 import org.jmanage.core.auth.UnAuthorizedAccessException;
27 import org.jmanage.core.util.PasswordField;
28 import org.jmanage.util.StringUtils;
29
30 import java.util.StringTokenizer JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 /**
36  *
37  * date: Feb 4, 2005
38  * @author Rakesh Kalra
39  */

40 public class Command {
41
42     private static final Logger JavaDoc logger = Loggers.getLogger(Command.class);
43
44     private String JavaDoc username;
45     private String JavaDoc password;
46     private String JavaDoc url;
47     private String JavaDoc name;
48     private Level JavaDoc logLevel = Level.WARNING; // default log level
49
private String JavaDoc[] args;
50
51     static Command get(String JavaDoc line, Command authenticatedCommand)
52         throws InvalidCommandException {
53
54         assert authenticatedCommand.username != null &&
55                 authenticatedCommand.password != null;
56         String JavaDoc[] args = toArgs(line);
57         Command command = get(args);
58         assert command.username == null && command.password == null &&
59                 command.url == null:"Invalid command.";
60         command.username = authenticatedCommand.username;
61         command.password = authenticatedCommand.password;
62         command.url = authenticatedCommand.url;
63         command.logLevel = authenticatedCommand.logLevel;
64         return command;
65     }
66
67     static Command get(String JavaDoc[] args) throws InvalidCommandException {
68
69         Command command = new Command();
70         for(int index=0; index < args.length; index++){
71             if(args[index].equals("-username")){
72                 command.username = args[++index];
73             }else if(args[index].equals("-password")){
74                 command.password = args[++index];
75             }else if(args[index].equals("-url")){
76                 command.url = args[++index];
77             }else if(args[index].startsWith("-verbose")){
78                 int i=args[index].indexOf("=");
79                 if(i != -1){
80                     command.logLevel = Level.parse(args[index].substring(i + 1));
81                 }else{
82                     command.logLevel = Level.FINE; // default verbose level
83
}
84             }else{
85                 /* we have the command name and arguments */
86                 command.name = args[index++];
87                 CommandHandlerFactory.validateCommand(command.name);
88                 command.args = new String JavaDoc[args.length - index];
89                 int argsIndex = 0;
90                 while(index < args.length){
91                     command.args[argsIndex++] = args[index++];
92                 }
93             }
94         }
95         logger.fine("Command=" + command);
96         return command;
97     }
98
99     private Command(){}
100
101     public String JavaDoc getUsername() {
102         return username;
103     }
104
105     public String JavaDoc getPassword() {
106         return password;
107     }
108
109     public String JavaDoc getUrl() {
110         return url;
111     }
112
113     public String JavaDoc getName() {
114         return name;
115     }
116
117     public String JavaDoc[] getArgs() {
118         return args;
119     }
120
121     public Level JavaDoc getLogLevel() {
122         return logLevel;
123     }
124
125     public boolean isAuthRequired(){
126         boolean authRequired = true;
127         if(name != null &&
128                 (name.equals(CommandConstants.HELP) ||
129                 name.equals(CommandConstants.EXIT))){
130             /* no auth required */
131             authRequired = false;
132         }
133         return authRequired;
134     }
135
136     public boolean authenticate() throws IOException JavaDoc {
137
138         while(true){
139             if(username == null){
140                 Out.print("Username: ");
141                 username = In.readLine();
142             }
143
144             if(password == null){
145                 /* get the password */
146                 password = new String JavaDoc(PasswordField.getPassword("Password:"));
147             }
148
149             /* authenticate with the server */
150             AuthService authService = ServiceFactory.getAuthService();
151             try {
152                 authService.login(new ServiceContextImpl(), username, password);
153                 break;
154             } catch (ServiceException e){
155                 Out.println(e.getMessage());
156                 username = null;
157                 password = null;
158             } catch (Exception JavaDoc e) {
159                 throw new RuntimeException JavaDoc(e);
160             }
161         }
162
163         return true;
164     }
165
166     public String JavaDoc toString(){
167         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
168         buff.append("username=");
169         buff.append(username);
170         buff.append(", url=");
171         buff.append(url);
172         buff.append(", name=");
173         buff.append(name);
174         buff.append(", args=");
175         buff.append(StringUtils.stringArrayToCSV(args));
176         return buff.toString();
177     }
178
179     public boolean execute() {
180         try {
181             assert getName() != null;
182             CommandHandler handler =
183                         CommandHandlerFactory.getHandler(getName());
184             boolean result = handler.execute(getHandlerContext());
185             Out.println();
186             return result;
187         } catch (InvalidCommandException e) {
188             throw new RuntimeException JavaDoc(e);
189         } catch (ServiceException e){
190             Out.println(e.getMessage());
191             Out.println();
192             return false;
193         } catch (UnAuthorizedAccessException e){
194             Out.println(e.getMessage()+ ", You are not authorized to perform this operation");
195             Out.println();
196             return false;
197         }
198     }
199
200     private static String JavaDoc[] toArgs(String JavaDoc str){
201         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(str, " ");
202         String JavaDoc[] args = new String JavaDoc[tokenizer.countTokens()];
203         for(int i=0; i<args.length; i++){
204             args[i] = tokenizer.nextToken();
205         }
206         return args;
207     }
208
209     private HandlerContext getHandlerContext(){
210         return new HandlerContext(this, isAuthRequired());
211     }
212 }
213
Popular Tags