public class DefaultTokenFactory extends Object implements TokenFactory
TokenFactory
that loads the token
parameters from the default Java user preference store or interactively
queries the user for the required token parameters if the token has not been
used before.
Interactive querying is performed via the command-line (using
System.err
for prompts and System.in
for reading user input.
As such, this class will only be really useful for interactive querying in
console applications. It is possible however to just use this class for
manually storing and retrieving tokens with the appropriate methods.
For this class to work in interactive mode, the token class must have a public no-args constructor.
Modifier and Type | Method and Description |
---|---|
static <T> void |
delete(Class<T> tokenClass)
Convenience method equivalent to
getInstance().deleteToken(tokenClass) |
static <T> void |
delete(Class<T> tokenClass,
String name)
Convenience method equivalent to
getInstance().deleteToken(tokenClass, name) |
<T> void |
deleteToken(Class<T> tokenClass)
Delete the default token parameters for the given class from the store.
|
<T> void |
deleteToken(Class<T> tokenClass,
String name)
Delete the named token parameters for the given class from the store.
|
static <T> T |
get(Class<T> tokenClass)
Convenience method equivalent to
getInstance().getToken(tokenClass).
|
static <T> T |
get(Class<T> tokenClass,
String name)
Convenience method equivalent to
getInstance().getToken(tokenClass, name).
|
static DefaultTokenFactory |
getInstance()
Get the default singleton instance
|
<T> T |
getToken(Class<T> tokenClass)
Get the default token for the given class.
|
<T> T |
getToken(Class<T> tokenClass,
String name)
Get the token for the given class, tagged with a specific name.
|
<T> T |
loadToken(Class<T> clz,
String name)
Load a token with an optional name tag from the backing store.
|
<T> void |
saveToken(T token,
String name)
Save the parameters of the given token to the backing preference store.
|
public static DefaultTokenFactory getInstance()
public <T> void deleteToken(Class<T> tokenClass) throws BackingStoreException
tokenClass
- the token classBackingStoreException
- if a problem occurred communicating with the backing
preference storepublic <T> void deleteToken(Class<T> tokenClass, String name) throws BackingStoreException
tokenClass
- the token classname
- the name of the token, or null for the default tokenBackingStoreException
- if a problem occurred communicating with the backing
preference storepublic <T> T getToken(Class<T> tokenClass)
TokenFactory
If a predefined token is unavailable, the token might be built by the underlying implementation; for example by interacting with the user.
getToken
in interface TokenFactory
tokenClass
- the class of the token to buildpublic <T> T getToken(Class<T> tokenClass, String name)
TokenFactory
If a predefined token is unavailable, the token might be built by the underlying implementation; for example by interacting with the user.
getToken
in interface TokenFactory
tokenClass
- the class of the token to buildname
- the name of the tokenpublic <T> void saveToken(T token, String name) throws IllegalArgumentException, IllegalAccessException, BackingStoreException
token
- the token to savename
- the name of the token, or null for the defaultIllegalArgumentException
- if the token class isn't annotated with Token
.IllegalAccessException
- if an error occurred reading a parameter of the tokenBackingStoreException
- if a problem occurred communicating with the backing
preference storepublic <T> T loadToken(Class<T> clz, String name) throws BackingStoreException, InstantiationException, IllegalAccessException
clz
- the class of the tokenname
- the name of the token, or null for the default tokenBackingStoreException
- if a problem occurred communicating with the backing
preference storeInstantiationException
- if the token could not be constructedIllegalAccessException
- if an error occurred setting a parameterpublic static <T> T get(Class<T> tokenClass)
tokenClass
- the class of the token to buildgetToken(Class)
public static <T> T get(Class<T> tokenClass, String name)
tokenClass
- the class of the token to buildname
- the name of the tokengetToken(Class, String)
public static <T> void delete(Class<T> tokenClass) throws BackingStoreException
getInstance().deleteToken(tokenClass)
tokenClass
- the class of the token to deleteBackingStoreException
- if a problem occurred communicating with the backing
preference storedeleteToken(Class)
public static <T> void delete(Class<T> tokenClass, String name) throws BackingStoreException
getInstance().deleteToken(tokenClass, name)
tokenClass
- the class of the token to deletename
- the name of the token, or null
for the default tokenBackingStoreException
- if a problem occurred communicating with the backing
preference storedeleteToken(Class, String)