1 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 ; 31 import java.util.logging.Logger ; 32 import java.util.logging.Level ; 33 import java.io.IOException ; 34 35 40 public class Command { 41 42 private static final Logger logger = Loggers.getLogger(Command.class); 43 44 private String username; 45 private String password; 46 private String url; 47 private String name; 48 private Level logLevel = Level.WARNING; private String [] args; 50 51 static Command get(String line, Command authenticatedCommand) 52 throws InvalidCommandException { 53 54 assert authenticatedCommand.username != null && 55 authenticatedCommand.password != null; 56 String [] 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 [] 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; } 84 }else{ 85 86 command.name = args[index++]; 87 CommandHandlerFactory.validateCommand(command.name); 88 command.args = new String [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 getUsername() { 102 return username; 103 } 104 105 public String getPassword() { 106 return password; 107 } 108 109 public String getUrl() { 110 return url; 111 } 112 113 public String getName() { 114 return name; 115 } 116 117 public String [] getArgs() { 118 return args; 119 } 120 121 public Level 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 131 authRequired = false; 132 } 133 return authRequired; 134 } 135 136 public boolean authenticate() throws IOException { 137 138 while(true){ 139 if(username == null){ 140 Out.print("Username: "); 141 username = In.readLine(); 142 } 143 144 if(password == null){ 145 146 password = new String (PasswordField.getPassword("Password:")); 147 } 148 149 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 e) { 159 throw new RuntimeException (e); 160 } 161 } 162 163 return true; 164 } 165 166 public String toString(){ 167 StringBuffer buff = new StringBuffer (); 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 (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 [] toArgs(String str){ 201 StringTokenizer tokenizer = new StringTokenizer (str, " "); 202 String [] args = new String [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 |