KickJava   Java API By Example, From Geeks To Geeks.

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


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.cli.commands;
25
26 import com.sun.enterprise.cli.framework.*;
27
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import java.io.IOException JavaDoc;
32
33 /**
34    This class gets called when create-file-user command is invoked.
35    This class will overwrite validOptions in GenericCommand to validate
36    the userpassword option. If the userpassword and passwordfile options
37    are not entered in the command line and interactive is true, then
38    CLI will prompt the user for the password.
39  */

40 public class FileUserCommand extends S1ASCommand
41 {
42     private static final String JavaDoc USER_PASSWORD = "userpassword";
43     private static final String JavaDoc FILE_USER_NAME = "fileuser";
44     
45     protected String JavaDoc getPasswordOptionName() {
46         return USER_PASSWORD;
47     }
48     
49     
50     protected String JavaDoc getPasswordPrompt()
51     {
52         return "FileUserPasswordPrompt";
53     }
54     
55
56     protected String JavaDoc getPasswordConfirmationPrompt()
57     {
58         return "FileUserPasswordConfirmationPrompt";
59     }
60     
61     
62     protected boolean confirmPassword() {
63         return true;
64     }
65
66         /*
67     protected String getPasswordOption() throws CommandValidationException
68     {
69         String password;
70         if (getOption(getPasswordOptionName()) != null)
71             password = getOption(getPasswordOptionName());
72         else //prompt for fileuserPassword
73         {
74             password = getInteractiveOptionWithConfirmation(
75                             getPasswordOptionName(),
76                             getLocalizedString(getPasswordPrompt()),
77                             getLocalizedString(getPasswordConfirmationPrompt()),
78                             false);
79         }
80         return password;
81     }
82         */

83     
84     protected String JavaDoc getUserOptionName() {
85         return FILE_USER_NAME;
86     }
87     
88     
89     protected String JavaDoc getUserPrompt()
90     {
91         return "FileUserPrompt";
92     }
93     
94     
95     /**
96      * this methods returns the user/alias password.
97      * @return user/alias password
98      * @throws CommandValidationException if could not get userpassword or
99      * aliaspassword option
100      */

101     protected String JavaDoc getPasswordOption()
102         throws CommandValidationException, CommandException
103     {
104         //getPassword(optionName, allowedOnCommandLine, readPrefsFile, readPasswordOptionFromPrefs, readMasterPasswordFile, mgr, config,
105
//promptUser, confirm, validate)
106
return getPassword(getPasswordOptionName(), getPasswordPrompt(),
107                             getPasswordConfirmationPrompt(),
108                             true, false, false, false, null, null,
109                             true, confirmPassword(), false, true);
110     }
111
112     
113     private String JavaDoc getUserName() throws CommandValidationException
114     {
115         String JavaDoc userName;
116         if ((getOperands() != null) && (getOperands().size() != 0))
117             userName = (String JavaDoc) getOperands().get(0);
118         else //prompt for fileuser
119
{
120             try {
121                 InputsAndOutputs.getInstance().getUserOutput().print(
122                                     getLocalizedString(this.getUserPrompt()));
123                 userName = InputsAndOutputs.getInstance().getUserInput().getLine();
124             }
125             catch (IOException JavaDoc ioe)
126             {
127                 throw new CommandValidationException(
128                             getLocalizedString("CannotReadOption",
129                                         new Object JavaDoc[]{getUserOptionName()}));
130             }
131         }
132         
133         return userName;
134     }
135     
136     
137     /**
138        Validate the userpassword option.
139      */

140     public boolean validateOptions() throws CommandValidationException
141     {
142         return super.validateOptions();
143     }
144     
145     public void runCommand() throws CommandException, CommandValidationException
146     {
147         validateOptions();
148         //use http connector
149
MBeanServerConnection JavaDoc mbsc = getMBeanServerConnection(getHost(), getPort(),
150                                                               getUser(), getPassword());
151         final String JavaDoc objectName = getObjectName();
152         final Object JavaDoc[] params = getParamsInfo();
153         final String JavaDoc operationName = getOperationName();
154         final String JavaDoc[] types = getTypesInfo();
155
156         //get fileuser and set it in the params
157
params[0] = getUserName();
158         //get fileuser password and set it in the params
159
params[1] = getPasswordOption();
160         try
161         {
162         //if (System.getProperty("Debug") != null) printDebug(mbsc, objectName);
163
Object JavaDoc returnValue = mbsc.invoke(new ObjectName JavaDoc(objectName),
164                          operationName, params, types);
165             handleReturnValue(returnValue);
166         CLILogger.getInstance().printDetailMessage(getLocalizedString(
167                                "CommandSuccessful",
168                                new Object JavaDoc[] {name}));
169         }
170         catch(Exception JavaDoc e)
171         {
172             displayExceptionMessage(e);
173         }
174     }
175 }
176
Popular Tags