KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > key > passwd > console > PromptingKeyProviderUI


1 /*
2  * Copyright 2006 Schlichtherle IT Services
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package de.schlichtherle.key.passwd.console;
18
19 import de.schlichtherle.key.*;
20
21 import java.io.*;
22 import java.util.*;
23
24 /**
25  * A out I/O based user interface to prompt for passwords.
26  * This class is thread safe.
27  *
28  * @author Christian Schlichtherle
29  * @version @version@
30  * @since TrueZIP 6.4
31  */

32 public class PromptingKeyProviderUI
33         implements de.schlichtherle.key.PromptingKeyProviderUI {
34
35     private static final String JavaDoc CLASS_NAME
36             = "de/schlichtherle/key/passwd/console/PromptingKeyProviderUI".replace('/', '.'); // support code obfuscation!
37
protected static final ResourceBundle resources
38             = ResourceBundle.getBundle(CLASS_NAME);
39
40     /**
41      * The console to use for I/O.
42      * If <code>null</code>, the prompt methods are never called, so it's
43      * safe to assume that it's not <code>null</code> in these methods.
44      */

45     protected static final Console con = System.console();
46
47     /**
48      * Used to lock out prompting by multiple threads.
49      */

50     private static final PromptingLock lock = new PromptingLock();
51
52     /** The minimum acceptable lenght of a password. */
53     private static final int MIN_PASSWD_LEN = 6;
54
55     /**
56      * The last resource ID used when prompting.
57      * Initialized to the empty string.
58      */

59     private static String JavaDoc lastResourceID = "";
60
61     public final void promptCreateKey(final PromptingKeyProvider provider) {
62         synchronized (lock) {
63             final String JavaDoc resourceID = provider.getResourceID();
64             if (!resourceID.equals(lastResourceID))
65                 printf(resources.getString("createKey.banner"),
66                         provider.getResourceID());
67             lastResourceID = resourceID;
68
69             while (true) {
70                 char[] newPasswd1 = readPassword(
71                         resources.getString("createKey.newPasswd1"));
72                 if (newPasswd1 == null || newPasswd1.length <= 0)
73                     return;
74
75                 char[] newPasswd2 = readPassword(
76                         resources.getString("createKey.newPasswd2"));
77                 if (newPasswd2 == null)
78                     return;
79
80                 if (!Arrays.equals(newPasswd1, newPasswd2)) {
81                     printf(resources.getString("createKey.passwd.noMatch"));
82                     continue;
83                 }
84
85                 if (newPasswd1.length < MIN_PASSWD_LEN) {
86                     printf(resources.getString("createKey.passwd.tooShort"));
87                     continue;
88                 }
89
90                 provider.setKey(newPasswd1);
91                 break;
92             }
93
94             promptExtraData(provider);
95         }
96     }
97
98     protected void promptExtraData(PromptingKeyProvider provider)
99     {
100     }
101
102     public final boolean promptUnknownOpenKey(PromptingKeyProvider provider) {
103         synchronized (lock) {
104             return promptOpenKey(provider, false);
105         }
106     }
107
108     public final boolean promptInvalidOpenKey(PromptingKeyProvider provider) {
109         synchronized (lock) {
110             return promptOpenKey(provider, true);
111         }
112     }
113
114     private boolean promptOpenKey(final PromptingKeyProvider provider, final boolean invalid) {
115         if (invalid)
116             printf(resources.getString("openKey.invalid"));
117
118         final String JavaDoc resourceID = provider.getResourceID();
119         if (!resourceID.equals(lastResourceID))
120             printf(resources.getString("openKey.banner"),
121                     provider.getResourceID());
122         lastResourceID = resourceID;
123
124         char[] passwd = readPassword(resources.getString("openKey.passwd"));
125         if (passwd == null || passwd.length <= 0) {
126             provider.setKey(null);
127             return false;
128         }
129
130         provider.setKey(passwd);
131
132         while (true) {
133             String JavaDoc changeKey = readLine(resources.getString("openKey.change"));
134             if (changeKey == null)
135                 return false;
136             changeKey = changeKey.toLowerCase();
137             if (changeKey.length() <= 0 || changeKey.equals(resources.getString("no")))
138                 return false;
139             else if (changeKey.equals(resources.getString("yes")))
140                 return true;
141         }
142     }
143
144     //
145
// TrueZIP 6.4 is still Java source level 1.4, so we need these helpers
146
// as a substitute for the varargs calls.
147
//
148

149     protected final Console printf(String JavaDoc format) {
150         return con.printf(format, null);
151     }
152
153     protected final Console printf(String JavaDoc format, Object JavaDoc arg) {
154         return con.printf(format, new Object JavaDoc[] { arg });
155     }
156
157     protected final String JavaDoc readLine(String JavaDoc format) {
158         return con.readLine(format, null);
159     }
160
161     protected final String JavaDoc readLine(String JavaDoc format, Object JavaDoc arg) {
162         return con.readLine(format, new Object JavaDoc[] { arg });
163     }
164
165     protected final char[] readPassword(String JavaDoc format) {
166         return con.readPassword(format, null);
167     }
168
169     //
170
// Miscellaneous.
171
//
172

173     private static class PromptingLock { }
174 }
175
Popular Tags