KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > security > TextLoginDialog


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 package com.sun.enterprise.security;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import javax.security.auth.callback.*;
29 import com.sun.enterprise.util.LocalStringManagerImpl;
30 import java.util.logging.*;
31 import com.sun.logging.*;
32
33
34 /**
35  * This implementation of LoginDialog, first looks at the environment
36  * variables "login.username" and "login.password". If these are set it
37  * uses these for username & password respectively.
38  * If these are not set, then it queries the user in the command window.
39  *
40  * @author Harish Prabandham
41  */

42 public final class TextLoginDialog implements LoginDialog {
43
44     private static Logger _logger=null;
45     static {
46         _logger=LogDomains.getLogger(LogDomains.SECURITY_LOGGER);
47     }
48
49     private String JavaDoc username = null;
50     private String JavaDoc password = null;
51     private static LocalStringManagerImpl localStrings =
52     new LocalStringManagerImpl(TextLoginDialog.class);
53
54     public TextLoginDialog() {
55     if((username == null) || (password == null)) {
56         BufferedReader JavaDoc d =
57         new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
58         do {
59         System.out.print
60             (localStrings.getLocalString
61              ("enterprise.security.login.username",
62               "User Name: "));
63         try {
64             username = d.readLine();
65         } catch(IOException JavaDoc e) {
66         }
67         } while( (username == null) ||
68              (username.trim().length() == 0));
69         
70         do {
71         System.out.print
72             (localStrings.getLocalString
73              ("enterprise.security.login.password",
74               "Password: "));
75         try {
76             password = d.readLine();
77         }catch (IOException JavaDoc e) {
78         }
79         }while( (password == null) || (password.trim().length() == 0));
80     }
81     }
82
83     public TextLoginDialog(Callback[] callbacks)
84     {
85     try {
86         for(int i = 0; i < callbacks.length; i++) {
87         if(callbacks[i] instanceof NameCallback) {
88             NameCallback nc = (NameCallback) callbacks[i];
89             System.err.print(nc.getPrompt());
90             System.err.flush();
91             nc.setName((new BufferedReader JavaDoc
92                 (new
93                  InputStreamReader JavaDoc(System.in))).readLine());
94             
95         } else if(callbacks[i] instanceof PasswordCallback) {
96             PasswordCallback pc = (PasswordCallback) callbacks[i];
97             System.err.print(pc.getPrompt());
98             System.err.flush();
99             
100             String JavaDoc psswd =
101             new BufferedReader JavaDoc
102             (new InputStreamReader JavaDoc(System.in)).readLine();
103             pc.setPassword(psswd.toCharArray());
104             
105         } else if(callbacks[i] instanceof ChoiceCallback) {
106             ChoiceCallback cc = (ChoiceCallback) callbacks[i];
107             /* Get the keystore password to see if the user is
108              * authorized to see the list of certificates
109              */

110             String JavaDoc lbl = (localStrings.getLocalString
111                   ("enterprise.security.keystore",
112                    "Enter the KeyStore Password "));
113             String JavaDoc keystorePass = SSLUtils.getKeyStorePass ();
114             System.out.println (lbl+
115                     " : (max 3 tries)");
116             int cnt=0;
117             for (cnt=0; cnt<3; cnt++){
118             // Let the user try putting password thrice
119
System.out.println (lbl+" : ");
120             String JavaDoc kp =
121                 (new BufferedReader JavaDoc
122                  (new InputStreamReader JavaDoc(System.in))).readLine();
123             if (kp.equals (keystorePass)) {
124                 break;
125             } else{
126                 String JavaDoc errmessage = localStrings.getLocalString("enterprise.security.IncorrectKeystorePassword","Incorrect Keystore Password");
127                 System.err.println (errmessage);
128             }
129             }
130             if (cnt>=3){
131             cc.setSelectedIndex (-1);
132             } else {
133             System.err.println(cc.getPrompt());
134             System.err.flush();
135             String JavaDoc[] choices = cc.getChoices();
136             for(int j = 0; j < choices.length; j++) {
137                 System.err.print("[" + j + "] ");
138                 System.err.println(choices[j]);
139             }
140             String JavaDoc line =
141                 (new BufferedReader JavaDoc
142                  (new InputStreamReader JavaDoc(System.in))).readLine();
143             
144             int sel = new Integer JavaDoc(line).intValue();
145             // System.out.println("SELECTED VAL:" + sel);
146
cc.setSelectedIndex(sel);
147             }
148         }
149         }
150     } catch(Exception JavaDoc e) {
151         _logger.log(Level.SEVERE,
152                         "java_security.name_password_entry_exception",e);
153     }
154
155     }
156
157   
158     /**
159      * @return The username of the user.
160      */

161     public String JavaDoc getUserName() {
162     return username;
163     }
164     /**
165      *@return The password of the user in plain text...
166      */

167     public String JavaDoc getPassword() {
168     return password;
169     }
170 }
171
Popular Tags