KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > key > passwd > swing > KeyMgmtLifeCycle


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.swing;
18
19 import de.schlichtherle.key.*;
20
21 import java.util.*;
22 import java.util.logging.*;
23
24 /**
25  * Simulates the typical life cycle of a protected resource and its
26  * associated key using the API in the package {@link de.schlichtherle.key}.
27  * <p>
28  * Note that this Runnable simulates the regular operation of a client
29  * application of the key manager package.
30  * It does <em>not</em> throw any <code>AssertionError</code>s.
31  *
32  * @author Christian Schlichtherle
33  * @version @version@
34  * @since TrueZIP 6.1
35  */

36 public class KeyMgmtLifeCycle implements Runnable JavaDoc {
37
38     private static final Logger logger
39             = Logger.getLogger(KeyMgmtLifeCycle.class.getName());
40
41     /** The identifier of the protected resource. */
42     public final String JavaDoc id;
43
44     /** The reference key for the resource. */
45     private Object JavaDoc refKey;
46
47     /**
48      * Contains non-<code>null</code> if and only if <code>run()</code> has
49      * terminated because the user cancelled the key prompting.
50      */

51     public Throwable JavaDoc throwable;
52
53     /**
54      * @param id The identifier of the protected resource.
55      */

56     public KeyMgmtLifeCycle(final String JavaDoc id) {
57         this.id = id;
58     }
59
60     public void run() {
61         try {
62             runIt();
63         } catch (Throwable JavaDoc t) {
64             throwable = t;
65         }
66     }
67
68     private void runIt() throws UnknownKeyException {
69         // Prompt for a key to create the resource.
70
createResource();
71
72         // Forget the key stored in the key manager.
73
KeyManager.resetKeyProvider(id);
74
75         // Prompt for the key again to open the resource.
76
openResource();
77
78         // Overwrite the resource, eventually prompting for a new
79
// key if the user has requested so during prompting for the
80
// key in openResource().
81
createResource();
82
83         // Use last entered key to open the resource yet again.
84
openResource();
85     }
86
87     private void createResource() throws UnknownKeyException {
88         final KeyManager manager = KeyManager.getInstance();
89         final KeyProvider provider = getKeyProvider(manager, id);
90
91         // Store the key, so we can later check the key stored in the
92
// key manager when opening the resource.
93
refKey = provider.getCreateKey();
94         String JavaDoc msg = id + ": getCreateKey() returned " + toString(refKey) + ".";
95         logger.fine(msg);
96
97         createResourceHook(provider);
98     }
99
100     protected KeyProvider getKeyProvider(KeyManager manager, String JavaDoc id) {
101         return manager.getKeyProvider(id);
102     }
103
104     private void openResource() throws UnknownKeyException {
105         final KeyManager manager = KeyManager.getInstance();
106         final KeyProvider provider = getKeyProvider(manager, id);
107
108         boolean correct = false;
109         while (!correct) {
110             correct = authenticateKey(provider);
111         }
112
113         openResourceHook(provider);
114     }
115
116     private boolean authenticateKey(final KeyProvider provider) throws UnknownKeyException {
117         final Object JavaDoc providedKey = provider.getOpenKey();
118         final boolean correct = equals(refKey, providedKey);
119         String JavaDoc msg = id + ": getOpenKey() returned " + toString(providedKey) + ". ";
120         // Nullifying the clone returned by getOpenKey() must not
121
// affect the key manager.
122
nullify(providedKey);
123         if (correct) {
124             msg += "That's correct.";
125             logger.fine(msg);
126         } else {
127             provider.invalidOpenKey();
128             msg += "That's wrong!";
129             logger.fine(msg);
130             try {
131                 Thread.sleep(3000);
132             } catch (InterruptedException JavaDoc ex) {
133                 ex.printStackTrace();
134             }
135         }
136
137         return correct;
138     }
139
140     private static boolean equals(final Object JavaDoc o1, final Object JavaDoc o2) {
141         if (o1.getClass() != o2.getClass())
142             return false;
143
144         if (o1 instanceof char[])
145             return Arrays.equals((char[]) o1, (char[]) o2);
146         else if (o1 instanceof byte[])
147                 return Arrays.equals((byte[]) o1, (byte[]) o2);
148             else
149                 return o1.equals(o2);
150     }
151
152     private static String JavaDoc toString(final Object JavaDoc o) {
153         if (o instanceof char[])
154             return "\"" + new String JavaDoc((char[]) o) + "\".toCharArray()";
155         else if (o instanceof byte[])
156                 return "\"" + new String JavaDoc((byte[]) o) + "\".getBytes(\"UTF16BE\")";
157             else
158                 return "\"" + o.toString() + "\".toString()";
159     }
160
161     private static void nullify(final Object JavaDoc o) {
162         if (o instanceof char[])
163             Arrays.fill((char[]) o, (char) 0);
164         else if (o instanceof byte[])
165                 Arrays.fill((byte[]) o, (byte) 0);
166     }
167
168     protected void createResourceHook(KeyProvider provider) {
169     }
170
171     protected void openResourceHook(KeyProvider provider) {
172     }
173 }
Popular Tags