KickJava   Java API By Example, From Geeks To Geeks.

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


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.appserv.management.client.prefs.LoginInfo;
27 import com.sun.appserv.management.client.prefs.LoginInfoStore;
28 import com.sun.appserv.management.client.prefs.LoginInfoStoreFactory;
29 import com.sun.enterprise.cli.framework.*;
30
31 import java.io.IOException JavaDoc;
32 import javax.management.MBeanServerConnection JavaDoc;
33
34 public class LoginCommand extends S1ASCommand
35 {
36
37     /**
38      * A method that validates the options/operands
39      * @return boolean returns true if success else returns false
40      * @throws CommandValidationException
41      */

42     public boolean validateOptions() throws CommandValidationException
43     {
44         return super.validateOptions();
45     }
46
47     
48     /**
49      * Method that Executes the command
50      * @throws CommandException, CommandValidationException
51      */

52     public void runCommand() throws CommandException, CommandValidationException
53     {
54         validateOptions();
55         String JavaDoc userName = getUser();
56         String JavaDoc password = getPassword();
57         final String JavaDoc host = getHost();
58         final int port = getPort();
59         authenticate(host, port, userName, password);
60         saveLogin(host, port, userName, password);
61     }
62     
63     protected String JavaDoc getUser() throws CommandValidationException
64     {
65         try {
66             InputsAndOutputs.getInstance().getUserOutput().print(
67                                         getLocalizedString("AdminUserPrompt"));
68             return InputsAndOutputs.getInstance().getUserInput().getLine();
69         }
70         catch (IOException JavaDoc ioe)
71         {
72             throw new CommandValidationException(getLocalizedString("CannotReadOption",
73                                                     new Object JavaDoc[]{"user"}));
74         }
75     }
76
77
78     protected String JavaDoc getPassword() throws CommandValidationException
79     {
80         String JavaDoc passwordValue;
81         
82         try
83         {
84             InputsAndOutputs.getInstance().getUserOutput().print(
85                                 getLocalizedString("AdminPasswordPrompt"));
86             passwordValue = new CliUtil().getPassword();
87         }
88         catch (java.lang.NoClassDefFoundError JavaDoc e)
89         {
90             passwordValue = readInput();
91         }
92         catch (java.lang.UnsatisfiedLinkError JavaDoc e)
93         {
94             passwordValue = readInput();
95         }
96         catch (Exception JavaDoc e)
97         {
98             throw new CommandValidationException(e);
99         }
100         return passwordValue;
101     }
102     
103     
104     private void authenticate(final String JavaDoc host, final int port, final String JavaDoc user, final String JavaDoc password)
105     throws CommandException, CommandValidationException {
106         try {
107             final Object JavaDoc[] params = new Object JavaDoc[] {host, ""+port};
108             CLILogger.getInstance().printMessage(getLocalizedString("AuthenticatingMsg", params));
109             final MBeanServerConnection JavaDoc mbsc = super.getMBeanServerConnection(host, port, user, password);
110             final String JavaDoc dd = mbsc.getDefaultDomain(); //calls a dummy method to make sure authentication is OK
111
final String JavaDoc msg = "Authentication succeeded to: " + host + "and port: " + port;
112             CLILogger.getInstance().printDebugMessage(msg);
113         }
114         catch(final IOException JavaDoc ioe) {
115             throw new CommandException(ioe);
116         }
117     }
118     
119     
120     private void saveLogin(final String JavaDoc host, final int port, final String JavaDoc user, final String JavaDoc password)
121     throws CommandException {
122         try {
123             final LoginInfoStore store = LoginInfoStoreFactory.getStore(null);
124             final LoginInfo login = new LoginInfo(host, port, user, password);
125             String JavaDoc msg = null;
126             final boolean storeIt = handleExists(store, login);
127             if (storeIt) {
128                 store.store(login, true);
129                 final Object JavaDoc[] params = new Object JavaDoc[] {login.getUser(), login.getHost(), ""+login.getPort(), store.getName()};
130                 msg = getLocalizedString("LoginInfoStored", params);
131             }
132             else {
133                 final Object JavaDoc[] params = new Object JavaDoc[] {login.getHost(), ""+login.getPort()};
134                 msg = getLocalizedString("LoginInfoNotStored", params);
135             }
136             CLILogger.getInstance().printMessage(msg);
137         }
138         catch(final Exception JavaDoc e) {
139             throw new CommandException(e);
140         }
141     }
142     
143     
144     private boolean handleExists(final LoginInfoStore store, final LoginInfo login) throws Exception JavaDoc {
145         boolean storeIt = true;
146         if (store.exists(login.getHost(), login.getPort())) {
147             storeIt = promptOnce(login);
148         }
149         return ( storeIt );
150     }
151     
152     
153     private boolean promptOnce(final LoginInfo login) throws Exception JavaDoc {
154         boolean userPressedYes = false;
155         final String JavaDoc YES = "y";
156         final Object JavaDoc[] loginId = new Object JavaDoc[] {login.getHost(), ""+login.getPort()};
157         final String JavaDoc prompt = getLocalizedString("ShouldOverwriteLogin", loginId);
158         final InputsAndOutputs io = InputsAndOutputs.getInstance();
159         io.getUserOutput().print(prompt);
160         final String JavaDoc in = io.getUserInput().getLine();
161         userPressedYes = YES.equalsIgnoreCase(in);
162         return ( userPressedYes );
163     }
164 }
Popular Tags