1 23 24 package com.sun.enterprise.tools.upgrade.common; 25 26 27 import java.util.logging.*; 28 import java.lang.reflect.Constructor ; 29 import java.util.Enumeration ; 30 import java.util.Vector ; 31 import java.util.HashMap ; 32 import java.util.StringTokenizer ; 33 34 import com.sun.enterprise.tools.upgrade.common.arguments.ParsedArgument; 35 import com.sun.enterprise.tools.upgrade.common.arguments.ARG_help; 36 import com.sun.enterprise.tools.upgrade.common.CommonInfoModel; 37 import com.sun.enterprise.tools.upgrade.logging.*; 38 import com.sun.enterprise.util.i18n.StringManager; 39 40 46 public class ArgsParser { 47 48 public static final String SOURCE = "source"; 49 public static final String SOURCE_SHORT = "s"; 50 public static final String TARGET = "target"; 51 public static final String TARGET_SHORT = "t"; 52 public static final String DOMAIN = "domain"; 53 public static final String DOMAIN_SHORT = "d"; 54 public static final String NSSPWD = "nsspwd"; 55 public static final String NSSPWDFILE = "nsspwdfile"; 56 public static final String NSSPWDFILE_SHORT = "n"; 57 public static final String TARGETNSSPWD = "targetnsspwd"; 58 public static final String TARGETNSSPWDFILE = "targetnsspwdfile"; 59 public static final String TARGETNSSPWDFILE_SHORT = "e"; 60 public static final String JKSPWD = "jkspwd"; 61 public static final String JKSPWDFILE = "jkspwdfile"; 62 public static final String JKSPWDFILE_SHORT = "j"; 63 public static final String CAPWD = "capwd"; 64 public static final String CAPWDFILE = "capwdfile"; 65 public static final String CAPWDFILE_SHORT = "p"; 66 public static final String ADMINUSER = "adminuser"; 67 public static final String ADMINUSER_SHORT = "a"; 68 public static final String ADMINPASSWORD = "adminpassword"; 69 public static final String ADMINPASSWORD_SHORT = "w"; 70 public static final String MASTERPASSWORD = "masterpassword"; 71 public static final String MASTERPASSWORD_SHORT = "m"; 72 public static final String CLINSTANCEINFO = "clinstancefiles"; 73 public static final String CLINSTANCEINFO_SHORT = "i"; 74 75 private static Logger _logger=LogService.getLogger(LogService.UPGRADE_LOGGER); 76 private String [] arguments; 77 private CommonInfoModel commonInfo; 78 private HashMap interactiveMap; 79 private StringManager sm; 80 81 82 public ArgsParser(String [] args, CommonInfoModel infoModel, HashMap hMap) { 83 sm = StringManager.getManager(LogService.UPGRADE_CLI_LOGGER); 84 commonInfo = infoModel; 85 interactiveMap = hMap; 86 arguments = args; 87 } 88 89 public ArgsParser(String [] args, CommonInfoModel infoModel) { 90 this(args, infoModel, new HashMap ()); 91 } 92 93 public HashMap parse() { 94 return parse(null); 95 } 96 97 public HashMap parse(InteractiveInput interactiveParser) { 98 String cmd = null; 99 Vector parameters = new Vector (); 100 Vector instructions = new Vector (); 101 Class instructionHandler = null; 102 103 for(int i=0;i<arguments.length;i++) { 104 if(arguments[i].startsWith("-")){ 105 String rawArg = arguments[i]; 106 String argument = rawArg.substring(rawArg.lastIndexOf("-") + 1, rawArg.length()); String delimeters = "="; 108 StringTokenizer st = new StringTokenizer (argument, delimeters, false); 109 if ( st.hasMoreTokens() ) { 110 cmd = st.nextToken(); 111 if(cmd.equals("?")) cmd = "h"; 112 instructionHandler = getHandler(cmd); 113 parameters = new Vector (); 114 } 115 if (st.hasMoreTokens()) { parameters.add(st.nextToken()); 117 } 118 while(i + 1 < arguments.length && !arguments[i + 1].startsWith("-")) { 119 i++; 120 parameters.add(arguments[i]); 121 } 122 if ( instructionHandler != null ) { 123 interactiveMap.put(cmd, parameters); 124 instructions.addElement(new ParsedArgument(instructionHandler, cmd, parameters, commonInfo, interactiveMap)); 125 } else { 126 String msg = sm.getString("enterprise.tools.upgrade.cli.invalid_option",rawArg); 127 if(cmd.equals(ADMINPASSWORD_SHORT) || cmd.equals(ADMINPASSWORD)) { 129 msg = msg + "\n" + sm.getString("enterprise.tools.upgrade.cli.deprecated_option",rawArg) + 130 " " + sm.getString("enterprise.tools.upgrade.cli.deprecated_option.adminpassword"); 131 } 132 helpUsage(msg); 134 System.exit(1); 135 } 136 } 137 } 138 139 Enumeration e = instructions.elements(); 141 while ( e.hasMoreElements() ) { 142 ParsedArgument pi = (ParsedArgument )e.nextElement(); 143 instructionHandler = pi.getHandler(); 144 try { 145 Class [] parameterTypes = new Class [] {pi.getClass()}; 146 Constructor constructor = instructionHandler.getConstructor(parameterTypes); 147 constructor.newInstance(new Object [] { pi }); 148 } catch (Exception ex) { 149 _logger.log(Level.INFO, sm.getString("enterprise.tools.upgrade.cli.invalid_option", pi.getMneumonic()), ex); 150 System.exit(1); 151 } 152 153 } 154 155 if(interactiveParser != null) { 156 interactiveParser.collectMissingArguments(interactiveMap); 157 } 158 159 return interactiveMap; 160 } 161 162 private Class getHandler(String cmd) { 163 Class instructionHandler = null; 164 try { 165 if(cmd.equals(ADMINPASSWORD_SHORT) || cmd.equals(ADMINPASSWORD)) 167 instructionHandler = null; 168 else 169 instructionHandler = Class.forName("com.sun.enterprise.tools.upgrade.common.arguments.ARG_" + cmd ); 171 return instructionHandler; 172 } catch (ClassNotFoundException cnf) { 173 174 } catch (Exception e1) { 175 _logger.log(Level.INFO, sm.getString("enterprise.tools.upgrade.cli.invalid_option",e1), e1); 176 } 177 return null; 178 } 179 180 public void helpUsage(String str){ 181 System.out.println("\n" + str + "\n"); 182 helpUsage(); 183 } 184 185 public void helpUsage(){ 186 commonInfo.recover(); 187 new ARG_help(new ParsedArgument(null, null, null, commonInfo, null)); 188 } 189 } 190 | Popular Tags |