de.schlichtherle.key
Class PromptingKeyManager

java.lang.Object
  extended by de.schlichtherle.key.KeyManager
      extended by de.schlichtherle.key.PromptingKeyManager
Direct Known Subclasses:
PromptingKeyManager, PromptingKeyManager

public abstract class PromptingKeyManager
extends KeyManager

An abstract KeyManager which prompts the user for a key if required.

This class maintains a map of user interface classes for the PromptingKeyProvider class and each of its subclasses which require an individual user interface. The particular user interface classes are determined by a subclass of this key manager. This enables the subclass to determine which user interface technology should actually be used to prompt the user for a key. For example, the implementation in the class PromptingKeyManager uses Swing to prompt the user for either a password or a key file.

Subclasses must use the method mapPromptingKeyProviderUIType(java.lang.String, java.lang.Class) to register a user interface class for a particular user interface class identifier (the value returned by PromptingKeyProvider.getUIClassID()). This is best done in the constructor of the subclass.

Note that class loading and instantiation may happen in a JVM shutdown hook, so class initializers and constructors must behave accordingly. In particular, it's not permitted to construct or use a Swing GUI there.

This class is thread safe.

Since:
TrueZIP 6.0
Version:
TrueZIP 6.7
Author:
Christian Schlichtherle

Nested Class Summary
 
Nested classes/interfaces inherited from class de.schlichtherle.key.KeyManager
KeyManager.KeyProviderCommand
 
Constructor Summary
PromptingKeyManager()
          Constructs a new PromptingKeyManager.
 
Method Summary
protected  void ensurePromptingImpl()
          Called by some methods in the PromptingKeyProvider class in order to ensure that prompting mode is enabled.
 KeyProvider getKeyProvider(String resourceID, Class keyProviderType)
          Behaves like the super class implementation, but adds additional behaviour in case the resulting key provider is an instance of PromptingKeyProvider.
static boolean isPrompting()
          Returns true if and only if prompting mode is enabled.
protected  boolean isPromptingImpl()
          Called by isPrompting() on the default key manager instance in order to implement its behaviour and allow subclasses to override it.
protected  void mapPromptingKeyProviderUIType(String uiClassID, Class uiClass)
          Subclasses must use this method to register a user interface class for a particular user interface class identifier as returned by PromptingKeyProvider.getUIClassID().
protected  void register(String uiClassID, Class uiClass)
          Deprecated. Use mapPromptingKeyProviderUIType(uiClassID, uiClass) instead.
static void resetCancelledPrompts()
          Resets all cancelled key prompts, forcing a new prompt on the next call to AbstractKeyProvider.getOpenKey() or AbstractKeyProvider.getCreateKey().
static void setPrompting(boolean prompting)
          Enables or disables prompting mode.
protected  void setPromptingImpl(boolean prompting)
          Called by isPrompting() on the default key manager instance in order to implement its behaviour and allow subclasses to override it.
 
Methods inherited from class de.schlichtherle.key.KeyManager
forEachKeyProvider, getInstance, getKeyProvider, mapKeyProviderType, moveKeyProvider, resetAndClearKeyProviders, resetAndRemoveKeyProvider, resetAndRemoveKeyProviders, resetKeyProvider, resetKeyProviders, setInstance, setKeyProvider
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PromptingKeyManager

public PromptingKeyManager()
Constructs a new PromptingKeyManager. This instance maps the following key provider types using KeyManager.mapKeyProviderType(java.lang.Class, java.lang.Class):
forKeyProviderType useKeyProviderType
KeyProvider.class PromptingKeyProvider.class
AesKeyProvider.class PromptingAesKeyProvider.class

Method Detail

isPrompting

public static boolean isPrompting()
Returns true if and only if prompting mode is enabled. This is a class property.

Note that subclasses might add additional behaviour to both isPrompting() and setPrompting(boolean) through the default key manager instance (see KeyManager.getInstance()). Regardless, an application may safely assume that isPrompting() reflects the actual behaviour of the API in this package although it may not reflect the parameter value of the last call to setPrompting(boolean).

Returns:
Whether or not the user will be prompted for a key if required.
See Also:
setPrompting(boolean)

isPromptingImpl

