001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.cmdui;
016:
017: import org.jmanage.cmdui.util.In;
018: import org.jmanage.cmdui.util.Out;
019: import org.jmanage.core.util.Loggers;
020: import org.jmanage.core.services.AuthService;
021: import org.jmanage.core.services.ServiceFactory;
022: import org.jmanage.core.services.ServiceContextImpl;
023: import org.jmanage.core.services.ServiceException;
024:
025: import org.jmanage.core.auth.UnAuthorizedAccessException;
026: import org.jmanage.core.util.PasswordField;
027: import org.jmanage.util.StringUtils;
028:
029: import java.util.StringTokenizer;
030: import java.util.logging.Logger;
031: import java.util.logging.Level;
032: import java.io.IOException;
033:
034: /**
035: *
036: * date: Feb 4, 2005
037: * @author Rakesh Kalra
038: */
039: public class Command {
040:
041: private static final Logger logger = Loggers
042: .getLogger(Command.class);
043:
044: private String username;
045: private String password;
046: private String url;
047: private String name;
048: private Level logLevel = Level.WARNING; // default log level
049: private String[] args;
050:
051: static Command get(String line, Command authenticatedCommand)
052: throws InvalidCommandException {
053:
054: assert authenticatedCommand.username != null
055: && authenticatedCommand.password != null;
056: String[] args = toArgs(line);
057: Command command = get(args);
058: assert command.username == null && command.password == null
059: && command.url == null : "Invalid command.";
060: command.username = authenticatedCommand.username;
061: command.password = authenticatedCommand.password;
062: command.url = authenticatedCommand.url;
063: command.logLevel = authenticatedCommand.logLevel;
064: return command;
065: }
066:
067: static Command get(String[] args) throws InvalidCommandException {
068:
069: Command command = new Command();
070: for (int index = 0; index < args.length; index++) {
071: if (args[index].equals("-username")) {
072: command.username = args[++index];
073: } else if (args[index].equals("-password")) {
074: command.password = args[++index];
075: } else if (args[index].equals("-url")) {
076: command.url = args[++index];
077: } else if (args[index].startsWith("-verbose")) {
078: int i = args[index].indexOf("=");
079: if (i != -1) {
080: command.logLevel = Level.parse(args[index]
081: .substring(i + 1));
082: } else {
083: command.logLevel = Level.FINE; // default verbose level
084: }
085: } else {
086: /* we have the command name and arguments */
087: command.name = args[index++];
088: CommandHandlerFactory.validateCommand(command.name);
089: command.args = new String[args.length - index];
090: int argsIndex = 0;
091: while (index < args.length) {
092: command.args[argsIndex++] = args[index++];
093: }
094: }
095: }
096: logger.fine("Command=" + command);
097: return command;
098: }
099:
100: private Command() {
101: }
102:
103: public String getUsername() {
104: return username;
105: }
106:
107: public String getPassword() {
108: return password;
109: }
110:
111: public String getUrl() {
112: return url;
113: }
114:
115: public String getName() {
116: return name;
117: }
118:
119: public String[] getArgs() {
120: return args;
121: }
122:
123: public Level getLogLevel() {
124: return logLevel;
125: }
126:
127: public boolean isAuthRequired() {
128: boolean authRequired = true;
129: if (name != null
130: && (name.equals(CommandConstants.HELP) || name
131: .equals(CommandConstants.EXIT))) {
132: /* no auth required */
133: authRequired = false;
134: }
135: return authRequired;
136: }
137:
138: public boolean authenticate() throws IOException {
139:
140: while (true) {
141: if (username == null) {
142: Out.print("Username: ");
143: username = In.readLine();
144: }
145:
146: if (password == null) {
147: /* get the password */
148: password = new String(PasswordField
149: .getPassword("Password:"));
150: }
151:
152: /* authenticate with the server */
153: AuthService authService = ServiceFactory.getAuthService();
154: try {
155: authService.login(new ServiceContextImpl(), username,
156: password);
157: break;
158: } catch (ServiceException e) {
159: Out.println(e.getMessage());
160: username = null;
161: password = null;
162: } catch (Exception e) {
163: throw new RuntimeException(e);
164: }
165: }
166:
167: return true;
168: }
169:
170: public String toString() {
171: StringBuffer buff = new StringBuffer();
172: buff.append("username=");
173: buff.append(username);
174: buff.append(", url=");
175: buff.append(url);
176: buff.append(", name=");
177: buff.append(name);
178: buff.append(", args=");
179: buff.append(StringUtils.stringArrayToCSV(args));
180: return buff.toString();
181: }
182:
183: public boolean execute() {
184: try {
185: assert getName() != null;
186: CommandHandler handler = CommandHandlerFactory
187: .getHandler(getName());
188: boolean result = handler.execute(getHandlerContext());
189: Out.println();
190: return result;
191: } catch (InvalidCommandException e) {
192: throw new RuntimeException(e);
193: } catch (ServiceException e) {
194: Out.println(e.getMessage());
195: Out.println();
196: return false;
197: } catch (UnAuthorizedAccessException e) {
198: Out
199: .println(e.getMessage()
200: + ", You are not authorized to perform this operation");
201: Out.println();
202: return false;
203: }
204: }
205:
206: private static String[] toArgs(String str) {
207: StringTokenizer tokenizer = new StringTokenizer(str, " ");
208: String[] args = new String[tokenizer.countTokens()];
209: for (int i = 0; i < args.length; i++) {
210: args[i] = tokenizer.nextToken();
211: }
212: return args;
213: }
214:
215: private HandlerContext getHandlerContext() {
216: return new HandlerContext(this, isAuthRequired());
217: }
218: }
|