KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > common > ArgsParser


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.upgrade.common;
25
26
27 import java.util.logging.*;
28 import java.lang.reflect.Constructor JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
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 /**
41  * Parse the arguments for the upgrade tool
42  * and invoke the appropriate handler
43  *
44  * @author Hans Hrasna
45  */

46 public class ArgsParser {
47     
48     public static final String JavaDoc SOURCE = "source";
49     public static final String JavaDoc SOURCE_SHORT = "s";
50     public static final String JavaDoc TARGET = "target";
51     public static final String JavaDoc TARGET_SHORT = "t";
52     public static final String JavaDoc DOMAIN = "domain";
53     public static final String JavaDoc DOMAIN_SHORT = "d";
54     public static final String JavaDoc NSSPWD = "nsspwd";
55     public static final String JavaDoc NSSPWDFILE = "nsspwdfile";
56     public static final String JavaDoc NSSPWDFILE_SHORT = "n";
57     public static final String JavaDoc TARGETNSSPWD = "targetnsspwd";
58     public static final String JavaDoc TARGETNSSPWDFILE = "targetnsspwdfile";
59     public static final String JavaDoc TARGETNSSPWDFILE_SHORT = "e";
60     public static final String JavaDoc JKSPWD = "jkspwd";
61     public static final String JavaDoc JKSPWDFILE = "jkspwdfile";
62     public static final String JavaDoc JKSPWDFILE_SHORT = "j";
63     public static final String JavaDoc CAPWD = "capwd";
64     public static final String JavaDoc CAPWDFILE = "capwdfile";
65     public static final String JavaDoc CAPWDFILE_SHORT = "p";
66     public static final String JavaDoc ADMINUSER = "adminuser";
67     public static final String JavaDoc ADMINUSER_SHORT = "a";
68     public static final String JavaDoc ADMINPASSWORD = "adminpassword";
69     public static final String JavaDoc ADMINPASSWORD_SHORT = "w";
70     public static final String JavaDoc MASTERPASSWORD = "masterpassword";
71     public static final String JavaDoc MASTERPASSWORD_SHORT = "m";
72     public static final String JavaDoc CLINSTANCEINFO = "clinstancefiles";
73     public static final String JavaDoc CLINSTANCEINFO_SHORT = "i";
74     
75     private static Logger _logger=LogService.getLogger(LogService.UPGRADE_LOGGER);
76     private String JavaDoc [] arguments;
77     private CommonInfoModel commonInfo;
78     private HashMap JavaDoc interactiveMap;
79     private StringManager sm;
80     
81     /** Creates a new instance of ArgsParser */
82     public ArgsParser(String JavaDoc [] args, CommonInfoModel infoModel, HashMap JavaDoc hMap) {
83         sm = StringManager.getManager(LogService.UPGRADE_CLI_LOGGER);
84         commonInfo = infoModel;
85         interactiveMap = hMap;
86         arguments = args;
87     }
88     
89     public ArgsParser(String JavaDoc [] args, CommonInfoModel infoModel) {
90         this(args, infoModel, new HashMap JavaDoc());
91     }
92     
93     public HashMap JavaDoc parse() {
94         return parse(null);
95     }
96     
97     public HashMap JavaDoc parse(InteractiveInput interactiveParser) {
98         String JavaDoc cmd = null;
99         Vector JavaDoc parameters = new Vector JavaDoc();
100         Vector JavaDoc instructions = new Vector JavaDoc();
101         Class JavaDoc instructionHandler = null;
102         
103         for(int i=0;i<arguments.length;i++) {
104             if(arguments[i].startsWith("-")){
105                 String JavaDoc rawArg = arguments[i];
106                 String JavaDoc argument = rawArg.substring(rawArg.lastIndexOf("-") + 1, rawArg.length()); //strip - or --
107
String JavaDoc delimeters = "=";
108                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(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 JavaDoc();
114                 }
115                 if (st.hasMoreTokens()) { //there was an equal sign
116
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 JavaDoc msg = sm.getString("enterprise.tools.upgrade.cli.invalid_option",rawArg);
127             //start CR 6396918
128
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             //end CR 6396918
133
helpUsage(msg);
134                     System.exit(1);
135                 }
136             }
137         }
138         
139         //now process the parsed instructions
140
Enumeration JavaDoc e = instructions.elements();
141         while ( e.hasMoreElements() ) {
142             ParsedArgument pi = (ParsedArgument )e.nextElement();
143             instructionHandler = pi.getHandler();
144             try {
145                 Class JavaDoc [] parameterTypes = new Class JavaDoc [] {pi.getClass()};
146                 Constructor JavaDoc constructor = instructionHandler.getConstructor(parameterTypes);
147                 constructor.newInstance(new Object JavaDoc [] { pi });
148             } catch (Exception JavaDoc 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 JavaDoc getHandler(String JavaDoc cmd) {
163         Class JavaDoc instructionHandler = null;
164         try {
165         //start CR 6396918
166
if(cmd.equals(ADMINPASSWORD_SHORT) || cmd.equals(ADMINPASSWORD))
167                 instructionHandler = null;
168         else
169         //end CR 6396918
170
instructionHandler = Class.forName("com.sun.enterprise.tools.upgrade.common.arguments.ARG_" + cmd );
171             return instructionHandler;
172         } catch (ClassNotFoundException JavaDoc cnf) {
173             
174         } catch (Exception JavaDoc 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 JavaDoc 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