KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.*;
22 import java.io.Console JavaDoc;
23
24 /**
25  * A simple key manager which enables users to enter passwords as keys using
26  * console I/O.
27  * This key manager is used by default if the JVM is running in headless mode
28  * and the API complies to JSE6 (i.e. the class <code>java.io.Console</code>
29  * is available)!
30  * To request it explicitly, set the system property
31  * <code>de.schlichtherle.key.KeyManager</code> to
32  * <code>de.schlichtherle.key.passwd.console.PromptingKeyManager</code>.
33  * <p>
34  * This key manager does not support key files and disables prompting if no
35  * console is attached to the JVM.
36  * <p>
37  * Note that class loading and instantiation may happen in a JVM shutdown hook,
38  * so class initializers and constructors must behave accordingly.
39  * In particular, it's not permitted to construct or use a Swing GUI there.
40  * <p>
41  * This class is thread safe.
42  *
43  * @author Christian Schlichtherle
44  * @since TrueZIP 6.4
45  * @version @version@
46  */

47 public class PromptingKeyManager extends de.schlichtherle.key.PromptingKeyManager {
48
49     /**
50      * Constructs a new <code>PromptingKeyManager</code>.
51      * This instance maps the following key provider UI types using
52      * {@link de.schlichtherle.key.PromptingKeyManager#mapPromptingKeyProviderUIType}:
53      * <table border="2" cellpadding="4">
54      * <tr>
55      * <th>uiClassID</th>
56      * <th>uiClass</th>
57      * </tr>
58      * <tr>
59      * <td>"PromptingKeyProvider"</td>
60      * <td>PromptingKeyProviderUI.class</td>
61      * </tr>
62      * <tr>
63      * <td>"PromptingAesKeyProvider"</td>
64      * <td>PromptingAesKeyProviderUI.class</td>
65      * </tr>
66      * </table>
67      */

68     public PromptingKeyManager() {
69         mapPromptingKeyProviderUIType(
70                 "PromptingKeyProvider",
71                 PromptingKeyProviderUI.class);
72         mapPromptingKeyProviderUIType(
73                 "PromptingAesKeyProvider",
74                 PromptingAesKeyProviderUI.class);
75     }
76
77     //
78
// Instance stuff:
79
//
80

81     protected boolean isPromptingImpl() {
82         return super.isPromptingImpl() && System.console() != null;
83     }
84
85     /**
86      * If no console is attached to this JVM, then this method throws a
87      * {@link KeyPromptingDisabledException}.
88      */

89     protected void ensurePromptingImpl()
90     throws KeyPromptingDisabledException {
91         if (System.console() == null)
92             throw new KeyPromptingDisabledException();
93         super.ensurePromptingImpl();
94     }
95 }
96
Popular Tags