KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > cli > CLIParser


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.cli;
25
26 /**
27  *
28  * author : Gautam Borah
29  *
30  */

31
32 import java.io.*;
33 import java.util.*;
34 import java.util.logging.*;
35 import com.sun.enterprise.tools.upgrade.logging.*;
36 import com.sun.enterprise.util.i18n.StringManager;
37 import com.sun.enterprise.tools.upgrade.common.*;
38 import com.sun.enterprise.cli.framework.*;
39
40 public class CLIParser extends ArgsParser implements InteractiveInput {
41     
42     private StringManager sm;
43     
44     private boolean interactiveInput = false;
45     private Map interactiveInputMap;
46     private String JavaDoc currentDomain;
47     private int index=0;
48     private CommonInfoModel commonInfo;
49     private ArgsParser parser;
50     
51     public CLIParser(){
52         this(new CommonInfoModel(), new String JavaDoc [] {});
53     }
54     
55     public CLIParser(CommonInfoModel cim, String JavaDoc [] args) {
56         super(args, cim);
57         commonInfo = cim;
58         sm = StringManager.getManager(LogService.UPGRADE_CLI_LOGGER);
59         interactiveInputMap = parse(this);
60         verifyCommonInfo(commonInfo);
61         if(!commonInfo.isUpgradeSupported()) {
62             getLogger().info(sm.getString("enterprise.tools.upgrade.cli.upgrade_not_supported") +
63                     commonInfo.getSourceVersionAndEdition() + " -> " + commonInfo.getTargetVersionAndEdition());
64             System.exit(1);
65         }
66         commonInfo.enlistDomainsFromSource();
67     }
68     
69     public void setCommonInfoModel(CommonInfoModel commonInfo){
70         this.commonInfo=commonInfo;
71     }
72     
73     public CommonInfoModel getCommonInfoModel(CommonInfoModel commonInfo){
74         return commonInfo;
75     }
76     
77     public void printInfo(){
78         commonInfo.printInfo();
79     }
80     
81     public Logger getLogger() {
82         return LogService.getLogger(LogService.UPGRADE_LOGGER);
83     }
84     
85     public void parseComandLineArguments(String JavaDoc[] args) {
86         parser = new ArgsParser(args, commonInfo);
87         interactiveInputMap = parser.parse(this);
88         verifyCommonInfo(commonInfo);
89         if(!commonInfo.isUpgradeSupported()) {
90             getLogger().info(sm.getString("enterprise.tools.upgrade.cli.upgrade_not_supported") +
91                     commonInfo.getSourceVersionAndEdition() + " -> " + commonInfo.getTargetVersionAndEdition());
92             System.exit(1);
93         }
94         commonInfo.enlistDomainsFromSource();
95     }
96     
97     public void collectMissingArguments(Map inputMap) {
98         if(inputMap.containsKey("noprompt"))
99             return;
100         List domainList = commonInfo.getDomainList();
101         
102         // Following are Invalid Input for Interaction
103
//If either source is null but domain name is not null
104
//If either nsspwd, jkdpwd, capwd not null but domain name is null
105
Iterator itr = domainList.iterator();
106         while(itr.hasNext()) {
107             String JavaDoc domainName = (String JavaDoc)itr.next();
108             String JavaDoc suffix = "-" + domainName;
109             if( inputMap.get(SOURCE) == null && inputMap.get(SOURCE_SHORT) == null
110                     && inputMap.get(DOMAIN+suffix) != null) {
111                 helpUsage();
112                 System.exit(1);
113             }
114         }
115         
116         if(inputMap.get(SOURCE) == null && inputMap.get(SOURCE_SHORT) == null) {
117             System.out.print(sm.getString("enterprise.tools.upgrade.cli.Source_input"));
118             byte b[] = new byte[1024];
119             try {
120                 int c = System.in.read(b);
121                 if (c == -1) { // input stream closed, maybe by ^C
122
System.exit(1);
123                 }
124                 String JavaDoc sourceDir = new String JavaDoc(b,0,c);
125                 String JavaDoc source = sourceDir.trim();
126                 File biDir = new File(source+File.separator+"bin");
127                 if(source.equals("") || !biDir.isDirectory()) {
128                     if(!(commonInfo.checkSourceInputAsDomainRoot(source))) {
129             //start CR 6401898
130
//helpUsage();
131
getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.not_valid_source_install"));
132                         helpUsage();
133                         //end CR 6401898
134
System.exit(1);
135                     }
136                 }
137                 if(commonInfo.checkSourceInputAsDomainRoot(source))
138                     commonInfo.setSourceDomainRoot(source);
139                 commonInfo.setSourceInstallDir(source);
140                 //commonInfo.enlistDomainsFromSource();
141
inputMap.put(SOURCE,source);
142                 interactiveInput = true;
143             }catch(Exception JavaDoc e) {
144                 getLogger().log(Level.SEVERE, sm.getString("enterprise.tools.upgrade.cli.unexpectedException"), e);
145             }
146         }
147         if(inputMap.get(TARGET) == null && inputMap.get(TARGET_SHORT) == null) {
148             System.out.print(sm.getString("enterprise.tools.upgrade.cli.Target_input"));
149             byte b[] = new byte[1024];
150             try {
151                 int c = System.in.read(b);
152                 if (c == -1) { // input stream closed, maybe by ^C
153
System.exit(1);
154                 }
155                 String JavaDoc targetDir = new String JavaDoc(b,0,c);
156                 String JavaDoc target = targetDir.trim();
157                 File biDir = new File(target+File.separator+"bin");
158                 if (!UpgradeUtils.getUpgradeUtils(commonInfo).isValidTargetPath(target)) {
159                     getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.not_valid_target_install"));
160                     helpUsage();
161                     System.exit(1);
162                 }
163                 commonInfo.setTargetDomainRoot(target);
164                 commonInfo.enlistDomainsFromSource();
165                 inputMap.put(TARGET,target);
166                 interactiveInput = true;
167             }catch(Exception JavaDoc e) {
168                 getLogger().log(Level.SEVERE, sm.getString("enterprise.tools.upgrade.cli.unexpectedException"), e);
169             }
170         }
171         if(inputMap.get(ADMINUSER) == null && inputMap.get(ADMINUSER_SHORT) == null) {
172             System.out.print(sm.getString("enterprise.tools.upgrade.cli.adminuser_input"));
173             byte b[] = new byte[1024];
174             try {
175                 int c = System.in.read(b);
176                 if (c == -1) { // input stream closed, maybe by ^C
177
System.exit(1);
178                 }
179                 String JavaDoc adminUser = new String JavaDoc(b,0,c);
180                 String JavaDoc admiuser = adminUser.trim();
181                 
182                 commonInfo.setAdminUserName(admiuser);
183                 inputMap.put(ADMINUSER,admiuser);
184                 interactiveInput = true;
185             }catch(Exception JavaDoc e) {
186                 getLogger().log(Level.SEVERE, sm.getString("enterprise.tools.upgrade.cli.unexpectedException"), e);
187             }
188         }
189         if(inputMap.get(ADMINPASSWORD) == null && inputMap.get(ADMINPASSWORD_SHORT) == null) {
190             System.out.print(sm.getString("enterprise.tools.upgrade.cli.adminpassword_input"));
191             byte b[] = new byte[1024];
192             try {
193                 String JavaDoc adminPassword = new CliUtil().getPassword();
194                 commonInfo.setAdminPassword(adminPassword);
195                 inputMap.put(ADMINPASSWORD,adminPassword);
196                 interactiveInput = true;
197                 if(commonInfo.getTargetVersionAndEdition().equals(UpgradeConstants.VERSION_AS90_PE))
198                     verifyUserAndPasswords(commonInfo.getAdminUserName(),commonInfo.getAdminPassword(),null);
199             }catch(Exception JavaDoc e) {
200                 getLogger().log(Level.SEVERE, sm.getString("enterprise.tools.upgrade.cli.unexpectedException"), e); }
201         }
202         
203         if(!commonInfo.getTargetVersionAndEdition().equals(UpgradeConstants.VERSION_AS90_PE)){
204             if(inputMap.get(MASTERPASSWORD) == null && inputMap.get(MASTERPASSWORD_SHORT) == null) {
205                 System.out.print(sm.getString("enterprise.tools.upgrade.cli.MasterPW_input"));
206                 String JavaDoc password = new CliUtil().getPassword();
207                 commonInfo.setMasterPassword(password);
208                 inputMap.put(MASTERPASSWORD, password);
209                 verifyUserAndPasswords(commonInfo.getAdminUserName(),commonInfo.getAdminPassword(),password);
210                 interactiveInput = true;
211             }
212         }
213         
214         if(inputMap.get(CLINSTANCEINFO) == null && inputMap.get(CLINSTANCEINFO_SHORT) == null && commonInfo.checkUpgradefrom7xeeto9xee()) {
215             System.out.print(sm.getString("enterprise.tools.upgrade.cli.clinstance_input"));
216             byte b[] = new byte[1024];
217             try {
218                 int c = System.in.read(b);
219                 if (c == -1) { // input stream closed, maybe by ^C
220
System.exit(1);
221                 }
222                 String JavaDoc clinstance = new String JavaDoc(b,0,c);
223                 String JavaDoc clinstancetrim = clinstance.trim();
224                 if (clinstancetrim.length() > 2) {
225                     StringTokenizer st = new StringTokenizer(clinstancetrim,",");
226                     Vector clinstanceList = new Vector();
227                     while(st.hasMoreTokens()) {
228                         clinstanceList.add(st.nextElement());
229                     }
230                     commonInfo.processClinstnceConfFiles(clinstanceList);
231                 }
232                 inputMap.put(CLINSTANCEINFO,clinstancetrim);
233                 interactiveInput = true;
234             }catch(Exception JavaDoc e) {
235                 getLogger().log(Level.SEVERE, sm.getString("enterprise.tools.upgrade.cli.unexpectedException"), e);
236             }
237         }
238         
239         if(commonInfo.isUpgradeJKStoJKS()) {
240             if(inputMap.containsKey(JKSPWDFILE) || inputMap.containsKey(JKSPWDFILE_SHORT)) {
241                 if(inputMap.containsKey(CAPWDFILE) || inputMap.containsKey(CAPWDFILE_SHORT)) {
242                     return;
243                 } else {
244                     helpUsage();
245                     System.exit(1);
246                 }
247             }
248         }
249         if(commonInfo.isUpgradeJKStoNSS() || commonInfo.isUpgradeNSStoJKS()) {
250             if(inputMap.containsKey(JKSPWDFILE) || inputMap.containsKey(JKSPWDFILE_SHORT)) {
251                 if(inputMap.containsKey(CAPWDFILE) || inputMap.containsKey(CAPWDFILE_SHORT)) {
252                     if(inputMap.containsKey(NSSPWDFILE) || inputMap.containsKey(NSSPWDFILE_SHORT)) {
253                         return;
254                     } else {
255                         helpUsage();
256                         System.exit(1);
257                     }
258                 }
259             }
260         }
261         if(commonInfo.isUpgradeNSStoNSS()) {
262             if(inputMap.containsKey(NSSPWDFILE) || inputMap.containsKey(NSSPWDFILE_SHORT)) {
263                 if(inputMap.containsKey(TARGETNSSPWDFILE) || inputMap.containsKey(TARGETNSSPWDFILE_SHORT)) {
264                     return;
265                 } else {
266                     helpUsage();
267                     System.exit(1);
268                 }
269             }
270         }
271         //certificate password collection starts here
272
if (commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)) {
273             List stdAloneInsts = UpgradeUtils.getUpgradeUtils(this.commonInfo).getStandAloneInstances(this.commonInfo.getDomainMapping());
274             itr = stdAloneInsts.iterator();
275         } else {
276             domainList = commonInfo.getDomainList();
277             itr = domainList.iterator();
278         }
279         
280         while(itr.hasNext()) {
281             String JavaDoc domainName;
282             String JavaDoc suffix;
283             String JavaDoc certName;
284             String JavaDoc certOption;
285             String JavaDoc instanceName = null;
286             if (commonInfo.getSourceVersion().equals(UpgradeConstants.VERSION_7X)) {
287                 Vector instDInfo = (Vector)itr.next();
288                 instanceName = (String JavaDoc)instDInfo.elementAt(0);
289                 DomainInfo dInfo = (DomainInfo)instDInfo.elementAt(1);
290                 domainName = dInfo.getDomainName();
291                 suffix = "-" + domainName;
292                 certName = domainName + ":" + instanceName;
293                 certOption = DOMAIN + "-" + certName;
294             } else {
295                 domainName = (String JavaDoc)itr.next();
296                 suffix = "-" + domainName;
297                 certName = domainName;
298                 certOption = DOMAIN + suffix;
299             }
300             boolean certmigration = false;
301             if(inputMap.get(certOption) == null) {
302                 String JavaDoc answer="no";
303                 while(true) {
304                     System.out.print(sm.getString("enterprise.tools.upgrade.cli.Ask_domain_cert_migration", certName));
305                     byte b[] = new byte[1024];
306                     try {
307                         int c = System.in.read(b);
308                         if (c == -1) { // input stream closed, maybe by ^C
309
System.exit(1);
310                         }
311                         String JavaDoc ans = new String JavaDoc(b,0,c);
312                         answer = ans.trim();
313                         if(answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")
314                         ||answer.equalsIgnoreCase("no") || answer.equalsIgnoreCase("n") || answer.equalsIgnoreCase(""))
315                             break;
316                     }catch(Exception JavaDoc e) {
317                         getLogger().log(Level.SEVERE, sm.getString("enterprise.tools.upgrade.cli.unexpectedException"), e);
318                     }
319                 }
320                 if(answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")) {
321                     if(!commonInfo.getDomainList().contains(domainName)){
322                         helpUsage();
323                         getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.domain_does_not_exist",domainName));
324                         System.exit(1);
325                     }
326                     commonInfo.setCurrentDomain(domainName);
327                     commonInfo.addDomainOptionName(domainName);
328                     inputMap.put(DOMAIN+"-"+commonInfo.getCurrentDomain(), domainName);
329                     interactiveInput = true;
330                     commonInfo.setCertificateConversionFlag(true);
331                     certmigration = true;
332                 }
333                 
334             } else {
335                 commonInfo.setCertificateConversionFlag(true);
336                 certmigration = true;
337             }
338             
339             if((inputMap.get(NSSPWD+suffix) == null && certmigration) && (commonInfo.isUpgradeNSStoJKS() || commonInfo.isUpgradeJKStoNSS() || commonInfo.isUpgradeNSStoNSS()) ) {
340                 //String password = PasswordUtil.acceptPassword(sm.getString("enterprise.tools.upgrade.cli.NSS_input"));
341
if(commonInfo.isUpgradeJKStoNSS())
342                     System.out.print(sm.getString("enterprise.tools.upgrade.cli.Target_NSS_pwd_input"));
343                 else
344                     System.out.print(sm.getString("enterprise.tools.upgrade.cli.NSS_pwd_input"));
345                 String JavaDoc password = new CliUtil().getPassword();
346                 if(instanceName != null) {
347                     commonInfo.setCertDbPassword(domainName,instanceName, password);
348                 } else {
349                     commonInfo.setCertDbPassword(password);
350                 }
351                 
352                 boolean validpwd = PasswordVerifier.verifySourceNSSPassword(commonInfo);
353                 if(!validpwd) {
354                     commonInfo.recover();
355                     getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.Invalid_nss_password"));
356                     System.exit(1);
357                 }
358                 inputMap.put(NSSPWD+"-"+domainName, password);
359                 interactiveInput = true;
360             }
361             if((inputMap.get(TARGETNSSPWD+suffix) == null && certmigration) && commonInfo.isUpgradeNSStoNSS() ) {
362                 //String password = PasswordUtil.acceptPassword(sm.getString("enterprise.tools.upgrade.cli.NSS_input"));
363
System.out.print(sm.getString("enterprise.tools.upgrade.cli.Target_NSS_pwd_input"));
364                 String JavaDoc password = new CliUtil().getPassword();
365                 commonInfo.setTargetCertDbPassword(password);
366                 boolean validpwd = PasswordVerifier.verifyTargetNSSPassword(commonInfo,commonInfo.getTargetConfig());
367                 if(!validpwd) {
368                     commonInfo.recover();
369                     getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.Invalid_target_nss_password"));
370                     System.exit(1);
371                 }
372                 inputMap.put(TARGETNSSPWD+"-"+domainName, password);
373                 interactiveInput = true;
374             }
375             
376             if((inputMap.get(JKSPWD+suffix) == null && certmigration) && ( commonInfo.isUpgradeJKStoJKS() || commonInfo.isUpgradeJKStoNSS() || commonInfo.isUpgradeNSStoJKS())) {
377                 //String password = PasswordUtil.acceptPassword(sm.getString("enterprise.tools.upgrade.cli.JKS_input"));
378
if(commonInfo.isUpgradeNSStoJKS())
379                     System.out.print(sm.getString("enterprise.tools.upgrade.cli.Target_JKS_input"));
380                 if(commonInfo.isUpgradeJKStoNSS())
381                     System.out.print(sm.getString("enterprise.tools.upgrade.cli.JKS_input"));
382                 if(commonInfo.isUpgradeJKStoJKS())
383                     System.out.print(sm.getString("enterprise.tools.upgrade.cli.JKS_password"));
384                 String JavaDoc password = new CliUtil().getPassword();
385                 commonInfo.setJksKeystorePassword(password);
386                 //String jksPath=commonInfo.getTargetJKSKeyStorePath();
387
String JavaDoc jksPath="";
388                 if(commonInfo.isUpgradeNSStoJKS())
389                     jksPath=commonInfo.getTargetJKSKeyStorePath();
390                 else
391                     jksPath=commonInfo.getSourceJKSKeyStorePath();
392                 boolean validpwd = PasswordVerifier.verifyKeystorePassword(jksPath,password );
393                 if(!validpwd) {
394                     commonInfo.recover();
395                     getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.Invalid_jks_keypair_password"));
396                     System.exit(1);
397                 }
398                 inputMap.put(JKSPWD+"-"+domainName, password);
399                 interactiveInput = true;
400             }
401             
402             if((inputMap.get(CAPWD+suffix) == null && certmigration) && ( commonInfo.isUpgradeJKStoJKS() || commonInfo.isUpgradeJKStoNSS() || commonInfo.isUpgradeNSStoJKS())) {
403                 //String password = PasswordUtil.acceptPassword(sm.getString("enterprise.tools.upgrade.cli.CA_input"));
404
if(commonInfo.isUpgradeNSStoJKS())
405                     System.out.print(sm.getString("enterprise.tools.upgrade.cli.Target_CA_input"));
406                 if(commonInfo.isUpgradeJKStoNSS())
407                     System.out.print(sm.getString("enterprise.tools.upgrade.cli.CA_input"));
408                 if(commonInfo.isUpgradeJKStoJKS())
409                     System.out.print(sm.getString("enterprise.tools.upgrade.cli.CA_password"));
410                 String JavaDoc password = new CliUtil().getPassword();
411                 commonInfo.setJksCAKeystorePassword(password);
412                 //String jksPath=commonInfo.getTargetTrustedJKSKeyStorePath();
413
String JavaDoc trustJksPath="";
414                 if(commonInfo.isUpgradeNSStoJKS())
415                     trustJksPath = commonInfo.getTargetTrustedJKSKeyStorePath();
416                 else
417                     trustJksPath = commonInfo.getSourceTrustedJKSKeyStorePath();
418                 boolean validpwd = PasswordVerifier.verifyKeystorePassword(trustJksPath,password );
419                 if(!validpwd) {
420                     commonInfo.recover();
421                     getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.Invalid_jks_CA_password"));
422                     System.exit(1);
423                 }
424                 inputMap.put(CAPWD+"-"+domainName, password);
425                 interactiveInput = true;
426             }
427         }
428     }
429     
430     private void verifyUserAndPasswords(String JavaDoc adminUser, String JavaDoc adminPassword, String JavaDoc masterPassword) {
431         
432         if(!UpgradeUtils.getUpgradeUtils(commonInfo).validateUserDetails(adminUser,adminPassword,masterPassword)) {
433             if(!commonInfo.getTargetVersionAndEdition().equals(UpgradeConstants.VERSION_AS90_PE)){
434                 getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.wrong_adminuser_or_adminpassword_or_masterpassword"));
435             } else {
436                 getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.wrong_adminuser_or_adminpassword"));
437             }
438             System.exit(1);
439         }
440         
441     }
442     
443     public void verifyCommonInfo(CommonInfoModel commonInfo){
444         if(commonInfo.getSourceInstallDir().equals(commonInfo.getTargetInstallDir())) {
445             String JavaDoc msg = sm.getString("enterprise.tools.upgrade.cli.Invalid_input_directory");
446             helpUsage(msg);
447             System.exit(1);
448         }
449         if(!(interactiveInputMap.containsKey(SOURCE) || interactiveInputMap.containsKey(SOURCE_SHORT) &&
450                 interactiveInputMap.containsKey(TARGET) || interactiveInputMap.containsKey(TARGET_SHORT) &&
451                 interactiveInputMap.containsKey(ADMINUSER) || interactiveInputMap.containsKey(ADMINUSER_SHORT)&&
452                 interactiveInputMap.containsKey(ADMINPASSWORD) || interactiveInputMap.containsKey(ADMINPASSWORD_SHORT))){
453             helpUsage();
454             System.exit(1);
455         }
456     }
457
458     
459     public static void main(String JavaDoc[] args) throws Exception JavaDoc{
460         CLIParser parser = new CLIParser();
461         parser.setCommonInfoModel(new CommonInfoModel());
462         parser.parseComandLineArguments(args);
463         parser.printInfo();
464     }
465     
466     private String JavaDoc readPassword(String JavaDoc pwdfile) {
467         try {
468             BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(pwdfile)));
469             String JavaDoc password = reader.readLine();
470             if(password != null)
471                 password = password.trim();
472             else {
473                 getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.password_missing",pwdfile));
474                 System.exit(1);
475             }
476             return password;
477         }catch(FileNotFoundException fe){
478             helpUsage();
479             getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.password_file_missing",pwdfile));
480             System.exit(1);
481         }catch(Exception JavaDoc io){
482             getLogger().severe(sm.getString("enterprise.tools.upgrade.cli.password_missing",pwdfile));
483             System.exit(1);
484         }
485         return null;
486     }
487 }
488
489
490
491
Popular Tags