1 16 17 package de.schlichtherle.key; 18 19 import java.lang.reflect.UndeclaredThrowableException ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 52 public abstract class PromptingKeyManager extends KeyManager { 53 54 private static volatile boolean prompting = true; 55 56 private final Map providerUITypes = new HashMap (); 57 58 77 public PromptingKeyManager() { 78 mapKeyProviderType(KeyProvider.class, PromptingKeyProvider.class); 79 mapKeyProviderType(AesKeyProvider.class, PromptingAesKeyProvider.class); 80 } 81 82 86 102 public static boolean isPrompting() { 103 KeyManager manager = getInstance(); 104 return manager instanceof PromptingKeyManager 105 && ((PromptingKeyManager) manager).isPromptingImpl(); 106 } 107 108 119 protected boolean isPromptingImpl() { 120 return prompting; 121 } 122 123 146 public static void setPrompting(boolean prompting) { 147 KeyManager manager = getInstance(); 148 if (manager instanceof PromptingKeyManager) 149 ((PromptingKeyManager) manager).setPromptingImpl(prompting); 150 } 151 152 163 protected void setPromptingImpl(boolean prompting) { 164 PromptingKeyManager.prompting = prompting; 165 } 166 167 static void ensurePrompting() 168 throws KeyPromptingDisabledException { 169 KeyManager manager = getInstance(); 170 if (manager instanceof PromptingKeyManager) 171 ((PromptingKeyManager) manager).ensurePromptingImpl(); 172 } 173 174 188 protected void ensurePromptingImpl() 189 throws KeyPromptingDisabledException { 190 if (!isPromptingImpl()) 191 throw new KeyPromptingDisabledException(); 192 } 193 194 201 public static void resetCancelledPrompts() { 202 forEachKeyProvider(new KeyProviderCommand() { 203 public void run(String resourceID, KeyProvider provider) { 204 if (provider instanceof PromptingKeyProvider) 205 ((PromptingKeyProvider) provider).resetCancelledPrompt(); 206 } 207 }); 208 } 209 210 214 218 protected final void register( 219 final String uiClassID, 220 final Class uiClass) { 221 mapPromptingKeyProviderUIType(uiClassID, uiClass); 222 } 223 224 243 protected synchronized final void mapPromptingKeyProviderUIType( 244 final String uiClassID, 245 final Class uiClass) { 246 if (uiClassID == null) 247 throw new NullPointerException ("uiClassID"); 248 if (!PromptingKeyProviderUI.class.isAssignableFrom(uiClass)) 249 throw new IllegalArgumentException ( 250 uiClass.getName() + " must be PromptingKeyProviderUI or a subclass!"); 251 try { 252 uiClass.getConstructor(null); 253 } catch (NoSuchMethodException noPublicNullaryConstructor) { 254 final IllegalArgumentException iae = new IllegalArgumentException ( 255 uiClass.getName() + " (no public nullary constructor)"); 256 iae.initCause(noPublicNullaryConstructor); 257 throw iae; 258 } 259 providerUITypes.put(uiClassID, uiClass); 260 } 261 262 270 public KeyProvider getKeyProvider(String resourceID) 271 throws NullPointerException { 272 return getKeyProvider(resourceID, KeyProvider.class); 273 } 274 275 284 public KeyProvider getKeyProvider( 285 final String resourceID, 286 final Class keyProviderType) 287 throws NullPointerException , ClassCastException , IllegalArgumentException { 288 final KeyProvider provider 289 = super.getKeyProvider(resourceID, keyProviderType); 290 291 if (provider instanceof PromptingKeyProvider) { 292 final PromptingKeyProvider pkp = (PromptingKeyProvider) provider; 293 pkp.setUI(getUI(pkp.getUIClassID())); 294 } 295 296 return provider; 297 } 298 299 private synchronized PromptingKeyProviderUI getUI(final String uiClassID) { 300 final Object value = providerUITypes.get(uiClassID); 301 302 final PromptingKeyProviderUI pkpui; 303 if (value instanceof Class ) { 304 try { 305 pkpui = (PromptingKeyProviderUI) ((Class ) value).newInstance(); 306 } catch (InstantiationException failure) { 307 throw new UndeclaredThrowableException (failure); 308 } catch (IllegalAccessException failure) { 309 throw new UndeclaredThrowableException (failure); 310 } 311 providerUITypes.put(uiClassID, pkpui); 312 } else if (value != null) { 313 pkpui = (PromptingKeyProviderUI) value; 314 } else { throw new IllegalArgumentException (uiClassID + 316 " (unknown user interface for PromptingKeyProvider)"); 317 } 318 319 return pkpui; 320 } 321 } 322 | Popular Tags |