KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > BaseLifeCycleCommand


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 /*
25  * $Id: BaseLifeCycleCommand.java,v 1.8 2006/02/18 20:53:41 pa100654 Exp $
26  */

27
28 package com.sun.enterprise.cli.commands;
29 import com.sun.enterprise.cli.framework.*;
30
31 import com.sun.enterprise.util.ProcessExecutor;
32 import com.sun.enterprise.util.ExecException;
33 import com.sun.enterprise.util.OS;
34 import com.sun.enterprise.util.StringUtils;
35 import com.sun.enterprise.util.SystemPropertyConstants;
36
37 import java.util.logging.Logger JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.Properties JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.io.IOException JavaDoc;
43 import com.sun.logging.LogDomains;
44
45 //Pluggable Feature Factory
46
import com.sun.enterprise.admin.pluggable.ClientPluggableFeatureFactory;
47 import com.sun.enterprise.admin.pluggable.ClientPluggableFeatureFactoryImpl;
48
49 import com.sun.enterprise.admin.servermgmt.RepositoryConfig;
50 import com.sun.enterprise.admin.servermgmt.RepositoryManager;
51 import com.sun.enterprise.admin.servermgmt.DomainsManager;
52 import com.sun.enterprise.admin.servermgmt.DomainConfig;
53
54
55 /**
56  *Abstract base class for the Lifecycle commands
57  *
58  */

