Lines 45-65
Link Here
|
45 |
import java.util.Arrays; |
45 |
import java.util.Arrays; |
46 |
import java.util.HashMap; |
46 |
import java.util.HashMap; |
47 |
import java.util.Map; |
47 |
import java.util.Map; |
|
|
48 |
import java.util.concurrent.Callable; |
49 |
import java.util.concurrent.ExecutionException; |
50 |
import java.util.concurrent.Future; |
51 |
import java.util.concurrent.TimeUnit; |
52 |
import java.util.concurrent.TimeoutException; |
48 |
import java.util.logging.Level; |
53 |
import java.util.logging.Level; |
49 |
import java.util.logging.Logger; |
54 |
import java.util.logging.Logger; |
|
|
55 |
import javax.swing.SwingUtilities; |
56 |
import org.netbeans.api.annotations.common.CheckForNull; |
57 |
import org.netbeans.api.annotations.common.NonNull; |
58 |
import org.netbeans.api.annotations.common.NullAllowed; |
59 |
import org.netbeans.api.progress.ProgressHandle; |
60 |
import org.netbeans.api.progress.ProgressUtils; |
50 |
import org.netbeans.spi.keyring.KeyringProvider; |
61 |
import org.netbeans.spi.keyring.KeyringProvider; |
|
|
62 |
import org.openide.util.Cancellable; |
51 |
import org.openide.util.Lookup; |
63 |
import org.openide.util.Lookup; |
|
|
64 |
import org.openide.util.NbBundle; |
52 |
import org.openide.util.Parameters; |
65 |
import org.openide.util.Parameters; |
|
|
66 |
import org.openide.util.RequestProcessor; |
53 |
|
67 |
|
54 |
/** |
68 |
/** |
55 |
* Client class for working with stored keys (such as passwords). |
69 |
* Client class for working with stored keys (such as passwords). |
56 |
* <p>The key identifier should be unique for the whole application, |
70 |
* <p>The key identifier should be unique for the whole application, |
57 |
* so qualify it with any prefixes as needed. |
71 |
* so qualify it with any prefixes as needed. |
58 |
* <p>Avoid calling methods on this class from the event dispatch thread, |
72 |
* <p> <i>Since 1.10</i> it is allowed to call methods of this class from even |
59 |
* as some provider implementations may need to block while displaying a dialog |
73 |
* dispatch thread. |
60 |
* (e.g. prompting for a master password to access the keyring). |
|
|
61 |
*/ |
74 |
*/ |
62 |
public class Keyring { |
75 |
public final class Keyring { |
|
|
76 |
|
77 |
// throughput 1 is intentional |
78 |
private static final RequestProcessor KEYRING_ACCESS = new RequestProcessor(Keyring.class); |
79 |
|
80 |
private static final long SAFE_DELAY = 70; |
63 |
|
81 |
|
64 |
private Keyring() {} |
82 |
private Keyring() {} |
65 |
|
83 |
|
Lines 82-124
Link Here
|
82 |
return PROVIDER; |
100 |
return PROVIDER; |
83 |
} |
101 |
} |
84 |
|
102 |
|
|
|
103 |
private static synchronized char[] readImpl(String key) { |
104 |
LOG.log(Level.FINEST, "reading: {0}", key); |
105 |
return provider().read(key); |
106 |
} |
107 |
|
85 |
/** |
108 |
/** |
86 |
* Reads a key from the ring. |
109 |
* Reads a key from the ring. |
|
|
110 |
* <p> |
111 |
* This method can be called from any thread. |
112 |
* All the changes done by previous calls to {@link #delete(java.lang.String)} |
113 |
* or {@link #save(java.lang.String, char[], java.lang.String)} methods |
114 |
* are guaranteed to be visible by subsequent calls to this method. |
115 |
* |
87 |
* @param key the identifier of the key |
116 |
* @param key the identifier of the key |
88 |
* @return its value if found (you may null out its elements), else null if not present |
117 |
* @return its value if found (you may null out its elements), else null if not present |
89 |
*/ |
118 |
*/ |
90 |
public static synchronized char[] read(String key) { |
119 |
@NbBundle.Messages("MSG_KeyringAccess=Requesting keyring access") |
|
|
120 |
@CheckForNull |
121 |
public static char[] read(@NonNull final String key) { |
91 |
Parameters.notNull("key", key); |
122 |
Parameters.notNull("key", key); |
92 |
LOG.log(Level.FINEST, "reading: {0}", key); |
123 |
|
93 |
return provider().read(key); |
124 |
try { |
|
|
125 |
final Future<char[]> result = KEYRING_ACCESS.submit(new Callable<char[]>() { |
126 |
@Override |
127 |
public char[] call() throws Exception { |
128 |
return Keyring.readImpl(key); |
129 |
} |
130 |
}); |
131 |
|
132 |
if (SwingUtilities.isEventDispatchThread()) { |
133 |
if (!result.isDone()) { |
134 |
try { |
135 |
// lets wait in awt to avoid flashing dialogs |
136 |
return result.get(SAFE_DELAY, TimeUnit.MILLISECONDS); |
137 |
} catch (TimeoutException ex) { |
138 |
// show progress dialog |
139 |
return ProgressUtils.showProgressDialogAndRun( |
140 |
new ProgressRunnable<char[]>(result), Bundle.MSG_KeyringAccess(), false); |
141 |
} |
142 |
} |
143 |
} |
144 |
return result.get(); |
145 |
} catch (InterruptedException ex) { |
146 |
Thread.currentThread().interrupt(); |
147 |
} catch (ExecutionException ex) { |
148 |
LOG.log(Level.INFO, null, ex); |
149 |
} |
150 |
return null; |
151 |
} |
152 |
|
153 |
private static synchronized void saveImpl(String key, char[] password, String description) { |
154 |
LOG.log(Level.FINEST, "saving: {0}", key); |
155 |
provider().save(key, password, description); |
156 |
Arrays.fill(password, (char) 0); |
94 |
} |
157 |
} |
95 |
|
158 |
|
96 |
/** |
159 |
/** |
97 |
* Saves a key to the ring. |
160 |
* Saves a key to the ring. |
98 |
* If it could not be saved, does nothing. |
161 |
* If it could not be saved, does nothing. |
99 |
* If the key already existed, overwrites the password. |
162 |
* If the key already existed, overwrites the password. |
|
|
163 |
* <p> |
164 |
* This method can be called from any thread. |
165 |
* The changes done by multiple calls to {@link #delete(java.lang.String)} |
166 |
* or {@link #save(java.lang.String, char[], java.lang.String)} methods |
167 |
* are guaranteed to be processed in order in which they were called. |
168 |
* |
100 |
* @param key a key identifier |
169 |
* @param key a key identifier |
101 |
* @param password the password or other sensitive information associated with the key |
170 |
* @param password the password or other sensitive information associated with the key |
102 |
* (its contents will be nulled out by end of call) |
171 |
* (its contents will be nulled out by end of call) |
103 |
* @param description a user-visible description of the key (may be null) |
172 |
* @param description a user-visible description of the key (may be null) |
104 |
*/ |
173 |
*/ |
105 |
public static synchronized void save(String key, char[] password, String description) { |
174 |
public static void save(@NonNull final String key, @NonNull final char[] password, |
|
|
175 |
@NullAllowed final String description) { |
176 |
|
106 |
Parameters.notNull("key", key); |
177 |
Parameters.notNull("key", key); |
107 |
Parameters.notNull("password", password); |
178 |
Parameters.notNull("password", password); |
108 |
LOG.log(Level.FINEST, "saving: {0}", key); |
179 |
|
109 |
provider().save(key, password, description); |
180 |
KEYRING_ACCESS.post(new Runnable() { |
110 |
Arrays.fill(password, (char) 0); |
181 |
|
|
|
182 |
@Override |
183 |
public void run() { |
184 |
Keyring.saveImpl(key, password, description); |
185 |
} |
186 |
}); |
187 |
} |
188 |
|
189 |
private static synchronized void deleteImpl(String key) { |
190 |
LOG.log(Level.FINEST, "deleting: {0}", key); |
191 |
provider().delete(key); |
111 |
} |
192 |
} |
112 |
|
193 |
|
113 |
/** |
194 |
/** |
114 |
* Deletes a key from the ring. |
195 |
* Deletes a key from the ring. |
115 |
* If the key was not in the ring to begin with, does nothing. |
196 |
* If the key was not in the ring to begin with, does nothing. |
|
|
197 |
* <p> |
198 |
* This method can be called from any thread. |
199 |
* The changes done by multiple calls to {@link #delete(java.lang.String)} |
200 |
* or {@link #save(java.lang.String, char[], java.lang.String)} methods |
201 |
* are guaranteed to be processed in order in which they were called. |
202 |
* |
116 |
* @param key a key identifier |
203 |
* @param key a key identifier |
117 |
*/ |
204 |
*/ |
118 |
public static synchronized void delete(String key) { |
205 |
public static void delete(@NonNull final String key) { |
119 |
Parameters.notNull("key", key); |
206 |
Parameters.notNull("key", key); |
120 |
LOG.log(Level.FINEST, "deleting: {0}", key); |
207 |
|
121 |
provider().delete(key); |
208 |
KEYRING_ACCESS.post(new Runnable() { |
|
|
209 |
|
210 |
@Override |
211 |
public void run() { |
212 |
Keyring.deleteImpl(key); |
213 |
} |
214 |
}); |
122 |
} |
215 |
} |
123 |
|
216 |
|
124 |
private static class DummyKeyringProvider implements KeyringProvider { |
217 |
private static class DummyKeyringProvider implements KeyringProvider { |
Lines 156-159
Link Here
|
156 |
return result; |
249 |
return result; |
157 |
} |
250 |
} |
158 |
|
251 |
|
|
|
252 |
private static class ProgressRunnable<T> implements org.netbeans.api.progress.ProgressRunnable<T>, Cancellable { |
253 |
|
254 |
private final Future<? extends T> task; |
255 |
|
256 |
public ProgressRunnable(Future<? extends T> task) { |
257 |
this.task = task; |
258 |
} |
259 |
|
260 |
@Override |
261 |
public T run(ProgressHandle handle) { |
262 |
try { |
263 |
return task.get(); |
264 |
} catch (InterruptedException ex) { |
265 |
Thread.currentThread().interrupt(); |
266 |
} catch (ExecutionException ex) { |
267 |
LOG.log(Level.INFO, null, ex); |
268 |
} |
269 |
return null; |
270 |
} |
271 |
|
272 |
@Override |
273 |
public boolean cancel() { |
274 |
return task.cancel(true); |
275 |
} |
276 |
} |
159 |
} |
277 |
} |