protected boolean isPromptingImpl()
Called by isPrompting() on the default key manager instance in order to implement its behaviour and allow subclasses to override it. Subclasses should call the implementation in this class when overriding this method.

Since:
TrueZIP 6.1
See Also:
setPromptingImpl(boolean), KeyManager.getInstance()

setPrompting

public static void setPrompting(boolean prompting)
Enables or disables prompting mode. If prompting mode is enabled, the user will be prompted for a key when a PromptingKeyProvider is first requested to provide a key for the respective resource. If prompting mode is disabled, all attempts to prompt the user will result in an UnknownKeyException until prompting mode is enabled again.

This is a class property.

Note that subclasses might add additional behaviour to both isPrompting() and setPrompting(boolean) through the default key manager instance (see KeyManager.getInstance()). Regardless, an application may safely assume that isPrompting() reflects the actual behaviour of the API in this package although it may not reflect the parameter value of the last call to setPrompting(boolean).

Parameters:
prompting - The value of the property prompting.
See Also:
isPrompting()

setPromptingImpl

protected void setPromptingImpl(boolean prompting)
Called by isPrompting() on the default key manager instance in order to implement its behaviour and allow subclasses to override it. Subclasses should call the implementation in this class when overriding this method.

Since:
TrueZIP 6.1
See Also:
isPromptingImpl(), KeyManager.getInstance()

ensurePromptingImpl

protected void ensurePromptingImpl()
                            throws KeyPromptingDisabledException
Called by some methods in the PromptingKeyProvider class in order to ensure that prompting mode is enabled. This method may be overridden by subclasses in order to throw a more detailed exception.

The implementation in this class is equivalent to:

        if (!isPromptingImpl())
            throw new KeyPromptingDisabledException();
 

Throws:
KeyPromptingDisabledException
Since:
TrueZIP 6.1

resetCancelledPrompts

public static void resetCancelledPrompts()
Resets all cancelled key prompts, forcing a new prompt on the next call to AbstractKeyProvider.getOpenKey() or AbstractKeyProvider.getCreateKey(). Of course, this call only affects instances of PromptingKeyProvider.


register

protected final void register(String uiClassID,
                              Class uiClass)
Deprecated. Use mapPromptingKeyProviderUIType(uiClassID, uiClass) instead.


mapPromptingKeyProviderUIType

protected final void mapPromptingKeyProviderUIType(String uiClassID,
                                                   Class uiClass)
Subclasses must use this method to register a user interface class for a particular user interface class identifier as returned by PromptingKeyProvider.getUIClassID(). This is best done in the constructor of the subclass.

Parameters:
uiClassID - The identifier of the user interface class.
uiClass - The class of the user interface. This must have
Throws:
NullPointerException - If any of the parameters is null.
IllegalArgumentException - If the runtime type of uiClass is not PromptingKeyProviderUI or a subclass or does not provide a public constructor with no parameters.
Since:
TrueZIP 6.1
See Also:
getKeyProvider(String, Class)

getKeyProvider

public KeyProvider getKeyProvider(String resourceID,
                                  Class keyProviderType)
                           throws NullPointerException,
                                  ClassCastException,
                                  IllegalArgumentException
Behaves like the super class implementation, but adds additional behaviour in case the resulting key provider is an instance of PromptingKeyProvider. In this case, the appropriate user interface instance is determined and installed in the key provider before it is returned.

Overrides:
getKeyProvider in class KeyManager
Parameters:
resourceID - The identifier of the protected resource.
keyProviderType - Unless another key provider is already mapped for the protected resource, this denotes the root of the class hierarchy to which the run time type of the returned instance may belong. In case the key manager does not know a more suitable class in this hierarchy, this parameter must denote an implementation of the KeyProvider interface with a public no-argument constructor.
Returns:
The KeyProvider mapped for the protected resource. If no key provider has been previously mapped for the protected resource, the run time type of this instance is guaranteed to be assignment compatible to the given keyProviderType.
Throws:
NullPointerException - If resourceID or keyProviderType is null.
ClassCastException - If no other key provider is mapped for the protected resource and the given class is not an implementation of the KeyProvider interface.
IllegalArgumentException - If any other precondition on the parameter keyProviderType does not hold.
See Also:
KeyManager.getKeyProvider(String, Class)