59 abstract public class BaseLifeCycleCommand extends S1ASCommand
60 {
61     private static final String JavaDoc DEFAULT_FEATURES_PROPERTY_CLASS =
62         "com.sun.enterprise.admin.pluggable.PEClientPluggableFeatureImpl";
63     /**
64     private static final String DEFAULT_FEATURES_PROPERTY_CLASS =
65         "com.sun.enterprise.admin.pluggable.EEClientPluggableFeatureImpl";
66     */

67     protected ClientPluggableFeatureFactory _featureFactory = null;
68    
69     protected static final String JavaDoc DOMAIN = "domain";
70     protected static final String JavaDoc DOMAINDIR = "domaindir";
71     protected static final String JavaDoc MASTER_PASSWORD = "masterpassword";
72     protected static final String JavaDoc NEW_MASTER_PASSWORD = "newmasterpassword";
73     protected static final String JavaDoc ADMIN_USER = "adminuser";
74     protected static final String JavaDoc ADMIN_PASSWORD = "adminpassword";
75     protected static final String JavaDoc PASSWORD = "password";
76     protected static final String JavaDoc DEFAULT_MASTER_PASSWORD = RepositoryManager.DEFAULT_MASTER_PASSWORD;
77     protected static final String JavaDoc ADMIN_PORT = "adminport";
78     protected static final String JavaDoc SAVE_MASTER_PASSWORD = "savemasterpassword";
79     
80     protected final static char ESCAPE_CHAR = '\\';
81     protected final static char EQUAL_SIGN = '=';
82     protected final static String JavaDoc DELIMITER = ":";
83
84     /** Creates new BaseLifeCycleCommand */
85     public BaseLifeCycleCommand()
86     {
87         _featureFactory = createPluggableFeatureFactory();
88     }
89
90     /**
91      * gets Logger from LogDomains
92      * @return Logger
93      */

94     protected Logger JavaDoc getLogger() {
95         return LogDomains.getLogger(LogDomains.CORE_LOGGER);
96     }
97
98     /** gets client pluggable feature factory
99      * @return ClientPluggableFeatureFactory
100      */

101     protected ClientPluggableFeatureFactory getFeatureFactory()
102     {
103         return _featureFactory;
104     }
105
106     /**
107      * Find and create a pluggable feature factory.
108      * @return ClientPluggableFeatureFactory
109      */

110     private ClientPluggableFeatureFactory createPluggableFeatureFactory() {
111     String JavaDoc featurePropClass = System.getProperty(
112                   ClientPluggableFeatureFactory.PLUGGABLE_FEATURES_PROPERTY_NAME,
113                   DEFAULT_FEATURES_PROPERTY_CLASS);
114         getLogger().log(Level.FINER, "featurePropClass: " + featurePropClass);
115         ClientPluggableFeatureFactoryImpl featureFactoryImpl =
116             new ClientPluggableFeatureFactoryImpl(getLogger());
117         ClientPluggableFeatureFactory featureFactory =
118         (ClientPluggableFeatureFactory)featureFactoryImpl.getInstance(featurePropClass);
119         if (featureFactory == null) {
120             getLogger().log(Level.WARNING,
121                 "j2eerunner.pluggable_feature_noinit", featurePropClass);
122         }
123         return featureFactory;
124     }
125
126
127     protected DomainConfig getDomainConfig(String JavaDoc domainName) throws CommandException
128     {
129         try {
130             DomainConfig dc = new DomainConfig(domainName, getDomainsRoot());
131
132         // add map entries for --verbose and --debug options to start-domain
133
if ( getBooleanOption("verbose") ) {
134         dc.put(DomainConfig.K_VERBOSE, Boolean.TRUE);
135         }
136         if ( getBooleanOption("debug") ) {
137         dc.put(DomainConfig.K_DEBUG, Boolean.TRUE);
138         }
139
140             return dc;
141
142         } catch (Exception JavaDoc e) {
143             throw new CommandException(e);
144         }
145     }
146     
147     protected Boolean JavaDoc getSaveMasterPassword(String JavaDoc masterPassword)
148     {
149         Boolean JavaDoc saveMasterPassword = new Boolean JavaDoc(getBooleanOption(SAVE_MASTER_PASSWORD));
150         if (masterPassword != null && masterPassword.equals(DEFAULT_MASTER_PASSWORD)) {
151             saveMasterPassword = Boolean.TRUE;
152         }
153         return saveMasterPassword;
154     }
155         
156     
157     protected String JavaDoc getDomainsRoot() throws CommandException
158     {
159         String JavaDoc domainDir = getOption(DOMAINDIR);
160         if (domainDir == null)
161         {
162             domainDir = System.getProperty(SystemPropertyConstants.DOMAINS_ROOT_PROPERTY);
163         }
164         if (domainDir == null)
165         {
166             throw new CommandException(getLocalizedString("InvalidDomainPath",
167                        new String JavaDoc[] {domainDir}) );
168         }
169         return domainDir;
170     }
171
172     protected String JavaDoc getDomainName() throws CommandException
173     {
174
175     String JavaDoc domainName = null;
176         if (operands.isEmpty())
177         {
178         // need to also support domain option for backward compatibility
179
if (getOption(DOMAIN)!=null)
180         domainName = getOption(DOMAIN);
181         else
182         {
183         final String JavaDoc[] domains = getDomains();
184         if (domains.length == 0)
185             throw new CommandException(getLocalizedString("NoDomains",
186                                   new Object JavaDoc[] {
187                                   getDomainsRoot()}));
188         else if (domains.length > 1)
189             throw new CommandException(getLocalizedString("NoDefaultDomain",
190                                   new Object JavaDoc[] {
191                                   getDomainsRoot()}));
192         else
193             domainName = domains[0]; //assign the only domain
194
}
195         }
196     else
197         domainName = (String JavaDoc)operands.firstElement();
198         CLILogger.getInstance().printDebugMessage("domainName = " + domainName);
199         return domainName;
200     }
201       
202        
203     /**
204      * this methods returns the admin user.
205      * first it checks if adminuser option is specified on command line.
206      * if not, then it'll try to get AS_ADMIN_ADMINUSER from .asadminprefs file.
207      * if that still does not exist then get AS_ADMIN_USER from ./asadminprefs file.
208      * if all else fails, an CommandValidationException is thrown.
209      * @return admin user.
210      * @throws CommandValidationException if could not get adminuser option
211      */

212     protected String JavaDoc getAdminUser() throws CommandValidationException
213     {
214         String JavaDoc adminUserVal = getOption(ADMIN_USER);
215         if (adminUserVal != null)
216             return adminUserVal;
217         else
218         {
219             adminUserVal = getValuesFromASADMINPREFS(ADMIN_USER);
220             if (adminUserVal != null)
221             {
222                 return adminUserVal;
223             }
224                 //if adminUserVal is still null then read AS_ADMIN_USER
225
//from .asadminprefs file
226
else
227             {
228                 adminUserVal = getValuesFromASADMINPREFS(USER);
229                 if (adminUserVal != null) return adminUserVal;
230                     //if all else fails, thrown and exception
231
else if (getBooleanOption(INTERACTIVE))
232                 {
233                     //prompt for user
234
try {
235                         InputsAndOutputs.getInstance().getUserOutput().print(
236                                         getLocalizedString("AdminUserPrompt"));
237                         return InputsAndOutputs.getInstance().getUserInput().getLine();
238                     }
239                     catch (IOException JavaDoc ioe)
240                     {
241                         throw new CommandValidationException(
242                                     getLocalizedString("CannotReadOption",
243                                                 new Object JavaDoc[]{"ADMIN_USER"}));
244                     }
245                 }
246                 else
247                     throw new CommandValidationException(getLocalizedString(
248                                                          "OptionIsRequired",
249                                                          new Object JavaDoc[] {ADMIN_USER}));
250             }
251         }
252     }
253     
254     /**
255      * this methods returns the master password and is used to get the master password
256      * for both create-domain and create-nodeagent commands. The password can be passed in
257      * on the command line (--masterpassword), environment variable (AS_ADMIN_MASTERPASSWORD),
258      * or in the password file (--passwordfile).
259      * first it checks if masterpassword option is specified on command line.
260      * if not, then it'll try to get AS_ADMIN_MASTERPASSWORD from the password file.
261      * if all else fails, then prompt the user for the password if interactive=true.
262      * @param confirmAndValidate - boolean whether to confirm and validate masterpassword
263      * @return admin password
264      * @throws CommandValidationException if could not get adminpassword option
265      */

266     protected String JavaDoc getMasterPassword(boolean confirmAndValidate)
267         throws CommandValidationException, CommandException
268     {
269            return getMasterPassword(confirmAndValidate, false);
270     }
271     
272
273     /**
274      * this methods returns the master password and is used to get the master password
275      * for both create-domain and create-nodeagent commands. The password can be passed in
276      * on the command line (--masterpassword), environment variable (AS_ADMIN_MASTERPASSWORD),
277      * or in the password file (--passwordfile).
278      * first it checks if masterpassword option is specified on command line.
279      * if not, then it'll try to get AS_ADMIN_MASTERPASSWORD from the password file.
280      * if all else fails, then prompt the user for the password if interactive=true.
281      * @param confirmAndValidate - boolean whether to confirm and validate masterpassword
282      * @param alwaysPrompt - boolean whether to always prompt for masterpassword reguardless of adminuser/password
283      * @return admin password
284      * @throws CommandValidationException if could not get adminpassword option
285      */

286     protected String JavaDoc getMasterPassword(boolean confirmAndValidate, boolean alwaysPrompt)
287         throws CommandValidationException, CommandException
288     {
289         //getPassword(optionName, allowedOnCommandLine, readPrefsFile, readPasswordOptionFromPrefs, readMasterPasswordFile, mgr, config,
290
//promptUser, confirm, validate)
291
String JavaDoc adminPassword = getPassword(ADMIN_PASSWORD, true, false, false, false, null, null,
292             false, false, false, false);
293         
294         if (adminPassword != null && !alwaysPrompt) {
295             //If we got here, then the admin password was found in the command line, password file,
296
//or environment variable
297
//getPassword(optionName, allowedOnCommandLine, readPrefsFile, readPasswordOptionFromPrefs, readMasterPasswordFile, mgr, config,
298
//promptUser, confirm, validate)
299
String JavaDoc masterPassword = getPassword(MASTER_PASSWORD, false, false, false, false, null, null,
300                 false, false, confirmAndValidate, false);
301             if (masterPassword == null) {
302                 //If we got here, then the master password was not found in the command line or
303
//password file, so we return the default master password rather than prompting.
304
return DEFAULT_MASTER_PASSWORD;
305             } else {
306                 return masterPassword;
307             }
308         }
309         //If the admin user was not provided on the command line (i.e. the user was prompted), then
310
//we can prompt for the master password.
311
//getPassword(optionName, allowedOnCommandLine, readPrefsFile, readPasswordOptionFromPrefs, readMasterPasswordFile, mgr, config,
312
//promptUser, confirm, validate)
313
return getPassword(MASTER_PASSWORD, "MasterPasswordPrompt", "MasterPasswordConfirmationPrompt",
314             false, false, false, false, null, null,
315             true, confirmAndValidate, confirmAndValidate, false);
316     }
317    
318     
319     
320     protected String JavaDoc getMasterPassword(RepositoryManager mgr, RepositoryConfig config)
321         throws CommandValidationException, CommandException
322     {
323         //getPassword(optionName, allowedOnCommandLine, readPrefsFile, readPasswordOptionFromPrefs, readMasterPasswordFile, config,
324
//promptUser, confirm, validate)
325
return getPassword(MASTER_PASSWORD, "MasterPasswordPrompt", null,
326             false, false, false, true, mgr, config,
327             true, false, false, false);
328     }
329     
330     protected String JavaDoc getNewMasterPassword()
331         throws CommandValidationException, CommandException
332     {
333         return getPassword(NEW_MASTER_PASSWORD,
334             "NewMasterPasswordPrompt", "NewMasterPasswordConfirmationPrompt",
335             false, false, false, false, null, null,
336             true, true, true, false);
337     }
338     
339     protected HashMap JavaDoc getExtraPasswords(String JavaDoc[] optionNames)
340         throws CommandValidationException, CommandException
341     {
342         HashMap JavaDoc result = new HashMap JavaDoc();
343         String JavaDoc password;
344         String JavaDoc optionName;
345         for (int i = 0; i < optionNames.length; i++) {
346             optionName = optionNames[i];
347             //Add the new option as a non-deprecated option, so a message will not be displayed.
348
NOT_DEPRECATED_PASSWORDFILE_OPTIONS += "|" + optionName;
349             //getPassword(optionName, allowedOnCommandLine, readPrefsFile, readPasswordOptionFromPrefs, readMasterPasswordFile, config,
350
//promptUser, confirm, validate)
351
password = getPassword(optionName, "ExtraPasswordPrompt", null, false, false, false, false, null, null,
352                 true, false, false, false);
353             result.put(optionName, password);
354         }
355         return result;
356     }
357    
358     protected String JavaDoc[] getDomains() throws CommandException
359     {
360         try
361         {
362             DomainsManager mgr = getFeatureFactory().getDomainsManager();
363             return mgr.listDomains(getDomainConfig(null));
364         }
365         catch(Exception JavaDoc e)
366         {
367             throw new CommandException(e.getLocalizedMessage());
368         }
369     }
370    
371     protected boolean isWindows()
372     {
373         final String JavaDoc osname = System.getProperty("os.name").toLowerCase();
374         CLILogger.getInstance().printDebugMessage("osname = " + osname);
375         return osname.indexOf("windows") != -1;
376     }
377    
378     protected boolean isSpaceInPath(String JavaDoc path)
379     {
380         return path.indexOf(' ') != -1;
381     }
382 }
383
384
Popular Tags